2020-02-13 00:46:27 +01:00
|
|
|
extends Node
|
2017-12-07 03:34:10 +01:00
|
|
|
# This will track the position of every pointer in its public `state` property, which is a
|
|
|
|
# Dictionary, in which each key is a pointer id (integer) and each value its position (Vector2).
|
|
|
|
# It works by listening to input events not handled by other means.
|
|
|
|
# It also remaps the pointer indices coming from the OS to the lowest available to be friendlier.
|
|
|
|
# It can be conveniently setup as a singleton.
|
|
|
|
|
|
|
|
var state = {}
|
|
|
|
var _os2own = {}
|
|
|
|
|
|
|
|
func _unhandled_input(event):
|
|
|
|
if event is InputEventScreenTouch:
|
2020-02-13 00:46:27 +01:00
|
|
|
if event.pressed: # Down.
|
|
|
|
if !_os2own.has(event.index): # Defensively discard index if already known.
|
2020-07-02 10:08:24 +02:00
|
|
|
var ptr_id = state.size()
|
2017-12-07 03:34:10 +01:00
|
|
|
state[ptr_id] = event.position
|
|
|
|
_os2own[event.index] = ptr_id
|
2020-02-13 00:46:27 +01:00
|
|
|
else: # Up.
|
|
|
|
if _os2own.has(event.index): # Defensively discard index if not known.
|
2017-12-07 03:34:10 +01:00
|
|
|
var ptr_id = _os2own[event.index]
|
|
|
|
state.erase(ptr_id)
|
|
|
|
_os2own.erase(event.index)
|
2020-03-14 08:07:16 +01:00
|
|
|
get_tree().set_input_as_handled()
|
2020-02-13 00:46:27 +01:00
|
|
|
|
|
|
|
elif event is InputEventScreenDrag: # Movement.
|
|
|
|
if _os2own.has(event.index): # Defensively discard index if not known.
|
2017-12-07 03:34:10 +01:00
|
|
|
var ptr_id = _os2own[event.index]
|
|
|
|
state[ptr_id] = event.position
|
2020-03-14 08:07:16 +01:00
|
|
|
get_tree().set_input_as_handled()
|