瀏覽代碼

Allow changing render zoom level

Johannes Hofmann 8 年之前
父節點
當前提交
b7988fd29f
共有 3 個檔案被更改,包括 57 行新增4 行删除
  1. 41
    2
      src/main.rs
  2. 11
    2
      src/map_view.rs
  3. 5
    0
      src/map_view_gl.rs

+ 41
- 2
src/main.rs 查看文件

@@ -46,6 +46,14 @@ enum Action {
46 46
 struct InputState {
47 47
     mouse_position: (i32, i32),
48 48
     mouse_pressed: bool,
49
+    lctrl_pressed: bool,
50
+    rctrl_pressed: bool,
51
+}
52
+
53
+impl InputState {
54
+    fn ctrl_pressed(&self) -> bool {
55
+        self.lctrl_pressed | self.rctrl_pressed
56
+    }
49 57
 }
50 58
 
51 59
 fn handle_event(event: &Event, map: &mut MapViewGl, input_state: &mut InputState, sources: &mut TileSources) -> Action {
@@ -112,6 +120,14 @@ fn handle_event(event: &Event, map: &mut MapViewGl, input_state: &mut InputState
112 120
                 VirtualKeyCode::Escape => {
113 121
                     Action::Close
114 122
                 },
123
+                VirtualKeyCode::LControl => {
124
+                    input_state.lctrl_pressed = true;
125
+                    Action::Nothing
126
+                },
127
+                VirtualKeyCode::RControl => {
128
+                    input_state.rctrl_pressed = true;
129
+                    Action::Nothing
130
+                },
115 131
                 VirtualKeyCode::PageUp => {
116 132
                     sources.switch_to_prev();
117 133
                     Action::Redraw
@@ -137,16 +153,37 @@ fn handle_event(event: &Event, map: &mut MapViewGl, input_state: &mut InputState
137 153
                     Action::Redraw
138 154
                 },
139 155
                 VirtualKeyCode::Add => {
140
-                    map.step_zoom(1, 0.5);
156
+                    if input_state.ctrl_pressed() {
157
+                        map.change_zoom_level_offset(1.0);
158
+                    } else {
159
+                        map.step_zoom(1, 0.5);
160
+                    }
141 161
                     Action::Redraw
142 162
                 },
143 163
                 VirtualKeyCode::Subtract => {
144
-                    map.step_zoom(-1, 0.5);
164
+                    if input_state.ctrl_pressed() {
165
+                        map.change_zoom_level_offset(-1.0);
166
+                    } else {
167
+                        map.step_zoom(-1, 0.5);
168
+                    }
145 169
                     Action::Redraw
146 170
                 },
147 171
                 _ => Action::Nothing,
148 172
             }
149 173
         },
174
+        Event::KeyboardInput(glutin::ElementState::Released, _, Some(keycode)) => {
175
+            match keycode {
176
+                VirtualKeyCode::LControl => {
177
+                    input_state.lctrl_pressed = false;
178
+                    Action::Nothing
179
+                },
180
+                VirtualKeyCode::RControl => {
181
+                    input_state.rctrl_pressed = false;
182
+                    Action::Nothing
183
+                },
184
+                _ => Action::Nothing,
185
+            }
186
+        },
150 187
         Event::Refresh => {
151 188
             Action::Redraw
152 189
         },
@@ -223,6 +260,8 @@ fn main() {
223 260
     let mut input_state = InputState {
224 261
         mouse_position: (0, 0),
225 262
         mouse_pressed: false,
263
+        lctrl_pressed: false,
264
+        rctrl_pressed: false,
226 265
     };
227 266
 
228 267
     let fps: f64 = matches.value_of("fps").map(|s| s.parse().unwrap()).unwrap_or(config.fps());

+ 11
- 2
src/map_view.rs 查看文件

@@ -8,6 +8,7 @@ pub struct MapView {
8 8
     pub tile_size: u32,
9 9
     pub center: MapCoord,
10 10
     pub zoom2: f64,
11
+    pub render_zoom_level_offset: f64,
11 12
 }
12 13
 
13 14
 #[derive(Clone, Debug)]
@@ -24,6 +25,7 @@ impl MapView {
24 25
             tile_size: tile_size,
25 26
             center: MapCoord::new(0.5, 0.5),
26 27
             zoom2: 0.0,
28
+            render_zoom_level_offset: 0.0,
27 29
         }
28 30
     }
29 31
 
@@ -91,9 +93,16 @@ impl MapView {
91 93
         visible_tiles
92 94
     }
93 95
 
94
-    //TODO make zoom level configurable
95 96
     pub fn render_zoom_level(&self) -> u32 {
96
-        self.zoom2.floor().max(0.0) as u32
97
+        (self.zoom2 + self.render_zoom_level_offset).floor().max(0.0) as u32
98
+    }
99
+
100
+    pub fn render_zoom_level_offset(&self) -> f64 {
101
+        self.render_zoom_level_offset
102
+    }
103
+
104
+    pub fn set_render_zoom_level_offset(&mut self, offset: f64) {
105
+        self.render_zoom_level_offset = offset;
97 106
     }
98 107
 
99 108
     pub fn set_size(&mut self, width: f64, height: f64) {

+ 5
- 0
src/map_view_gl.rs 查看文件

@@ -193,6 +193,11 @@ impl<'a> MapViewGl<'a> {
193 193
         self.map_view.center.normalize_xy();
194 194
     }
195 195
 
196
+    pub fn change_zoom_level_offset(&mut self, delta_offset: f64) {
197
+        let offset = self.map_view.render_zoom_level_offset();
198
+        self.map_view.set_render_zoom_level_offset(offset + delta_offset);
199
+    }
200
+
196 201
     pub fn move_pixel(&mut self, delta_x: f64, delta_y: f64) {
197 202
         self.map_view.move_pixel(delta_x, delta_y);
198 203
         self.map_view.center.normalize_xy();