summaryrefslogtreecommitdiff
path: root/engine/src/windowing/dpi.rs
blob: e11fa251e883c30a2e4f65697365582e74b45ba8 (plain)
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
macro_rules! gen_struct_from_to_impls {
    ($struct: ident, fields=($($field: ident),*)) => {
        impl<Pixel> From<winit::dpi::$struct<Pixel>> for $struct<Pixel>
        {
            fn from(source: winit::dpi::$struct<Pixel>) -> Self
            {
                Self {
                    $($field: source.$field),*
                }
            }
        }

        impl<Pixel> From<$struct<Pixel>> for winit::dpi::$struct<Pixel>
        {
            fn from(source: $struct<Pixel>) -> Self
            {
                Self {
                    $($field: source.$field),*
                }
            }
        }
    };
}

macro_rules! gen_enum_from_to_impls {
    ($enum: ident, variants=($($variant: ident),*)) => {
        impl From<winit::dpi::$enum> for $enum
        {
            fn from(source: winit::dpi::$enum) -> Self
            {
                match source {
                    $(winit::dpi::$enum::$variant(pos) => Self::$variant(pos.into())),*
                }
            }
        }

        impl From<$enum> for winit::dpi::$enum
        {
            fn from(source: $enum) -> Self
            {
                match source {
                    $($enum::$variant(pos) => Self::$variant(pos.into())),*
                }
            }
        }
    };
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct PhysicalPosition<Pixel>
{
    pub x: Pixel,
    pub y: Pixel
}

impl<Pixel> PhysicalPosition<Pixel>
{
    pub fn to_logical<LogicalPixel>(&self, scale_factor: f64) -> LogicalPosition<LogicalPixel>
    where
        Pixel: Into<f64> + Clone,
        LogicalPixel: From<f64>
    {
        let x = self.x.clone().into();
        let y = self.y.clone().into();

        LogicalPosition {
            x: (x / scale_factor).into(),
            y: (y / scale_factor).into()
        }
    }

    pub fn try_convert_from<SourcePixel>(source: PhysicalPosition<SourcePixel>) -> Result<Self, Pixel::Error>
    where
        Pixel: TryFrom<SourcePixel>
    {
        Ok(Self {
            x: source.x.try_into()?,
            y: source.y.try_into()?
        })
    }
}

gen_struct_from_to_impls!(PhysicalPosition, fields=(x, y));

#[derive(Debug, Default, Clone, PartialEq)]
pub struct LogicalPosition<Pixel>
{
    pub x: Pixel,
    pub y: Pixel
}

impl<Pixel> LogicalPosition<Pixel>
{
    pub fn try_convert_from<SourcePixel>(source: LogicalPosition<SourcePixel>) -> Result<Self, Pixel::Error>
    where
        Pixel: TryFrom<SourcePixel>
    {
        Ok(Self {
            x: source.x.try_into()?,
            y: source.y.try_into()?
        })
    }
}

gen_struct_from_to_impls!(LogicalPosition, fields=(x, y));

#[derive(Debug, Clone, PartialEq)]
pub enum Position
{
    Physical(PhysicalPosition<i32>),
    Logical(LogicalPosition<f64>)
}

gen_enum_from_to_impls!(Position, variants=(Physical, Logical));

#[derive(Debug, Default, Clone, PartialEq)]
pub struct PhysicalSize<Pixel>
{
    pub width: Pixel,
    pub height: Pixel
}

impl<Pixel> PhysicalSize<Pixel>
{
    pub fn to_logical<LogicalPixel>(&self, scale_factor: f64) -> LogicalSize<LogicalPixel>
    where
        Pixel: Into<f64> + Clone,
        LogicalPixel: From<f64>
    {
        let width = self.width.clone().into();
        let height = self.height.clone().into();

        LogicalSize {
            width: (width / scale_factor).into(),
            height: (height / scale_factor).into()
        }
    }
}

impl<Pixel> PhysicalSize<Pixel>
{
    pub fn try_convert_from<SourcePixel>(source: PhysicalSize<SourcePixel>) -> Result<Self, Pixel::Error>
    where
        Pixel: TryFrom<SourcePixel>
    {
        Ok(Self {
            width: source.width.try_into()?,
            height: source.height.try_into()?
        })
    }
}

gen_struct_from_to_impls!(PhysicalSize, fields=(width, height));

#[derive(Debug, Default, Clone, PartialEq)]
pub struct LogicalSize<Pixel>
{
    pub width: Pixel,
    pub height: Pixel
}

impl<Pixel> LogicalSize<Pixel>
{
    pub fn try_convert_from<SourcePixel>(source: LogicalSize<SourcePixel>) -> Result<Self, Pixel::Error>
    where
        Pixel: TryFrom<SourcePixel>
    {
        Ok(Self {
            width: source.width.try_into()?,
            height: source.height.try_into()?
        })
    }
}

gen_struct_from_to_impls!(LogicalSize, fields=(width, height));

#[derive(Debug, Clone, PartialEq)]
pub enum Size
{
    Physical(PhysicalSize<u32>),
    Logical(LogicalSize<f64>)
}

gen_enum_from_to_impls!(Size, variants=(Physical, Logical));