est_render/math/
position.rs1use num_traits::ToPrimitive;
2use winit::dpi::PhysicalPosition;
3
4#[derive(Clone, Copy, Debug)]
5pub struct Position {
6 pub x: i32,
7 pub y: i32,
8}
9
10impl Position {
11 pub fn new<T: ToPrimitive>(x: T, y: T) -> Self {
12 Self {
13 x: x.to_i32().unwrap_or(0),
14 y: y.to_i32().unwrap_or(0),
15 }
16 }
17
18 pub fn zero() -> Self {
19 Self { x: 0, y: 0 }
20 }
21
22 pub fn one() -> Self {
23 Self { x: 1, y: 1 }
24 }
25}
26
27impl Default for Position {
28 fn default() -> Self {
29 Self::zero()
30 }
31}
32
33use std::ops::*;
34
35impl Add for Position {
36 type Output = Self;
37
38 fn add(self, rhs: Self) -> Self {
39 Self {
40 x: self.x + rhs.x,
41 y: self.y + rhs.y,
42 }
43 }
44}
45
46impl AddAssign for Position {
47 fn add_assign(&mut self, rhs: Self) {
48 self.x += rhs.x;
49 self.y += rhs.y;
50 }
51}
52
53impl Sub for Position {
54 type Output = Self;
55
56 fn sub(self, rhs: Self) -> Self {
57 Self {
58 x: self.x - rhs.x,
59 y: self.y - rhs.y,
60 }
61 }
62}
63
64impl SubAssign for Position {
65 fn sub_assign(&mut self, rhs: Self) {
66 self.x -= rhs.x;
67 self.y -= rhs.y;
68 }
69}
70
71impl Mul<i32> for Position {
72 type Output = Self;
73
74 fn mul(self, rhs: i32) -> Self {
75 Self {
76 x: self.x * rhs,
77 y: self.y * rhs,
78 }
79 }
80}
81
82impl MulAssign<i32> for Position {
83 fn mul_assign(&mut self, rhs: i32) {
84 self.x *= rhs;
85 self.y *= rhs;
86 }
87}
88
89impl Div<i32> for Position {
90 type Output = Self;
91
92 fn div(self, rhs: i32) -> Self {
93 Self {
94 x: self.x / rhs,
95 y: self.y / rhs,
96 }
97 }
98}
99
100impl DivAssign<i32> for Position {
101 fn div_assign(&mut self, rhs: i32) {
102 self.x /= rhs;
103 self.y /= rhs;
104 }
105}
106
107impl Neg for Position {
108 type Output = Self;
109
110 fn neg(self) -> Self {
111 Self {
112 x: -self.x,
113 y: -self.y,
114 }
115 }
116}
117
118impl PartialEq for Position {
119 fn eq(&self, other: &Self) -> bool {
120 self.x == other.x && self.y == other.y
121 }
122}
123
124impl Eq for Position {}
125
126impl Into<PhysicalPosition<u32>> for Position {
127 fn into(self) -> PhysicalPosition<u32> {
128 PhysicalPosition::new(self.x as u32, self.y as u32)
129 }
130}
131
132impl Into<PhysicalPosition<i32>> for Position {
133 fn into(self) -> PhysicalPosition<i32> {
134 PhysicalPosition::new(self.x, self.y)
135 }
136}
137
138impl From<PhysicalPosition<i32>> for Position {
139 fn from(pos: PhysicalPosition<i32>) -> Self {
140 Self { x: pos.x, y: pos.y }
141 }
142}
143
144impl From<PhysicalPosition<u32>> for Position {
145 fn from(pos: PhysicalPosition<u32>) -> Self {
146 Self {
147 x: pos.x as i32,
148 y: pos.y as i32,
149 }
150 }
151}