Basic mouse scroll-wheel event (#222)

add simple mouse wheel event + example
This commit is contained in:
Claire C 2020-08-20 19:04:01 -05:00 committed by GitHub
parent 3c5251f6f2
commit 45312a945a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 3 deletions

View File

@ -11,7 +11,7 @@ pub mod prelude {
use bevy_app::prelude::*;
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
use bevy_ecs::IntoQuerySystem;
@ -24,6 +24,7 @@ impl Plugin for InputPlugin {
app.add_event::<KeyboardInput>()
.add_event::<MouseButtonInput>()
.add_event::<MouseMotion>()
.add_event::<MouseWheel>()
.init_resource::<Input<KeyCode>>()
.add_system_to_stage(
bevy_app::stage::EVENT_UPDATE,

View File

@ -26,6 +26,21 @@ pub struct MouseMotion {
pub delta: Vec2,
}
/// Unit of scroll
#[derive(Debug, Clone)]
pub enum MouseScrollUnit {
Line,
Pixel,
}
/// A mouse scroll wheel event, where x represents horizontal scroll and y represents vertical scroll.
#[derive(Debug, Clone)]
pub struct MouseWheel {
pub unit: MouseScrollUnit,
pub x: f32,
pub y: f32,
}
/// State used by the mouse button input system
#[derive(Default)]
pub struct MouseButtonInputState {

View File

@ -4,7 +4,7 @@ pub use winit_windows::*;
use bevy_input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion},
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
};
use bevy_app::{prelude::*, AppExit};
@ -119,6 +119,26 @@ pub fn winit_runner(mut app: App) {
state: converters::convert_element_state(state),
});
}
WindowEvent::MouseWheel { delta, .. } => match delta {
event::MouseScrollDelta::LineDelta(x, y) => {
let mut mouse_wheel_input_events =
app.resources.get_mut::<Events<MouseWheel>>().unwrap();
mouse_wheel_input_events.send(MouseWheel {
unit: MouseScrollUnit::Line,
x,
y,
});
}
event::MouseScrollDelta::PixelDelta(p) => {
let mut mouse_wheel_input_events =
app.resources.get_mut::<Events<MouseWheel>>().unwrap();
mouse_wheel_input_events.send(MouseWheel {
unit: MouseScrollUnit::Pixel,
x: p.x as f32,
y: p.y as f32,
});
}
},
_ => {}
},
event::Event::DeviceEvent { ref event, .. } => {

View File

@ -1,5 +1,5 @@
use bevy::{
input::mouse::{MouseButtonInput, MouseMotion},
input::mouse::{MouseButtonInput, MouseMotion, MouseWheel},
prelude::*,
window::CursorMoved,
};
@ -17,6 +17,7 @@ struct State {
mouse_button_event_reader: EventReader<MouseButtonInput>,
mouse_motion_event_reader: EventReader<MouseMotion>,
cursor_moved_event_reader: EventReader<CursorMoved>,
mouse_wheel_event_reader: EventReader<MouseWheel>,
}
/// This system prints out all mouse events as they come in
@ -25,6 +26,7 @@ fn print_mouse_events_system(
mouse_button_input_events: Res<Events<MouseButtonInput>>,
mouse_motion_events: Res<Events<MouseMotion>>,
cursor_moved_events: Res<Events<CursorMoved>>,
mouse_wheel_events: Res<Events<MouseWheel>>,
) {
for event in state
.mouse_button_event_reader
@ -40,4 +42,8 @@ fn print_mouse_events_system(
for event in state.cursor_moved_event_reader.iter(&cursor_moved_events) {
println!("{:?}", event);
}
for event in state.mouse_wheel_event_reader.iter(&mouse_wheel_events) {
println!("{:?}", event);
}
}