immersive-home/app/content/system/hands/hands.gd

131 lines
4.6 KiB
GDScript3
Raw Normal View History

2023-11-27 01:23:19 +02:00
extends Node3D
2024-02-27 17:39:47 +02:00
const Pointer = preload ("res://lib/utils/pointer/pointer.gd")
const Initiator = preload ("res://lib/utils/pointer/initiator.gd")
const Finger = preload ("res://lib/utils/touch/finger.gd")
const Touch = preload ("res://lib/utils/touch/touch.gd")
const Collide = preload ("res://lib/utils/touch/collide.gd")
2024-04-17 21:45:18 +03:00
const Miniature = preload ("res://content/system/house/mini/miniature.gd")
2023-11-27 01:23:19 +02:00
2024-04-17 21:45:18 +03:00
@onready var main = $"/root/Main"
2023-11-27 01:23:19 +02:00
@onready var hand_right: OpenXRHand = $XRHandRight
@onready var hand_left: OpenXRHand = $XRHandLeft
2024-04-17 21:45:18 +03:00
@onready var palm = $XRHandLeft/Palm
@onready var quick_actions = $XRHandLeft/Palm/QuickActions
@onready var mini_view_button = $XRHandLeft/Palm/QuickActions/MiniView
@onready var temperature_button = $XRHandLeft/Palm/QuickActions/Temperature
@onready var humidity_button = $XRHandLeft/Palm/QuickActions/Humidity
2023-11-27 01:23:19 +02:00
@export var ray_left: RayCast3D
@export var ray_right: RayCast3D
var left_initiator: Initiator = Initiator.new()
var right_initiator: Initiator = Initiator.new()
2023-11-28 00:46:05 +02:00
var touch: Touch
2024-02-27 17:39:47 +02:00
var collide: Collide
var left_pointer: Pointer
var right_pointer: Pointer
2023-11-27 01:23:19 +02:00
var press_distance = 0.03
2023-11-28 00:46:05 +02:00
var grip_distance = 0.03
2023-11-27 01:23:19 +02:00
var pressed_left = false
var pressed_right = false
var grabbed_left = false
var grabbed_right = false
func _ready():
2024-02-27 17:39:47 +02:00
var fingers = {
2023-11-28 15:20:38 +02:00
Finger.Type.INDEX_RIGHT: $XRHandRight/IndexTip/TouchArea,
Finger.Type.INDEX_LEFT: $XRHandLeft/IndexTip/TouchArea,
Finger.Type.MIDDLE_RIGHT: $XRHandRight/MiddleTip/TouchArea,
Finger.Type.MIDDLE_LEFT: $XRHandLeft/MiddleTip/TouchArea
2024-02-27 17:39:47 +02:00
}
touch = Touch.new(fingers)
2024-03-21 17:19:09 +02:00
collide = Collide.new(hand_left, hand_right, $XRHandLeft/IndexTip/TouchArea, $XRHandRight/IndexTip/TouchArea)
2023-11-28 00:46:05 +02:00
add_child(touch)
2024-02-27 17:39:47 +02:00
add_child(collide)
2023-11-28 00:46:05 +02:00
_ready_hand()
2023-11-27 01:23:19 +02:00
2024-04-17 21:45:18 +03:00
mini_view_button.on_button_up.connect(func():
House.body.mini_view.small.value=!House.body.mini_view.small.value
)
temperature_button.on_button_up.connect(func():
if House.body.mini_view.heatmap_type.value == Miniature.HeatmapType.TEMPERATURE:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.NONE
else:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.TEMPERATURE
)
humidity_button.on_button_up.connect(func():
if House.body.mini_view.heatmap_type.value == Miniature.HeatmapType.HUMIDITY:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.NONE
else:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.HUMIDITY
)
func _ready_hand():
left_initiator.type = Initiator.Type.HAND_LEFT
left_initiator.node = ray_left.get_parent()
2023-11-27 01:23:19 +02:00
left_pointer = Pointer.new(left_initiator, ray_left)
add_child(left_pointer)
right_initiator.type = Initiator.Type.HAND_RIGHT
right_initiator.node = ray_right.get_parent()
right_pointer = Pointer.new(right_initiator, ray_right)
add_child(right_pointer)
2023-11-27 01:23:19 +02:00
2024-04-17 21:45:18 +03:00
func _process(_delta):
if main.camera.global_transform.basis.z.dot(palm.global_transform.basis.y) > 0.85:
if quick_actions.is_inside_tree() == false: palm.add_child(quick_actions)
else:
if quick_actions.is_inside_tree(): palm.remove_child(quick_actions)
2024-02-27 17:39:47 +02:00
func _physics_process(_delta):
2024-04-17 21:45:18 +03:00
_process_hand(hand_left)
2023-11-27 01:23:19 +02:00
_process_hand(hand_right)
func _process_hand(hand: OpenXRHand):
var index_tip = hand.get_node("IndexTip/Marker3D")
var thumb_tip = hand.get_node("ThumbTip/Marker3D")
var middle_tip = hand.get_node("MiddleTip/Marker3D")
var _ray = ray_left if hand == hand_left else ray_right
var initiator = left_initiator if hand == hand_left else right_initiator
2023-11-27 01:23:19 +02:00
var distance_trigger = index_tip.global_position.distance_to(thumb_tip.global_position)
var distance_grab = middle_tip.global_position.distance_to(thumb_tip.global_position)
2023-11-28 00:46:05 +02:00
var trigger_close = distance_trigger <= press_distance
var grab_close = distance_grab <= grip_distance
2023-11-27 01:23:19 +02:00
if hand == hand_left:
2024-04-30 10:01:10 +03:00
if trigger_close&&!pressed_left:
initiator.on_press.emit(Initiator.EventType.TRIGGER)
pressed_left = true
elif !trigger_close&&pressed_left:
initiator.on_release.emit(Initiator.EventType.TRIGGER)
pressed_left = false
if grab_close&&!grabbed_left:
initiator.on_press.emit(Initiator.EventType.GRIP)
grabbed_left = true
elif !grab_close&&grabbed_left:
initiator.on_release.emit(Initiator.EventType.GRIP)
grabbed_left = false
2023-11-27 01:23:19 +02:00
else:
2024-04-30 10:01:10 +03:00
if trigger_close&&!pressed_right:
initiator.on_press.emit(Initiator.EventType.TRIGGER)
pressed_right = true
elif !trigger_close&&pressed_right:
initiator.on_release.emit(Initiator.EventType.TRIGGER)
pressed_right = false
if grab_close&&!grabbed_right:
initiator.on_press.emit(Initiator.EventType.GRIP)
grabbed_right = true
elif !grab_close&&grabbed_right:
initiator.on_release.emit(Initiator.EventType.GRIP)
grabbed_right = false