2019-11-10 15:09:14 +10:30
|
|
|
extends Label
|
|
|
|
|
2019-11-13 00:48:06 +10:30
|
|
|
var touch_points = {} # dict containing all points touched on the screen
|
|
|
|
var touch_positions = [] # array of above
|
2019-11-10 15:09:14 +10:30
|
|
|
var fingers = 0 setget set_fingers # setter for show fingers number on screen
|
|
|
|
var txt_ball = preload("res://assets/ball.png") # preload our ball texture
|
|
|
|
var default_font = preload("res://assets/NotoSans.tres") # point to godot standard font
|
|
|
|
|
2019-11-13 00:48:06 +10:30
|
|
|
|
|
|
|
|
|
|
|
func _init():
|
|
|
|
pass
|
|
|
|
|
2019-11-10 15:09:14 +10:30
|
|
|
func _ready():
|
|
|
|
set_process_unhandled_input(true) # process user input
|
2019-11-13 00:48:06 +10:30
|
|
|
set_fingers(0)
|
|
|
|
|
2019-11-10 15:09:14 +10:30
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# draw fingers points on screen
|
|
|
|
func _draw():
|
|
|
|
# draw points
|
|
|
|
for i in touch_points:
|
|
|
|
var point = touch_points[i]
|
2019-11-13 00:48:06 +10:30
|
|
|
# if point.pressed:
|
|
|
|
# DRAW POINTS ################################################
|
|
|
|
draw_texture(txt_ball, point.position - Vector2(24, 24))
|
|
|
|
draw_string(default_font, point.position - Vector2(24, 24), str(i))
|
|
|
|
# if len(touch_positions) > 1:
|
|
|
|
# for i in range(len(touch_positions)-1):
|
|
|
|
# # Draw line
|
|
|
|
# draw_line(touch_positions[i], touch_positions[i+1], Color(1,1,1,1))
|
|
|
|
|
|
|
|
func update_data():
|
|
|
|
touch_positions.clear()
|
|
|
|
for i in touch_points:
|
|
|
|
touch_positions.push_back(touch_points[i].position)
|
2019-11-10 15:09:14 +10:30
|
|
|
set_fingers(len(touch_positions))
|
2019-11-13 00:48:06 +10:30
|
|
|
update()
|
|
|
|
|
2019-11-10 15:09:14 +10:30
|
|
|
##########################################################################
|
|
|
|
func _input(event):
|
2019-11-13 00:48:06 +10:30
|
|
|
# Unfortunately event.device does NOT differentiate touchscreen inputs on X11, Godot v3.1.1
|
|
|
|
# As such, we'll need to do some fancy mapping for multiple inputs
|
2019-11-10 15:09:14 +10:30
|
|
|
if (event is InputEventScreenDrag):
|
|
|
|
touch_points[event.index] = {pressed = true, position = event.position}
|
2019-11-13 00:48:06 +10:30
|
|
|
elif (event is InputEventScreenTouch):
|
2019-11-10 15:09:14 +10:30
|
|
|
if event.pressed:
|
|
|
|
if not touch_points.has(event.index):
|
|
|
|
touch_points[event.index] = {}
|
|
|
|
touch_points[event.index].position = event.position # update position
|
2019-11-13 00:48:06 +10:30
|
|
|
# touch_points[event.index].pressed = event.pressed # update "pressed" flag
|
2019-11-10 15:09:14 +10:30
|
|
|
else:
|
|
|
|
if touch_points.has(event.index):
|
|
|
|
touch_points.erase(event.index)
|
2019-11-13 00:48:06 +10:30
|
|
|
update_data()
|
2019-11-10 15:09:14 +10:30
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# write how many fingers are tapping the screen
|
|
|
|
func set_fingers(value):
|
|
|
|
fingers = value
|
|
|
|
if fingers > 0:
|
2019-11-13 00:48:06 +10:30
|
|
|
set_text("Fingers: " + str(fingers))
|
2019-11-10 15:09:14 +10:30
|
|
|
else:
|
2019-11-13 00:48:06 +10:30
|
|
|
set_text("Fingers: 0")
|