raui_core/widget/unit/
text.rs

1use crate::{
2    props::Props,
3    widget::{
4        node::WidgetNode,
5        unit::WidgetUnitData,
6        utils::{Color, Transform},
7        WidgetId,
8    },
9    PrefabValue, Scalar,
10};
11use serde::{Deserialize, Serialize};
12use std::convert::TryFrom;
13
14#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub enum TextBoxHorizontalAlign {
16    #[default]
17    Left,
18    Center,
19    Right,
20}
21
22#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub enum TextBoxVerticalAlign {
24    #[default]
25    Top,
26    Middle,
27    Bottom,
28}
29
30#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub enum TextBoxDirection {
32    #[default]
33    HorizontalLeftToRight,
34    HorizontalRightToLeft,
35    VerticalTopToBottom,
36    VerticalBottomToTop,
37}
38
39impl TextBoxDirection {
40    pub fn is_horizontal(&self) -> bool {
41        *self == Self::HorizontalLeftToRight || *self == Self::HorizontalRightToLeft
42    }
43
44    pub fn is_vertical(&self) -> bool {
45        *self == Self::VerticalTopToBottom || *self == Self::VerticalBottomToTop
46    }
47
48    pub fn is_order_ascending(&self) -> bool {
49        *self == Self::HorizontalLeftToRight || *self == Self::VerticalTopToBottom
50    }
51
52    pub fn is_order_descending(&self) -> bool {
53        *self == Self::HorizontalRightToLeft || *self == Self::VerticalBottomToTop
54    }
55}
56
57#[derive(Debug, Default, Clone, Serialize, Deserialize)]
58pub struct TextBoxFont {
59    #[serde(default)]
60    pub name: String,
61    #[serde(default)]
62    pub size: Scalar,
63}
64
65#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
66pub enum TextBoxSizeValue {
67    #[default]
68    Fill,
69    Exact(Scalar),
70}
71
72#[derive(Debug, Default, Clone, Serialize, Deserialize)]
73pub struct TextBox {
74    #[serde(default)]
75    pub id: WidgetId,
76    #[serde(default)]
77    pub text: String,
78    #[serde(default)]
79    pub width: TextBoxSizeValue,
80    #[serde(default)]
81    pub height: TextBoxSizeValue,
82    #[serde(default)]
83    pub horizontal_align: TextBoxHorizontalAlign,
84    #[serde(default)]
85    pub vertical_align: TextBoxVerticalAlign,
86    #[serde(default)]
87    pub direction: TextBoxDirection,
88    #[serde(default)]
89    pub font: TextBoxFont,
90    #[serde(default)]
91    pub color: Color,
92    #[serde(default)]
93    pub transform: Transform,
94}
95
96impl WidgetUnitData for TextBox {
97    fn id(&self) -> &WidgetId {
98        &self.id
99    }
100}
101
102impl TryFrom<TextBoxNode> for TextBox {
103    type Error = ();
104
105    fn try_from(node: TextBoxNode) -> Result<Self, Self::Error> {
106        let TextBoxNode {
107            id,
108            text,
109            width,
110            height,
111            horizontal_align,
112            vertical_align,
113            direction,
114            font,
115            color,
116            transform,
117            ..
118        } = node;
119        Ok(Self {
120            id,
121            text,
122            width,
123            height,
124            horizontal_align,
125            vertical_align,
126            direction,
127            font,
128            color,
129            transform,
130        })
131    }
132}
133
134#[derive(Debug, Default, Clone)]
135pub struct TextBoxNode {
136    pub id: WidgetId,
137    pub props: Props,
138    pub text: String,
139    pub width: TextBoxSizeValue,
140    pub height: TextBoxSizeValue,
141    pub horizontal_align: TextBoxHorizontalAlign,
142    pub vertical_align: TextBoxVerticalAlign,
143    pub direction: TextBoxDirection,
144    pub font: TextBoxFont,
145    pub color: Color,
146    pub transform: Transform,
147}
148
149impl TextBoxNode {
150    pub fn remap_props<F>(&mut self, mut f: F)
151    where
152        F: FnMut(Props) -> Props,
153    {
154        let props = std::mem::take(&mut self.props);
155        self.props = (f)(props);
156    }
157}
158
159impl From<TextBoxNode> for WidgetNode {
160    fn from(data: TextBoxNode) -> Self {
161        Self::Unit(data.into())
162    }
163}
164
165#[derive(Debug, Default, Clone, Serialize, Deserialize)]
166pub(crate) struct TextBoxNodePrefab {
167    #[serde(default)]
168    pub id: WidgetId,
169    #[serde(default)]
170    pub props: PrefabValue,
171    #[serde(default)]
172    pub text: String,
173    #[serde(default)]
174    pub width: TextBoxSizeValue,
175    #[serde(default)]
176    pub height: TextBoxSizeValue,
177    #[serde(default)]
178    pub horizontal_align: TextBoxHorizontalAlign,
179    #[serde(default)]
180    pub vertical_align: TextBoxVerticalAlign,
181    #[serde(default)]
182    pub direction: TextBoxDirection,
183    #[serde(default)]
184    pub font: TextBoxFont,
185    #[serde(default)]
186    pub color: Color,
187    #[serde(default)]
188    pub transform: Transform,
189}