@@ -254,7 +254,7 @@ pub struct DragEntry {
254
254
/// An entry in the cache that drives the `pointer_events` system, storing additional data
255
255
/// about pointer button presses.
256
256
#[ derive( Debug , Clone , Default ) ]
257
- pub struct PointerState {
257
+ pub struct PointerButtonState {
258
258
/// Stores the press location and start time for each button currently being pressed by the pointer.
259
259
pub pressing : HashMap < Entity , ( Location , Instant , HitData ) > ,
260
260
/// Stores the starting and current locations for each entity currently being dragged by the pointer.
@@ -263,6 +263,42 @@ pub struct PointerState {
263
263
pub dragging_over : HashMap < Entity , HitData > ,
264
264
}
265
265
266
+ /// State for all pointers.
267
+ #[ derive( Debug , Clone , Default , Resource ) ]
268
+ pub struct PointerState {
269
+ /// Pressing and dragging state, organized by pointer and button.
270
+ pub pointer_buttons : HashMap < ( PointerId , PointerButton ) , PointerButtonState > ,
271
+ }
272
+
273
+ impl PointerState {
274
+ /// Retrieves the current state for a specific pointer and button, if it has been created.
275
+ pub fn get ( & self , pointer_id : PointerId , button : PointerButton ) -> Option < & PointerButtonState > {
276
+ self . pointer_buttons . get ( & ( pointer_id, button) )
277
+ }
278
+
279
+ /// Provides write access to the state of a pointer and button, creating it if it does not yet exist.
280
+ pub fn get_mut (
281
+ & mut self ,
282
+ pointer_id : PointerId ,
283
+ button : PointerButton ,
284
+ ) -> & mut PointerButtonState {
285
+ self . pointer_buttons
286
+ . entry ( ( pointer_id, button) )
287
+ . or_default ( )
288
+ }
289
+
290
+ /// Clears all the data assoceated with all of the buttons on a pointer. Does not free the underlying memory.
291
+ pub fn clear ( & mut self , pointer_id : PointerId ) {
292
+ for button in PointerButton :: iter ( ) {
293
+ if let Some ( state) = self . pointer_buttons . get_mut ( & ( pointer_id, button) ) {
294
+ state. pressing . clear ( ) ;
295
+ state. dragging . clear ( ) ;
296
+ state. dragging_over . clear ( ) ;
297
+ }
298
+ }
299
+ }
300
+ }
301
+
266
302
/// A helper system param for accessing the picking event writers.
267
303
#[ derive( SystemParam ) ]
268
304
pub struct PickingEventWriters < ' w > {
@@ -316,8 +352,7 @@ pub fn pointer_events(
316
352
pointer_map : Res < PointerMap > ,
317
353
hover_map : Res < HoverMap > ,
318
354
previous_hover_map : Res < PreviousHoverMap > ,
319
- // Local state
320
- mut pointer_state : Local < HashMap < ( PointerId , PointerButton ) , PointerState > > ,
355
+ mut pointer_state : ResMut < PointerState > ,
321
356
// Output
322
357
mut commands : Commands ,
323
358
mut event_writers : PickingEventWriters ,
@@ -352,7 +387,7 @@ pub fn pointer_events(
352
387
353
388
// Possibly send DragEnter events
354
389
for button in PointerButton :: iter ( ) {
355
- let state = pointer_state. entry ( ( pointer_id, button) ) . or_default ( ) ;
390
+ let state = pointer_state. get_mut ( pointer_id, button) ;
356
391
357
392
for drag_target in state
358
393
. dragging
@@ -397,7 +432,7 @@ pub fn pointer_events(
397
432
match action {
398
433
// Pressed Button
399
434
PointerAction :: Pressed { direction, button } => {
400
- let state = pointer_state. entry ( ( pointer_id, button) ) . or_default ( ) ;
435
+ let state = pointer_state. get_mut ( pointer_id, button) ;
401
436
402
437
// The sequence of events emitted depends on if this is a press or a release
403
438
match direction {
@@ -519,7 +554,7 @@ pub fn pointer_events(
519
554
PointerAction :: Moved { delta } => {
520
555
// Triggers during movement even if not over an entity
521
556
for button in PointerButton :: iter ( ) {
522
- let state = pointer_state. entry ( ( pointer_id, button) ) . or_default ( ) ;
557
+ let state = pointer_state. get_mut ( pointer_id, button) ;
523
558
524
559
// Emit DragEntry and DragStart the first time we move while pressing an entity
525
560
for ( press_target, ( location, _, hit) ) in state. pressing . iter ( ) {
@@ -619,14 +654,8 @@ pub fn pointer_events(
619
654
commands. trigger_targets ( cancel_event. clone ( ) , hovered_entity) ;
620
655
event_writers. cancel_events . send ( cancel_event) ;
621
656
}
622
- // Clear the local state for the canceled pointer
623
- for button in PointerButton :: iter ( ) {
624
- if let Some ( state) = pointer_state. get_mut ( & ( pointer_id, button) ) {
625
- state. pressing . clear ( ) ;
626
- state. dragging . clear ( ) ;
627
- state. dragging_over . clear ( ) ;
628
- }
629
- }
657
+ // Clear the state for the canceled pointer
658
+ pointer_state. clear ( pointer_id) ;
630
659
}
631
660
}
632
661
}
@@ -662,7 +691,7 @@ pub fn pointer_events(
662
691
663
692
// Possibly send DragLeave events
664
693
for button in PointerButton :: iter ( ) {
665
- let state = pointer_state. entry ( ( pointer_id, button) ) . or_default ( ) ;
694
+ let state = pointer_state. get_mut ( pointer_id, button) ;
666
695
state. dragging_over . remove ( & hovered_entity) ;
667
696
for drag_target in state. dragging . keys ( ) {
668
697
let drag_leave_event = Pointer :: new (
0 commit comments