Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0][iOS] Fix touch handling for overlay views #57605

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions platform/iphone/godot_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ class String;
- (void)stopRendering;
- (void)startRendering;

- (void)godotTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)godotTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)godotTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)godotTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

@end
8 changes: 4 additions & 4 deletions platform/iphone/godot_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ - (void)clearTouches {
}
}

- (void)touchesBegan:(NSSet *)touchesSet withEvent:(UIEvent *)event {
- (void)godotTouchesBegan:(NSSet *)touchesSet withEvent:(UIEvent *)event {
NSArray *tlist = [event.allTouches allObjects];
for (unsigned int i = 0; i < [tlist count]; i++) {
if ([touchesSet containsObject:[tlist objectAtIndex:i]]) {
Expand All @@ -349,7 +349,7 @@ - (void)touchesBegan:(NSSet *)touchesSet withEvent:(UIEvent *)event {
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)godotTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *tlist = [event.allTouches allObjects];
for (unsigned int i = 0; i < [tlist count]; i++) {
if ([touches containsObject:[tlist objectAtIndex:i]]) {
Expand All @@ -363,7 +363,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)godotTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *tlist = [event.allTouches allObjects];
for (unsigned int i = 0; i < [tlist count]; i++) {
if ([touches containsObject:[tlist objectAtIndex:i]]) {
Expand All @@ -377,7 +377,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)godotTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *tlist = [event.allTouches allObjects];
for (unsigned int i = 0; i < [tlist count]; i++) {
if ([touches containsObject:[tlist objectAtIndex:i]]) {
Expand Down
31 changes: 22 additions & 9 deletions platform/iphone/godot_view_gesture_recognizer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/*************************************************************************/

#import "godot_view_gesture_recognizer.h"
#import "godot_view.h"

#include "core/config/project_settings.h"

Expand Down Expand Up @@ -58,6 +59,10 @@ @interface GodotViewGestureRecognizer ()

@implementation GodotViewGestureRecognizer

- (GodotView *)godotView {
return (GodotView *)self.view;
}

- (instancetype)init {
self = [super init];

Expand Down Expand Up @@ -104,7 +109,7 @@ - (void)fireDelayedTouches:(id)timer {
self.delayTimer = nil;

if (self.delayedTouches) {
[self.view touchesBegan:self.delayedTouches withEvent:self.delayedEvent];
[self.godotView godotTouchesBegan:self.delayedTouches withEvent:self.delayedEvent];
}

self.delayedTouches = nil;
Expand All @@ -114,6 +119,8 @@ - (void)fireDelayedTouches:(id)timer {
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseBegan];
[self delayTouches:cleared andEvent:event];

[super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
Expand All @@ -123,8 +130,8 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// We should check if movement was significant enough to fire an event
// for dragging to work correctly.
for (UITouch *touch in cleared) {
CGPoint from = [touch locationInView:self.view];
CGPoint to = [touch previousLocationInView:self.view];
CGPoint from = [touch locationInView:self.godotView];
CGPoint to = [touch previousLocationInView:self.godotView];
CGFloat xDistance = from.x - to.x;
CGFloat yDistance = from.y - to.y;

Expand All @@ -133,34 +140,40 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// Early exit, since one of touches has moved enough to fire a drag event.
if (distance > kGLGestureMovementDistance) {
[self.delayTimer fire];
[self.view touchesMoved:cleared withEvent:event];
[self.godotView godotTouchesMoved:cleared withEvent:event];
return;
}
}

return;
}

[self.view touchesMoved:cleared withEvent:event];
[self.godotView touchesMoved:cleared withEvent:event];

[super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delayTimer fire];

NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseEnded];
[self.view touchesEnded:cleared withEvent:event];
[self.godotView godotTouchesEnded:cleared withEvent:event];

[super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delayTimer fire];
[self.view touchesCancelled:touches withEvent:event];
};
[self.godotView godotTouchesCancelled:touches withEvent:event];

[super touchesCancelled:touches withEvent:event];
}

- (NSSet *)copyClearedTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave {
NSMutableSet *cleared = [touches mutableCopy];

for (UITouch *touch in touches) {
if (touch.phase != phaseToSave) {
if (touch.view != self.view || touch.phase != phaseToSave) {
[cleared removeObject:touch];
}
}
Expand Down