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

rename touchpad to gesture, and add new gestures #13660

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions crates/bevy_input/src/gestures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Gestures functionnality, from touchscreens and touchpads.

Check warning on line 1 in crates/bevy_input/src/gestures.rs

View workflow job for this annotation

GitHub Actions / typos

"functionnality" should be "functionality".
mockersf marked this conversation as resolved.
Show resolved Hide resolved

use bevy_ecs::event::Event;
use bevy_math::Vec2;
use bevy_reflect::Reflect;

#[cfg(feature = "serialize")]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};

/// Two-finger pinch gesture, often used for magnifications.
///
/// Positive delta values indicate magnification (zooming in) and
/// negative delta values indicate shrinking (zooming out).
///
/// ## Platform-specific
///
/// - Only available on **`macOS`** and **`iOS`**.
/// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct PinchGesture(pub f32);

/// Two-finger rotation gesture.
///
/// Positive delta values indicate rotation counterclockwise and
/// negative delta values indicate rotation clockwise.
///
/// ## Platform-specific
///
/// - Only available on **`macOS`** and **`iOS`**.
/// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct RotationGesture(pub f32);

/// Double tap gesture.
///
/// ## Platform-specific
///
/// - Only available on **`macOS`** and **`iOS`**.
/// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct DoubleTapGesture;

/// Pan gesture.
///
/// ## Platform-specific
///
/// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct PanGesture(pub Vec2);
16 changes: 10 additions & 6 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ mod button_input;
/// Common run conditions
pub mod common_conditions;
pub mod gamepad;
pub mod gestures;
pub mod keyboard;
pub mod mouse;
pub mod touch;
pub mod touchpad;

