Skip to content

Commit

Permalink
Basic mouse scroll-wheel event (bevyengine#222)
Browse files Browse the repository at this point in the history
add simple mouse wheel event + example
  • Loading branch information
ifletsomeclaire authored Aug 21, 2020
1 parent 3c5251f commit 45312a9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_input/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 21 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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, .. } => {
Expand Down
8 changes: 7 additions & 1 deletion examples/input/mouse_input_events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{
input::mouse::{MouseButtonInput, MouseMotion},
input::mouse::{MouseButtonInput, MouseMotion, MouseWheel},
prelude::*,
window::CursorMoved,
};
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

0 comments on commit 45312a9

Please sign in to comment.