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
use crate::Canvas;
use geom::{trim_f64, Polygon, Pt2D};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScreenPt {
pub x: f64,
pub y: f64,
}
impl ScreenPt {
pub fn new(x: f64, y: f64) -> ScreenPt {
ScreenPt { x, y }
}
pub fn to_pt(self) -> Pt2D {
Pt2D::new(self.x, self.y)
}
}
impl From<winit::dpi::LogicalPosition<f64>> for ScreenPt {
fn from(lp: winit::dpi::LogicalPosition<f64>) -> ScreenPt {
ScreenPt { x: lp.x, y: lp.y }
}
}
#[derive(Clone, Debug)]
pub struct ScreenRectangle {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
}
impl ScreenRectangle {
pub fn top_left(top_left: ScreenPt, dims: ScreenDims) -> ScreenRectangle {
ScreenRectangle {
x1: top_left.x,
y1: top_left.y,
x2: top_left.x + dims.width,
y2: top_left.y + dims.height,
}
}
pub fn placeholder() -> ScreenRectangle {
ScreenRectangle {
x1: 0.0,
y1: 0.0,
x2: 0.0,
y2: 0.0,
}
}
pub fn contains(&self, pt: ScreenPt) -> bool {
pt.x >= self.x1 && pt.x <= self.x2 && pt.y >= self.y1 && pt.y <= self.y2
}
pub fn pt_to_percent(&self, pt: ScreenPt) -> Option<(f64, f64)> {
if self.contains(pt) {
Some((
(pt.x - self.x1) / self.width(),
(pt.y - self.y1) / self.height(),
))
} else {
None
}
}
pub fn percent_to_pt(&self, x: f64, y: f64) -> ScreenPt {
ScreenPt::new(self.x1 + x * self.width(), self.y1 + y * self.height())
}
pub fn width(&self) -> f64 {
self.x2 - self.x1
}
pub fn height(&self) -> f64 {
self.y2 - self.y1
}
pub fn dims(&self) -> ScreenDims {
ScreenDims::new(self.x2 - self.x1, self.y2 - self.y1)
}
pub fn center(&self) -> ScreenPt {
ScreenPt::new((self.x1 + self.x2) / 2.0, (self.y1 + self.y2) / 2.0)
}
pub fn to_polygon(&self) -> Polygon {
Polygon::rectangle(self.width(), self.height()).translate(self.x1, self.y1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ScreenDims {
pub width: f64,
pub height: f64,
}
impl ScreenDims {
pub fn new(width: f64, height: f64) -> ScreenDims {
ScreenDims {
width: trim_f64(width),
height: trim_f64(height),
}
}
pub fn top_left_for_corner(&self, corner: ScreenPt, canvas: &Canvas) -> ScreenPt {
if corner.x + self.width < canvas.window_width {
if corner.y + self.height < canvas.window_height {
corner
} else {
ScreenPt::new(corner.x, corner.y - self.height)
}
} else {
if corner.y + self.height < canvas.window_height {
ScreenPt::new(corner.x - self.width, corner.y)
} else {
ScreenPt::new(corner.x - self.width, corner.y - self.height)
}
}
}
pub fn scaled(&self, factor: f64) -> ScreenDims {
ScreenDims::new(self.width * factor, self.height * factor)
}
}
impl From<winit::dpi::LogicalSize<f64>> for ScreenDims {
fn from(size: winit::dpi::LogicalSize<f64>) -> ScreenDims {
ScreenDims {
width: size.width,
height: size.height,
}
}
}
impl From<ScreenDims> for winit::dpi::LogicalSize<f64> {
fn from(dims: ScreenDims) -> winit::dpi::LogicalSize<f64> {
winit::dpi::LogicalSize::new(dims.width, dims.height)
}
}