From f398a6159c4d17bdd44dc54818f2867f3e87afc5 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 8 Dec 2024 03:20:31 +0200 Subject: [PATCH] Play audio at the appropriate position when the animation runs, and stop when the pause button is pressed --- src/UI/Timeline/LayerButton.gd | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/UI/Timeline/LayerButton.gd b/src/UI/Timeline/LayerButton.gd index 0962fe354..4163eb82b 100644 --- a/src/UI/Timeline/LayerButton.gd +++ b/src/UI/Timeline/LayerButton.gd @@ -14,6 +14,7 @@ var button_pressed := false: main_button.button_pressed = value get: return main_button.button_pressed +var animation_running := false var audio_player: AudioStreamPlayer @onready var properties: AcceptDialog = Global.control.find_child("LayerProperties") @@ -45,6 +46,8 @@ func _ready() -> void: audio_player.stream = layer.audio layer.audio_changed.connect(func(): audio_player.stream = layer.audio) add_child(audio_player) + Global.animation_timeline.animation_started.connect(_on_animation_started) + Global.animation_timeline.animation_finished.connect(_on_animation_finished) custom_minimum_size.y = Global.animation_timeline.cel_size label.text = layer.name line_edit.text = layer.name @@ -64,13 +67,30 @@ func _ready() -> void: func _on_cel_switched() -> void: z_index = 1 if button_pressed else 0 + if not animation_running or Global.current_project.current_frame == 0: + _play_audio() + + +func _on_animation_started(_dir: bool) -> void: + animation_running = true + _play_audio() + + +func _on_animation_finished() -> void: + animation_running = false if is_instance_valid(audio_player): - var audio_length := audio_player.stream.get_length() - var normalized_pos := Global.current_project.current_frame / Global.current_project.fps - if normalized_pos < 1: - audio_player.play(normalized_pos * audio_length) - else: - audio_player.stop() + audio_player.stop() + + +func _play_audio() -> void: + if not is_instance_valid(audio_player): + return + var audio_length := audio_player.stream.get_length() + var normalized_pos := Global.current_project.current_frame / Global.current_project.fps + if normalized_pos < 1: + audio_player.play(normalized_pos * audio_length) + else: + audio_player.stop() func update_buttons() -> void: