2022-04-24 22:57:02 +00:00
|
|
|
use crate::{ButtonState, 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
|
|
|
|
2022-04-26 17:32:54 +00:00
|
|
|
/// A mouse button input event.
|
|
|
|
///
|
|
|
|
/// This event is the translated version of the `WindowEvent::MouseInput` from the `winit` crate.
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// The event is read inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system)
|
|
|
|
/// to update the [`Input<MouseButton>`](crate::Input<MouseButton>) resource.
|
Derived `Copy` trait for `bevy_input` events, `Serialize`/`Deserialize` for events in `bevy_input` and `bevy_windows`, `PartialEq` for events in both, and `Eq` where possible in both. (#6023)
# Objective
Add traits to events in `bevy_input` and `bevy_windows`: `Copy`, `Serialize`/`Deserialize`, `PartialEq`, and `Eq`, as requested in https://github.com/bevyengine/bevy/issues/6022, https://github.com/bevyengine/bevy/issues/6023, https://github.com/bevyengine/bevy/issues/6024.
## Solution
Added the traits to events in `bevy_input` and `bevy_windows`. Added dependency of `serde` in `Cargo.toml` of `bevy_input`.
## Migration Guide
If one has been `.clone()`'ing `bevy_input` events, Clippy will now complain about that. Just remove `.clone()` to solve.
## Other Notes
Some events in `bevy_input` had `f32` fields, so `Eq` trait was not derived for them.
Some events in `bevy_windows` had `String` fields, so `Copy` trait was not derived for them.
Co-authored-by: targrub <62773321+targrub@users.noreply.github.com>
2022-09-20 18:24:00 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-04-05 07:32:53 +00:00
|
|
|
pub struct MouseButtonInput {
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The mouse button assigned to the event.
|
2020-04-05 06:42:39 +00:00
|
|
|
pub button: MouseButton,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The pressed state of the button.
|
2022-04-24 22:57:02 +00:00
|
|
|
pub state: ButtonState,
|
2020-04-05 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 17:32:54 +00:00
|
|
|
/// A button on a mouse device.
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `bevy`
|
|
|
|
/// resource.
|
|
|
|
///
|
|
|
|
/// ## Updating
|
|
|
|
///
|
|
|
|
/// The resource is updated inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system).
|
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 {
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The left mouse button.
|
2020-04-05 06:42:39 +00:00
|
|
|
Left,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The right mouse button.
|
2020-04-05 06:42:39 +00:00
|
|
|
Right,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The middle mouse button.
|
2020-04-05 06:42:39 +00:00
|
|
|
Middle,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// Another mouse button with the associated number.
|
2020-12-13 19:27:54 +00:00
|
|
|
Other(u16),
|
2020-04-05 07:32:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 13:40:43 +00:00
|
|
|
/// An event reporting the change in physical position of a pointing device.
|
2022-04-26 17:32:54 +00:00
|
|
|
///
|
2022-06-26 13:40:43 +00:00
|
|
|
/// This represents raw, unfiltered physical motion.
|
|
|
|
/// It is the translated version of [`DeviceEvent::MouseMotion`] from the `winit` crate.
|
|
|
|
///
|
|
|
|
/// All pointing devices connected to a single machine at the same time can emit the event independently.
|
|
|
|
/// However, the event data does not make it possible to distinguish which device it is referring to.
|
|
|
|
///
|
|
|
|
/// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion
|
Derived `Copy` trait for `bevy_input` events, `Serialize`/`Deserialize` for events in `bevy_input` and `bevy_windows`, `PartialEq` for events in both, and `Eq` where possible in both. (#6023)
# Objective
Add traits to events in `bevy_input` and `bevy_windows`: `Copy`, `Serialize`/`Deserialize`, `PartialEq`, and `Eq`, as requested in https://github.com/bevyengine/bevy/issues/6022, https://github.com/bevyengine/bevy/issues/6023, https://github.com/bevyengine/bevy/issues/6024.
## Solution
Added the traits to events in `bevy_input` and `bevy_windows`. Added dependency of `serde` in `Cargo.toml` of `bevy_input`.
## Migration Guide
If one has been `.clone()`'ing `bevy_input` events, Clippy will now complain about that. Just remove `.clone()` to solve.
## Other Notes
Some events in `bevy_input` had `f32` fields, so `Eq` trait was not derived for them.
Some events in `bevy_windows` had `String` fields, so `Copy` trait was not derived for them.
Co-authored-by: targrub <62773321+targrub@users.noreply.github.com>
2022-09-20 18:24:00 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-06-05 06:34:21 +00:00
|
|
|
pub struct MouseMotion {
|
2022-06-26 13:40:43 +00:00
|
|
|
/// The change in the position of the pointing device since the last event was sent.
|
2020-06-04 06:22:32 +00:00
|
|
|
pub delta: Vec2,
|
2020-06-05 06:34:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The scroll unit.
|
|
|
|
///
|
|
|
|
/// Describes how a value of a [`MouseWheel`](crate::mouse::MouseWheel) event has to be interpreted.
|
|
|
|
///
|
|
|
|
/// The value of the event can either be interpreted as the amount of lines or the amount of pixels
|
|
|
|
/// to scroll.
|
2022-06-19 16:53:49 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
Derived `Copy` trait for `bevy_input` events, `Serialize`/`Deserialize` for events in `bevy_input` and `bevy_windows`, `PartialEq` for events in both, and `Eq` where possible in both. (#6023)
# Objective
Add traits to events in `bevy_input` and `bevy_windows`: `Copy`, `Serialize`/`Deserialize`, `PartialEq`, and `Eq`, as requested in https://github.com/bevyengine/bevy/issues/6022, https://github.com/bevyengine/bevy/issues/6023, https://github.com/bevyengine/bevy/issues/6024.
## Solution
Added the traits to events in `bevy_input` and `bevy_windows`. Added dependency of `serde` in `Cargo.toml` of `bevy_input`.
## Migration Guide
If one has been `.clone()`'ing `bevy_input` events, Clippy will now complain about that. Just remove `.clone()` to solve.
## Other Notes
Some events in `bevy_input` had `f32` fields, so `Eq` trait was not derived for them.
Some events in `bevy_windows` had `String` fields, so `Copy` trait was not derived for them.
Co-authored-by: targrub <62773321+targrub@users.noreply.github.com>
2022-09-20 18:24:00 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-08-21 00:04:01 +00:00
|
|
|
pub enum MouseScrollUnit {
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The line scroll unit.
|
|
|
|
///
|
|
|
|
/// The delta of the associated [`MouseWheel`](crate::mouse::MouseWheel) event corresponds
|
|
|
|
/// to the amount of lines or rows to scroll.
|
2020-08-21 00:04:01 +00:00
|
|
|
Line,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The pixel scroll unit.
|
|
|
|
///
|
|
|
|
/// The delta of the associated [`MouseWheel`](crate::mouse::MouseWheel) event corresponds
|
|
|
|
/// to the amount of pixels to scroll.
|
2020-08-21 00:04:01 +00:00
|
|
|
Pixel,
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:32:54 +00:00
|
|
|
/// A mouse wheel event.
|
|
|
|
///
|
|
|
|
/// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate.
|
Derived `Copy` trait for `bevy_input` events, `Serialize`/`Deserialize` for events in `bevy_input` and `bevy_windows`, `PartialEq` for events in both, and `Eq` where possible in both. (#6023)
# Objective
Add traits to events in `bevy_input` and `bevy_windows`: `Copy`, `Serialize`/`Deserialize`, `PartialEq`, and `Eq`, as requested in https://github.com/bevyengine/bevy/issues/6022, https://github.com/bevyengine/bevy/issues/6023, https://github.com/bevyengine/bevy/issues/6024.
## Solution
Added the traits to events in `bevy_input` and `bevy_windows`. Added dependency of `serde` in `Cargo.toml` of `bevy_input`.
## Migration Guide
If one has been `.clone()`'ing `bevy_input` events, Clippy will now complain about that. Just remove `.clone()` to solve.
## Other Notes
Some events in `bevy_input` had `f32` fields, so `Eq` trait was not derived for them.
Some events in `bevy_windows` had `String` fields, so `Copy` trait was not derived for them.
Co-authored-by: targrub <62773321+targrub@users.noreply.github.com>
2022-09-20 18:24:00 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-08-21 00:04:01 +00:00
|
|
|
pub struct MouseWheel {
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The mouse scroll unit.
|
2020-08-21 00:04:01 +00:00
|
|
|
pub unit: MouseScrollUnit,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The horizontal scroll value.
|
2020-08-21 00:04:01 +00:00
|
|
|
pub x: f32,
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The vertical scroll value.
|
2020-08-21 00:04:01 +00:00
|
|
|
pub y: f32,
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:32:54 +00:00
|
|
|
/// Updates the [`Input<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
|
|
|
|
///
|
|
|
|
/// ## Differences
|
|
|
|
///
|
|
|
|
/// The main difference between the [`MouseButtonInput`] event and the [`Input<MouseButton>`] resource is that
|
|
|
|
/// the latter has convenient functions like [`Input::pressed`], [`Input::just_pressed`] and [`Input::just_released`].
|
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 {
|
2022-04-24 22:57:02 +00:00
|
|
|
ButtonState::Pressed => mouse_button_input.press(event.button),
|
|
|
|
ButtonState::Released => mouse_button_input.release(event.button),
|
2020-06-05 06:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|