-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Touch support implementation #696
Conversation
4023bc0
to
ecc938f
Compare
crates/bevy_input/src/touch.rs
Outdated
} | ||
|
||
#[derive(Default)] | ||
pub struct TouchInputState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that we should make this the "go to" high level interface for touch interaction (much like the Input<T>
abstractions). This would reduce the need to directly consume events.
Some thoughts on changes to make here:
- Rename
TouchInputState
, to the more friendlyTouches
- Rename
ActiveTouch
to justTouch
- Add the "touch id" to ActiveTouch
- Add an iter() method to Touches, which iterates all Touches
- Make all Touches fields private + add relevant methods to expose functionality
That would result in user code looking something like this:
fn touch_system(touches: Res<Touches>) {
for touch in touches.iter() {
println!(
"active touch: {} {} {} {}",
touch.id, touch.position, touch.previous_position, touch.start_position
);
if touches.just_pressed(touch.id) {
println!(
"just pressed touch with id: {:?}, at: {:?}",
touch.id, touch.position
);
}
if touches.just_released(touch.id) {
println!(
"just released touch with id: {:?}, at: {:?}",
touch.id, touch.position
);
}
if touches.just_cancelled(touch.id) {
println!("cancelled touch with id: {:?}", event.id);
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Partial example:
#[derive(Default)]
pub struct Touches {
active_touches: HashMap<u64, Touch>,
just_pressed: HashSet<u64>,
just_released: HashSet<u64>,
just_cancelled: HashSet<u64>,
}
impl Touches {
pub fn iter(&self) -> impl Iterator<Item=&ActiveTouch> + '_ {
self.active_touches.values()
}
pub fn just_pressed(&self, id: u64) -> bool {
self.just_pressed.contains(&id)
}
pub fn iter_just_pressed(&self) -> impl Iterator<Item=&ActiveTouch> + '_ {
self.just_pressed.iter().map(move |id| self.active_touches.get(id).unwrap())
}
}
examples/input/touch_input.rs
Outdated
touch_event_reader: EventReader<TouchInput>, | ||
} | ||
|
||
fn touch_system( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we make "high level" touch changes, I think we should have two touch examples. One that illustrates how to use touch events (like the current example does) and one that illustrates the high level interface.
crates/bevy_input/src/touch.rs
Outdated
use bevy_app::{EventReader, Events}; | ||
use bevy_ecs::{Local, Res, ResMut}; | ||
use bevy_math::Vec2; | ||
use std::collections::{HashMap, HashSet}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets use the bevy_utils::{HashMap, HashSet}
here. They're still the std types, but they use the much faster aHash algorithm instead of the defaults.
Adds a basic touch input system Co-authored-by: Michael Hills <[email protected]>
ecc938f
to
9c950ca
Compare
This looks good to me. I think we should make one of the two examples actually consume touch events, but I'll just do that in a followup pr so we can merge this faster. Thanks! |
Adds a basic touch input system
Added initial touch input implementation.
Replaces #334 as it seems abandoned.
Required for #87 and probably #86
Related issue #300
Checklist
I confirm that I have done the following (if applicable):