Skip to content

Commit 4034740

Browse files
authored
Add window entity to TouchInput events (bevyengine#11128)
# Objective If you have multiple windows, there is no way to determine which window a `TouchInput` event applies to. This fixes that. ## Solution - Add the window entity directly to `TouchInput`, just like the other input events. - Fixes bevyengine#6011. ## Migration Guide + Add a `window` field when constructing or destructuring a `TouchInput` struct.
1 parent d8d8bcf commit 4034740

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

crates/bevy_input/src/touch.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The touch input functionality.
22
3+
use bevy_ecs::entity::Entity;
34
use bevy_ecs::event::{Event, EventReader};
45
use bevy_ecs::system::{ResMut, Resource};
56
use bevy_math::Vec2;
@@ -44,6 +45,8 @@ pub struct TouchInput {
4445
pub phase: TouchPhase,
4546
/// The position of the finger on the touchscreen.
4647
pub position: Vec2,
48+
/// The window entity registering the touch.
49+
pub window: Entity,
4750
/// Describes how hard the screen was pressed.
4851
///
4952
/// May be [`None`] if the platform does not support pressure sensitivity.
@@ -408,6 +411,7 @@ mod test {
408411
#[test]
409412
fn touch_process() {
410413
use crate::{touch::TouchPhase, TouchInput, Touches};
414+
use bevy_ecs::entity::Entity;
411415
use bevy_math::Vec2;
412416

413417
let mut touches = Touches::default();
@@ -417,6 +421,7 @@ mod test {
417421
let touch_event = TouchInput {
418422
phase: TouchPhase::Started,
419423
position: Vec2::splat(4.0),
424+
window: Entity::PLACEHOLDER,
420425
force: None,
421426
id: 4,
422427
};
@@ -432,6 +437,7 @@ mod test {
432437
let moved_touch_event = TouchInput {
433438
phase: TouchPhase::Moved,
434439
position: Vec2::splat(5.0),
440+
window: Entity::PLACEHOLDER,
435441
force: None,
436442
id: touch_event.id,
437443
};
@@ -453,6 +459,7 @@ mod test {
453459
let cancel_touch_event = TouchInput {
454460
phase: TouchPhase::Canceled,
455461
position: Vec2::ONE,
462+
window: Entity::PLACEHOLDER,
456463
force: None,
457464
id: touch_event.id,
458465
};
@@ -468,6 +475,7 @@ mod test {
468475
let end_touch_event = TouchInput {
469476
phase: TouchPhase::Ended,
470477
position: Vec2::splat(4.0),
478+
window: Entity::PLACEHOLDER,
471479
force: None,
472480
id: touch_event.id,
473481
};
@@ -487,13 +495,15 @@ mod test {
487495
#[test]
488496
fn touch_pressed() {
489497
use crate::{touch::TouchPhase, TouchInput, Touches};
498+
use bevy_ecs::entity::Entity;
490499
use bevy_math::Vec2;
491500

492501
let mut touches = Touches::default();
493502

494503
let touch_event = TouchInput {
495504
phase: TouchPhase::Started,
496505
position: Vec2::splat(4.0),
506+
window: Entity::PLACEHOLDER,
497507
force: None,
498508
id: 4,
499509
};
@@ -509,13 +519,15 @@ mod test {
509519
#[test]
510520
fn touch_released() {
511521
use crate::{touch::TouchPhase, TouchInput, Touches};
522+
use bevy_ecs::entity::Entity;
512523
use bevy_math::Vec2;
513524

514525
let mut touches = Touches::default();
515526

516527
let touch_event = TouchInput {
517528
phase: TouchPhase::Ended,
518529
position: Vec2::splat(4.0),
530+
window: Entity::PLACEHOLDER,
519531
force: None,
520532
id: 4,
521533
};
@@ -531,13 +543,15 @@ mod test {
531543
#[test]
532544
fn touch_canceled() {
533545
use crate::{touch::TouchPhase, TouchInput, Touches};
546+
use bevy_ecs::entity::Entity;
534547
use bevy_math::Vec2;
535548

536549
let mut touches = Touches::default();
537550

538551
let touch_event = TouchInput {
539552
phase: TouchPhase::Canceled,
540553
position: Vec2::splat(4.0),
554+
window: Entity::PLACEHOLDER,
541555
force: None,
542556
id: 4,
543557
};

crates/bevy_winit/src/converters.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn convert_mouse_button(mouse_button: winit::event::MouseButton) -> MouseBut
4040
pub fn convert_touch_input(
4141
touch_input: winit::event::Touch,
4242
location: winit::dpi::LogicalPosition<f64>,
43+
window_entity: Entity,
4344
) -> TouchInput {
4445
TouchInput {
4546
phase: match touch_input.phase {
@@ -49,6 +50,7 @@ pub fn convert_touch_input(
4950
winit::event::TouchPhase::Cancelled => TouchPhase::Canceled,
5051
},
5152
position: Vec2::new(location.x as f32, location.y as f32),
53+
window: window_entity,
5254
force: touch_input.force.map(|f| match f {
5355
winit::event::Force::Calibrated {
5456
force,

crates/bevy_winit/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ pub fn winit_runner(mut app: App) {
515515
.to_logical(window.resolution.scale_factor() as f64);
516516
event_writers
517517
.touch_input
518-
.send(converters::convert_touch_input(touch, location));
518+
.send(converters::convert_touch_input(
519+
touch,
520+
location,
521+
window_entity,
522+
));
519523
}
520524
WindowEvent::ScaleFactorChanged {
521525
scale_factor,

0 commit comments

Comments
 (0)