43 lines
891 B
GDScript3
43 lines
891 B
GDScript3
|
extends Node
|
||
|
|
||
|
|
||
|
signal swipe_left
|
||
|
signal swipe_right
|
||
|
signal swipe_up
|
||
|
signal swipe_down
|
||
|
|
||
|
var swipe_start: Vector2
|
||
|
var swipe_done = false
|
||
|
|
||
|
const MAX_SWIPE_DISTANCE = 80
|
||
|
|
||
|
|
||
|
func _unhandled_input(event):
|
||
|
if event is InputEventScreenTouch:
|
||
|
if event.pressed:
|
||
|
swipe_done = false
|
||
|
swipe_start = event.get_position()
|
||
|
elif not swipe_done:
|
||
|
_interpret_swipe(event.get_position())
|
||
|
elif event is InputEventScreenDrag:
|
||
|
if not swipe_done:
|
||
|
_interpret_swipe(event.position)
|
||
|
|
||
|
|
||
|
func _interpret_swipe(swipe_end: Vector2):
|
||
|
var diff = swipe_end - swipe_start
|
||
|
|
||
|
if diff.length_squared() > MAX_SWIPE_DISTANCE * MAX_SWIPE_DISTANCE:
|
||
|
var normalized = diff.normalized()
|
||
|
|
||
|
if normalized.x > 0.5:
|
||
|
emit_signal("swipe_right")
|
||
|
elif normalized.x < -0.5:
|
||
|
emit_signal("swipe_left")
|
||
|
elif normalized.y > 0.5:
|
||
|
emit_signal("swipe_down")
|
||
|
else:
|
||
|
emit_signal("swipe_up")
|
||
|
|
||
|
swipe_done = true
|