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
use serde::{Deserialize, Serialize};

use geom::{trim_f64, Polygon, Pt2D};

use crate::{Canvas, EdgeInsets};

/// ScreenPt is in units of logical pixels, as opposed to physical pixels.
#[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 }
    }

    // The geom layer operates in map-space, but currently reusing lots of geom abstractions for
    // screen-space.
    pub fn to_pt(self) -> Pt2D {
        Pt2D::new(self.x, self.y)
    }

    pub fn zero() -> Self {
        Self { x: 0.0, y: 0.0 }
    }

    pub fn translated(&self, x: f64, y: f64) -> Self {
        Self {
            x: self.x + x,
            y: self.y + y,
        }
    }
}

impl From<winit::dpi::LogicalPosition<f64>> for ScreenPt {
    fn from(lp: winit::dpi::LogicalPosition<f64>) -> ScreenPt {
        ScreenPt { x: lp.x, y: lp.y }
    }
}

/// ScreenRectangle is in units of logical pixels, as opposed to physical pixels.
#[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())
    }

    // TODO Remove these in favor of dims()
    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)
    }
}

// REVIEW: Rename to something shorter? e.g. Dims / Size
/// ScreenDims is in units of logical pixels, as opposed to physical pixels.
#[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 zero() -> Self {
        ScreenDims {
            width: 0.0,
            height: 0.0,
        }
    }

    pub fn square(square: f64) -> Self {
        Self::new(square, square)
    }

    pub fn pad(&self, edge_insets: EdgeInsets) -> Self {
        Self {
            width: self.width + edge_insets.left + edge_insets.right,
            height: self.height + edge_insets.top + edge_insets.bottom,
        }
    }

    pub fn top_left_for_corner(&self, corner: ScreenPt, canvas: &Canvas) -> ScreenPt {
        // TODO Ideally also avoid covered canvas areas
        if corner.x + self.width < canvas.window_width {
            // corner.x is the left corner
            if corner.y + self.height < canvas.window_height {
                // corner.y is the top corner
                corner
            } else {
                // corner.y is the bottom corner
                ScreenPt::new(corner.x, corner.y - self.height)
            }
        } else {
            // corner.x is the right corner
            if corner.y + self.height < canvas.window_height {
                // corner.y is the top corner
                ScreenPt::new(corner.x - self.width, corner.y)
            } else {
                // corner.y is the bottom corner
                ScreenPt::new(corner.x - self.width, corner.y - self.height)
            }
        }
    }
}

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)
    }
}

impl From<f64> for ScreenDims {
    fn from(square: f64) -> ScreenDims {
        ScreenDims::square(square)
    }
}

/// (Width, Height) -> ScreenDims
impl From<(f64, f64)> for ScreenDims {
    fn from(width_and_height: (f64, f64)) -> ScreenDims {
        ScreenDims::new(width_and_height.0, width_and_height.1)
    }
}

impl From<geom::Bounds> for ScreenDims {
    fn from(bounds: geom::Bounds) -> Self {
        ScreenDims::new(bounds.width(), bounds.height())
    }
}

impl From<ScreenDims> for stretch::geometry::Size<stretch::style::Dimension> {
    fn from(dims: ScreenDims) -> Self {
        Self {
            width: stretch::style::Dimension::Points(dims.width as f32),
            height: stretch::style::Dimension::Points(dims.height as f32),
        }
    }
}