GestureArea.gd 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. extends Control
  2. @export var target: NodePath
  3. @export var min_scale = 0.1
  4. @export var max_scale = 3.0
  5. @export var one_finger_rot_x = true
  6. @export var one_finger_rot_y = true
  7. @export var two_fingers_rot_z = true
  8. @export var two_fingers_zoom = true
  9. var base_state
  10. var curr_state
  11. var target_node
  12. # We keep here a copy of the state before the number of fingers changed to avoid accumulation errors.
  13. var base_xform
  14. func _ready():
  15. base_state = {}
  16. curr_state = {}
  17. target_node = get_node(target)
  18. func _gui_input(event):
  19. # We must start touching inside, but we can drag or unpress outside.
  20. # if not (event is InputEventScreenDrag or
  21. # (event is InputEventScreenTouch and (not event.pressed or get_global_rect().has_point(event.position)))):
  22. # return
  23. var finger_count = base_state.size()
  24. if finger_count == 0:
  25. # No fingers => Accept press.
  26. if event is InputEventScreenTouch:
  27. if event.pressed:
  28. # A finger started touching.
  29. base_state = {
  30. event.index: event.position,
  31. }
  32. elif finger_count == 1:
  33. # One finger => For rotating around X and Y.
  34. # Accept one more press, unpress or drag.
  35. if event is InputEventScreenTouch:
  36. if event.pressed:
  37. # One more finger started touching.
  38. # Reset the base state to the only current and the new fingers.
  39. base_state = {
  40. curr_state.keys()[0]: curr_state.values()[0],
  41. event.index: event.position,
  42. }
  43. else:
  44. if base_state.has(event.index):
  45. # Only touching finger released.
  46. base_state.clear()
  47. elif event is InputEventScreenDrag:
  48. if curr_state.has(event.index):
  49. # Touching finger dragged.
  50. var unit_drag = _px2unit(base_state[base_state.keys()[0]] - event.position)
  51. if one_finger_rot_x:
  52. target_node.global_rotate(Vector3.UP, deg_to_rad(180.0 * unit_drag.x))
  53. if one_finger_rot_y:
  54. target_node.global_rotate(Vector3.RIGHT, deg_to_rad(180.0 * unit_drag.y))
  55. # Since rotating around two axes, we have to reset the base constantly.
  56. curr_state[event.index] = event.position
  57. base_state[event.index] = event.position
  58. base_xform = target_node.get_transform()
  59. elif finger_count == 2:
  60. # Two fingers => To pinch-zoom and rotate around Z.
  61. # Accept unpress or drag.
  62. if event is InputEventScreenTouch:
  63. if not event.pressed and base_state.has(event.index):
  64. # Some known touching finger released.
  65. # Clear the base state
  66. base_state.clear()
  67. elif event is InputEventScreenDrag:
  68. if curr_state.has(event.index):
  69. # Some known touching finger dragged.
  70. curr_state[event.index] = event.position
  71. # Compute base and current inter-finger vectors.
  72. var base_segment = base_state[base_state.keys()[0]] - base_state[base_state.keys()[1]]
  73. var new_segment = curr_state[curr_state.keys()[0]] - curr_state[curr_state.keys()[1]]
  74. # Get the base scale from the base matrix.
  75. var base_scale = Vector3(base_xform.basis.x.x, base_xform.basis.y.y, base_xform.basis.z.z).length()
  76. if two_fingers_zoom:
  77. # Compute the new scale limiting it and taking into account the base scale.
  78. var new_scale = clamp(base_scale * (new_segment.length() / base_segment.length()), min_scale, max_scale) / base_scale
  79. target_node.set_transform(base_xform.scaled(new_scale * Vector3.ONE))
  80. else:
  81. target_node.set_transform(base_xform)
  82. if two_fingers_rot_z:
  83. # Apply rotation between base inter-finger vector and the current one.
  84. var rot = new_segment.angle_to(base_segment)
  85. target_node.global_rotate(Vector3.BACK, rot)
  86. # Finger count changed?
  87. if base_state.size() != finger_count:
  88. # Copy new base state to the current state.
  89. curr_state = {}
  90. for idx in base_state.keys():
  91. curr_state[idx] = base_state[idx]
  92. # Remember the base transform.
  93. base_xform = target_node.get_transform()
  94. # Converts a vector in pixels to a unitary magnitude,
  95. # considering the number of pixels of the shorter axis is the unit.
  96. func _px2unit(v):
  97. var shortest = min(get_size().x, get_size().y)
  98. return v * (1.0 / shortest)