pub use axis::*;
pub use button_input::*;
Expand All @@ -41,10 +41,10 @@ pub mod prelude {
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use gestures::*;
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
use touch::{touch_screen_input_system, TouchInput, Touches};
use touchpad::{TouchpadMagnify, TouchpadRotate};

use gamepad::{
gamepad_axis_event_system, gamepad_button_event_system, gamepad_connection_system,
Expand Down Expand Up @@ -77,8 +77,10 @@ impl Plugin for InputPlugin {
.add_event::<MouseWheel>()
.init_resource::<ButtonInput<MouseButton>>()
.add_systems(PreUpdate, mouse_button_input_system.in_set(InputSystem))
.add_event::<TouchpadMagnify>()
.add_event::<TouchpadRotate>()
.add_event::<PinchGesture>()
.add_event::<RotationGesture>()
.add_event::<DoubleTapGesture>()
.add_event::<PanGesture>()
// gamepad
.add_event::<GamepadConnectionEvent>()
.add_event::<GamepadButtonChangedEvent>()
Expand Down Expand Up @@ -114,8 +116,10 @@ impl Plugin for InputPlugin {
app.register_type::<ButtonState>()
.register_type::<KeyboardInput>()
.register_type::<MouseButtonInput>()
.register_type::<TouchpadMagnify>()
.register_type::<TouchpadRotate>()
.register_type::<PinchGesture>()
.register_type::<RotationGesture>()
.register_type::<DoubleTapGesture>()
.register_type::<PanGesture>()
.register_type::<TouchInput>()
.register_type::<GamepadEvent>()
.register_type::<GamepadButtonInput>()
Expand Down
41 changes: 0 additions & 41 deletions crates/bevy_input/src/touchpad.rs

This file was deleted.

31 changes: 31 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,33 @@
/// [`wgpu::SurfaceConfiguration::desired_maximum_frame_latency`]:
/// https://docs.rs/wgpu/latest/wgpu/type.SurfaceConfiguration.html#structfield.desired_maximum_frame_latency
pub desired_maximum_frame_latency: Option<NonZeroU32>,
/// Sets wether this window recognize [`PinchGesture`]

Check warning on line 284 in crates/bevy_window/src/window.rs

View workflow job for this annotation

GitHub Actions / typos

"wether" should be "weather" or "whether".
///
/// ## Platform-specific
///
/// - Only used on iOS.
/// - On macOS, they are recognized by default and can't be disabled.
pub recognize_pinch_gesture: bool,
/// Sets wether this window recognize [`RotationGesture`]

Check warning on line 291 in crates/bevy_window/src/window.rs

View workflow job for this annotation

GitHub Actions / typos

"wether" should be "weather" or "whether".
///
/// ## Platform-specific
///
/// - Only used on iOS.
/// - On macOS, they are recognized by default and can't be disabled.
pub recognize_rotation_gesture: bool,
/// Sets wether this window recognize [`DoubleTapGesture`]

Check warning on line 298 in crates/bevy_window/src/window.rs

View workflow job for this annotation

GitHub Actions / typos

"wether" should be "weather" or "whether".
///
/// ## Platform-specific
///
/// - Only used on iOS.
/// - On macOS, they are recognized by default and can't be disabled.
pub recognize_doubletap_gesture: bool,
/// Sets wether this window recognize [`PanGesture`], with a number of fingers between the first value and the last.

Check warning on line 305 in crates/bevy_window/src/window.rs

View workflow job for this annotation

GitHub Actions / typos

"wether" should be "weather" or "whether".
///
/// ## Platform-specific
///
/// - Only used on iOS.
pub recognize_pan_gesture: Option<(u8, u8)>,
}

impl Default for Window {
Expand Down Expand Up @@ -311,6 +338,10 @@
visible: true,
skip_taskbar: false,
desired_maximum_frame_latency: None,
recognize_pinch_gesture: false,
recognize_rotation_gesture: false,
recognize_doubletap_gesture: false,
recognize_pan_gesture: None,
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions crates/bevy_winit/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use bevy_ecs::prelude::*;
use bevy_ecs::system::SystemState;
use bevy_ecs::world::FromWorld;
use bevy_input::{
gestures::*,
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
touchpad::{TouchpadMagnify, TouchpadRotate},
};
use bevy_log::{error, trace, warn};
use bevy_math::{ivec2, DVec2, Vec2};
Expand Down Expand Up @@ -264,10 +264,19 @@ impl<T: Event> ApplicationHandler<T> for WinitAppRunnerState<T> {
});
}
WindowEvent::PinchGesture { delta, .. } => {
self.winit_events.send(TouchpadMagnify(delta as f32));
self.winit_events.send(PinchGesture(delta as f32));
}
WindowEvent::RotationGesture { delta, .. } => {
self.winit_events.send(TouchpadRotate(delta));
self.winit_events.send(RotationGesture(delta));
}
WindowEvent::DoubleTapGesture { .. } => {
self.winit_events.send(DoubleTapGesture);
}
WindowEvent::PanGesture { delta, .. } => {
self.winit_events.send(PanGesture(Vec2 {
x: delta.x,
y: delta.y,
}));
}
WindowEvent::MouseWheel { delta, .. } => match delta {
event::MouseScrollDelta::LineDelta(x, y) => {
Expand Down Expand Up @@ -674,10 +683,16 @@ impl<T: Event> WinitAppRunnerState<T> {
WinitEvent::MouseWheel(e) => {
world.send_event(e);
}
WinitEvent::TouchpadMagnify(e) => {
WinitEvent::PinchGesture(e) => {
world.send_event(e);
}
WinitEvent::RotationGesture(e) => {
world.send_event(e);
}
WinitEvent::DoubleTapGesture(e) => {
world.send_event(e);
}
WinitEvent::TouchpadRotate(e) => {
WinitEvent::PanGesture(e) => {
world.send_event(e);
}
WinitEvent::TouchInput(e) => {
Expand Down
40 changes: 40 additions & 0 deletions crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize};
use winit::event_loop::ActiveEventLoop;

use bevy_ecs::query::With;
#[cfg(target_os = "ios")]
use winit::platform::ios::WindowExtIOS;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::WindowExtWebSys;

Expand Down Expand Up @@ -97,6 +99,19 @@ pub fn create_windows<F: QueryFilter + 'static>(
style.set_property("height", "100%").unwrap();
}
}

#[cfg(target_os = "ios")]
{
winit_window.recognize_pinch_gesture(window.recognize_pinch_gesture);
winit_window.recognize_rotation_gesture(window.recognize_rotation_gesture);
winit_window.recognize_doubletap_gesture(window.recognize_doubletap_gesture);
if let Some((min, max)) = window.recognize_pan_gesture {
winit_window.recognize_pan_gesture(true, min, max);
} else {
winit_window.recognize_pan_gesture(false, 0, 0);
}
}

window_created_events.send(WindowCreated { window: entity });
}
}
Expand Down Expand Up @@ -360,6 +375,31 @@ pub(crate) fn changed_windows(
winit_window.set_visible(window.visible);
}

#[cfg(target_os = "ios")]
{
if window.recognize_pinch_gesture != cache.window.recognize_pinch_gesture {
winit_window.recognize_pinch_gesture(window.recognize_pinch_gesture);
}
if window.recognize_rotation_gesture != cache.window.recognize_rotation_gesture {
winit_window.recognize_rotation_gesture(window.recognize_rotation_gesture);
}
if window.recognize_doubletap_gesture != cache.window.recognize_doubletap_gesture {
winit_window.recognize_doubletap_gesture(window.recognize_doubletap_gesture);
}
if window.recognize_pan_gesture != cache.window.recognize_pan_gesture {
match (
window.recognize_pan_gesture,
cache.window.recognize_pan_gesture,
) {
(Some(_), Some(_)) => {
warn!("Bevy currently doesn't support modifying PanGesture number of fingers recognition. Please disable it before re-enabling it with the new number of fingers");
}
(Some((min, max)), _) => winit_window.recognize_pan_gesture(true, min, max),
_ => winit_window.recognize_pan_gesture(false, 0, 0),
}
}
}

cache.window = window.clone();
}
}
30 changes: 21 additions & 9 deletions crates/bevy_winit/src/winit_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use bevy_ecs::prelude::*;
use bevy_input::keyboard::KeyboardInput;
use bevy_input::touch::TouchInput;
use bevy_input::{
gestures::*,
mouse::{MouseButtonInput, MouseMotion, MouseWheel},
touchpad::{TouchpadMagnify, TouchpadRotate},
};
use bevy_reflect::Reflect;
#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -55,8 +55,10 @@ pub enum WinitEvent {
MouseMotion(MouseMotion),
MouseWheel(MouseWheel),

TouchpadMagnify(TouchpadMagnify),
TouchpadRotate(TouchpadRotate),
PinchGesture(PinchGesture),
RotationGesture(RotationGesture),
DoubleTapGesture(DoubleTapGesture),
PanGesture(PanGesture),

TouchInput(TouchInput),

Expand Down Expand Up @@ -168,14 +170,24 @@ impl From<MouseWheel> for WinitEvent {
Self::MouseWheel(e)
}
}
impl From<TouchpadMagnify> for WinitEvent {
fn from(e: TouchpadMagnify) -> Self {
Self::TouchpadMagnify(e)
impl From<PinchGesture> for WinitEvent {
fn from(e: PinchGesture) -> Self {
Self::PinchGesture(e)
}
}
impl From<TouchpadRotate> for WinitEvent {
fn from(e: TouchpadRotate) -> Self {
Self::TouchpadRotate(e)
impl From<RotationGesture> for WinitEvent {
fn from(e: RotationGesture) -> Self {
Self::RotationGesture(e)
}
}
impl From<DoubleTapGesture> for WinitEvent {
fn from(e: DoubleTapGesture) -> Self {
Self::DoubleTapGesture(e)
}
}
impl From<PanGesture> for WinitEvent {
fn from(e: PanGesture) -> Self {
Self::PanGesture(e)
}
}
impl From<TouchInput> for WinitEvent {
Expand Down
Loading
Loading