Fix for multitouch_view

This commit is contained in:
pkruszynski
2020-08-04 00:20:11 +01:00
parent ddffbd7599
commit f9c93b0d28
5 changed files with 14 additions and 32 deletions

View File

@@ -9,14 +9,14 @@ func _draw():
# Get the touch helper singleton.
var touch_helper = get_node("/root/TouchHelper")
# Draw every pointer as a circle.
for ptr_id in touch_helper.state.keys():
var pos = touch_helper.state[ptr_id]
var color = _get_color_for_ptr_id(ptr_id)
for ptr_index in touch_helper.state.keys():
var pos = touch_helper.state[ptr_index]
var color = _get_color_for_ptr_index(ptr_index)
color.a = 0.75
draw_circle(pos, 40.0, color)
# Just a way of getting different colors.
func _get_color_for_ptr_id(id):
var x = (id % 7) + 1
func _get_color_for_ptr_index(index):
var x = (index % 7) + 1
return Color(float(bool(x & 1)), float(bool(x & 2)), float(bool(x & 4)))

View File

@@ -1,6 +1,6 @@
# Multitouch View with GDScript
Simple debugger for multitouch input. Shows red dots everywhere you press.
Simple debugger for multitouch input. Shows colored dots everywhere you press.
Language: GDScript

View File

@@ -1,29 +1,20 @@
extends Node
# 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).
# Dictionary, in which each key is a pointer index (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:
if event.pressed: # Down.
if !_os2own.has(event.index): # Defensively discard index if already known.
var ptr_id = state.size()
state[ptr_id] = event.position
_os2own[event.index] = ptr_id
state[event.index] = event.position
else: # Up.
if _os2own.has(event.index): # Defensively discard index if not known.
var ptr_id = _os2own[event.index]
state.erase(ptr_id)
_os2own.erase(event.index)
state.erase(event.index)
get_tree().set_input_as_handled()
elif event is InputEventScreenDrag: # Movement.
if _os2own.has(event.index): # Defensively discard index if not known.
var ptr_id = _os2own[event.index]
state[ptr_id] = event.position
state[event.index] = event.position
get_tree().set_input_as_handled()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -1,29 +1,20 @@
extends Node
# 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).
# Dictionary, in which each key is a pointer index (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:
if event.pressed: # Down.
if !_os2own.has(event.index): # Defensively discard index if already known.
var ptr_id = state.size()
state[ptr_id] = event.position
_os2own[event.index] = ptr_id
state[event.index] = event.position
else: # Up.
if _os2own.has(event.index): # Defensively discard index if not known.
var ptr_id = _os2own[event.index]
state.erase(ptr_id)
_os2own.erase(event.index)
state.erase(event.index)
get_tree().set_input_as_handled()
elif event is InputEventScreenDrag: # Movement.
if _os2own.has(event.index): # Defensively discard index if not known.
var ptr_id = _os2own[event.index]
state[ptr_id] = event.position
state[event.index] = event.position
get_tree().set_input_as_handled()