Skip to main content

widgetkit_core/
geometry.rs

1#[derive(Clone, Copy, Debug, Default, PartialEq)]
2pub struct Point {
3    pub x: f32,
4    pub y: f32,
5}
6
7impl Point {
8    pub const fn new(x: f32, y: f32) -> Self {
9        Self { x, y }
10    }
11}
12
13#[derive(Clone, Copy, Debug, Default, PartialEq)]
14pub struct Size {
15    pub width: f32,
16    pub height: f32,
17}
18
19impl Size {
20    pub const fn new(width: f32, height: f32) -> Self {
21        Self { width, height }
22    }
23
24    pub fn is_empty(self) -> bool {
25        self.width <= 0.0 || self.height <= 0.0
26    }
27}
28
29#[derive(Clone, Copy, Debug, Default, PartialEq)]
30pub struct Rect {
31    pub origin: Point,
32    pub size: Size,
33}
34
35impl Rect {
36    pub const fn new(origin: Point, size: Size) -> Self {
37        Self { origin, size }
38    }
39
40    pub const fn xywh(x: f32, y: f32, width: f32, height: f32) -> Self {
41        Self::new(Point::new(x, y), Size::new(width, height))
42    }
43
44    pub fn x(self) -> f32 {
45        self.origin.x
46    }
47
48    pub fn y(self) -> f32 {
49        self.origin.y
50    }
51
52    pub fn width(self) -> f32 {
53        self.size.width
54    }
55
56    pub fn height(self) -> f32 {
57        self.size.height
58    }
59
60    pub fn right(self) -> f32 {
61        self.origin.x + self.size.width
62    }
63
64    pub fn bottom(self) -> f32 {
65        self.origin.y + self.size.height
66    }
67
68    pub fn inset(self, insets: Insets) -> Self {
69        let x = self.x() + insets.left;
70        let y = self.y() + insets.top;
71        let width = (self.width() - insets.left - insets.right).max(0.0);
72        let height = (self.height() - insets.top - insets.bottom).max(0.0);
73        Self::xywh(x, y, width, height)
74    }
75}
76
77#[derive(Clone, Copy, Debug, Default, PartialEq)]
78pub struct Insets {
79    pub top: f32,
80    pub right: f32,
81    pub bottom: f32,
82    pub left: f32,
83}
84
85impl Insets {
86    pub const fn all(value: f32) -> Self {
87        Self {
88            top: value,
89            right: value,
90            bottom: value,
91            left: value,
92        }
93    }
94}