godot_view_gesture_recognizer.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*************************************************************************/
  2. /* godot_view_gesture_recognizer.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #import "godot_view_gesture_recognizer.h"
  31. #include "core/config/project_settings.h"
  32. // Minimum distance for touches to move to fire
  33. // a delay timer before scheduled time.
  34. // Should be the low enough to not cause issues with dragging
  35. // but big enough to allow click to work.
  36. const CGFloat kGLGestureMovementDistance = 0.5;
  37. @interface GodotViewGestureRecognizer ()
  38. @property(nonatomic, readwrite, assign) NSTimeInterval delayTimeInterval;
  39. @end
  40. @interface GodotViewGestureRecognizer ()
  41. // Timer used to delay begin touch message.
  42. // Should work as simple emulation of UIDelayedAction
  43. @property(strong, nonatomic) NSTimer *delayTimer;
  44. // Delayed touch parameters
  45. @property(strong, nonatomic) NSSet *delayedTouches;
  46. @property(strong, nonatomic) UIEvent *delayedEvent;
  47. @end
  48. @implementation GodotViewGestureRecognizer
  49. - (instancetype)init {
  50. self = [super init];
  51. self.cancelsTouchesInView = YES;
  52. self.delaysTouchesBegan = YES;
  53. self.delaysTouchesEnded = YES;
  54. self.delayTimeInterval = GLOBAL_GET("input_devices/pointing/ios/touch_delay");
  55. return self;
  56. }
  57. - (void)dealloc {
  58. if (self.delayTimer) {
  59. [self.delayTimer invalidate];
  60. self.delayTimer = nil;
  61. }
  62. if (self.delayedTouches) {
  63. self.delayedTouches = nil;
  64. }
  65. if (self.delayedEvent) {
  66. self.delayedEvent = nil;
  67. }
  68. }
  69. - (void)delayTouches:(NSSet *)touches andEvent:(UIEvent *)event {
  70. [self.delayTimer fire];
  71. self.delayedTouches = touches;
  72. self.delayedEvent = event;
  73. self.delayTimer = [NSTimer
  74. scheduledTimerWithTimeInterval:self.delayTimeInterval
  75. target:self
  76. selector:@selector(fireDelayedTouches:)
  77. userInfo:nil
  78. repeats:NO];
  79. }
  80. - (void)fireDelayedTouches:(id)timer {
  81. [self.delayTimer invalidate];
  82. self.delayTimer = nil;
  83. if (self.delayedTouches) {
  84. [self.view touchesBegan:self.delayedTouches withEvent:self.delayedEvent];
  85. }
  86. self.delayedTouches = nil;
  87. self.delayedEvent = nil;
  88. }
  89. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  90. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseBegan];
  91. [self delayTouches:cleared andEvent:event];
  92. }
  93. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  94. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseMoved];
  95. if (self.delayTimer) {
  96. // We should check if movement was significant enough to fire an event
  97. // for dragging to work correctly.
  98. for (UITouch *touch in cleared) {
  99. CGPoint from = [touch locationInView:self.view];
  100. CGPoint to = [touch previousLocationInView:self.view];
  101. CGFloat xDistance = from.x - to.x;
  102. CGFloat yDistance = from.y - to.y;
  103. CGFloat distance = sqrt(xDistance * xDistance + yDistance * yDistance);
  104. // Early exit, since one of touches has moved enough to fire a drag event.
  105. if (distance > kGLGestureMovementDistance) {
  106. [self.delayTimer fire];
  107. [self.view touchesMoved:cleared withEvent:event];
  108. return;
  109. }
  110. }
  111. return;
  112. }
  113. [self.view touchesMoved:cleared withEvent:event];
  114. }
  115. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  116. [self.delayTimer fire];
  117. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseEnded];
  118. [self.view touchesEnded:cleared withEvent:event];
  119. }
  120. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  121. [self.delayTimer fire];
  122. [self.view touchesCancelled:touches withEvent:event];
  123. };
  124. - (NSSet *)copyClearedTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave {
  125. NSMutableSet *cleared = [touches mutableCopy];
  126. for (UITouch *touch in touches) {
  127. if (touch.phase != phaseToSave) {
  128. [cleared removeObject:touch];
  129. }
  130. }
  131. return cleared;
  132. }
  133. @end