mousewheel

This commit is contained in:
2019-07-27 18:52:16 +02:00
parent 21d642ec59
commit d53170c5ce
4 changed files with 51 additions and 11 deletions

View File

@@ -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 {