@@ -3,38 +3,45 @@ use bevy_ecs::system::ResMut;
3
3
use bevy_math:: Vec2 ;
4
4
use bevy_utils:: HashMap ;
5
5
6
- /// Represents a touch event
6
+ /// A touch input event.
7
7
///
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`]
10
12
/// event is generated with the same finger id.
11
13
///
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`]
13
15
/// events when the finger is moved or the touch pressure changes.
14
16
///
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
17
19
/// to do with the old finger and is a new finger.
18
20
///
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
20
22
/// touch, such as when the window loses focus, or on iOS if the user moves the
21
23
/// 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.
22
29
#[ derive( Debug , Clone , Copy , PartialEq ) ]
23
30
pub struct TouchInput {
31
+ /// The phase of the touch input.
24
32
pub phase : TouchPhase ,
33
+ /// The position of the finger on the touchscreen.
25
34
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.
28
36
///
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+.
32
39
pub force : Option < ForceTouch > ,
33
- /// Unique identifier of a finger.
40
+ /// The unique identifier of the finger.
34
41
pub id : u64 ,
35
42
}
36
43
37
- /// Describes the force of a touch event
44
+ /// A force description of a [`Touch`](crate:: touch::Touch) input.
38
45
#[ derive( Debug , Clone , Copy , PartialEq ) ]
39
46
pub enum ForceTouch {
40
47
/// On iOS, the force is calibrated so that the same number corresponds to
@@ -63,65 +70,107 @@ pub enum ForceTouch {
63
70
/// If the platform reports the force as normalized, we have no way of
64
71
/// knowing how much pressure 1.0 corresponds to – we know it's the maximum
65
72
/// 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.
67
74
Normalized ( f64 ) ,
68
75
}
69
76
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.
71
85
#[ derive( Debug , Hash , PartialEq , Eq , Clone , Copy ) ]
72
86
#[ cfg_attr( feature = "serialize" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
73
87
pub enum TouchPhase {
88
+ /// A finger started to touch the touchscreen.
74
89
Started ,
90
+ /// A finger moved over the touchscreen.
75
91
Moved ,
92
+ /// A finger stopped touching the touchscreen.
76
93
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.
77
98
Cancelled ,
78
99
}
79
100
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.
80
108
#[ derive( Debug , Clone , Copy ) ]
81
109
pub struct Touch {
110
+ /// The id of the touch input.
82
111
id : u64 ,
112
+ /// The starting position of the touch input.
83
113
start_position : Vec2 ,
114
+ /// The starting force of the touch input.
84
115
start_force : Option < ForceTouch > ,
116
+ /// The previous position of the touch input.
85
117
previous_position : Vec2 ,
118
+ /// The previous force of the touch input.
86
119
previous_force : Option < ForceTouch > ,
120
+ /// The current position of the touch input.
87
121
position : Vec2 ,
122
+ /// The current force of the touch input.
88
123
force : Option < ForceTouch > ,
89
124
}
90
125
91
126
impl Touch {
127
+ /// The delta of the current `position` and the `previous_position`.
92
128
pub fn delta ( & self ) -> Vec2 {
93
129
self . position - self . previous_position
94
130
}
95
131
132
+ /// The distance of the `start_position` and the current `position`.
96
133
pub fn distance ( & self ) -> Vec2 {
97
134
self . position - self . start_position
98
135
}
99
136
137
+ /// Returns the `id` of the touch.
100
138
#[ inline]
101
139
pub fn id ( & self ) -> u64 {
102
140
self . id
103
141
}
104
142
143
+ /// Returns the `start_position` of the touch.
105
144
#[ inline]
106
145
pub fn start_position ( & self ) -> Vec2 {
107
146
self . start_position
108
147
}
109
148
149
+ /// Returns the `start_force` of the touch.
110
150
#[ inline]
111
151
pub fn start_force ( & self ) -> Option < ForceTouch > {
112
152
self . start_force
113
153
}
114
154
155
+ /// Returns the `previous_position` of the touch.
115
156
#[ inline]
116
157
pub fn previous_position ( & self ) -> Vec2 {
117
158
self . previous_position
118
159
}
119
160
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.
120
168
#[ inline]
121
169
pub fn position ( & self ) -> Vec2 {
122
170
self . position
123
171
}
124
172
173
+ /// Returns the current `force` of the touch.
125
174
#[ inline]
126
175
pub fn force ( & self ) -> Option < ForceTouch > {
127
176
self . force
@@ -142,51 +191,76 @@ impl From<&TouchInput> for Touch {
142
191
}
143
192
}
144
193
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).
145
204
#[ derive( Debug , Clone , Default ) ]
146
205
pub struct Touches {
206
+ /// A collection of every [`Touch`] that is currently being pressed.
147
207
pressed : HashMap < u64 , Touch > ,
208
+ /// A collection of every [`Touch`] that just got pressed.
148
209
just_pressed : HashMap < u64 , Touch > ,
210
+ /// A collection of every [`Touch`] that just got released.
149
211
just_released : HashMap < u64 , Touch > ,
212
+ /// A collection of every [`Touch`] that just got cancelled.
150
213
just_cancelled : HashMap < u64 , Touch > ,
151
214
}
152
215
153
216
impl Touches {
217
+ /// An iterator visiting every pressed [`Touch`] input in arbitrary order.
154
218
pub fn iter ( & self ) -> impl Iterator < Item = & Touch > + ' _ {
155
219
self . pressed . values ( )
156
220
}
157
221
222
+ /// Returns the [`Touch`] input corresponding to the `id` if it is being pressed.
158
223
pub fn get_pressed ( & self , id : u64 ) -> Option < & Touch > {
159
224
self . pressed . get ( & id)
160
225
}
161
226
227
+ /// Returns `true` if the input corresponding to the `id` has just been pressed.
162
228
pub fn just_pressed ( & self , id : u64 ) -> bool {
163
229
self . just_pressed . contains_key ( & id)
164
230
}
165
231
232
+ /// An iterator visiting every just pressed [`Touch`] input in arbitrary order.
166
233
pub fn iter_just_pressed ( & self ) -> impl Iterator < Item = & Touch > {
167
234
self . just_pressed . values ( )
168
235
}
169
236
237
+ /// Returns the [`Touch`] input corresponding to the `id` if it has just been released.
170
238
pub fn get_released ( & self , id : u64 ) -> Option < & Touch > {
171
239
self . just_released . get ( & id)
172
240
}
173
241
242
+ /// Returns `true` if the input corresponding to the `id` has just been released.
174
243
pub fn just_released ( & self , id : u64 ) -> bool {
175
244
self . just_released . contains_key ( & id)
176
245
}
177
246
247
+ /// An iterator visiting every just released [`Touch`] input in arbitrary order.
178
248
pub fn iter_just_released ( & self ) -> impl Iterator < Item = & Touch > {
179
249
self . just_released . values ( )
180
250
}
181
251
252
+ /// Returns `true` if the input corresponding to the `id` has just been cancelled.
182
253
pub fn just_cancelled ( & self , id : u64 ) -> bool {
183
254
self . just_cancelled . contains_key ( & id)
184
255
}
185
256
257
+ /// An iterator visiting every just cancelled [`Touch`] input in arbitrary order.
186
258
pub fn iter_just_cancelled ( & self ) -> impl Iterator < Item = & Touch > {
187
259
self . just_cancelled . values ( )
188
260
}
189
261
262
+ /// Processes a [`TouchInput`] event by updating the `pressed`, `just_pressed`,
263
+ /// `just_released`, and `just_cancelled` collections.
190
264
fn process_touch_event ( & mut self , event : & TouchInput ) {
191
265
match event. phase {
192
266
TouchPhase :: Started => {
@@ -213,14 +287,26 @@ impl Touches {
213
287
} ;
214
288
}
215
289
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.
216
297
fn update ( & mut self ) {
217
298
self . just_pressed . clear ( ) ;
218
299
self . just_released . clear ( ) ;
219
300
self . just_cancelled . clear ( ) ;
220
301
}
221
302
}
222
303
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`].
224
310
pub fn touch_screen_input_system (
225
311
mut touch_state : ResMut < Touches > ,
226
312
mut touch_input_events : EventReader < TouchInput > ,
0 commit comments