1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use std::ops::{Div, Mul};

/*
/// Units in logical points.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Logical;
*/

/// Units in physical pixels.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Physical;

/// A point in units of logical points.
///
/// Alias for ```euclid::default::Point2D<f32>```.
pub type Point = euclid::default::Point2D<f32>;

/// A point in units of logical points.
///
/// Alias for ```euclid::default::Point2D<i32>```.
pub type PointI32 = euclid::default::Point2D<i32>;

/// A vector in units of logical points.
///
/// Alias for ```euclid::default::Vector2D<f32>```.
pub type Vector = euclid::default::Vector2D<f32>;

/// A size in units of logical points.
///
/// Alias for ```euclid::default::Size2D<f32>```.
pub type Size = euclid::default::Size2D<f32>;

/// A size in units of logical points.
///
/// Alias for ```euclid::default::Size2D<i32>```.
pub type SizeI32 = euclid::default::Size2D<i32>;

/// Alias for ```euclid::default::Box2D<f32>```
pub type Box2D = euclid::default::Box2D<f32>;

/// Alias for ```euclid::default::Transform2D<f32>```
pub type Transform = euclid::default::Transform2D<f32>;

/// Alias for ```euclid::default::Rotation2D<f32>```
pub type Rotation = euclid::default::Rotation2D<f32>;

/// Alias for ```euclid::default::Translation2D<f32>```
pub type Translation = euclid::Translation2D<f32, euclid::UnknownUnit, euclid::UnknownUnit>;

/// Alias for ```euclid::default::Scale<f32>```
pub type Scale = euclid::default::Scale<f32>;

/// A rectangle in units of logical points.
///
/// Alias for ```euclid::default::Rect<f32>```
pub type Rect = euclid::default::Rect<f32>;

/// A rectangle in units of logical points.
///
/// Alias for ```euclid::default::Rect<i32>```
pub type RectI32 = euclid::default::Rect<i32>;

/// An angle in radians (f32).
///
/// Alias for ```euclid::Angle<f32>```
pub type Angle = euclid::Angle<f32>;

/*
/// A point in units of logical points.
pub type LogicalPoint = euclid::Point2D<f32, Logical>;
*/
/// A point in units of physical pixels.
pub type PhysicalPoint = euclid::Point2D<f32, Physical>;
/// A point in units of physical pixels.
pub type PhysicalPointU32 = euclid::Point2D<u32, Physical>;
/// A point in units of physical pixels.
pub type PhysicalPointI32 = euclid::Point2D<i32, Physical>;

/*
/// A size in units of logical points.
pub type LogicalSize = euclid::Size2D<f32, Logical>;
*/
/// A size in units of physical pixels.
pub type PhysicalSize = euclid::Size2D<f32, Physical>;
/// A size in units of physical pixels.
pub type PhysicalSizeU32 = euclid::Size2D<u32, Physical>;
/// A size in units of physical pixels.
pub type PhysicalSizeI32 = euclid::Size2D<i32, Physical>;

/*
/// A rectangle in units of logical points.
pub type LogicalRect = euclid::Rect<f32, Logical>;
*/
/// A rectagngle in units of physical pixels.
pub type PhysicalRect = euclid::Rect<f32, Physical>;
/// A rectagngle in units of physical pixels.
pub type PhysicalRectU32 = euclid::Rect<u32, Physical>;
/// A rectagngle in units of physical pixels.
pub type PhysicalRectI32 = euclid::Rect<i32, Physical>;

