raui_core/widget/
utils.rs

1use crate::{Integer, PropsData, Scalar};
2use serde::{Deserialize, Serialize};
3
4#[repr(C)]
5#[derive(PropsData, Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
6#[props_data(crate::props::PropsData)]
7#[prefab(crate::Prefab)]
8pub struct Vec2 {
9    #[serde(default)]
10    pub x: Scalar,
11    #[serde(default)]
12    pub y: Scalar,
13}
14
15impl From<Scalar> for Vec2 {
16    fn from(v: Scalar) -> Self {
17        Self { x: v, y: v }
18    }
19}
20
21impl From<(Scalar, Scalar)> for Vec2 {
22    fn from((x, y): (Scalar, Scalar)) -> Self {
23        Self { x, y }
24    }
25}
26
27impl From<[Scalar; 2]> for Vec2 {
28    fn from([x, y]: [Scalar; 2]) -> Self {
29        Self { x, y }
30    }
31}
32
33#[repr(C)]
34#[derive(PropsData, Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
35#[props_data(crate::props::PropsData)]
36#[prefab(crate::Prefab)]
37pub struct IntVec2 {
38    #[serde(default)]
39    pub x: Integer,
40    #[serde(default)]
41    pub y: Integer,
42}
43
44impl From<Integer> for IntVec2 {
45    fn from(v: Integer) -> Self {
46        Self { x: v, y: v }
47    }
48}
49
50impl From<(Integer, Integer)> for IntVec2 {
51    fn from((x, y): (Integer, Integer)) -> Self {
52        Self { x, y }
53    }
54}
55
56impl From<[Integer; 2]> for IntVec2 {
57    fn from([x, y]: [Integer; 2]) -> Self {
58        Self { x, y }
59    }
60}
61
62#[repr(C)]
63#[derive(PropsData, Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
64#[props_data(crate::props::PropsData)]
65#[prefab(crate::Prefab)]
66pub struct Rect {
67    #[serde(default)]
68    pub left: Scalar,
69    #[serde(default)]
70    pub right: Scalar,
71    #[serde(default)]
72    pub top: Scalar,
73    #[serde(default)]
74    pub bottom: Scalar,
75}
76
77impl From<Scalar> for Rect {
78    fn from(v: Scalar) -> Self {
79        Self {
80            left: v,
81            right: v,
82            top: v,
83            bottom: v,
84        }
85    }
86}
87
88impl From<(Scalar, Scalar)> for Rect {
89    fn from((w, h): (Scalar, Scalar)) -> Self {
90        Self {
91            left: 0.0,
92            right: w,
93            top: 0.0,
94            bottom: h,
95        }
96    }
97}
98
99impl From<[Scalar; 2]> for Rect {
100    fn from([w, h]: [Scalar; 2]) -> Self {
101        Self {
102            left: 0.0,
103            right: w,
104            top: 0.0,
105            bottom: h,
106        }
107    }
108}
109
110impl From<(Scalar, Scalar, Scalar, Scalar)> for Rect {
111    fn from((left, right, top, bottom): (Scalar, Scalar, Scalar, Scalar)) -> Self {
112        Self {
113            left,
114            right,
115            top,
116            bottom,
117        }
118    }
119}
120
121impl From<[Scalar; 4]> for Rect {
122    fn from([left, right, top, bottom]: [Scalar; 4]) -> Self {
123        Self {
124            left,
125            right,
126            top,
127            bottom,
128        }
129    }
130}
131
132impl Rect {
133    #[inline]
134    pub fn width(&self) -> Scalar {
135        self.right - self.left
136    }
137
138    #[inline]
139    pub fn height(&self) -> Scalar {
140        self.bottom - self.top
141    }
142
143    #[inline]
144    pub fn size(&self) -> Vec2 {
145        Vec2 {
146            x: self.width(),
147            y: self.height(),
148        }
149    }
150}
151
152#[repr(C)]
153#[derive(PropsData, Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
154#[props_data(crate::props::PropsData)]
155#[prefab(crate::Prefab)]
156pub struct IntRect {
157    #[serde(default)]
158    pub left: Integer,
159    #[serde(default)]
160    pub right: Integer,
161    #[serde(default)]
162    pub top: Integer,
163    #[serde(default)]
164    pub bottom: Integer,
165}
166
167impl IntRect {
168    #[inline]
169    pub fn width(&self) -> Integer {
170        self.right - self.left
171    }
172
173    #[inline]
174    pub fn height(&self) -> Integer {
175        self.bottom - self.top
176    }
177
178    #[inline]
179    pub fn size(&self) -> IntVec2 {
180        IntVec2 {
181            x: self.width(),
182            y: self.height(),
183        }
184    }
185}
186
187impl From<Integer> for IntRect {
188    fn from(v: Integer) -> Self {
189        Self {
190            left: v,
191            right: v,
192            top: v,
193            bottom: v,
194        }
195    }
196}
197
198impl From<(Integer, Integer)> for IntRect {
199    fn from((w, h): (Integer, Integer)) -> Self {
200        Self {
201            left: 0,
202            right: w,
203            top: 0,
204            bottom: h,
205        }
206    }
207}
208
209#[repr(C)]
210#[derive(PropsData, Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
211#[props_data(crate::props::PropsData)]
212#[prefab(crate::Prefab)]
213pub struct Color {
214    #[serde(default)]
215    pub r: Scalar,
216    #[serde(default)]
217    pub g: Scalar,
218    #[serde(default)]
219    pub b: Scalar,
220    #[serde(default)]
221    pub a: Scalar,
222}
223
224impl Default for Color {
225    fn default() -> Self {
226        Self {
227            r: 1.0,
228            g: 1.0,
229            b: 1.0,
230            a: 1.0,
231        }
232    }
233}
234
235impl Color {
236    pub fn transparent() -> Self {
237        Self {
238            r: 0.0,
239            g: 0.0,
240            b: 0.0,
241            a: 0.0,
242        }
243    }
244}
245
246#[derive(PropsData, Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
247#[props_data(crate::props::PropsData)]
248#[prefab(crate::Prefab)]
249pub struct Transform {
250    /// Rectangle center of mass. Values in range: <0;1>
251    #[serde(default)]
252    pub pivot: Vec2,
253    /// Translation in rectangle fraction units. Values in range: <0;1>
254    #[serde(default)]
255    pub align: Vec2,
256    /// Translation in regular units.
257    #[serde(default)]
258    pub translation: Vec2,
259    /// Rotation in radian angle units.
260    #[serde(default)]
261    pub rotation: Scalar,
262    /// Scale in regular units.
263    #[serde(default)]
264    pub scale: Vec2,
265    /// Skewing in radian angle units.
266    /// {angle X, angle Y}
267    #[serde(default)]
268    pub skew: Vec2,
269}
270
271impl Default for Transform {
272    fn default() -> Self {
273        Self {
274            pivot: Default::default(),
275            align: Default::default(),
276            translation: Default::default(),
277            rotation: Default::default(),
278            scale: Self::default_scale(),
279            skew: Default::default(),
280        }
281    }
282}
283
284impl Transform {
285    fn default_scale() -> Vec2 {
286        Vec2 { x: 1.0, y: 1.0 }
287    }
288}
289
290#[inline]
291pub fn lerp(from: Scalar, to: Scalar, factor: Scalar) -> Scalar {
292    from + (to - from) * factor
293}
294
295#[inline]
296pub fn lerp_clamped(from: Scalar, to: Scalar, factor: Scalar) -> Scalar {
297    lerp(from, to, factor.clamp(0.0, 1.0))
298}