1#[derive(Clone, Copy, Debug, Default, PartialEq)]
12pub struct Point {
13 pub x: f32,
14 pub y: f32,
15}
16
17impl Point {
18 pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
19 pub fn new(x: f32, y: f32) -> Self {
20 Self { x, y }
21 }
22}
23
24impl std::ops::Add for Point {
25 type Output = Point;
26 fn add(self, rhs: Point) -> Point {
27 Point::new(self.x + rhs.x, self.y + rhs.y)
28 }
29}
30
31#[derive(Clone, Copy, Debug, Default, PartialEq)]
33pub struct Size {
34 pub width: f32,
35 pub height: f32,
36}
37
38impl Size {
39 pub const ZERO: Self = Self { width: 0.0, height: 0.0 };
40 pub fn new(width: f32, height: f32) -> Self {
41 Self { width, height }
42 }
43}
44
45#[derive(Clone, Copy, Debug, Default, PartialEq)]
47pub struct Rect {
48 pub x: f32,
49 pub y: f32,
50 pub width: f32,
51 pub height: f32,
52}
53
54impl Rect {
55 pub const ZERO: Self = Self { x: 0.0, y: 0.0, width: 0.0, height: 0.0 };
56 pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
57 Self { x, y, width, height }
58 }
59
60 pub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Self {
61 Self { x: left, y: top, width: right - left, height: bottom - top }
62 }
63
64 pub fn right(&self) -> f32 {
65 self.x + self.width
66 }
67 pub fn bottom(&self) -> f32 {
68 self.y + self.height
69 }
70 pub fn size(&self) -> Size {
71 Size::new(self.width, self.height)
72 }
73
74 pub fn contains(&self, p: Point) -> bool {
76 p.x >= self.x && p.x <= self.right() && p.y >= self.y && p.y <= self.bottom()
77 }
78}
79
80#[derive(Clone, Copy, Debug, PartialEq)]
82pub struct Color {
83 pub r: f32,
84 pub g: f32,
85 pub b: f32,
86 pub a: f32,
87}
88
89impl Color {
90 pub const TRANSPARENT: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 0.0 };
91 pub const WHITE: Self = Self { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
92 pub const BLACK: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 1.0 };
93
94 pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
95 Self { r, g, b, a }
96 }
97
98 pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
99 Self { r, g, b, a: 1.0 }
100 }
101
102 pub fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
104 Self::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0)
105 }
106
107 pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
108 Self::rgba(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0)
109 }
110}
111
112#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
114pub enum Axis {
115 #[default]
116 Vertical,
117 Horizontal,
118}
119
120impl Axis {
121 pub fn main(&self, p: Point) -> f32 {
123 match self {
124 Axis::Vertical => p.y,
125 Axis::Horizontal => p.x,
126 }
127 }
128 pub fn cross(&self, p: Point) -> f32 {
130 match self {
131 Axis::Vertical => p.x,
132 Axis::Horizontal => p.y,
133 }
134 }
135 pub fn main_size(&self, s: Size) -> f32 {
137 match self {
138 Axis::Vertical => s.height,
139 Axis::Horizontal => s.width,
140 }
141 }
142 pub fn cross_size(&self, s: Size) -> f32 {
144 match self {
145 Axis::Vertical => s.width,
146 Axis::Horizontal => s.height,
147 }
148 }
149}
150
151#[derive(Clone, Copy, Debug, PartialEq)]
166pub struct Transform2D {
167 pub rotation: f32,
169 pub scale_x: f32,
171 pub scale_y: f32,
173 pub translate: Point,
175}
176
177impl Default for Transform2D {
178 fn default() -> Self {
179 Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO }
180 }
181}
182
183impl Transform2D {
184 pub const IDENTITY: Self = Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO };
186
187 pub fn is_identity(&self) -> bool {
189 self.rotation == 0.0 && self.scale_x == 1.0 && self.scale_y == 1.0 && self.translate == Point::ZERO
190 }
191
192 pub fn translate(x: f32, y: f32) -> Self {
194 Self { translate: Point::new(x, y), ..Self::IDENTITY }
195 }
196
197 pub fn rotate(angle: f32) -> Self {
199 Self { rotation: angle, ..Self::IDENTITY }
200 }
201
202 pub fn scale(s: f32) -> Self {
204 Self { scale_x: s, scale_y: s, ..Self::IDENTITY }
205 }
206
207 pub fn stretch(sx: f32, sy: f32) -> Self {
209 Self { scale_x: sx, scale_y: sy, ..Self::IDENTITY }
210 }
211}
212
213#[derive(Clone, Copy, Debug, PartialEq)]
215pub struct RRectDef {
216 pub radius: f32,
218}
219
220impl RRectDef {
221 pub const fn new(radius: f32) -> Self {
222 Self { radius }
223 }
224}