mitsuami_core/
geometry.rs1use std::fmt;
2
3use mitsuami_reactive::{IntoValue, Value};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct Size {
8 pub width: f32,
9 pub height: f32,
10}
11
12impl Size {
13 pub const ZERO: Size = Size { width: 0.0, height: 0.0 };
14
15 pub const fn new(width: f32, height: f32) -> Size {
16 Size { width, height }
17 }
18
19 pub fn is_empty(&self) -> bool {
20 self.width <= 0.0 || self.height <= 0.0
21 }
22}
23
24#[derive(Clone, Copy, Debug, PartialEq)]
26pub enum WindowSize {
27 Fixed(Size),
28 FitHeight(f32),
32 FollowHeight(f32),
37 FollowHeightUntilResized(f32),
40}
41
42impl From<Size> for WindowSize {
43 fn from(size: Size) -> WindowSize {
44 WindowSize::Fixed(size)
45 }
46}
47
48impl IntoValue<WindowSize> for WindowSize {
49 fn into_value(self) -> Value<WindowSize> {
50 Value::Static(self)
51 }
52}
53
54impl IntoValue<WindowSize> for Size {
55 fn into_value(self) -> Value<WindowSize> {
56 Value::Static(WindowSize::Fixed(self))
57 }
58}
59
60#[derive(Clone, Copy, Debug, Default, PartialEq)]
61pub struct Point {
62 pub x: f32,
63 pub y: f32,
64}
65
66impl Point {
67 pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
68
69 pub const fn new(x: f32, y: f32) -> Point {
70 Point { x, y }
71 }
72}
73
74#[derive(Clone, Copy, Debug, Default, PartialEq)]
76pub struct Insets {
77 pub top: f32,
78 pub right: f32,
79 pub bottom: f32,
80 pub left: f32,
81}
82
83impl Insets {
84 pub const ZERO: Insets = Insets { top: 0.0, right: 0.0, bottom: 0.0, left: 0.0 };
85
86 pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Insets {
87 Insets { top, right, bottom, left }
88 }
89}
90
91#[derive(Clone, Copy, Debug, Default, PartialEq)]
93pub struct Rect {
94 pub origin: Point,
95 pub size: Size,
96}
97
98impl Rect {
99 pub const ZERO: Rect = Rect { origin: Point::ZERO, size: Size::ZERO };
100
101 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
102 Rect { origin: Point { x, y }, size: Size { width, height } }
103 }
104
105 pub fn x(&self) -> f32 {
106 self.origin.x
107 }
108
109 pub fn y(&self) -> f32 {
110 self.origin.y
111 }
112
113 pub fn width(&self) -> f32 {
114 self.size.width
115 }
116
117 pub fn height(&self) -> f32 {
118 self.size.height
119 }
120
121 pub fn max_x(&self) -> f32 {
122 self.origin.x + self.size.width
123 }
124
125 pub fn max_y(&self) -> f32 {
126 self.origin.y + self.size.height
127 }
128
129 pub fn offset(&self, by: Point) -> Rect {
130 Rect { origin: Point::new(self.origin.x + by.x, self.origin.y + by.y), size: self.size }
131 }
132
133 pub fn inset(&self, by: Insets) -> Rect {
135 Rect::new(
136 self.x() + by.left,
137 self.y() + by.top,
138 (self.width() - by.left - by.right).max(0.0),
139 (self.height() - by.top - by.bottom).max(0.0),
140 )
141 }
142
143 pub fn union(&self, other: &Rect) -> Rect {
145 let (x, y) = (self.x().min(other.x()), self.y().min(other.y()));
146 Rect::new(x, y, self.max_x().max(other.max_x()) - x, self.max_y().max(other.max_y()) - y)
147 }
148
149 pub fn intersection(&self, other: &Rect) -> Option<Rect> {
151 let x = self.x().max(other.x());
152 let y = self.y().max(other.y());
153 let max_x = self.max_x().min(other.max_x());
154 let max_y = self.max_y().min(other.max_y());
155 (max_x > x && max_y > y).then(|| Rect::new(x, y, max_x - x, max_y - y))
156 }
157}
158
159impl fmt::Display for Rect {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 write!(f, "{},{} {}×{}", Num(self.origin.x), Num(self.origin.y), Num(self.size.width), Num(self.size.height))
162 }
163}
164
165pub struct Num(pub f32);
167
168impl fmt::Display for Num {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 let rounded = (self.0 * 100.0).round() / 100.0;
171 if rounded == rounded.trunc() { write!(f, "{}", rounded as i64) } else { write!(f, "{rounded}") }
172 }
173}