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

Touch support implementation #696

Merged
merged 1 commit into from
Oct 18, 2020
Merged

Conversation

naithar
Copy link
Contributor

@naithar naithar commented Oct 17, 2020

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):

  • Added a changelog entry
  • Added respective unit tests

}

#[derive(Default)]
pub struct TouchInputState {
Copy link
Member

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 friendly Touches
  • Rename ActiveTouch to just Touch
  • 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);
        }
    }
}

Copy link
Member

@cart cart Oct 17, 2020

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())
    }
}

touch_event_reader: EventReader<TouchInput>,
}

fn touch_system(
Copy link
Member

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.

use bevy_app::{EventReader, Events};
use bevy_ecs::{Local, Res, ResMut};
use bevy_math::Vec2;
use std::collections::{HashMap, HashSet};
Copy link
Member

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.

@memoryruins memoryruins added C-Enhancement A new feature A-Input Player input via keyboard, mouse, gamepad, and more labels Oct 17, 2020
Adds a basic touch input system

Co-authored-by: Michael Hills <mhills@gmail.com>
@cart
Copy link
Member

cart commented Oct 18, 2020

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!

@cart cart merged commit a80469b into bevyengine:master Oct 18, 2020
@enfipy enfipy mentioned this pull request Oct 18, 2020
This was referenced Oct 20, 2020
joshuajbouw pushed a commit to joshuajbouw/bevy that referenced this pull request Oct 24, 2020
Adds a basic touch input system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Enhancement A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants