From 33cf7ba9d1ea38eb01beda7be3a7428ce5e2fbd6 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 10:22:39 -0600 Subject: [PATCH] Change bevy_input::Touch API to match similar APIs --- crates/bevy_input/src/touch.rs | 44 ++++++++++++++++++++++++++++------ crates/bevy_ui/src/focus.rs | 2 +- examples/input/touch_input.rs | 10 ++++---- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 9e78f7d05bedf..3c32b01e4f6d8 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -85,13 +85,13 @@ pub struct TouchSystemState { #[derive(Debug, Clone, Copy)] pub struct Touch { - pub id: u64, - pub start_position: Vec2, - pub start_force: Option, - pub previous_position: Vec2, - pub previous_force: Option, - pub position: Vec2, - pub force: Option, + id: u64, + start_position: Vec2, + start_force: Option, + previous_position: Vec2, + previous_force: Option, + position: Vec2, + force: Option, } impl Touch { @@ -102,6 +102,36 @@ impl Touch { pub fn distance(&self) -> Vec2 { self.position - self.start_position } + + #[inline] + pub fn id(&self) -> u64 { + self.id + } + + #[inline] + pub fn start_position(&self) -> Vec2 { + self.start_position + } + + #[inline] + pub fn start_force(&self) -> Option { + self.start_force + } + + #[inline] + pub fn previous_position(&self) -> Vec2 { + self.previous_position + } + + #[inline] + pub fn position(&self) -> Vec2 { + self.position + } + + #[inline] + pub fn force(&self) -> Option { + self.force + } } impl From<&TouchInput> for Touch { diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 5ced61ebd9571..663b27b4c63db 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -56,7 +56,7 @@ pub fn ui_focus_system( state.cursor_position = cursor_moved.position; } if let Some(touch) = touches_input.get_pressed(0) { - state.cursor_position = touch.position; + state.cursor_position = touch.position(); } if mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0) { diff --git a/examples/input/touch_input.rs b/examples/input/touch_input.rs index 2c9873a16ce55..13dc7ea658d21 100644 --- a/examples/input/touch_input.rs +++ b/examples/input/touch_input.rs @@ -11,24 +11,26 @@ fn touch_system(touches: Res) { for touch in touches.iter_just_pressed() { println!( "just pressed touch with id: {:?}, at: {:?}", - touch.id, touch.position + touch.id(), + touch.position() ); } for touch in touches.iter_just_released() { println!( "just released touch with id: {:?}, at: {:?}", - touch.id, touch.position + touch.id(), + touch.position() ); } for touch in touches.iter_just_cancelled() { - println!("cancelled touch with id: {:?}", touch.id); + println!("cancelled touch with id: {:?}", touch.id()); } // you can also iterate all current touches and retrieve their state like this: for touch in touches.iter() { println!("active touch: {:?}", touch); - println!(" just_pressed: {}", touches.just_pressed(touch.id)); + println!(" just_pressed: {}", touches.just_pressed(touch.id())); } }