immersive-home/content/ui/components/button/button.gd

162 lines
3.5 KiB
GDScript3
Raw Normal View History

@tool
2023-11-28 00:46:05 +02:00
extends Node3D
2023-11-16 20:23:17 +02:00
class_name Button3D
2023-11-28 00:46:05 +02:00
const Proxy = preload("res://lib/utils/proxy.gd")
2023-11-22 02:44:07 +02:00
signal on_button_down()
signal on_button_up()
const IconFont = preload("res://assets/icons/icons.tres")
2023-11-28 00:46:05 +02:00
@onready var label_node: Label3D = $Body/Label
@onready var finger_area: Area3D = $FingerArea
@export var focusable: bool = true:
set(value):
focusable = value
if !is_node_ready(): await ready
if value:
$Body.add_to_group("ui_focus_skip")
else:
$Body.remove_from_group("ui_focus_skip")
2023-11-28 00:46:05 +02:00
@export var font_size: int = 10:
set(value):
font_size = value
if !is_node_ready(): await ready
label_node.font_size = value
@export var label: String = "":
set(value):
2023-11-26 04:06:31 +02:00
label = value
2023-11-26 13:58:31 +02:00
if !is_node_ready(): await ready
label_node.text = value
@export var icon: bool = false:
set(value):
icon = value
2023-11-26 13:58:31 +02:00
if !is_node_ready(): await ready
2023-11-26 04:06:31 +02:00
if value:
label_node.font = IconFont
label_node.font_size = 48
label_node.width = 1000
label_node.autowrap_mode = TextServer.AUTOWRAP_OFF
2023-11-28 00:46:05 +02:00
else:
label_node.font = null
label_node.font_size = font_size
label_node.width = 50
label_node.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
2023-11-26 04:06:31 +02:00
2023-11-16 20:23:17 +02:00
@export var toggleable: bool = false
@export var disabled: bool = false
2023-11-18 15:27:42 +02:00
@export var initial_active: bool = false
2023-11-28 00:46:05 +02:00
var external_value: Proxy = null
var active: bool = false:
get:
if external_value != null:
return external_value.value
return active
2023-11-16 20:23:17 +02:00
set(value):
2023-11-28 00:46:05 +02:00
if external_value != null:
external_value.value = value
2023-11-16 20:23:17 +02:00
else:
2023-11-28 00:46:05 +02:00
active = value
update_animation()
2023-11-16 20:23:17 +02:00
@onready var animation_player: AnimationPlayer = $AnimationPlayer
2023-11-18 15:27:42 +02:00
func _ready():
if initial_active:
active = true
2023-11-28 00:46:05 +02:00
func update_animation():
print(1)
var length = animation_player.get_animation("down").length
if active && animation_player.current_animation_position != length:
print(2)
animation_player.play("down")
elif !active && animation_player.current_animation_position != 0:
print(3)
animation_player.play_backwards("down")
2023-11-22 02:44:07 +02:00
func _on_press_down(event):
2023-11-16 20:23:17 +02:00
if disabled:
2023-11-22 02:44:07 +02:00
event.bubbling = false
2023-11-16 20:23:17 +02:00
return
AudioPlayer.play_effect("click")
2023-11-28 00:46:05 +02:00
if toggleable:
2023-11-16 20:23:17 +02:00
return
2023-11-22 02:44:07 +02:00
active = true
on_button_down.emit()
2023-11-16 20:23:17 +02:00
2023-11-22 02:44:07 +02:00
func _on_press_up(event):
2023-11-16 20:23:17 +02:00
if disabled:
2023-11-22 02:44:07 +02:00
event.bubbling = false
2023-11-16 20:23:17 +02:00
return
2023-11-22 02:44:07 +02:00
if toggleable:
active = !active
2023-11-22 02:44:07 +02:00
if active:
on_button_down.emit()
else:
on_button_up.emit()
else:
active = false
on_button_up.emit()
2023-11-28 00:46:05 +02:00
func _on_touch_enter(event: EventTouch):
animation_player.stop()
animation_player.speed_scale = 0
animation_player.current_animation = "down"
_touch_change(event)
func _on_touch_move(event: EventTouch):
_touch_change(event)
func _on_touch_leave(_event: EventTouch):
animation_player.stop()
animation_player.speed_scale = 1
if toggleable:
active = !active
if active:
on_button_up.emit()
else:
on_button_down.emit()
func _touch_change(event: EventTouch):
if disabled:
event.bubbling = false
return
var pos = Vector3(0, 1, 0)
for finger in event.fingers:
var finger_pos = to_local(finger.area.global_position)
if pos.y > finger_pos.y:
pos = finger_pos
var button_height = finger_area.get_node("CollisionShape3D").shape.size.y
var button_center = finger_area.position.y
var percent = clamp((button_center + button_height / 2 - pos.y) / (button_height / 2), 0, 1)
if !active && percent < 1:
on_button_down.emit()
elif active && percent >= 1:
on_button_up.emit()
animation_player.seek(percent * animation_player.current_animation_length, true)
if toggleable:
return
active = percent < 1