/// Convert a point from logical points to physical pixels.
#[inline]
pub fn to_physical_point(point: Point, scale_factor: ScaleFactor) -> PhysicalPoint {
    PhysicalPoint::new(point.x * scale_factor.0, point.y * scale_factor.0)
}
/// Convert a point from logical points to physical pixels.
#[inline]
pub fn to_logical_point(point: PhysicalPoint, scale_factor: ScaleFactor) -> Point {
    Point::new(point.x / scale_factor.0, point.y / scale_factor.0)
}
/// Convert a point from logical points to physical pixels.
#[inline]
pub fn to_logical_point_u32(point: PhysicalPointU32, scale_factor: ScaleFactor) -> Point {
    Point::new(
        point.x as f32 / scale_factor.0,
        point.y as f32 / scale_factor.0,
    )
}
/// Convert a point from logical points to physical pixels.
#[inline]
pub fn to_logical_point_i32(point: PhysicalPointI32, scale_factor: ScaleFactor) -> Point {
    Point::new(
        point.x as f32 / scale_factor.0,
        point.y as f32 / scale_factor.0,
    )
}
/// Convert a point from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_point_from_recip(point: PhysicalPoint, scale_factor_recip: f32) -> Point {
    Point::new(point.x * scale_factor_recip, point.y * scale_factor_recip)
}
/// Convert a point from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_point_from_recip_u32(point: PhysicalPointU32, scale_factor_recip: f32) -> Point {
    Point::new(
        point.x as f32 * scale_factor_recip,
        point.y as f32 * scale_factor_recip,
    )
}
/// Convert a point from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_point_from_recip_i32(point: PhysicalPointI32, scale_factor_recip: f32) -> Point {
    Point::new(
        point.x as f32 * scale_factor_recip,
        point.y as f32 * scale_factor_recip,
    )
}

/// Convert a size from logical points to physical pixels.
#[inline]
pub fn to_physical_size(size: Size, scale_factor: ScaleFactor) -> PhysicalSize {
    PhysicalSize::new(size.width * scale_factor.0, size.height * scale_factor.0)
}
/// Convert a size from logical points to physical pixels.
#[inline]
pub fn to_logical_size(size: PhysicalSize, scale_factor: ScaleFactor) -> Size {
    Size::new(size.width / scale_factor.0, size.height / scale_factor.0)
}
/// Convert a size from logical points to physical pixels.
#[inline]
pub fn to_logical_size_u32(size: PhysicalSizeU32, scale_factor: ScaleFactor) -> Size {
    Size::new(
        size.width as f32 / scale_factor.0,
        size.height as f32 / scale_factor.0,
    )
}
/// Convert a size from logical points to physical pixels.
#[inline]
pub fn to_logical_size_i32(size: PhysicalSizeI32, scale_factor: ScaleFactor) -> Size {
    Size::new(
        size.width as f32 / scale_factor.0,
        size.height as f32 / scale_factor.0,
    )
}
/// Convert a size from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_size_from_recip(size: PhysicalSize, scale_factor_recip: f32) -> Size {
    Size::new(
        size.width * scale_factor_recip,
        size.height * scale_factor_recip,
    )
}
/// Convert a size from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_size_from_recip_u32(size: PhysicalSizeU32, scale_factor_recip: f32) -> Size {
    Size::new(
        size.width as f32 * scale_factor_recip,
        size.height as f32 * scale_factor_recip,
    )
}
/// Convert a size from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_size_from_recip_i32(size: PhysicalSizeI32, scale_factor_recip: f32) -> Size {
    Size::new(
        size.width as f32 * scale_factor_recip,
        size.height as f32 * scale_factor_recip,
    )
}

