Added scroll wheel zoom
parent
13f74b9d85
commit
732ebabfdc
|
@ -1,9 +1,11 @@
|
|||
use winit::event::{ElementState, VirtualKeyCode};
|
||||
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
|
||||
|
||||
pub struct InputStatus {
|
||||
scroll_speed: f32,
|
||||
pub key_left: bool,
|
||||
pub key_right: bool,
|
||||
pub key_thrust: bool,
|
||||
pub v_scroll: f32,
|
||||
}
|
||||
|
||||
impl InputStatus {
|
||||
|
@ -12,10 +14,12 @@ impl InputStatus {
|
|||
key_left: false,
|
||||
key_right: false,
|
||||
key_thrust: false,
|
||||
v_scroll: 0.0,
|
||||
scroll_speed: 10.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process(&mut self, state: &ElementState, key: &VirtualKeyCode) {
|
||||
pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
|
||||
let down = state == &ElementState::Pressed;
|
||||
match key {
|
||||
VirtualKeyCode::Left => self.key_left = down,
|
||||
|
@ -24,4 +28,23 @@ impl InputStatus {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
|
||||
let _down = state == &ElementState::Pressed;
|
||||
match key {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_scroll(&mut self, delta: &MouseScrollDelta, _phase: &TouchPhase) {
|
||||
match delta {
|
||||
MouseScrollDelta::LineDelta(_h, v) => {
|
||||
self.v_scroll -= self.scroll_speed * v;
|
||||
}
|
||||
// TODO: handle this better
|
||||
MouseScrollDelta::PixelDelta(v) => {
|
||||
self.v_scroll -= v.x as f32;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
25
src/main.rs
25
src/main.rs
|
@ -78,8 +78,16 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
fn process_input(&mut self, gpu: &ElementState, key: &VirtualKeyCode) {
|
||||
self.input.process(gpu, key)
|
||||
fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
|
||||
self.input.process_key(state, key)
|
||||
}
|
||||
|
||||
fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
|
||||
self.input.process_click(state, key)
|
||||
}
|
||||
|
||||
fn process_scroll(&mut self, delta: &MouseScrollDelta, phase: &TouchPhase) {
|
||||
self.input.process_scroll(delta, phase)
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
|
@ -98,6 +106,11 @@ impl Game {
|
|||
self.player.body.rot(Deg { 0: -15.0 } * t);
|
||||
}
|
||||
|
||||
if self.input.v_scroll != 0.0 {
|
||||
self.camera.zoom += self.input.v_scroll;
|
||||
self.input.v_scroll = 0.0;
|
||||
}
|
||||
|
||||
self.player.body.tick(t);
|
||||
self.camera.pos = self.player.body.pos;
|
||||
|
||||
|
@ -160,7 +173,13 @@ pub async fn run() -> Result<()> {
|
|||
..
|
||||
},
|
||||
..
|
||||
} => game.process_input(state, key),
|
||||
} => game.process_key(state, key),
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
game.process_click(state, button)
|
||||
}
|
||||
WindowEvent::MouseWheel { delta, phase, .. } => {
|
||||
game.process_scroll(delta, phase)
|
||||
}
|
||||
WindowEvent::Resized(physical_size) => {
|
||||
gpu.resize(*physical_size);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue