godot_view_gesture_recognizer.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/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. @implementation GodotViewGestureRecognizer
  41. - (instancetype)init {
  42. self = [super init];
  43. self.cancelsTouchesInView = YES;
  44. self.delaysTouchesBegan = YES;
  45. self.delaysTouchesEnded = YES;
  46. self.delayTimeInterval = GLOBAL_GET("input_devices/pointing/ios/touch_delay");
  47. return self;
  48. }
  49. - (void)delayTouches:(NSSet *)touches andEvent:(UIEvent *)event {
  50. [delayTimer fire];
  51. delayedTouches = touches;
  52. delayedEvent = event;
  53. delayTimer = [NSTimer scheduledTimerWithTimeInterval:self.delayTimeInterval target:self selector:@selector(fireDelayedTouches:) userInfo:nil repeats:NO];
  54. }
  55. - (void)fireDelayedTouches:(id)timer {
  56. [delayTimer invalidate];
  57. delayTimer = nil;
  58. if (delayedTouches) {
  59. [self.view touchesBegan:delayedTouches withEvent:delayedEvent];
  60. }
  61. delayedTouches = nil;
  62. delayedEvent = nil;
  63. }
  64. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  65. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseBegan];
  66. [self delayTouches:cleared andEvent:event];
  67. }
  68. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  69. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseMoved];
  70. if (delayTimer) {
  71. // We should check if movement was significant enough to fire an event
  72. // for dragging to work correctly.
  73. for (UITouch *touch in cleared) {
  74. CGPoint from = [touch locationInView:self.view];
  75. CGPoint to = [touch previousLocationInView:self.view];
  76. CGFloat xDistance = from.x - to.x;
  77. CGFloat yDistance = from.y - to.y;
  78. CGFloat distance = sqrt(xDistance * xDistance + yDistance * yDistance);
  79. // Early exit, since one of touches has moved enough to fire a drag event.
  80. if (distance > kGLGestureMovementDistance) {
  81. [delayTimer fire];
  82. [self.view touchesMoved:cleared withEvent:event];
  83. return;
  84. }
  85. }
  86. return;
  87. }
  88. [self.view touchesMoved:cleared withEvent:event];
  89. }
  90. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  91. [delayTimer fire];
  92. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseEnded];
  93. [self.view touchesEnded:cleared withEvent:event];
  94. }
  95. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  96. [delayTimer fire];
  97. [self.view touchesCancelled:touches withEvent:event];
  98. };
  99. - (NSSet *)copyClearedTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave {
  100. NSMutableSet *cleared = [touches mutableCopy];
  101. for (UITouch *touch in touches) {
  102. if (touch.phase != phaseToSave) {
  103. [cleared removeObject:touch];
  104. }
  105. }
  106. return cleared;
  107. }
  108. @end