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
/// A position in logical coordinate.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct LogicalPosition<T> {
    pub x: T,
    pub y: T,
}

impl<T> LogicalPosition<T> {
    pub fn new(x: T, y: T) -> Self {
        Self { x, y }
    }
}

/// A position in physical coordinate.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct PhysicalPosition<T> {
    pub x: T,
    pub y: T,
}

impl<T> PhysicalPosition<T> {
    pub fn new(x: T, y: T) -> Self {
        Self { x, y }
    }
}

/// A size in logical coordinate.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct LogicalSize<T> {
    pub width: T,
    pub height: T,
}

impl<T> LogicalSize<T> {
    pub fn new(width: T, height: T) -> Self {
        Self { width, height }
    }
}

/// A size in physical coordinate.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct PhysicalSize<T> {
    pub width: T,
    pub height: T,
}

impl<T> PhysicalSize<T> {
    pub fn new(width: T, height: T) -> Self {
        Self { width, height }
    }
}

/// Converts to a logical position.
pub trait ToLogicalPosition<T> {
    fn to_logical(&self, scale: T) -> LogicalPosition<T>;
}

/// Converts to a physical position.
pub trait ToPhysicalPosition<T> {
    fn to_physical(&self, scale: T) -> PhysicalPosition<T>;
}

/// Converts to a logical size.
pub trait ToLogicalSize<T> {
    fn to_logical(&self, scale: T) -> LogicalSize<T>;
}

/// Converts to a physical size.
pub trait ToPhysicalSize<T> {
    fn to_physical(&self, scale: T) -> PhysicalSize<T>;
}

impl<T: Clone> ToLogicalPosition<T> for LogicalPosition<T> {
    fn to_logical(&self, _: T) -> LogicalPosition<T> {
        self.clone()
    }
}

impl<T> ToLogicalPosition<T> for PhysicalPosition<T>
where
    T: std::ops::Div<T, Output = T> + Copy,
{
    fn to_logical(&self, scale: T) -> LogicalPosition<T> {
        LogicalPosition {
            x: self.x / scale,
            y: self.y / scale,
        }
    }
}

impl<T> ToPhysicalPosition<T> for LogicalPosition<T>
where
    T: std::ops::Mul<T, Output = T> + Copy,
{
    fn to_physical(&self, scale: T) -> PhysicalPosition<T> {
        PhysicalPosition {
            x: self.x * scale,
            y: self.y * scale,
        }
    }
}

impl<T: Clone> ToPhysicalPosition<T> for PhysicalPosition<T> {
    fn to_physical(&self, _: T) -> PhysicalPosition<T> {
        self.clone()
    }
}

impl<T: Clone> ToLogicalSize<T> for LogicalSize<T> {
    fn to_logical(&self, _: T) -> LogicalSize<T> {
        self.clone()
    }
}

impl<T> ToLogicalSize<T> for PhysicalSize<T>
where
    T: std::ops::Div<T, Output = T> + Copy,
{
    fn to_logical(&self, scale: T) -> LogicalSize<T> {
        LogicalSize {
            width: self.width / scale,
            height: self.height / scale,
        }
    }
}

impl<T> ToPhysicalSize<T> for LogicalSize<T>
where
    T: std::ops::Mul<T, Output = T> + Copy,
{
    fn to_physical(&self, scale: T) -> PhysicalSize<T> {
        PhysicalSize {
            width: self.width * scale,
            height: self.height * scale,
        }
    }
}

impl<T: Clone> ToPhysicalSize<T> for PhysicalSize<T> {
    fn to_physical(&self, _: T) -> PhysicalSize<T> {
        self.clone()
    }
}

/// A position in screen coordinate.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ScreenPosition {
    pub x: i32,
    pub y: i32,
}

impl ScreenPosition {
    pub fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn logical_to_logical_position() {
        let src = LogicalPosition::new(128.0, 256.0);
        let dest = src.to_logical(2.0f32);
        assert!((dest.x - src.x).abs() <= std::f32::EPSILON);
        assert!((dest.y - src.y).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn logical_to_physical_position() {
        let src = LogicalPosition::new(128.0, 256.0);
        let dest = src.to_physical(2.0f32);
        assert!((dest.x - src.x * 2.0).abs() <= std::f32::EPSILON);
        assert!((dest.y - src.y * 2.0).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn physical_to_physical_position() {
        let src = PhysicalPosition::new(128.0, 256.0);
        let dest = src.to_physical(2.0f32);
        assert!((dest.x - src.x).abs() <= std::f32::EPSILON);
        assert!((dest.y - src.y).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn physical_to_logical_position() {
        let src = PhysicalPosition::new(128.0, 256.0);
        let dest = src.to_logical(2.0f32);
        assert!((dest.x - src.x / 2.0).abs() <= std::f32::EPSILON);
        assert!((dest.y - src.y / 2.0).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn logical_to_logical_size() {
        let src = LogicalSize::new(128.0, 256.0);
        let dest = src.to_logical(2.0f32);
        assert!((dest.width - src.width).abs() <= std::f32::EPSILON);
        assert!((dest.height - src.height).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn logical_to_physical_size() {
        let src = LogicalSize::new(128.0, 256.0);
        let dest = src.to_physical(2.0f32);
        assert!((dest.width - src.width * 2.0).abs() <= std::f32::EPSILON);
        assert!((dest.height - src.height * 2.0).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn physical_to_physical_size() {
        let src = PhysicalSize::new(128.0, 256.0);
        let dest = src.to_physical(2.0f32);
        assert!((dest.width - src.width).abs() <= std::f32::EPSILON);
        assert!((dest.height - src.height).abs() <= std::f32::EPSILON);
    }

    #[test]
    fn physical_to_logical_size() {
        let src = PhysicalSize::new(128.0, 256.0);
        let dest = src.to_logical(2.0f32);
        assert!((dest.width - src.width / 2.0).abs() <= std::f32::EPSILON);
        assert!((dest.height - src.height / 2.0).abs() <= std::f32::EPSILON);
    }
}