mousewheel
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -1 +1 @@
|
||||
input = false
|
||||
input = true
|
||||
44
src/input.rs
44
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 {
|
||||
|
||||
|
||||
12
src/main.rs
12
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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user