60 lines
1.5 KiB
GDScript
60 lines
1.5 KiB
GDScript
extends Node
|
|
|
|
signal buttonPressed
|
|
signal buttonReleased
|
|
signal buttonUpdated
|
|
|
|
@export var isBeingPressed = false
|
|
@export var text : String
|
|
@export var isLoopingUpdate : bool = false
|
|
@export var color : Color
|
|
|
|
# FIX: Do not instantly assign an empty .new() if it might have been loaded
|
|
var material : StandardMaterial3D
|
|
|
|
func _ready() -> void:
|
|
# Reconstruct or override the material instance cleanly on load
|
|
material = StandardMaterial3D.new()
|
|
material.albedo_color = color
|
|
|
|
ButtonAnimation.loop_mode = Animation.LOOP_NONE
|
|
$StaticBody3D/Label3D.text = text
|
|
$StaticBody3D/MeshInstance3D.material_override = material
|
|
|
|
# Keep animation visually synced with the saved press state
|
|
if isBeingPressed:
|
|
$AnimationPlayer.play("interact")
|
|
$AnimationPlayer.advance(ButtonAnimation.length)
|
|
else:
|
|
$AnimationPlayer.play_backwards("interact")
|
|
|
|
@onready var ButtonAnimation = $AnimationPlayer.get_animation("interact")
|
|
|
|
func _press():
|
|
if not isBeingPressed:
|
|
emit_signal("buttonPressed")
|
|
isBeingPressed = true
|
|
$AnimationPlayer.play("interact")
|
|
$Sound.pitch_scale = 1.5
|
|
$Sound.play()
|
|
emit_signal("buttonUpdated")
|
|
|
|
func _release():
|
|
if isBeingPressed:
|
|
emit_signal("buttonReleased")
|
|
isBeingPressed = false
|
|
$AnimationPlayer.play_backwards("interact")
|
|
$Sound.pitch_scale = 1.3
|
|
$Sound.play()
|
|
emit_signal("buttonUpdated")
|
|
|
|
func _interact_press(is_alt_click: bool = false) -> void:
|
|
_press()
|
|
|
|
func _interact_release() -> void:
|
|
_release()
|
|
|
|
func _process(delta: float) -> void:
|
|
if isLoopingUpdate:
|
|
emit_signal("buttonUpdated")
|