Skip to content

Commit 5463b3a

Browse files
authored
Don't crash if we get a touch event for a touch point not being tracked (#239)
1 parent 5c1d326 commit 5463b3a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Modules/@babylonjs/react-native/ios/BabylonNativeInterop.mm

+12-6
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,34 @@ + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches eve
8989

9090
switch (touch.phase) {
9191
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.
9294
NSUInteger pointerId = [activeTouches indexOfObject:[NSNull null]];
93-
if (pointerId == NSNotFound) {
95+
if (pointerId != NSNotFound) {
96+
[activeTouches replaceObjectAtIndex:pointerId withObject:touch];
97+
} else {
9498
pointerId = [activeTouches count];
9599
[activeTouches addObject:touch];
96-
} else {
97-
[activeTouches replaceObjectAtIndex:pointerId withObject:touch];
98100
}
99101
Babylon::SetTouchButtonState(static_cast<uint32_t>(pointerId), true, x, y);
100102
break;
101103
}
102104

103105
case UITouchPhaseMoved: {
104106
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+
}
106110
break;
107111
}
108112

109113
case UITouchPhaseEnded:
110114
case UITouchPhaseCancelled: {
111115
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+
}
114120
break;
115121
}
116122

0 commit comments

Comments
 (0)