bevy/crates/bevy_input/src/mouse.rs

57 lines
1.4 KiB
Rust
Raw Normal View History

use crate::{ButtonState, Input};
use bevy_ecs::{event::EventReader, system::ResMut};
use bevy_math::Vec2;
2020-04-05 06:42:39 +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: ButtonState,
2020-04-05 06:42:39 +00:00
}
/// A button on a mouse device
2020-04-05 06:42:39 +00:00
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
2020-04-05 06:42:39 +00:00
pub enum MouseButton {
Left,
Right,
Middle,
Other(u16),
2020-04-05 07:32:53 +00:00
}
/// A mouse motion event
2020-04-05 07:32:53 +00:00
#[derive(Debug, Clone)]
pub struct MouseMotion {
pub delta: Vec2,
}
/// Unit of scroll
#[derive(Debug, Clone, Copy)]
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,
}
/// Updates the `Input<MouseButton>` resource with the latest `MouseButtonInput` events
pub fn mouse_button_input_system(
mut mouse_button_input: ResMut<Input<MouseButton>>,
mut mouse_button_input_events: EventReader<MouseButtonInput>,
) {
mouse_button_input.clear();
for event in mouse_button_input_events.iter() {
match event.state {
ButtonState::Pressed => mouse_button_input.press(event.button),
ButtonState::Released => mouse_button_input.release(event.button),
}
}
}