gl_view_gesture_recognizer.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************/
  2. /* gl_view_gesture_recognizer.m */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "gl_view_gesture_recognizer.h"
  31. // Using same delay interval that is used for `UIScrollView`
  32. const NSTimeInterval kGLGestureDelayInterval = 0.150;
  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. @implementation GLViewGestureRecognizer
  39. - (instancetype)init {
  40. self = [super init];
  41. self.cancelsTouchesInView = YES;
  42. self.delaysTouchesBegan = YES;
  43. self.delaysTouchesEnded = YES;
  44. return self;
  45. }
  46. - (void)delayTouches:(NSSet *)touches andEvent:(UIEvent *)event {
  47. [delayTimer fire];
  48. delayedTouches = touches;
  49. delayedEvent = event;
  50. delayTimer = [NSTimer scheduledTimerWithTimeInterval:kGLGestureDelayInterval target:self selector:@selector(fireDelayedTouches:) userInfo:nil repeats:NO];
  51. }
  52. - (void)fireDelayedTouches:(id)timer {
  53. [delayTimer invalidate];
  54. delayTimer = nil;
  55. if (delayedTouches) {
  56. [self.view touchesBegan:delayedTouches withEvent:delayedEvent];
  57. }
  58. [delayedTouches release];
  59. delayedTouches = nil;
  60. delayedEvent = nil;
  61. }
  62. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  63. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseBegan];
  64. [self delayTouches:cleared andEvent:event];
  65. }
  66. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  67. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseMoved];
  68. if (delayTimer) {
  69. // We should check if movement was significant enough to fire an event
  70. // for dragging to work correctly.
  71. for (UITouch *touch in cleared) {
  72. CGPoint from = [touch locationInView:self.view];
  73. CGPoint to = [touch previousLocationInView:self.view];
  74. CGFloat xDistance = from.x - to.x;
  75. CGFloat yDistance = from.y - to.y;
  76. CGFloat distance = sqrt(xDistance * xDistance + yDistance * yDistance);
  77. // Early exit, since one of touches has moved enough to fire a drag event.
  78. if (distance > kGLGestureMovementDistance) {
  79. [delayTimer fire];
  80. [self.view touchesMoved:cleared withEvent:event];
  81. [cleared release];
  82. return;
  83. }
  84. }
  85. [cleared release];
  86. return;
  87. }
  88. [self.view touchesMoved:cleared withEvent:event];
  89. [cleared release];
  90. }
  91. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  92. [delayTimer fire];
  93. NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseEnded];
  94. [self.view touchesEnded:cleared withEvent:event];
  95. [cleared release];
  96. }
  97. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  98. [delayTimer fire];
  99. [self.view touchesCancelled:touches withEvent:event];
  100. };
  101. - (NSSet *)copyClearedTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave {
  102. NSMutableSet *cleared = [touches mutableCopy];
  103. for (UITouch *touch in touches) {
  104. if (touch.phase != phaseToSave) {
  105. [cleared removeObject:touch];
  106. }
  107. }
  108. return cleared;
  109. }
  110. @end