-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a basic touch input system Co-authored-by: Michael Hills <mhills@gmail.com>
- Loading branch information
1 parent
d004bce
commit ecc938f
Showing
7 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use bevy_app::{EventReader, Events}; | ||
use bevy_ecs::{Local, Res, ResMut}; | ||
use bevy_math::Vec2; | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
/// A touch input event | ||
#[derive(Debug, Clone)] | ||
pub struct TouchInput { | ||
pub phase: TouchPhase, | ||
pub position: Vec2, | ||
/// | ||
/// ## Platform-specific | ||
/// | ||
/// Unique identifier of a finger. | ||
pub id: u64, | ||
} | ||
|
||
/// Describes touch-screen input state. | ||
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] | ||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] | ||
pub enum TouchPhase { | ||
Started, | ||
Moved, | ||
Ended, | ||
Cancelled, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct TouchSystemState { | ||
touch_event_reader: EventReader<TouchInput>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ActiveTouch { | ||
start_position: Vec2, | ||
previous_position: Vec2, | ||
current_position: Vec2, | ||
} | ||
|
||
impl ActiveTouch { | ||
pub fn delta(&self) -> Vec2 { | ||
self.current_position - self.previous_position | ||
} | ||
|
||
pub fn distance(&self) -> Vec2 { | ||
self.current_position - self.start_position | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct TouchInputState { | ||
pub active_touches: HashMap<u64, ActiveTouch>, | ||
pub just_pressed: HashSet<u64>, | ||
pub just_released: HashSet<u64>, | ||
pub just_cancelled: HashSet<u64>, | ||
} | ||
|
||
/// Updates the TouchInputState resource with the latest TouchInput events | ||
pub fn touch_screen_input_system( | ||
mut state: Local<TouchSystemState>, | ||
mut touch_state: ResMut<TouchInputState>, | ||
touch_input_events: Res<Events<TouchInput>>, | ||
) { | ||
touch_state.just_pressed.clear(); | ||
touch_state.just_released.clear(); | ||
touch_state.just_cancelled.clear(); | ||
|
||
for event in state.touch_event_reader.iter(&touch_input_events) { | ||
let active_touch = touch_state.active_touches.get(&event.id); | ||
match event.phase { | ||
TouchPhase::Started => { | ||
touch_state.active_touches.insert( | ||
event.id, | ||
ActiveTouch { | ||
start_position: event.position, | ||
previous_position: event.position, | ||
current_position: event.position, | ||
}, | ||
); | ||
touch_state.just_pressed.insert(event.id); | ||
} | ||
TouchPhase::Moved => { | ||
let old_touch = active_touch.unwrap(); | ||
let mut new_touch = old_touch.clone(); | ||
new_touch.previous_position = new_touch.current_position; | ||
new_touch.current_position = event.position; | ||
touch_state.active_touches.insert(event.id, new_touch); | ||
} | ||
TouchPhase::Ended => { | ||
touch_state.active_touches.remove(&event.id); | ||
touch_state.just_released.insert(event.id); | ||
} | ||
TouchPhase::Cancelled => { | ||
touch_state.active_touches.remove(&event.id); | ||
touch_state.just_cancelled.insert(event.id); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use bevy::{input::touch::*, prelude::*}; | ||
|
||
fn main() { | ||
App::build() | ||
.add_default_plugins() | ||
.init_resource::<TouchHandlerState>() | ||
.add_system(touch_system.system()) | ||
.run(); | ||
} | ||
|
||
#[derive(Default)] | ||
struct TouchHandlerState { | ||
touch_event_reader: EventReader<TouchInput>, | ||
} | ||
|
||
fn touch_system( | ||
mut state: ResMut<TouchHandlerState>, | ||
touch_events: Res<Events<TouchInput>>, | ||
touch_input_state: Res<TouchInputState>, | ||
) { | ||
for event in state.touch_event_reader.iter(&touch_events) { | ||
if touch_input_state.just_pressed.contains(&event.id) { | ||
println!( | ||
"just pressed touch with id: {:?}, at: {:?}", | ||
event.id, event.position | ||
); | ||
} | ||
|
||
if touch_input_state.just_released.contains(&event.id) { | ||
println!( | ||
"just released touch with id: {:?}, at: {:?}", | ||
event.id, event.position | ||
); | ||
} | ||
|
||
if touch_input_state.just_cancelled.contains(&event.id) { | ||
println!("cancelled touch with id: {:?}", event.id); | ||
} | ||
} | ||
} |