/// Convert a rectangle from logical points to physical pixels.
#[inline]
pub fn to_physical_rect(rect: Rect, scale_factor: ScaleFactor) -> PhysicalRect {
    PhysicalRect::new(
        to_physical_point(rect.origin, scale_factor),
        to_physical_size(rect.size, scale_factor),
    )
}
/// Convert a rectangle from logical points to physical pixels.
#[inline]
pub fn to_logical_rect(rect: PhysicalRect, scale_factor: ScaleFactor) -> Rect {
    Rect::new(
        to_logical_point(rect.origin, scale_factor),
        to_logical_size(rect.size, scale_factor),
    )
}
/// Convert a rectangle from logical points to physical pixels.
#[inline]
pub fn to_logical_rect_u32(rect: PhysicalRectU32, scale_factor: ScaleFactor) -> Rect {
    Rect::new(
        to_logical_point_u32(rect.origin, scale_factor),
        to_logical_size_u32(rect.size, scale_factor),
    )
}
/// Convert a rectangle from logical points to physical pixels.
#[inline]
pub fn to_logical_rect_i32(rect: PhysicalRectI32, scale_factor: ScaleFactor) -> Rect {
    Rect::new(
        to_logical_point_i32(rect.origin, scale_factor),
        to_logical_size_i32(rect.size, scale_factor),
    )
}
/// Convert a rectangle from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_rect_from_recip(rect: PhysicalRect, scale_factor_recip: f32) -> Rect {
    Rect::new(
        to_logical_point_from_recip(rect.origin, scale_factor_recip),
        to_logical_size_from_recip(rect.size, scale_factor_recip),
    )
}
/// Convert a rectangle from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_rect_from_recip_u32(rect: PhysicalRectU32, scale_factor_recip: f32) -> Rect {
    Rect::new(
        to_logical_point_from_recip_u32(rect.origin, scale_factor_recip),
        to_logical_size_from_recip_u32(rect.size, scale_factor_recip),
    )
}
/// Convert a rectangle from logical points to physical pixels using the reciprocal of the scale factor.
#[inline]
pub fn to_logical_rect_from_recip_i32(rect: PhysicalRectI32, scale_factor_recip: f32) -> Rect {
    Rect::new(
        to_logical_point_from_recip_i32(rect.origin, scale_factor_recip),
        to_logical_size_from_recip_i32(rect.size, scale_factor_recip),
    )
}

/// Shorthand for `Vector::new(x, y)`.
#[inline]
pub fn vector(x: f32, y: f32) -> Vector {
    Vector::new(x, y)
}

/// Shorthand for `Point::new(x, y)`.
#[inline]
pub fn point(x: f32, y: f32) -> Point {
    Point::new(x, y)
}

/// Shorthand for `Size::new(x, y)`.
#[inline]
pub fn size(w: f32, h: f32) -> Size {
    Size::new(w, h)
}

/// Shorthand for `Angle { radians: value }`.
#[inline]
pub fn radians(radians: f32) -> Angle {
    Angle { radians }
}

/// A scaling factor in points per pixel.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct ScaleFactor(pub f32);

impl ScaleFactor {
    pub fn new(scale_factor: f32) -> Self {
        Self(scale_factor)
    }

    pub fn recip(&self) -> f32 {
        self.0.recip()
    }
}

impl Default for ScaleFactor {
    fn default() -> Self {
        Self(1.0)
    }
}

impl From<f32> for ScaleFactor {
    fn from(s: f32) -> Self {
        Self(s)
    }
}

impl From<f64> for ScaleFactor {
    fn from(s: f64) -> Self {
        Self(s as f32)
    }
}

impl From<ScaleFactor> for f32 {
    fn from(s: ScaleFactor) -> Self {
        s.0
    }
}

impl From<ScaleFactor> for f64 {
    fn from(s: ScaleFactor) -> Self {
        s.0 as f64
    }
}

impl Mul<ScaleFactor> for f32 {
    type Output = f32;
    fn mul(self, rhs: ScaleFactor) -> Self::Output {
        self * rhs.0
    }
}

impl Div<ScaleFactor> for f32 {
    type Output = f32;
    fn div(self, rhs: ScaleFactor) -> Self::Output {
        self / rhs.0
    }
}

/// Returns a scaling vector that can be used to convert screen coordinates
/// to clip coordinates in shaders.
pub fn screen_to_clip_scale(screen_size: PhysicalSizeI32, scale_factor: ScaleFactor) -> [f32; 2] {
    [
        2.0 * scale_factor * (screen_size.width as f32).recip(),
        2.0 * scale_factor * (screen_size.height as f32).recip(),
    ]
}