@@ -89,28 +89,34 @@ + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches eve
89
89
90
90
switch (touch.phase ) {
91
91
case UITouchPhaseBegan: {
92
+ // The activeTouches array only grows, it does not shrink (to keep indices constant since they are used as pointer ids),
93
+ // so look for an unused (null) array element and reuse it if found. Otherwise, add a new entry to the array.
92
94
NSUInteger pointerId = [activeTouches indexOfObject: [NSNull null ]];
93
- if (pointerId == NSNotFound ) {
95
+ if (pointerId != NSNotFound ) {
96
+ [activeTouches replaceObjectAtIndex: pointerId withObject: touch];
97
+ } else {
94
98
pointerId = [activeTouches count ];
95
99
[activeTouches addObject: touch];
96
- } else {
97
- [activeTouches replaceObjectAtIndex: pointerId withObject: touch];
98
100
}
99
101
Babylon::SetTouchButtonState (static_cast <uint32_t >(pointerId), true , x, y);
100
102
break ;
101
103
}
102
104
103
105
case UITouchPhaseMoved: {
104
106
NSUInteger pointerId = [activeTouches indexOfObject: touch];
105
- Babylon::SetTouchPosition (static_cast <uint32_t >(pointerId), x, y);
107
+ if (pointerId != NSNotFound ) {
108
+ Babylon::SetTouchPosition (static_cast <uint32_t >(pointerId), x, y);
109
+ }
106
110
break ;
107
111
}
108
112
109
113
case UITouchPhaseEnded:
110
114
case UITouchPhaseCancelled: {
111
115
NSUInteger pointerId = [activeTouches indexOfObject: touch];
112
- [activeTouches replaceObjectAtIndex: pointerId withObject: [NSNull null ]];
113
- Babylon::SetTouchButtonState (static_cast <uint32_t >(pointerId), false , x, y);
116
+ if (pointerId != NSNotFound ) {
117
+ [activeTouches replaceObjectAtIndex: pointerId withObject: [NSNull null ]];
118
+ Babylon::SetTouchButtonState (static_cast <uint32_t >(pointerId), false , x, y);
119
+ }
114
120
break ;
115
121
}
116
122
0 commit comments