2023-11-28 00:46:05 +02:00
|
|
|
extends Node
|
2024-03-17 01:14:31 +02:00
|
|
|
## Handles touch events and emits them to the EventSystem
|
2023-11-28 00:46:05 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
const Finger = preload ("res://lib/utils/touch/finger.gd")
|
2023-11-28 00:46:05 +02:00
|
|
|
|
|
|
|
## Record<Finger.Type, Area3D>
|
|
|
|
var finger_areas: Dictionary
|
|
|
|
|
|
|
|
var areas_entered = {}
|
|
|
|
|
|
|
|
func _init(finger_areas: Dictionary):
|
|
|
|
self.finger_areas = finger_areas
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
for finger_type in finger_areas.keys():
|
|
|
|
finger_areas[finger_type].area_entered.connect(func(area):
|
|
|
|
_on_area_entered(finger_type, area)
|
|
|
|
)
|
|
|
|
|
|
|
|
finger_areas[finger_type].area_exited.connect(func(area):
|
|
|
|
_on_area_exited(finger_type, area)
|
|
|
|
)
|
|
|
|
|
|
|
|
func _physics_process(_delta):
|
|
|
|
for area in areas_entered.keys():
|
2024-01-15 17:47:09 +02:00
|
|
|
if areas_entered.has(area) == false:
|
|
|
|
return
|
2023-11-28 00:46:05 +02:00
|
|
|
_emit_event("touch_move", area)
|
|
|
|
|
|
|
|
func _on_area_entered(finger_type, area):
|
|
|
|
if areas_entered.has(area):
|
|
|
|
if !areas_entered[area].has(finger_type):
|
|
|
|
areas_entered[area].append(finger_type)
|
|
|
|
_emit_event("touch_enter", area)
|
|
|
|
else:
|
|
|
|
areas_entered[area] = [finger_type]
|
|
|
|
_emit_event("touch_enter", area)
|
|
|
|
|
|
|
|
func _on_area_exited(finger_type, area):
|
|
|
|
if areas_entered.has(area):
|
|
|
|
if areas_entered[area].has(finger_type):
|
|
|
|
areas_entered[area].erase(finger_type)
|
|
|
|
|
|
|
|
if areas_entered[area].size() == 0:
|
|
|
|
_emit_event("touch_leave", area)
|
|
|
|
areas_entered.erase(area)
|
|
|
|
|
|
|
|
func _emit_event(type: String, target):
|
|
|
|
var event = EventTouch.new()
|
|
|
|
event.target = target
|
|
|
|
var fingers: Array[Finger] = []
|
|
|
|
for finger_type in areas_entered[target]:
|
|
|
|
var finger = Finger.new()
|
|
|
|
finger.type = finger_type
|
|
|
|
finger.area = finger_areas[finger_type]
|
|
|
|
fingers.append(finger)
|
|
|
|
|
|
|
|
event.fingers = fingers
|
|
|
|
|
|
|
|
EventSystem.emit(type, event)
|