1use num_traits::ToPrimitive;
2
3#[repr(C)]
4#[derive(Clone, Copy, Default, Debug)]
5pub struct Rect {
6 pub x: i32,
7 pub y: i32,
8 pub w: i32,
9 pub h: i32,
10}
11
12impl Rect {
13 pub fn new<T: ToPrimitive>(x: T, y: T, w: T, h: T) -> Self {
14 Self {
15 x: x.to_i32().unwrap_or(0),
16 y: y.to_i32().unwrap_or(0),
17 w: w.to_i32().unwrap_or(0),
18 h: h.to_i32().unwrap_or(0),
19 }
20 }
21
22 pub fn with_pos(x: i32, y: i32) -> Self {
23 Self { x, y, w: 0, h: 0 }
24 }
25
26 pub fn with_size(w: i32, h: i32) -> Self {
27 Self { x: 0, y: 0, w, h }
28 }
29
30 pub fn is_touch(&self, x: i32, y: i32) -> bool {
31 x >= self.x && x < self.x + self.w && y >= self.y && y < self.y + self.h
32 }
33
34 pub fn is_empty(&self) -> bool {
35 self.w <= 0 || self.h <= 0
36 }
37}
38
39impl PartialEq for Rect {
40 fn eq(&self, other: &Self) -> bool {
41 self.x == other.x && self.y == other.y && self.w == other.w && self.h == other.h
42 }
43}
44
45impl Into<wgpu::Extent3d> for Rect {
46 fn into(self) -> wgpu::Extent3d {
47 wgpu::Extent3d {
48 width: self.w as u32,
49 height: self.h as u32,
50 depth_or_array_layers: 1,
51 }
52 }
53}
54
55impl Eq for Rect {}
56
57#[repr(C)]
58#[derive(Clone, Copy, Debug)]
59pub struct RectF {
60 pub x: f32,
61 pub y: f32,
62 pub w: f32,
63 pub h: f32,
64}
65
66#[allow(dead_code)]
67impl RectF {
68 pub fn new<T: ToPrimitive>(x: T, y: T, w: T, h: T) -> Self {
69 Self {
70 x: x.to_f32().unwrap_or(0.0),
71 y: y.to_f32().unwrap_or(0.0),
72 w: w.to_f32().unwrap_or(0.0),
73 h: h.to_f32().unwrap_or(0.0),
74 }
75 }
76
77 pub fn with_pos(x: f32, y: f32) -> Self {
78 Self {
79 x,
80 y,
81 w: 0.0,
82 h: 0.0,
83 }
84 }
85
86 pub fn with_size(w: f32, h: f32) -> Self {
87 Self {
88 x: 0.0,
89 y: 0.0,
90 w,
91 h,
92 }
93 }
94
95 pub fn is_touch(&self, x: f32, y: f32) -> bool {
96 x >= self.x && x < self.x + self.w && y >= self.y && y < self.y + self.h
97 }
98
99 pub fn is_empty(&self) -> bool {
100 self.w <= 0.0 || self.h <= 0.0
101 }
102}
103
104impl PartialEq for RectF {
105 fn eq(&self, other: &Self) -> bool {
106 self.x == other.x && self.y == other.y && self.w == other.w && self.h == other.h
107 }
108}
109
110impl Eq for RectF {}