Added scroll wheel zoom

master
Mark 2023-12-22 19:18:19 -08:00
parent 13f74b9d85
commit 732ebabfdc
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
2 changed files with 47 additions and 5 deletions

View File

@ -1,9 +1,11 @@
use winit::event::{ElementState, VirtualKeyCode}; use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
pub struct InputStatus { pub struct InputStatus {
scroll_speed: f32,
pub key_left: bool, pub key_left: bool,
pub key_right: bool, pub key_right: bool,
pub key_thrust: bool, pub key_thrust: bool,
pub v_scroll: f32,
} }
impl InputStatus { impl InputStatus {
@ -12,10 +14,12 @@ impl InputStatus {
key_left: false, key_left: false,
key_right: false, key_right: false,
key_thrust: 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; let down = state == &ElementState::Pressed;
match key { match key {
VirtualKeyCode::Left => self.key_left = down, 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;
}
}
}
} }

View File

@ -78,8 +78,16 @@ impl Game {
} }
} }
fn process_input(&mut self, gpu: &ElementState, key: &VirtualKeyCode) { fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
self.input.process(gpu, key) 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) { fn update(&mut self) {
@ -98,6 +106,11 @@ impl Game {
self.player.body.rot(Deg { 0: -15.0 } * t); 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.player.body.tick(t);
self.camera.pos = self.player.body.pos; 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) => { WindowEvent::Resized(physical_size) => {
gpu.resize(*physical_size); gpu.resize(*physical_size);
} }