immersive-home/app/lib/utils/touch/touch.gd

66 lines
1.7 KiB
GDScript3
Raw Normal View History

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 = {}
2024-05-06 21:35:31 +03:00
func is_touching() -> bool:
return areas_entered.keys().size() > 0
2024-05-06 19:22:12 +03:00
func add_finger(finger_type: Finger.Type, area: Area3D):
finger_areas[finger_type] = area
area.area_entered.connect(func(entered_area):
_on_area_entered(finger_type, entered_area)
)
area.area_exited.connect(func(entered_area):
_on_area_exited(finger_type, entered_area)
)
func remove_finger(finger_type: Finger.Type):
if finger_areas.has(finger_type):
finger_areas.erase(finger_type)
2023-11-28 00:46:05 +02:00
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)