2020-11-03 02:16:13 +00:00
|
|
|
use crate::{ElementState, Input};
|
2021-04-13 20:36:37 +00:00
|
|
|
use bevy_ecs::{event::EventReader, system::ResMut};
|
2020-07-16 23:51:45 +00:00
|
|
|
use bevy_math::Vec2;
|
2020-04-05 06:42:39 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A mouse button input event
|
2020-04-05 06:42:39 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-05 07:32:53 +00:00
|
|
|
pub struct MouseButtonInput {
|
2020-04-05 06:42:39 +00:00
|
|
|
pub button: MouseButton,
|
|
|
|
pub state: ElementState,
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A button on a mouse device
|
2020-04-05 06:42:39 +00:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
|
2020-08-22 01:13:50 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-04-05 06:42:39 +00:00
|
|
|
pub enum MouseButton {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Middle,
|
2020-12-13 19:27:54 +00:00
|
|
|
Other(u16),
|
2020-04-05 07:32:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A mouse motion event
|
2020-04-05 07:32:53 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-06-05 06:34:21 +00:00
|
|
|
pub struct MouseMotion {
|
2020-06-04 06:22:32 +00:00
|
|
|
pub delta: Vec2,
|
2020-06-05 06:34:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 00:04:01 +00:00
|
|
|
/// Unit of scroll
|
2020-09-06 19:56:09 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2020-08-21 00:04:01 +00:00
|
|
|
pub enum MouseScrollUnit {
|
|
|
|
Line,
|
|
|
|
Pixel,
|
|
|
|
}
|
|
|
|
|
2021-03-11 00:27:30 +00:00
|
|
|
/// A mouse scroll wheel event, where x represents horizontal scroll and y represents vertical
|
|
|
|
/// scroll.
|
2020-08-21 00:04:01 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MouseWheel {
|
|
|
|
pub unit: MouseScrollUnit,
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
}
|
|
|
|
|
2021-12-29 17:38:10 +00:00
|
|
|
/// Updates the `Input<MouseButton>` resource with the latest `MouseButtonInput` events
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn mouse_button_input_system(
|
|
|
|
mut mouse_button_input: ResMut<Input<MouseButton>>,
|
2021-01-19 06:23:30 +00:00
|
|
|
mut mouse_button_input_events: EventReader<MouseButtonInput>,
|
2020-06-05 06:34:21 +00:00
|
|
|
) {
|
2021-04-13 03:13:48 +00:00
|
|
|
mouse_button_input.clear();
|
2021-01-19 06:23:30 +00:00
|
|
|
for event in mouse_button_input_events.iter() {
|
2020-06-05 06:34:21 +00:00
|
|
|
match event.state {
|
|
|
|
ElementState::Pressed => mouse_button_input.press(event.button),
|
|
|
|
ElementState::Released => mouse_button_input.release(event.button),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|