Bladeren bron

Update glutin (to 0.17) and become DPI aware

Johannes Hofmann 7 jaren geleden
bovenliggende
commit
cd29a5f0c8
6 gewijzigde bestanden met toevoegingen van 78 en 42 verwijderingen
  1. 6
    6
      Cargo.lock
  2. 1
    1
      Cargo.toml
  3. 2
    2
      src/coord.rs
  4. 49
    29
      src/main.rs
  5. 16
    2
      src/map_view_gl.rs
  6. 4
    2
      src/marker_layer.rs

+ 6
- 6
Cargo.lock Bestand weergeven

@@ -280,7 +280,7 @@ dependencies = [
280 280
  "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
281 281
  "gl 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
282 282
  "gl_generator 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
283
- "glutin 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
283
+ "glutin 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
284 284
  "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
285 285
  "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
286 286
  "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -431,7 +431,7 @@ dependencies = [
431 431
 
432 432
 [[package]]
433 433
 name = "glutin"
434
-version = "0.16.0"
434
+version = "0.17.0"
435 435
 source = "registry+https://github.com/rust-lang/crates.io-index"
436 436
 dependencies = [
437 437
  "android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -447,7 +447,7 @@ dependencies = [
447 447
  "shared_library 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
448 448
  "wayland-client 0.20.10 (registry+https://github.com/rust-lang/crates.io-index)",
449 449
  "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
450
- "winit 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
450
+ "winit 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
451 451
  "x11-dl 2.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
452 452
 ]
453 453
 
@@ -1709,7 +1709,7 @@ dependencies = [
1709 1709
 
1710 1710
 [[package]]
1711 1711
 name = "winit"
1712
-version = "0.15.1"
1712
+version = "0.16.0"
1713 1713
 source = "registry+https://github.com/rust-lang/crates.io-index"
1714 1714
 dependencies = [
1715 1715
  "android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1807,7 +1807,7 @@ dependencies = [
1807 1807
 "checksum gl 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "81457bb802910ad5b535eb48541c51830a761804aa5b7087adbc9d049aa57aca"
1808 1808
 "checksum gl_generator 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a795170cbd85b5a7baa58d6d7525cae6a03e486859860c220f7ebbbdd379d0a"
1809 1809
 "checksum gleam 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "12b793fcf40a23dd372f184c228ab3eb96f88c50bb4fba8319c483aa025a4e45"
1810
-"checksum glutin 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "42fb2de780307bd2bedbe013bc585659a683e7c6307d0baa878aec3da9250fc1"
1810
+"checksum glutin 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a70c5fe78efbd5a3b243a804ea1032053c584510f8822819f94cfb29b2100317"
1811 1811
 "checksum httparse 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23801d98b42eed0318e5709b0527894ba7c3793d0236814618d6a9b6224152ff"
1812 1812
 "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"
1813 1813
 "checksum hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)" = "34a590ca09d341e94cddf8e5af0bbccde205d5fbc2fa3c09dd67c7f85cea59d7"
@@ -1953,7 +1953,7 @@ dependencies = [
1953 1953
 "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
1954 1954
 "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
1955 1955
 "checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767"
1956
-"checksum winit 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4bb6ea35de8bd722201914b28a33d503ee45b09fcf85fc4bbf574845e41f79e2"
1956
+"checksum winit 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b9612edff5352d247ca263aa28e9a902ba80981cf4d3e88caf1833366ba9d354"
1957 1957
 "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
1958 1958
 "checksum x11-dl 2.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "931d8584b49771143af0c422f372d8aef4280afd5920dad39b0a95a8e51df1e9"
1959 1959
 "checksum xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2"

+ 1
- 1
Cargo.toml Bestand weergeven

@@ -16,7 +16,7 @@ clap = "2.29"
16 16
 directories = "0.10"
17 17
 env_logger = "0.5.0-rc.2"
18 18
 gl = "0.10"
19
-glutin = "0.16"
19
+glutin = "0.17"
20 20
 image = "0.19"
21 21
 lazy_static = "1.0"
22 22
 linked-hash-map = "0.5.0"

+ 2
- 2
src/coord.rs Bestand weergeven

@@ -156,7 +156,7 @@ impl MapCoord {
156 156
     }
157 157
 }
158 158
 
159
-/// A position on the screen in pixels. Top-left corner is (0.0, 0.0).
159
+/// A position on the screen in physical pixels. Top-left corner is (0.0, 0.0).
160 160
 #[derive(Copy, Clone, Debug)]
161 161
 pub struct ScreenCoord {
162 162
     pub x: f64,
@@ -181,7 +181,7 @@ impl ScreenCoord {
181 181
     }
182 182
 }
183 183
 
184
-/// A rectangle in screen coordinates.
184
+/// A rectangle in screen coordinates. Position and size are in physical pixels.
185 185
 #[derive(Copy, Clone, Debug)]
186 186
 pub struct ScreenRect {
187 187
     pub x: f64,

+ 49
- 29
src/main.rs Bestand weergeven

@@ -44,6 +44,7 @@ pub mod vertex_attrib;
44 44
 
45 45
 use coord::{LatLonDeg, ScreenCoord};
46 46
 use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
47
+use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalPosition};
47 48
 use map_view_gl::MapViewGl;
48 49
 use std::error::Error;
49 50
 use std::sync::mpsc;
@@ -51,11 +52,11 @@ use std::time::{Duration, Instant};
51 52
 use tile_source::TileSource;
52 53
 
53 54
 
54
-#[derive(Copy, Clone, Debug, PartialEq, Eq)]
55
+#[derive(Copy, Clone, Debug, PartialEq)]
55 56
 enum Action {
56 57
     Nothing,
57 58
     Redraw,
58
-    Resize(u32, u32),
59
+    Resize(LogicalSize),
59 60
     Close,
60 61
 }
61 62
 
@@ -63,8 +64,8 @@ impl Action {
63 64
     fn combine_with(&mut self, newer_action: Self) {
64 65
         *self = match (*self, newer_action) {
65 66
             (Action::Close, _) | (_, Action::Close) => Action::Close,
66
-            (Action::Resize(..), Action::Resize(w, h)) => Action::Resize(w, h),
67
-            (Action::Resize(w, h), _) | (_, Action::Resize(w, h)) => Action::Resize(w, h),
67
+            (Action::Resize(..), Action::Resize(size)) => Action::Resize(size),
68
+            (Action::Resize(size), _) | (_, Action::Resize(size)) => Action::Resize(size),
68 69
             (Action::Redraw, _) | (_, Action::Redraw) => Action::Redraw,
69 70
             (Action::Nothing, Action::Nothing) => Action::Nothing,
70 71
         };
@@ -73,8 +74,10 @@ impl Action {
73 74
 
74 75
 #[derive(Copy, Clone, Debug, PartialEq)]
75 76
 struct InputState {
76
-    mouse_position: (f64, f64),
77
+    mouse_position: LogicalPosition,
77 78
     mouse_pressed: bool,
79
+    viewport_size: LogicalSize,
80
+    dpi_factor: f64,
78 81
 }
79 82
 
80 83
 fn handle_event(
@@ -101,46 +104,51 @@ fn handle_event(
101 104
                 input_state.mouse_pressed = false;
102 105
                 Action::Nothing
103 106
             },
104
-            WindowEvent::CursorMoved { position: (x, y), .. } => {
107
+            WindowEvent::CursorMoved { position: pos, .. } => {
105 108
                 if input_state.mouse_pressed {
106 109
                     map.move_pixel(
107
-                        input_state.mouse_position.0 - x,
108
-                        input_state.mouse_position.1 - y,
110
+                        (input_state.mouse_position.x - pos.x) * input_state.dpi_factor,
111
+                        (input_state.mouse_position.y - pos.y) * input_state.dpi_factor,
109 112
                     );
110
-                    input_state.mouse_position = (x, y);
113
+                    input_state.mouse_position = pos;
111 114
                     Action::Redraw
112 115
                 } else {
113
-                    input_state.mouse_position = (x, y);
116
+                    input_state.mouse_position = pos;
114 117
                     Action::Nothing
115 118
                 }
116 119
             },
117 120
             WindowEvent::MouseWheel { delta, modifiers, .. } => {
118
-                let (dx, dy) = match delta {
121
+                let delta: PhysicalPosition = match delta {
119 122
                     MouseScrollDelta::LineDelta(dx, dy) => {
120 123
                         // filter strange wheel events with huge values.
121 124
                         // (maybe this is just a personal touchpad driver issue)
122 125
                         if dx.abs() < 16.0 && dy.abs() < 16.0 {
123 126
                             //TODO find a sensible line height value (servo (the glutin port) uses 38)
124
-                            (dx, dy * 38.0)
127
+                            PhysicalPosition::new(
128
+                                f64::from(dx) * input_state.dpi_factor,
129
+                                f64::from(dy) * 38.0 * input_state.dpi_factor,
130
+                            )
125 131
                         } else {
126
-                            (0.0, 0.0)
132
+                            PhysicalPosition::new(0.0, 0.0)
127 133
                         }
128 134
                     },
129
-                    MouseScrollDelta::PixelDelta(dx, dy) => (dx, dy),
135
+                    MouseScrollDelta::PixelDelta(size) => {
136
+                        size.to_physical(input_state.dpi_factor)
137
+                    }
130 138
                 };
131 139
 
132 140
                 //TODO add option for default mouse wheel behavior (scroll or zoom?)
133 141
                 //TODO add option to reverse scroll/zoom direction
134 142
 
135 143
                 if modifiers.ctrl {
136
-                    map.move_pixel(f64::from(-dx), f64::from(-dy));
144
+                    map.move_pixel(-delta.x, -delta.y);
137 145
                 } else {
138 146
                     map.zoom_at(
139 147
                         ScreenCoord::new(
140
-                            input_state.mouse_position.0,
141
-                            input_state.mouse_position.1,
148
+                            input_state.mouse_position.x * input_state.dpi_factor,
149
+                            input_state.mouse_position.y * input_state.dpi_factor,
142 150
                         ),
143
-                        f64::from(dy) * (1.0 / 320.0),
151
+                        f64::from(delta.y) * (1.0 / 320.0),
144 152
                     );
145 153
                 }
146 154
                 Action::Redraw
@@ -210,9 +218,15 @@ fn handle_event(
210 218
             WindowEvent::Refresh => {
211 219
                 Action::Redraw
212 220
             },
213
-            WindowEvent::Resized(w, h) => {
214
-                Action::Resize(w, h)
221
+            WindowEvent::Resized(size) => {
222
+                input_state.viewport_size = size;
223
+                Action::Resize(size)
215 224
             },
225
+            WindowEvent::HiDpiFactorChanged(dpi_factor) => {
226
+                input_state.dpi_factor = dpi_factor;
227
+                map.set_dpi_factor(dpi_factor);
228
+                Action::Resize(input_state.viewport_size)
229
+            }
216 230
             _ => Action::Nothing,
217 231
         },
218 232
         _ => Action::Nothing,
@@ -258,12 +272,20 @@ fn run() -> Result<(), Box<Error>> {
258 272
     let _ = unsafe { gl_window.make_current() };
259 273
     let mut cx = context::Context::from_gl_window(&gl_window);
260 274
 
275
+    let mut input_state = InputState {
276
+        mouse_position: LogicalPosition::new(0.0, 0.0),
277
+        mouse_pressed: false,
278
+        viewport_size: window.get_inner_size().unwrap(),
279
+        dpi_factor: window.get_hidpi_factor(),
280
+    };
281
+
261 282
     let mut map = {
262 283
         let proxy = events_loop.create_proxy();
263 284
 
264 285
         map_view_gl::MapViewGl::new(
265 286
             &mut cx,
266
-            window.get_inner_size().unwrap(),
287
+            input_state.viewport_size.to_physical(input_state.dpi_factor).into(),
288
+            input_state.dpi_factor,
267 289
             move || { proxy.wakeup().unwrap(); },
268 290
             config.use_network(),
269 291
             config.async(),
@@ -274,11 +296,6 @@ fn run() -> Result<(), Box<Error>> {
274 296
         map.restore_session(session);
275 297
     }
276 298
 
277
-    let mut input_state = InputState {
278
-        mouse_position: (0.0, 0.0),
279
-        mouse_pressed: false,
280
-    };
281
-
282 299
     let (marker_tx, marker_rx) = mpsc::channel();
283 300
     if let (Some(path), Some(pattern)) = (config.pbf_path(), config.search_pattern()) {
284 301
         let proxy = events_loop.create_proxy();
@@ -361,9 +378,12 @@ fn run() -> Result<(), Box<Error>> {
361 378
             }
362 379
         }
363 380
 
364
-        if let Action::Resize(w, h) = action {
365
-            gl_window.resize(w, h);
366
-            map.set_viewport_size(&mut cx, w, h);
381
+        if let Action::Resize(size) = action {
382
+            let phys_size = size.to_physical(input_state.dpi_factor);
383
+            gl_window.resize(phys_size);
384
+
385
+            let phys_size: (u32, u32) = phys_size.into();
386
+            map.set_viewport_size(&mut cx, phys_size.0, phys_size.1);
367 387
         }
368 388
 
369 389
         let redraw = match action {

+ 16
- 2
src/map_view_gl.rs Bestand weergeven

@@ -4,7 +4,6 @@ use ortho_tile_layer::OrthoTileLayer;
4 4
 use map_view::MapView;
5 5
 use marker_layer::MarkerLayer;
6 6
 use mercator_view::MercatorView;
7
-use orthografic_view::OrthograficView;
8 7
 use session::Session;
9 8
 use texture::{Texture, TextureFormat};
10 9
 use tile_atlas::TileAtlas;
@@ -19,7 +18,9 @@ const MAX_ZOOM_LEVEL: f64 = 22.0;
19 18
 #[derive(Debug)]
20 19
 pub struct MapViewGl {
21 20
     map_view: MapView,
21
+    /// Size in physical pixels
22 22
     viewport_size: (u32, u32),
23
+    dpi_factor: f64,
23 24
     tile_cache: TileCache,
24 25
     tile_atlas: TileAtlas,
25 26
     tile_layer: TileLayer,
@@ -49,6 +50,7 @@ impl MapViewGl {
49 50
     pub fn new<F>(
50 51
         cx: &mut Context,
51 52
         initial_size: (u32, u32),
53
+        dpi_factor: f64,
52 54
         update_func: F,
53 55
         use_network: bool,
54 56
         use_async: bool,
@@ -91,6 +93,7 @@ impl MapViewGl {
91 93
         MapViewGl {
92 94
             map_view,
93 95
             viewport_size: initial_size,
96
+            dpi_factor,
94 97
             tile_cache: TileCache::new(move |_tile| update_func(), use_network),
95 98
             tile_atlas,
96 99
             tile_layer,
@@ -107,6 +110,10 @@ impl MapViewGl {
107 110
         cx.set_viewport(0, 0, width, height);
108 111
     }
109 112
 
113
+    pub fn set_dpi_factor(&mut self, dpi_factor: f64) {
114
+        self.dpi_factor = dpi_factor;
115
+    }
116
+
110 117
     pub fn add_marker(&mut self, map_coord: MapCoord) {
111 118
         self.marker_layer.add_marker(map_coord);
112 119
     }
@@ -158,7 +165,13 @@ impl MapViewGl {
158 165
         }
159 166
 
160 167
         //TODO remove viewport_size parameter
161
-        self.marker_layer.draw(cx, &self.map_view, self.viewport_size, snap_to_pixel);
168
+        self.marker_layer.draw(
169
+            cx,
170
+            &self.map_view,
171
+            self.viewport_size,
172
+            self.dpi_factor,
173
+            snap_to_pixel,
174
+        );
162 175
     }
163 176
 
164 177
     fn draw_ortho_tiles(&mut self, cx: &mut Context, source: &TileSource) {
@@ -238,6 +251,7 @@ impl MapViewGl {
238 251
         self.map_view.set_tile_zoom_offset(offset + delta_offset);
239 252
     }
240 253
 
254
+    //TODO Make sure to use physical pixel deltas
241 255
     pub fn move_pixel(&mut self, delta_x: f64, delta_y: f64) {
242 256
         //TODO implement for OrthograficView
243 257
         MercatorView::move_pixel(&mut self.map_view, delta_x, delta_y);

+ 4
- 2
src/marker_layer.rs Bestand weergeven

@@ -32,6 +32,7 @@ impl MarkerLayer {
32 32
         ).unwrap();
33 33
         check_gl_errors!(cx);
34 34
 
35
+        //TODO Create textures for higher DPI factors / use mipmaps
35 36
         let texture = {
36 37
             let img = image::load_from_memory(
37 38
                 include_bytes!("../img/marker.png"),
@@ -80,12 +81,13 @@ impl MarkerLayer {
80 81
         cx: &mut Context,
81 82
         map_view: &MapView,
82 83
         viewport_size: (u32, u32),
84
+        dpi_factor: f64,
83 85
         snap_to_pixel: bool
84 86
     ) {
85 87
         let mut vertex_data: Vec<f32> = vec![];
86 88
 
87
-        let marker_size = vec2::<f64>(40.0, 50.0);
88
-        let marker_offset = vec2::<f64>(-20.0, -50.0);
89
+        let marker_size = vec2::<f64>(40.0, 50.0) * dpi_factor;
90
+        let marker_offset = vec2::<f64>(-20.0, -50.0) * dpi_factor;
89 91
 
90 92
         let scale_x = 2.0 / viewport_size.0 as f32;
91 93
         let scale_y = -2.0 / viewport_size.1 as f32;