godot_view_gesture_recognizer.mm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #import "godot_view.h"
  32. #include "core/project_settings.h"
  33. // Minimum distance for touches to move to fire
  34. // a delay timer before scheduled time.
  35. // Should be the low enough to not cause issues with dragging
  36. // but big enough to allow click to work.
  37. const CGFloat kGLGestureMovementDistance = 0.5;
  38. @interface GodotViewGestureRecognizer ()
  39. @property(nonatomic, readwrite, assign) NSTimeInterval delayTimeInterval;
  40. @end
  41. @implementation GodotViewGestureRecognizer
  42. - (GodotView *)godotView {
  43. return (GodotView *)self.view;
  44. }
  45. - (instancetype)init {
  46. self = [super init];
  47. self.cancelsTouchesInView = YES;
  48. self.delaysTouchesBegan = YES;
  49. self.delaysTouchesEnded = YES;
  50. self.requiresExclusiveTouchType = NO;
  51. self.delayTimeInterval = GLOBAL_GET("input_devices/pointing/ios/touch_delay");
  52. return self;
  53. }
  54. - (void)delayTouches:(NSSet *)touches andEvent:(UIEvent *)event {
  55. [delayTimer fire];
  56. delayedTouches = touches;
  57. delayedEvent = event;
  58. delayTimer = [NSTimer scheduledTimerWithTimeInterval:self.delayTimeInterval target:self selector:@selector(fireDelayedTouches:) userInfo:nil repeats:NO];
  59. }
  60. - (void)fireDelayedTouches:(id)timer {
  61. [delayTimer invalidate];
  62. delayTimer = nil;
  63. if (delayedTouches) {
  64. [self.godotView godotTouchesBegan:delayedTouches withEvent:delayedEvent];
  65. }
  66. delayedTouches = nil;
  67. delayedEvent = nil;
  68. }
  69. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  70. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseBegan];
  71. [self delayTouches:cleared andEvent:event];
  72. [super touchesBegan:touches withEvent:event];
  73. }
  74. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  75. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseMoved];
  76. if (delayTimer) {
  77. // We should check if movement was significant enough to fire an event
  78. // for dragging to work correctly.
  79. for (UITouch *touch in cleared) {
  80. CGPoint from = [touch locationInView:self.godotView];
  81. CGPoint to = [touch previousLocationInView:self.godotView];
  82. CGFloat xDistance = from.x - to.x;
  83. CGFloat yDistance = from.y - to.y;
  84. CGFloat distance = sqrt(xDistance * xDistance + yDistance * yDistance);
  85. // Early exit, since one of touches has moved enough to fire a drag event.
  86. if (distance > kGLGestureMovementDistance) {
  87. [delayTimer fire];
  88. [self.godotView godotTouchesMoved:cleared withEvent:event];
  89. return;
  90. }
  91. }
  92. return;
  93. }
  94. [self.godotView godotTouchesMoved:cleared withEvent:event];
  95. [super touchesMoved:touches withEvent:event];
  96. }
  97. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  98. [delayTimer fire];
  99. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseEnded];
  100. [self.godotView godotTouchesEnded:cleared withEvent:event];
  101. [super touchesEnded:touches withEvent:event];
  102. }
  103. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  104. [delayTimer fire];
  105. [self.godotView godotTouchesCancelled:touches withEvent:event];
  106. [super touchesCancelled:touches withEvent:event];
  107. }
  108. - (NSSet *)copyClearedTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave {
  109. NSMutableSet *cleared = [touches mutableCopy];
  110. for (UITouch *touch in touches) {
  111. if (touch.view != self.view || touch.phase != phaseToSave) {
  112. [cleared removeObject:touch];
  113. }
  114. }
  115. return cleared;
  116. }
  117. @end