Files
tli/scripts/ui/components/animated_texture_button.gd
2024-10-21 14:44:45 +10:00

50 lines
1.1 KiB
GDScript

extends TextureButton
class_name AnimatedTextureButton
const TWEEN_DURATION: float = 0.5
const HOVER_SCALE: Vector2 = Vector2(0.8, 1.2)
const PRESS_SCALE: Vector2 = Vector2(1.2, 0.8)
var _tween: Tween
func _ready() -> void:
button_down.connect(_press_down_animation)
button_up.connect(_press_up_animation)
mouse_entered.connect(_hover_over_animation)
mouse_exited.connect(_hover_off_animation)
func _press_down_animation() -> void:
SoundManager.audio_player.play_sound(SoundManager.press_down())
_animate(PRESS_SCALE)
func _press_up_animation() -> void:
if is_hovered():
SoundManager.audio_player.play_sound(SoundManager.press_up())
_animate(HOVER_SCALE)
else:
_animate(Vector2.ONE)
func _hover_over_animation() -> void:
SoundManager.audio_player.play_sound(SoundManager.hover())
_animate(HOVER_SCALE)
func _hover_off_animation() -> void:
_animate(Vector2.ONE)
func _animate(to_scale: Vector2) -> void:
if _tween:
_tween.kill()
_tween = create_tween()
await (
_tween
.tween_property(self, "scale", to_scale, TWEEN_DURATION)
.set_ease(Tween.EASE_OUT)
.set_trans(Tween.TRANS_ELASTIC)
.finished
)