mitsuami_core/
geometry.rs1use std::fmt;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq)]
5pub struct Size {
6 pub width: f32,
7 pub height: f32,
8}
9
10impl Size {
11 pub const ZERO: Size = Size { width: 0.0, height: 0.0 };
12
13 pub const fn new(width: f32, height: f32) -> Size {
14 Size { width, height }
15 }
16
17 pub fn is_empty(&self) -> bool {
18 self.width <= 0.0 || self.height <= 0.0
19 }
20}
21
22#[derive(Clone, Copy, Debug, PartialEq)]
24pub enum WindowSize {
25 Fixed(Size),
26 FitHeight(f32),
30}
31
32impl From<Size> for WindowSize {
33 fn from(size: Size) -> WindowSize {
34 WindowSize::Fixed(size)
35 }
36}
37
38#[derive(Clone, Copy, Debug, Default, PartialEq)]
39pub struct Point {
40 pub x: f32,
41 pub y: f32,
42}
43
44impl Point {
45 pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
46
47 pub const fn new(x: f32, y: f32) -> Point {
48 Point { x, y }
49 }
50}
51
52#[derive(Clone, Copy, Debug, Default, PartialEq)]
54pub struct Rect {
55 pub origin: Point,
56 pub size: Size,
57}
58
59impl Rect {
60 pub const ZERO: Rect = Rect { origin: Point::ZERO, size: Size::ZERO };
61
62 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
63 Rect { origin: Point { x, y }, size: Size { width, height } }
64 }
65
66 pub fn x(&self) -> f32 {
67 self.origin.x
68 }
69
70 pub fn y(&self) -> f32 {
71 self.origin.y
72 }
73
74 pub fn width(&self) -> f32 {
75 self.size.width
76 }
77
78 pub fn height(&self) -> f32 {
79 self.size.height
80 }
81
82 pub fn max_x(&self) -> f32 {
83 self.origin.x + self.size.width
84 }
85
86 pub fn max_y(&self) -> f32 {
87 self.origin.y + self.size.height
88 }
89
90 pub fn offset(&self, by: Point) -> Rect {
91 Rect { origin: Point::new(self.origin.x + by.x, self.origin.y + by.y), size: self.size }
92 }
93
94 pub fn intersection(&self, other: &Rect) -> Option<Rect> {
96 let x = self.x().max(other.x());
97 let y = self.y().max(other.y());
98 let max_x = self.max_x().min(other.max_x());
99 let max_y = self.max_y().min(other.max_y());
100 (max_x > x && max_y > y).then(|| Rect::new(x, y, max_x - x, max_y - y))
101 }
102}
103
104impl fmt::Display for Rect {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 write!(f, "{},{} {}×{}", Num(self.origin.x), Num(self.origin.y), Num(self.size.width), Num(self.size.height))
107 }
108}
109
110pub struct Num(pub f32);
112
113impl fmt::Display for Num {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 let rounded = (self.0 * 100.0).round() / 100.0;
116 if rounded == rounded.trunc() { write!(f, "{}", rounded as i64) } else { write!(f, "{rounded}") }
117 }
118}