2023-11-05 22:32:50 +02:00
|
|
|
@tool
|
|
|
|
extends Function
|
|
|
|
class_name Movable
|
|
|
|
|
2024-01-24 18:43:44 +02:00
|
|
|
signal on_move(position: Vector3, rotation: Vector3)
|
2024-01-27 16:13:43 +02:00
|
|
|
signal on_moved()
|
2024-01-24 18:43:44 +02:00
|
|
|
|
2024-01-16 16:00:30 +02:00
|
|
|
@export var restricted: bool = false
|
2024-01-24 18:43:44 +02:00
|
|
|
@export var restrict_movement: Callable
|
2024-01-16 16:00:30 +02:00
|
|
|
@export var lock_rotation: bool = false
|
2024-04-26 14:58:20 +03:00
|
|
|
@export var disabled: bool = false
|
2023-11-06 16:39:53 +02:00
|
|
|
var hit_node := Node3D.new()
|
2024-04-25 20:09:52 +03:00
|
|
|
var initiator = null
|
2023-11-05 22:32:50 +02:00
|
|
|
|
2023-11-27 01:23:19 +02:00
|
|
|
func _on_grab_down(event: EventPointer):
|
2024-04-26 14:58:20 +03:00
|
|
|
if disabled:
|
|
|
|
return
|
|
|
|
|
2024-04-25 20:09:52 +03:00
|
|
|
if restricted&&event.target != get_parent():
|
2024-01-16 16:00:30 +02:00
|
|
|
return
|
|
|
|
|
2024-04-25 20:09:52 +03:00
|
|
|
initiator = event.initiator
|
|
|
|
|
|
|
|
if hit_node.get_parent() != null:
|
|
|
|
hit_node.get_parent().remove_child(hit_node)
|
|
|
|
|
|
|
|
initiator.node.add_child(hit_node)
|
2023-11-14 01:51:48 +02:00
|
|
|
hit_node.global_transform = get_parent().global_transform
|
2023-11-05 22:32:50 +02:00
|
|
|
|
2024-04-25 20:09:52 +03:00
|
|
|
func _on_grab_move(event: EventPointer):
|
2024-01-24 18:43:44 +02:00
|
|
|
if hit_node.get_parent() == null:
|
|
|
|
return
|
2024-04-25 20:09:52 +03:00
|
|
|
|
|
|
|
if event.initiator != initiator:
|
|
|
|
return
|
2024-01-24 18:43:44 +02:00
|
|
|
|
|
|
|
if restrict_movement:
|
|
|
|
get_parent().global_position = restrict_movement.call(hit_node.global_position)
|
|
|
|
else:
|
|
|
|
get_parent().global_position = hit_node.global_position
|
2024-01-16 16:00:30 +02:00
|
|
|
|
|
|
|
if !lock_rotation:
|
2024-01-25 13:23:07 +02:00
|
|
|
get_parent().global_basis = hit_node.global_basis
|
2024-01-24 18:43:44 +02:00
|
|
|
on_move.emit(get_parent().global_position, get_parent().global_rotation)
|
|
|
|
else:
|
|
|
|
on_move.emit(get_parent().global_position, Vector3(0, 0, 0))
|
|
|
|
|
2023-11-27 01:23:19 +02:00
|
|
|
func _on_grab_up(event: EventPointer):
|
2024-04-25 20:09:52 +03:00
|
|
|
if event.initiator != initiator:
|
|
|
|
return
|
|
|
|
|
|
|
|
if hit_node.get_parent() == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
initiator = null
|
|
|
|
hit_node.get_parent().remove_child(hit_node)
|
2024-01-27 16:13:43 +02:00
|
|
|
on_moved.emit()
|
2023-11-05 22:32:50 +02:00
|
|
|
|
|
|
|
func _get_configuration_warnings() -> PackedStringArray:
|
|
|
|
var warnings := PackedStringArray()
|
|
|
|
|
|
|
|
if get_parent() is StaticBody3D == false:
|
|
|
|
warnings.append("Movable requires a StaticBody3D as parent.")
|
|
|
|
|
2023-11-06 16:39:53 +02:00
|
|
|
return warnings
|