diff --git a/config/input.toml b/config/input.toml index 8f47aa5..c012252 100644 --- a/config/input.toml +++ b/config/input.toml @@ -4,11 +4,11 @@ scan_code = 1 [[button]] name = "FORWARD" -mouse = "Left" +mouse = "WheelUp" [[button]] name = "BACKWARD" -mouse = "Right" +mouse = "WheelDown" [[button]] name = "RELOAD_SHADERS" diff --git a/config/log.toml b/config/log.toml index 77f557f..6215a06 100644 --- a/config/log.toml +++ b/config/log.toml @@ -1 +1 @@ -input = false \ No newline at end of file +input = true \ No newline at end of file diff --git a/src/input.rs b/src/input.rs index d70c6f1..8c321a2 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,4 +1,4 @@ -use winit::{ScanCode, ModifiersState, MouseButton, ElementState}; +use winit::{ScanCode, ModifiersState, MouseButton, ElementState, MouseScrollDelta}; use std::collections::{HashMap, HashSet}; use std::fs; @@ -74,13 +74,15 @@ impl InputState { let input = if let Some(scan_code) = bn.scan_code { DigitalInput::Keyboard(KeyboardInput { scan_code, modifiers }) } else if let Some(button_name) = &bn.mouse { - let button = match button_name.to_lowercase().as_str() { - "left" => MouseButton::Left, - "middle" => MouseButton::Middle, - "right" => MouseButton::Right, - other => MouseButton::Other(other.parse().expect(&format!("Unknown button: {:?}", other))) - }; - DigitalInput::Mouse(MouseInput { button, modifiers }) + let button_name_lower = button_name.to_lowercase(); + match button_name_lower.as_str() { + "left" => DigitalInput::Mouse(MouseInput { button: MouseButton::Left, modifiers }), + "middle" => DigitalInput::Mouse(MouseInput { button: MouseButton::Middle, modifiers }), + "right" => DigitalInput::Mouse(MouseInput { button: MouseButton::Right, modifiers }), + "wheelup" => DigitalInput::Wheel(WheelInput { direction: WheelInputDirection::Up, modifiers }), + "wheeldown" => DigitalInput::Wheel(WheelInput { direction: WheelInputDirection::Down, modifiers }), + other => DigitalInput::Mouse(MouseInput { button: MouseButton::Other(other.parse().expect(&format!("Unknown button: {:?}", other))), modifiers }), + } } else { panic!("No mouse or keyboard input for button {:?}", bn.name); }; @@ -184,6 +186,9 @@ impl InputState { DigitalInput::Mouse(mouse_input) => { self.pressed_mouse_buttons.contains(&mouse_input.button) && self.modifiers_are_pressed(mouse_input.modifiers) + }, + DigitalInput::Wheel(wheel_input) => { + self.input_events.contains(&DigitalInputEvent::Pressed(DigitalInput::Wheel(wheel_input.clone()))) } } } @@ -227,6 +232,16 @@ impl InputState { } } + pub fn on_mouse_wheel_event(self: &mut Self, delta: &MouseScrollDelta, modifiers: &ModifiersState) { + let direction = match delta { + MouseScrollDelta::LineDelta(_x, y) => if *y >= 0.0 { WheelInputDirection::Up } else { WheelInputDirection::Down }, + MouseScrollDelta::PixelDelta(pixels) => if pixels.y >= 0.0 { WheelInputDirection::Up } else { WheelInputDirection::Down }, + }; + let input = DigitalInput::Wheel(WheelInput { direction, modifiers: modifiers.clone() }); + self.input_events.insert(DigitalInputEvent::Pressed(input.clone())); + self.input_events.insert(DigitalInputEvent::Released(input)); + } + pub fn frame_end(self: &mut Self) { self.input_events.clear(); } @@ -249,6 +264,7 @@ pub enum AxisInput { #[derive(Debug, Eq, PartialEq, Hash, Clone)] pub enum DigitalInput { Keyboard(KeyboardInput), + Wheel(WheelInput), Mouse(MouseInput) } @@ -264,6 +280,18 @@ pub struct MouseInput { modifiers: ModifiersState } +#[derive(Debug, Eq, PartialEq, Hash, Clone)] +pub struct WheelInput { + direction: WheelInputDirection, + modifiers: ModifiersState +} + +#[derive(Debug, Eq, PartialEq, Hash, Clone)] +pub enum WheelInputDirection { + Up, + Down +} + #[derive(Debug, Eq, PartialEq, Clone)] pub struct AnalogInput { diff --git a/src/main.rs b/src/main.rs index 607e7a7..4f88786 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,6 +83,18 @@ impl Game for TestGame { } self.input.on_mouse_event(state, button, modifiers); + }, + Event::WindowEvent { event: WindowEvent::MouseWheel { device_id, delta, phase, modifiers }, .. } => { + if self.log_config.input { + let mods = mods_to_string(modifiers); + if mods.len() > 0 { + println!("Scroll {:?} {:?} {:?} + {:?}", device_id, phase, &mods, delta) + } else { + println!("Scroll {:?} {:?} {:?}", device_id, phase, delta) + } + } + + self.input.on_mouse_wheel_event(&delta, &modifiers); } _ => {} }