2020-04-05 06:42:39 +00:00
|
|
|
use super::keyboard::ElementState;
|
2020-06-05 06:34:21 +00:00
|
|
|
use crate::Input;
|
2020-07-17 01:47:51 +00:00
|
|
|
use bevy_app::prelude::{EventReader, Events};
|
2020-08-09 23:13:04 +00:00
|
|
|
use bevy_ecs::{Res, ResMut, Local};
|
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)]
|
|
|
|
pub enum MouseButton {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Middle,
|
|
|
|
Other(u8),
|
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-09 23:13:04 +00:00
|
|
|
/// State used by the mouse button input system
|
2020-06-05 06:34:21 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MouseButtonInputState {
|
|
|
|
mouse_button_input_event_reader: EventReader<MouseButtonInput>,
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:13:04 +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(
|
2020-08-09 23:13:04 +00:00
|
|
|
mut state: Local<MouseButtonInputState>,
|
2020-06-05 06:34:21 +00:00
|
|
|
mut mouse_button_input: ResMut<Input<MouseButton>>,
|
|
|
|
mouse_button_input_events: Res<Events<MouseButtonInput>>,
|
|
|
|
) {
|
|
|
|
mouse_button_input.update();
|
|
|
|
for event in state
|
|
|
|
.mouse_button_input_event_reader
|
|
|
|
.iter(&mouse_button_input_events)
|
|
|
|
{
|
|
|
|
match event.state {
|
|
|
|
ElementState::Pressed => mouse_button_input.press(event.button),
|
|
|
|
ElementState::Released => mouse_button_input.release(event.button),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|