Change bevy_input::Touch API to match similar APIs (#952)

This commit is contained in:
Amber Kowalski 2020-11-30 20:14:08 -05:00 committed by GitHub
parent c7b9ad5097
commit 4c1bc02723
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 12 deletions

View file

@ -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<ForceTouch>,
pub previous_position: Vec2,
pub previous_force: Option<ForceTouch>,
pub position: Vec2,
pub force: Option<ForceTouch>,
id: u64,
start_position: Vec2,
start_force: Option<ForceTouch>,
previous_position: Vec2,
previous_force: Option<ForceTouch>,
position: Vec2,
force: Option<ForceTouch>,
}
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<ForceTouch> {
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<ForceTouch> {
self.force
}
}
impl From<&TouchInput> for Touch {

View file

@ -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) {

View file

@ -11,24 +11,26 @@ fn touch_system(touches: Res<Touches>) {
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()));
}
}