Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touchpad magnify and rotate events #8791

Merged
merged 10 commits into from
Jun 8, 2023
9 changes: 7 additions & 2 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use bevy_ecs::prelude::*;
use bevy_reflect::{FromReflect, Reflect};
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput, ScanCode};
use mouse::{
mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseScrollUnit,
MouseWheel,
mouse_button_input_system, Magnify, MouseButton, MouseButtonInput, MouseMotion,
MouseScrollUnit, MouseWheel, Rotate,
};
use touch::{touch_screen_input_system, ForceTouch, TouchInput, TouchPhase, Touches};

Expand Down Expand Up @@ -67,6 +67,8 @@ impl Plugin for InputPlugin {
.add_event::<MouseWheel>()
.init_resource::<Input<MouseButton>>()
.add_systems(PreUpdate, mouse_button_input_system.in_set(InputSystem))
.add_event::<Magnify>()
.add_event::<Rotate>()
// gamepad
.add_event::<GamepadConnectionEvent>()
.add_event::<GamepadButtonChangedEvent>()
Expand Down Expand Up @@ -112,6 +114,9 @@ impl Plugin for InputPlugin {
.register_type::<MouseScrollUnit>()
.register_type::<MouseWheel>();

// Register touchpad types
app.register_type::<Magnify>().register_type::<Rotate>();

// Register touch types
app.register_type::<TouchInput>()
.register_type::<ForceTouch>()
Expand Down
26 changes: 26 additions & 0 deletions crates/bevy_input/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ pub struct MouseWheel {
pub y: f32,
}

/// Touchpad magnification event with two-finger pinch gesture.
///
/// Positive delta values indicate magnification (zooming in) and
/// negative delta values indicate shrinking (zooming out).
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect, FromReflect)]
#[reflect(Debug, PartialEq)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add FromReflect to this attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that already the case? It is the last argument to #[derive(...)].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, to clarify I’m referring to adding a ReflectFromReflect registration like #[reflect(Debug, PartialEq, FromReflect)]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. I've added it to #[reflect(...)] but the reason for it not being initially there is that none of the types in bevy_input::mouse use it. So its probably missing there as well, right? Or is that intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it should be there as well. #8776 addresses that issue.

#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct Magnify(pub f32);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these also be prefixed with Touchpad for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these types into their own module as I think that this is more future proof.

Copy link
Member

@MrGVSV MrGVSV Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like they should still be renamed to tie them back to the touchpad. Rotate especially is pretty generic and could be confusing to some users.


/// Touchpad rotation event with two-finger rotation gesture.
///
/// Positive delta values indicate rotation counterclockwise and
/// negative delta values indicate rotation clockwise.
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect, FromReflect)]
#[reflect(Debug, PartialEq)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct Rotate(pub f32);

/// Updates the [`Input<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
///
/// ## Differences
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use bevy_ecs::event::{Events, ManualEventReader};
use bevy_ecs::prelude::*;
use bevy_input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
mouse::{Magnify, MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel, Rotate},
touch::TouchInput,
};
use bevy_math::{ivec2, DVec2, Vec2};
Expand Down Expand Up @@ -236,6 +236,8 @@ struct InputEvents<'w> {
keyboard_input: EventWriter<'w, KeyboardInput>,
character_input: EventWriter<'w, ReceivedCharacter>,
mouse_button_input: EventWriter<'w, MouseButtonInput>,
magnify_input: EventWriter<'w, Magnify>,
rotate_input: EventWriter<'w, Rotate>,
mouse_wheel_input: EventWriter<'w, MouseWheel>,
touch_input: EventWriter<'w, TouchInput>,
ime_input: EventWriter<'w, Ime>,
Expand Down Expand Up @@ -481,6 +483,12 @@ pub fn winit_runner(mut app: App) {
state: converters::convert_element_state(state),
});
}
WindowEvent::TouchpadMagnify { delta, .. } => {
input_events.magnify_input.send(Magnify(delta as f32));
}
WindowEvent::TouchpadRotate { delta, .. } => {
input_events.rotate_input.send(Rotate(delta as f32));
}
WindowEvent::MouseWheel { delta, .. } => match delta {
event::MouseScrollDelta::LineDelta(x, y) => {
input_events.mouse_wheel_input.send(MouseWheel {
Expand Down
12 changes: 11 additions & 1 deletion examples/input/mouse_input_events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Prints all mouse events to the console.

use bevy::{
input::mouse::{MouseButtonInput, MouseMotion, MouseWheel},
input::mouse::{Magnify, MouseButtonInput, MouseMotion, MouseWheel, Rotate},
prelude::*,
};

Expand All @@ -18,6 +18,8 @@ fn print_mouse_events_system(
mut mouse_motion_events: EventReader<MouseMotion>,
mut cursor_moved_events: EventReader<CursorMoved>,
mut mouse_wheel_events: EventReader<MouseWheel>,
mut magnify_events: EventReader<Magnify>,
mut rotate_events: EventReader<Rotate>,
) {
for event in mouse_button_input_events.iter() {
info!("{:?}", event);
Expand All @@ -34,4 +36,12 @@ fn print_mouse_events_system(
for event in mouse_wheel_events.iter() {
info!("{:?}", event);
}

for event in magnify_events.iter() {
info!("{:?}", event);
}

for event in rotate_events.iter() {
info!("{:?}", event);
}
}