Skip to content

Commit b3e39d0

Browse files
author
KDecay
committed
Update touch.rs docs (#4523)
# Objective - Part of the splitting process of #3692. ## Solution - Document `touch.rs` inside of `bevy_input`.
1 parent 06d709b commit b3e39d0

File tree

1 file changed

+103
-17
lines changed

1 file changed

+103
-17
lines changed

crates/bevy_input/src/touch.rs

Lines changed: 103 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,45 @@ use bevy_ecs::system::ResMut;
33
use bevy_math::Vec2;
44
use bevy_utils::HashMap;
55

6-
/// Represents a touch event
6+
/// A touch input event.
77
///
8-
/// Every time the user touches the screen, a new `Start` event with an unique
9-
/// identifier for the finger is generated. When the finger is lifted, an `End`
8+
/// ## Logic
9+
///
10+
/// Every time the user touches the screen, a new [`TouchPhase::Started`] event with an unique
11+
/// identifier for the finger is generated. When the finger is lifted, the [`TouchPhase::Ended`]
1012
/// event is generated with the same finger id.
1113
///
12-
/// After a `Start` event has been emitted, there may be zero or more `Move`
14+
/// After a [`TouchPhase::Started`] event has been emitted, there may be zero or more [`TouchPhase::Moved`]
1315
/// events when the finger is moved or the touch pressure changes.
1416
///
15-
/// The finger id may be reused by the system after an `End` event. The user
16-
/// should assume that a new `Start` event received with the same id has nothing
17+
/// The finger id may be reused by the system after an [`TouchPhase::Ended`] event. The user
18+
/// should assume that a new [`TouchPhase::Started`] event received with the same id has nothing
1719
/// to do with the old finger and is a new finger.
1820
///
19-
/// A `Cancelled` event is emitted when the system has canceled tracking this
21+
/// A [`TouchPhase::Cancelled`] event is emitted when the system has canceled tracking this
2022
/// touch, such as when the window loses focus, or on iOS if the user moves the
2123
/// device against their face.
24+
///
25+
/// ## Note
26+
///
27+
/// This event is the translated version of the `WindowEvent::Touch` from the `winit` crate.
28+
/// It is available to the end user and can be used for game logic.
2229
#[derive(Debug, Clone, Copy, PartialEq)]
2330
pub struct TouchInput {
31+
/// The phase of the touch input.
2432
pub phase: TouchPhase,
33+
/// The position of the finger on the touchscreen.
2534
pub position: Vec2,
26-
/// Describes how hard the screen was pressed. May be `None` if the platform
27-
/// does not support pressure sensitivity.
35+
/// Describes how hard the screen was pressed.
2836
///
29-
/// ## Platform-specific
30-
///
31-
/// - Only available on **iOS** 9.0+ and **Windows** 8+.
37+
/// May be [`None`] if the platform does not support pressure sensitivity.
38+
/// This feature is only available on **iOS** 9.0+ and **Windows** 8+.
3239
pub force: Option<ForceTouch>,
33-
/// Unique identifier of a finger.
40+
/// The unique identifier of the finger.
3441
pub id: u64,
3542
}
3643

37-
/// Describes the force of a touch event
44+
/// A force description of a [`Touch`](crate::touch::Touch) input.
3845
#[derive(Debug, Clone, Copy, PartialEq)]
3946
pub enum ForceTouch {
4047
/// On iOS, the force is calibrated so that the same number corresponds to
@@ -63,65 +70,107 @@ pub enum ForceTouch {
6370
/// If the platform reports the force as normalized, we have no way of
6471
/// knowing how much pressure 1.0 corresponds to – we know it's the maximum
6572
/// amount of force, but as to how much force, you might either have to
66-
/// press really really hard, or not hard at all, depending on the device.
73+
/// press really hard, or not hard at all, depending on the device.
6774
Normalized(f64),
6875
}
6976

70-
/// Describes touch-screen input state.
77+
/// A phase of a [`TouchInput`](crate::touch::TouchInput).
78+
///
79+
/// ## Usage
80+
///
81+
/// It is used to describe the phase of the touch input that is currently active.
82+
/// This includes a phase that indicates that a touch input has started or ended,
83+
/// or that a finger has moved. There is also a cancelled phase that indicates that
84+
/// the system cancelled the tracking of the finger.
7185
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
7286
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
7387
pub enum TouchPhase {
88+
/// A finger started to touch the touchscreen.
7489
Started,
90+
/// A finger moved over the touchscreen.
7591
Moved,
92+
/// A finger stopped touching the touchscreen.
7693
Ended,
94+
/// The system cancelled the tracking of the finger.
95+
///
96+
/// This occurs when the window loses focus, or on iOS if the user moves the
97+
/// device against their face.
7798
Cancelled,
7899
}
79100

101+
/// A touch input.
102+
///
103+
/// ## Usage
104+
///
105+
/// It is used to store the position and force of a touch input and also the `id` of the finger.
106+
/// The data of the touch input comes from the [`TouchInput`] event and is being stored
107+
/// inside of the [`Touches`] `bevy` resource.
80108
#[derive(Debug, Clone, Copy)]
81109
pub struct Touch {
110+
/// The id of the touch input.
82111
id: u64,
112+
/// The starting position of the touch input.
83113
start_position: Vec2,
114+
/// The starting force of the touch input.
84115
start_force: Option<ForceTouch>,
116+
/// The previous position of the touch input.
85117
previous_position: Vec2,
118+
/// The previous force of the touch input.
86119
previous_force: Option<ForceTouch>,
120+
/// The current position of the touch input.
87121
position: Vec2,
122+
/// The current force of the touch input.
88123
force: Option<ForceTouch>,
89124
}
90125

91126
impl Touch {
127+
/// The delta of the current `position` and the `previous_position`.
92128
pub fn delta(&self) -> Vec2 {
93129
self.position - self.previous_position
94130
}
95131

132+
/// The distance of the `start_position` and the current `position`.
96133
pub fn distance(&self) -> Vec2 {
97134
self.position - self.start_position
98135
}
99136

137+
/// Returns the `id` of the touch.
100138
#[inline]
101139
pub fn id(&self) -> u64 {
102140
self.id
103141
}
104142

143+
/// Returns the `start_position` of the touch.
105144
#[inline]
106145
pub fn start_position(&self) -> Vec2 {
107146
self.start_position
108147
}
109148

149+
/// Returns the `start_force` of the touch.
110150
#[inline]
111151
pub fn start_force(&self) -> Option<ForceTouch> {
112152
self.start_force
113153
}
114154

155+
/// Returns the `previous_position` of the touch.
115156
#[inline]
116157
pub fn previous_position(&self) -> Vec2 {
117158
self.previous_position
118159
}
119160

161+
/// Returns the `previous_force` of the touch.
162+
#[inline]
163+
pub fn previous_force(&self) -> Option<ForceTouch> {
164+
self.previous_force
165+
}
166+
167+
/// Returns the current `position` of the touch.
120168
#[inline]
121169
pub fn position(&self) -> Vec2 {
122170
self.position
123171
}
124172

173+
/// Returns the current `force` of the touch.
125174
#[inline]
126175
pub fn force(&self) -> Option<ForceTouch> {
127176
self.force
@@ -142,51 +191,76 @@ impl From<&TouchInput> for Touch {
142191
}
143192
}
144193

194+
/// A collection of [`Touch`]es.
195+
///
196+
/// ## Usage
197+
///
198+
/// It is used to create a `bevy` resource that stores the data of the touches on a touchscreen
199+
/// and can be accessed inside of a system.
200+
///
201+
/// ## Updating
202+
///
203+
/// The resource is updated inside of the [`touch_screen_input_system`](crate::touch::touch_screen_input_system).
145204
#[derive(Debug, Clone, Default)]
146205
pub struct Touches {
206+
/// A collection of every [`Touch`] that is currently being pressed.
147207
pressed: HashMap<u64, Touch>,
208+
/// A collection of every [`Touch`] that just got pressed.
148209
just_pressed: HashMap<u64, Touch>,
210+
/// A collection of every [`Touch`] that just got released.
149211
just_released: HashMap<u64, Touch>,
212+
/// A collection of every [`Touch`] that just got cancelled.
150213
just_cancelled: HashMap<u64, Touch>,
151214
}
152215

153216
impl Touches {
217+
/// An iterator visiting every pressed [`Touch`] input in arbitrary order.
154218
pub fn iter(&self) -> impl Iterator<Item = &Touch> + '_ {
155219
self.pressed.values()
156220
}
157221

222+
/// Returns the [`Touch`] input corresponding to the `id` if it is being pressed.
158223
pub fn get_pressed(&self, id: u64) -> Option<&Touch> {
159224
self.pressed.get(&id)
160225
}
161226

227+
/// Returns `true` if the input corresponding to the `id` has just been pressed.
162228
pub fn just_pressed(&self, id: u64) -> bool {
163229
self.just_pressed.contains_key(&id)
164230
}
165231

232+
/// An iterator visiting every just pressed [`Touch`] input in arbitrary order.
166233
pub fn iter_just_pressed(&self) -> impl Iterator<Item = &Touch> {
167234
self.just_pressed.values()
168235
}
169236

237+
/// Returns the [`Touch`] input corresponding to the `id` if it has just been released.
170238
pub fn get_released(&self, id: u64) -> Option<&Touch> {
171239
self.just_released.get(&id)
172240
}
173241

242+
/// Returns `true` if the input corresponding to the `id` has just been released.
174243
pub fn just_released(&self, id: u64) -> bool {
175244
self.just_released.contains_key(&id)
176245
}
177246

247+
/// An iterator visiting every just released [`Touch`] input in arbitrary order.
178248
pub fn iter_just_released(&self) -> impl Iterator<Item = &Touch> {
179249
self.just_released.values()
180250
}
181251

252+
/// Returns `true` if the input corresponding to the `id` has just been cancelled.
182253
pub fn just_cancelled(&self, id: u64) -> bool {
183254
self.just_cancelled.contains_key(&id)
184255
}
185256

257+
/// An iterator visiting every just cancelled [`Touch`] input in arbitrary order.
186258
pub fn iter_just_cancelled(&self) -> impl Iterator<Item = &Touch> {
187259
self.just_cancelled.values()
188260
}
189261

262+
/// Processes a [`TouchInput`] event by updating the `pressed`, `just_pressed`,
263+
/// `just_released`, and `just_cancelled` collections.
190264
fn process_touch_event(&mut self, event: &TouchInput) {
191265
match event.phase {
192266
TouchPhase::Started => {
@@ -213,14 +287,26 @@ impl Touches {
213287
};
214288
}
215289

290+
/// Clears the `just_pressed`, `just_released`, and `just_cancelled` collections.
291+
///
292+
/// This is not clearing the `pressed` collection, because it could incorrectly mark
293+
/// a touch input as not pressed eventhough it is pressed. This could happen if the
294+
/// touch input is not moving for a single frame and would therefore be marked as
295+
/// not pressed, because this function is called on every single frame no matter
296+
/// if there was an event or not.
216297
fn update(&mut self) {
217298
self.just_pressed.clear();
218299
self.just_released.clear();
219300
self.just_cancelled.clear();
220301
}
221302
}
222303

223-
/// Updates the `Touches` resource with the latest `TouchInput` events
304+
/// Updates the [`Touches`] resource with the latest [`TouchInput`] events.
305+
///
306+
/// ## Differences
307+
///
308+
/// The main difference between the [`TouchInput`] event and the [`Touches`] resource is that
309+
/// the latter has convenient functions like [`Touches::just_pressed`] and [`Touches::just_released`].
224310
pub fn touch_screen_input_system(
225311
mut touch_state: ResMut<Touches>,
226312
mut touch_input_events: EventReader<TouchInput>,

0 commit comments

Comments
 (0)