2023-08-23 12:44:49 +00:00
|
|
|
//! The mouse input functionality.
|
|
|
|
|
2023-12-06 20:32:34 +00:00
|
|
|
use crate::{ButtonInput, ButtonState};
|
2023-06-06 14:44:32 +00:00
|
|
|
use bevy_ecs::{
|
|
|
|
change_detection::DetectChangesMut,
|
2024-09-24 11:42:59 +00:00
|
|
|
entity::Entity,
|
2023-06-06 14:44:32 +00:00
|
|
|
event::{Event, EventReader},
|
2024-09-24 11:42:59 +00:00
|
|
|
system::{ResMut, Resource},
|
2023-06-06 14:44:32 +00:00
|
|
|
};
|
2020-07-16 23:51:45 +00:00
|
|
|
use bevy_math::Vec2;
|
2024-07-08 01:09:07 +00:00
|
|
|
#[cfg(feature = "bevy_reflect")]
|
2024-09-27 00:59:59 +00:00
|
|
|
use {
|
|
|
|
bevy_ecs::reflect::ReflectResource,
|
|
|
|
bevy_reflect::{std_traits::ReflectDefault, Reflect},
|
|
|
|
};
|
2022-10-26 19:52:20 +00:00
|
|
|
|
2024-07-08 01:09:07 +00:00
|
|
|
#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
|
2022-10-26 19:52:20 +00:00
|
|
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
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
|
|
|
|
///
|
2023-10-06 00:31:10 +00:00
|
|
|
/// The event is read inside of the [`mouse_button_input_system`]
|
2024-02-24 18:41:17 +00:00
|
|
|
/// to update the [`ButtonInput<MouseButton>`] resource.
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2022-10-26 19:52:20 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2022-10-26 19:52:20 +00:00
|
|
|
reflect(Serialize, 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,
|
2023-06-16 13:54:06 +00:00
|
|
|
/// Window that received the input.
|
|
|
|
pub window: Entity,
|
2020-04-05 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 17:32:54 +00:00
|
|
|
/// A button on a mouse device.
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
2023-12-06 20:32:34 +00:00
|
|
|
/// It is used as the generic `T` value of an [`ButtonInput`] to create a `bevy`
|
2022-04-26 17:32:54 +00:00
|
|
|
/// resource.
|
|
|
|
///
|
|
|
|
/// ## Updating
|
|
|
|
///
|
2023-10-06 00:31:10 +00:00
|
|
|
/// The resource is updated inside of the [`mouse_button_input_system`].
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
|
2022-10-26 19:52:20 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
feature = "bevy_reflect",
|
|
|
|
derive(Reflect),
|
|
|
|
reflect(Debug, Hash, PartialEq)
|
|
|
|
)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
|
|
|
#[cfg_attr(
|
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2022-10-26 19:52:20 +00:00
|
|
|
reflect(Serialize, 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,
|
Update winit dependency to 0.29 (#10702)
# Objective
- Update winit dependency to 0.29
## Changelog
### KeyCode changes
- Removed `ScanCode`, as it was [replaced by
KeyCode](https://github.com/rust-windowing/winit/blob/master/CHANGELOG.md#0292).
- `ReceivedCharacter.char` is now a `SmolStr`, [relevant
doc](https://docs.rs/winit/latest/winit/event/struct.KeyEvent.html#structfield.text).
- Changed most `KeyCode` values, and added more.
KeyCode has changed meaning. With this PR, it refers to physical
position on keyboard rather than the printed letter on keyboard keys.
In practice this means:
- On QWERTY keyboard layouts, nothing changes
- On any other keyboard layout, `KeyCode` no longer reflects the label
on key.
- This is "good". In bevy 0.12, when you used WASD for movement, users
with non-QWERTY keyboards couldn't play your game! This was especially
bad for non-latin keyboards. Now, WASD represents the physical keys. A
French player will press the ZQSD keys, which are near each other,
Kyrgyz players will use "Цфыв".
- This is "bad" as well. You can't know in advance what the label of the
key for input is. Your UI says "press WASD to move", even if in reality,
they should be pressing "ZQSD" or "Цфыв". You also no longer can use
`KeyCode` for text inputs. In any case, it was a pretty bad API for text
input. You should use `ReceivedCharacter` now instead.
### Other changes
- Use `web-time` rather than `instant` crate.
(https://github.com/rust-windowing/winit/pull/2836)
- winit did split `run_return` in `run_onDemand` and `pump_events`, I
did the same change in bevy_winit and used `pump_events`.
- Removed `return_from_run` from `WinitSettings` as `winit::run` now
returns on supported platforms.
- I left the example "return_after_run" as I think it's still useful.
- This winit change is done partly to allow to create a new window after
quitting all windows: https://github.com/emilk/egui/issues/1918 ; this
PR doesn't address.
- added `width` and `height` properties in the `canvas` from wasm
example
(https://github.com/bevyengine/bevy/pull/10702#discussion_r1420567168)
## Known regressions (important follow ups?)
- Provide an API for reacting when a specific key from current layout
was released.
- possible solutions: use winit::Key from winit::KeyEvent ; mapping
between KeyCode and Key ; or .
- We don't receive characters through alt+numpad (e.g. alt + 151 = "ù")
anymore ; reproduced on winit example "ime". maybe related to
https://github.com/rust-windowing/winit/issues/2945
- (windows) Window content doesn't refresh at all when resizing. By
reading https://github.com/rust-windowing/winit/issues/2900 ; I suspect
we should just fire a `window.request_redraw();` from `AboutToWait`, and
handle actual redrawing within `RedrawRequested`. I'm not sure how to
move all that code so I'd appreciate it to be a follow up.
- (windows) unreleased winit fix for using set_control_flow in
AboutToWait https://github.com/rust-windowing/winit/issues/3215 ; ⚠️ I'm
not sure what the implications are, but that feels bad 🤔
## Follow up
I'd like to avoid bloating this PR, here are a few follow up tasks
worthy of a separate PR, or new issue to track them once this PR is
closed, as they would either complicate reviews, or at risk of being
controversial:
- remove CanvasParentResizePlugin
(https://github.com/bevyengine/bevy/pull/10702#discussion_r1417068856)
- avoid mentionning explicitly winit in docs from bevy_window ?
- NamedKey integration on bevy_input:
https://github.com/rust-windowing/winit/pull/3143 introduced a new
NamedKey variant. I implemented it only on the converters but we'd
benefit making the same changes to bevy_input.
- Add more info in KeyboardInput
https://github.com/bevyengine/bevy/pull/10702#pullrequestreview-1748336313
- https://github.com/bevyengine/bevy/pull/9905 added a workaround on a
bug allegedly fixed by winit 0.29. We should check if it's still
necessary.
- update to raw_window_handle 0.6
- blocked by wgpu
- Rename `KeyCode` to `PhysicalKeyCode`
https://github.com/bevyengine/bevy/pull/10702#discussion_r1404595015
- remove `instant` dependency, [replaced
by](https://github.com/rust-windowing/winit/pull/2836) `web_time`), we'd
need to update to :
- fastrand >= 2.0
- [`async-executor`](https://github.com/smol-rs/async-executor) >= 1.7
- [`futures-lite`](https://github.com/smol-rs/futures-lite) >= 2.0
- Verify license, see
[discussion](https://github.com/bevyengine/bevy/pull/8745#discussion_r1402439800)
- we might be missing a short notice or description of changes made
- Consider using https://github.com/rust-windowing/cursor-icon directly
rather than vendoring it in bevy.
- investigate [this
unwrap](https://github.com/bevyengine/bevy/pull/8745#discussion_r1387044986)
(`winit_window.canvas().unwrap();`)
- Use more good things about winit's update
- https://github.com/bevyengine/bevy/pull/10689#issuecomment-1823560428
## Migration Guide
This PR should have one.
2023-12-21 07:40:47 +00:00
|
|
|
/// The back mouse button.
|
|
|
|
Back,
|
|
|
|
/// The forward mouse button.
|
|
|
|
Forward,
|
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
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Event, Debug, Clone, Copy, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2022-10-26 19:52:20 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2022-10-26 19:52:20 +00:00
|
|
|
reflect(Serialize, 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.
|
|
|
|
///
|
2023-10-06 00:31:10 +00:00
|
|
|
/// Describes how a value of a [`MouseWheel`] event has to be interpreted.
|
2022-04-26 17:32:54 +00:00
|
|
|
///
|
|
|
|
/// The value of the event can either be interpreted as the amount of lines or the amount of pixels
|
|
|
|
/// to scroll.
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2022-10-26 19:52:20 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2022-10-26 19:52:20 +00:00
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-08-21 00:04:01 +00:00
|
|
|
pub enum MouseScrollUnit {
|
2022-04-26 17:32:54 +00:00
|
|
|
/// The line scroll unit.
|
|
|
|
///
|
2023-10-06 00:31:10 +00:00
|
|
|
/// The delta of the associated [`MouseWheel`] event corresponds
|
2022-04-26 17:32:54 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2023-10-06 00:31:10 +00:00
|
|
|
/// The delta of the associated [`MouseWheel`] event corresponds
|
2022-04-26 17:32:54 +00:00
|
|
|
/// 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.
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Event, Debug, Clone, Copy, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2022-10-26 19:52:20 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2022-10-26 19:52:20 +00:00
|
|
|
reflect(Serialize, 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,
|
2023-06-16 13:54:06 +00:00
|
|
|
/// Window that received the input.
|
|
|
|
pub window: Entity,
|
2020-08-21 00:04:01 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 20:32:34 +00:00
|
|
|
/// Updates the [`ButtonInput<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
|
2022-04-26 17:32:54 +00:00
|
|
|
///
|
|
|
|
/// ## Differences
|
|
|
|
///
|
2023-12-06 20:32:34 +00:00
|
|
|
/// The main difference between the [`MouseButtonInput`] event and the [`ButtonInput<MouseButton>`] resource is that
|
|
|
|
/// the latter has convenient functions like [`ButtonInput::pressed`], [`ButtonInput::just_pressed`] and [`ButtonInput::just_released`].
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn mouse_button_input_system(
|
2023-12-06 20:32:34 +00:00
|
|
|
mut mouse_button_input: ResMut<ButtonInput<MouseButton>>,
|
2021-01-19 06:23:30 +00:00
|
|
|
mut mouse_button_input_events: EventReader<MouseButtonInput>,
|
2020-06-05 06:34:21 +00:00
|
|
|
) {
|
2022-12-11 18:22:09 +00:00
|
|
|
mouse_button_input.bypass_change_detection().clear();
|
2023-08-30 14:20:03 +00:00
|
|
|
for event in mouse_button_input_events.read() {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-01 14:27:21 +00:00
|
|
|
|
|
|
|
/// Tracks how much the mouse has moved every frame.
|
|
|
|
///
|
|
|
|
/// This resource is reset to zero every frame.
|
|
|
|
///
|
|
|
|
/// This resource sums the total [`MouseMotion`] events received this frame.
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Resource, Debug, Clone, Copy, PartialEq, Default)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "bevy_reflect",
|
|
|
|
derive(Reflect),
|
|
|
|
reflect(Debug, Default, Resource, PartialEq)
|
|
|
|
)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2024-07-01 14:27:21 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2024-07-01 14:27:21 +00:00
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
|
|
|
pub struct AccumulatedMouseMotion {
|
|
|
|
/// The change in mouse position.
|
|
|
|
pub delta: Vec2,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tracks how much the mouse has scrolled every frame.
|
|
|
|
///
|
|
|
|
/// This resource is reset to zero every frame.
|
|
|
|
///
|
|
|
|
/// This resource sums the total [`MouseWheel`] events received this frame.
|
2024-07-08 01:09:07 +00:00
|
|
|
#[derive(Resource, Debug, Clone, Copy, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "bevy_reflect",
|
|
|
|
derive(Reflect),
|
|
|
|
reflect(Debug, Default, Resource, PartialEq)
|
|
|
|
)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2024-07-01 14:27:21 +00:00
|
|
|
#[cfg_attr(
|
2024-07-08 01:09:07 +00:00
|
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
2024-07-01 14:27:21 +00:00
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
|
|
|
pub struct AccumulatedMouseScroll {
|
|
|
|
/// The mouse scroll unit.
|
|
|
|
/// If this value changes while scrolling, then the
|
|
|
|
/// result of the accumulation could be incorrect
|
|
|
|
pub unit: MouseScrollUnit,
|
|
|
|
/// The change in scroll position.
|
|
|
|
pub delta: Vec2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AccumulatedMouseScroll {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
unit: MouseScrollUnit::Line,
|
|
|
|
delta: Vec2::ZERO,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the [`AccumulatedMouseMotion`] resource using the [`MouseMotion`] event.
|
|
|
|
/// The value of [`AccumulatedMouseMotion`] is reset to zero every frame
|
|
|
|
pub fn accumulate_mouse_motion_system(
|
|
|
|
mut mouse_motion_event: EventReader<MouseMotion>,
|
|
|
|
mut accumulated_mouse_motion: ResMut<AccumulatedMouseMotion>,
|
|
|
|
) {
|
|
|
|
let mut delta = Vec2::ZERO;
|
|
|
|
for event in mouse_motion_event.read() {
|
|
|
|
delta += event.delta;
|
|
|
|
}
|
|
|
|
accumulated_mouse_motion.delta = delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the [`AccumulatedMouseScroll`] resource using the [`MouseWheel`] event.
|
|
|
|
/// The value of [`AccumulatedMouseScroll`] is reset to zero every frame
|
|
|
|
pub fn accumulate_mouse_scroll_system(
|
|
|
|
mut mouse_scroll_event: EventReader<MouseWheel>,
|
|
|
|
mut accumulated_mouse_scroll: ResMut<AccumulatedMouseScroll>,
|
|
|
|
) {
|
|
|
|
let mut delta = Vec2::ZERO;
|
|
|
|
let mut unit = MouseScrollUnit::Line;
|
|
|
|
for event in mouse_scroll_event.read() {
|
|
|
|
if event.unit != unit {
|
|
|
|
unit = event.unit;
|
|
|
|
}
|
|
|
|
delta += Vec2::new(event.x, event.y);
|
|
|
|
}
|
|
|
|
accumulated_mouse_scroll.delta = delta;
|
|
|
|
accumulated_mouse_scroll.unit = unit;
|
|
|
|
}
|