TouchHelper.gd 806 B

123456789101112131415161718192021222324
  1. extends Node
  2. # This will track the position of every pointer in its public `state` property, which is a
  3. # Dictionary, in which each key is a pointer index (integer) and each value its position (Vector2).
  4. # It works by listening to input events not handled by other means.
  5. # It also remaps the pointer indices coming from the OS to the lowest available to be friendlier.
  6. # It can be conveniently setup as a singleton.
  7. var state := {}
  8. func _unhandled_input(event: InputEvent) -> void:
  9. if event is InputEventScreenTouch:
  10. if event.pressed:
  11. # Down.
  12. state[event.index] = event.position
  13. else:
  14. # Up.
  15. state.erase(event.index)
  16. get_viewport().set_input_as_handled()
  17. elif event is InputEventScreenDrag:
  18. # Movement.
  19. state[event.index] = event.position
  20. get_viewport().set_input_as_handled()