2023-11-22 02:44:07 +02:00
|
|
|
extends RayCast3D
|
|
|
|
|
2024-03-17 19:04:20 +02:00
|
|
|
const Pointer = preload ("res://lib/utils/pointer/pointer.gd")
|
|
|
|
const Initiator = preload ("res://lib/utils/pointer/initiator.gd")
|
2023-11-27 01:23:19 +02:00
|
|
|
|
2023-11-26 14:48:27 +02:00
|
|
|
@onready var cursor: Node3D = $Cursor
|
2024-05-10 14:16:17 +03:00
|
|
|
@onready var default_cursor: Sprite3D = $Cursor/DefaultCursor
|
|
|
|
@onready var retro_cursor: Sprite3D = $Cursor/RetroCursor
|
2023-11-26 14:48:27 +02:00
|
|
|
@onready var decal: Decal = $Decal
|
2024-05-23 01:36:49 +03:00
|
|
|
@onready var grid: MeshInstance3D = $Grid
|
|
|
|
@onready var wall_collider: RayCast3D = $WallCollider
|
2023-11-26 14:48:27 +02:00
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
@export var is_right: bool = true
|
2023-11-26 14:48:27 +02:00
|
|
|
@export var with_decal: bool = false
|
2024-05-23 01:36:49 +03:00
|
|
|
@export var with_grid: bool = false
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2023-11-27 01:23:19 +02:00
|
|
|
@onready var pointer: Pointer
|
2024-03-17 19:04:20 +02:00
|
|
|
@onready var visual_ray: MeshInstance3D = $Ray
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2023-11-27 01:23:19 +02:00
|
|
|
var _event_type_map = {
|
|
|
|
"trigger_click": Initiator.EventType.TRIGGER,
|
|
|
|
"grip_click": Initiator.EventType.GRIP,
|
|
|
|
}
|
2023-11-22 02:44:07 +02:00
|
|
|
|
|
|
|
func _ready():
|
2023-11-27 01:23:19 +02:00
|
|
|
var initiator = Initiator.new()
|
|
|
|
initiator.type = Initiator.Type.CONTROLLER_RIGHT if is_right else Initiator.Type.CONTROLLER_LEFT
|
|
|
|
initiator.node = get_parent()
|
|
|
|
assert(get_parent() is XRController3D, "Parent must be XRController3D")
|
|
|
|
|
|
|
|
pointer = Pointer.new(initiator, self)
|
|
|
|
add_child(pointer)
|
|
|
|
|
|
|
|
get_parent().button_pressed.connect(func(button: String):
|
2024-05-28 19:28:18 +03:00
|
|
|
EventSystem.emit_action(button, true, initiator)
|
|
|
|
|
2023-11-27 01:23:19 +02:00
|
|
|
if _event_type_map.has(button):
|
2024-05-09 13:40:41 +03:00
|
|
|
pointer.pressed(_event_type_map[button])
|
2023-11-27 01:23:19 +02:00
|
|
|
)
|
|
|
|
get_parent().button_released.connect(func(button: String):
|
2024-05-28 19:28:18 +03:00
|
|
|
EventSystem.emit_action(button, false, initiator)
|
|
|
|
|
2023-11-27 01:23:19 +02:00
|
|
|
if _event_type_map.has(button):
|
2024-05-09 13:40:41 +03:00
|
|
|
pointer.released(_event_type_map[button])
|
2023-11-27 01:23:19 +02:00
|
|
|
)
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2024-05-28 19:28:18 +03:00
|
|
|
get_parent().input_float_changed.connect(func(action_name, value):
|
|
|
|
EventSystem.emit_action(action_name, value, initiator)
|
|
|
|
)
|
|
|
|
|
|
|
|
get_parent().input_vector2_changed.connect(func(action_name, value):
|
|
|
|
EventSystem.emit_action(action_name, value, initiator)
|
|
|
|
)
|
|
|
|
|
2024-05-10 14:16:17 +03:00
|
|
|
R.effect(func(_arg):
|
2024-05-23 01:36:49 +03:00
|
|
|
var style=Store.settings.state.cursor_style
|
|
|
|
|
|
|
|
default_cursor.visible=style == 0
|
|
|
|
retro_cursor.visible=style == 1
|
2024-05-10 14:16:17 +03:00
|
|
|
)
|
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
func _physics_process(_delta):
|
2023-11-26 14:48:27 +02:00
|
|
|
_handle_cursor()
|
2024-05-23 01:36:49 +03:00
|
|
|
_handle_grid()
|
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
if grid.visible:
|
|
|
|
grid.material_override.set_shader_parameter("dot_offset", TransformTools.plane_2d_coords(grid.global_transform) / grid.mesh.size.x)
|
|
|
|
|
|
|
|
func _handle_grid():
|
|
|
|
var collider = wall_collider.get_collider()
|
|
|
|
|
|
|
|
if collider == null:
|
|
|
|
grid.visible = false
|
|
|
|
|
|
|
|
grid.visible = with_grid
|
|
|
|
|
|
|
|
grid.global_position = wall_collider.get_collision_point() + wall_collider.get_collision_normal() * 0.01
|
|
|
|
|
|
|
|
if abs(wall_collider.get_collision_normal().dot(Vector3.UP)) > 0.9:
|
|
|
|
grid.global_transform.basis = Basis.looking_at(wall_collider.get_collision_normal(), Vector3.FORWARD, true)
|
|
|
|
else:
|
|
|
|
grid.global_transform.basis = Basis.looking_at(wall_collider.get_collision_normal(), Vector3.UP, true)
|
2023-11-26 14:48:27 +02:00
|
|
|
|
|
|
|
func _handle_cursor():
|
|
|
|
var collider = get_collider()
|
2024-03-17 19:04:20 +02:00
|
|
|
var distance = get_collision_point().distance_to(global_position)
|
2023-11-26 14:48:27 +02:00
|
|
|
|
|
|
|
if collider == null:
|
|
|
|
cursor.visible = false
|
2024-03-17 19:04:20 +02:00
|
|
|
visual_ray.visible = true
|
|
|
|
visual_ray.scale.y = 1
|
2023-11-26 14:48:27 +02:00
|
|
|
if with_decal: decal.visible = true
|
|
|
|
return
|
|
|
|
|
2024-03-17 19:04:20 +02:00
|
|
|
if distance < 0.15:
|
|
|
|
visual_ray.visible = false
|
|
|
|
else:
|
|
|
|
visual_ray.visible = true
|
|
|
|
visual_ray.scale.y = clamp(distance * 2 - 0.1, 0.15, 1)
|
|
|
|
|
2023-11-26 14:48:27 +02:00
|
|
|
cursor.visible = true
|
|
|
|
decal.visible = false
|
2024-05-23 01:36:49 +03:00
|
|
|
|
2023-11-26 14:48:27 +02:00
|
|
|
cursor.global_transform.origin = get_collision_point() + get_collision_normal() * 0.001 # offset to avoid z-fighting
|
2024-05-10 14:16:17 +03:00
|
|
|
|
|
|
|
if abs(get_collision_normal().dot(Vector3.UP)) > 0.9:
|
|
|
|
var ray_dir_inv = global_transform.basis.z
|
|
|
|
cursor.global_transform.basis = Basis.looking_at(get_collision_normal().lerp(ray_dir_inv, 0.01), Vector3.UP, true)
|
|
|
|
else:
|
|
|
|
cursor.global_transform.basis = Basis.looking_at(get_collision_normal(), Vector3.UP, true)
|
|
|
|
|
|
|
|
var cursor_scale = clamp(distance * 1.5 - 0.75, 1.0, 3.0)
|
|
|
|
|
|
|
|
cursor.scale = Vector3(cursor_scale, cursor_scale, cursor_scale)
|