WIP music control

This commit is contained in:
JKuijperM 2024-03-26 12:33:16 +01:00
parent f59bec5bbe
commit 48b253b93a
2 changed files with 39 additions and 9 deletions

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://bsafhva6xh2nf"] [gd_scene load_steps=5 format=3 uid="uid://bsafhva6xh2nf"]
[ext_resource type="Script" path="res://Scripts/TimerHUD.gd" id="1_k2tpr"] [ext_resource type="Script" path="res://Scripts/TimerHUD.gd" id="1_k2tpr"]
[ext_resource type="PackedScene" uid="uid://cxho0ur4g4r2c" path="res://Scenes/circular_progress_bar.tscn" id="2_ptdoy"] [ext_resource type="PackedScene" uid="uid://cxho0ur4g4r2c" path="res://Scenes/circular_progress_bar.tscn" id="2_ptdoy"]
[ext_resource type="AudioStream" uid="uid://cfn6m777tj8sy" path="res://Assets/Bob_lennon_-_Gutalala_sudalala.mp3" id="3_nfvhu"]
[sub_resource type="LabelSettings" id="LabelSettings_gikwe"] [sub_resource type="LabelSettings" id="LabelSettings_gikwe"]
font_size = 100 font_size = 100
@ -173,7 +174,17 @@ offset_right = 152.0
offset_bottom = 110.0 offset_bottom = 110.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
value = 50.0 min_value = -24.0
max_value = 0.0
value = -12.0
[node name="BackgroundMusic" type="AudioStreamPlayer" parent="."]
stream = ExtResource("3_nfvhu")
volume_db = -28.0
[node name="HBoxContainer" type="HBoxContainer" parent="."]
offset_right = 40.0
offset_bottom = 40.0
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"] [connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]
[connection signal="pressed" from="PlayButton" to="." method="_on_play_button_pressed"] [connection signal="pressed" from="PlayButton" to="." method="_on_play_button_pressed"]
@ -182,3 +193,4 @@ value = 50.0
[connection signal="pressed" from="EditButton" to="." method="_on_edit_button_pressed"] [connection signal="pressed" from="EditButton" to="." method="_on_edit_button_pressed"]
[connection signal="value_changed" from="SpinBoxMinutes" to="." method="_on_spin_box_minutes_value_changed"] [connection signal="value_changed" from="SpinBoxMinutes" to="." method="_on_spin_box_minutes_value_changed"]
[connection signal="value_changed" from="SpinBoxSeconds" to="." method="_on_spin_box_seconds_value_changed"] [connection signal="value_changed" from="SpinBoxSeconds" to="." method="_on_spin_box_seconds_value_changed"]
[connection signal="drag_ended" from="VolumeSlider" to="." method="_on_volume_slider_drag_ended"]

View File

@ -1,17 +1,27 @@
extends CanvasLayer extends CanvasLayer
@export var default_seconds = 30 @export var default_seconds = 30
@export var default_minutes = 1 @export var default_minutes = 1
@onready var background_music = $BackgroundMusic
var seconds var seconds
var minutes var minutes
var max_seconds var max_seconds
var current_seconds var current_seconds
var timer_is_playing = false var timer_is_playing = false
signal play_timer
var edit_menu_visible = false var edit_menu_visible = false
signal play_timer
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
# Get the length of the music
if background_music.stream:
var stream_length = background_music.stream.get_length()
default_minutes = int(stream_length / 60)
default_seconds = int(fmod(stream_length, 60))
reset_remaining_time_label() reset_remaining_time_label()
$SpinBoxMinutes.get_line_edit().context_menu_enabled = false $SpinBoxMinutes.get_line_edit().context_menu_enabled = false
@ -30,8 +40,6 @@ func _ready():
$CircularProgressBar.set_value(calculate_equivalent_progress(current_seconds)) $CircularProgressBar.set_value(calculate_equivalent_progress(current_seconds))
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
pass pass
@ -57,19 +65,22 @@ func stop_timer():
$Timer.paused = false $Timer.paused = false
$Timer.stop() $Timer.stop()
$CircularProgressBar.set_value(calculate_equivalent_progress(max_seconds)) $CircularProgressBar.set_value(calculate_equivalent_progress(max_seconds))
background_music.stop()
func calculate_equivalent_progress(value): func calculate_equivalent_progress(value):
return (value * 100) / max_seconds return (value * 100) / max_seconds
func _on_play_button_pressed(): func _on_play_button_pressed():
if $Timer.is_paused(): if $Timer.is_paused():
$Timer.paused = false $Timer.paused = false
background_music.stream_paused = false
timer_is_playing = true
else: else:
if !timer_is_playing: if !timer_is_playing:
timer_is_playing = true timer_is_playing = true
reset_timer() reset_timer()
$Timer.start() $Timer.start()
background_music.play()
func _on_timer_timeout(): func _on_timer_timeout():
if seconds == 0: if seconds == 0:
@ -78,6 +89,7 @@ func _on_timer_timeout():
seconds == 60 seconds == 60
else: else:
$Timer.stop() $Timer.stop()
background_music.stop()
elif seconds > 0: elif seconds > 0:
seconds -= 1 seconds -= 1
current_seconds -= 1 current_seconds -= 1
@ -91,6 +103,7 @@ func _on_timer_timeout():
func _on_pause_button_pressed(): func _on_pause_button_pressed():
timer_is_playing = false timer_is_playing = false
$Timer.paused = true $Timer.paused = true
background_music.stream_paused = true
func _on_stop_button_pressed(): func _on_stop_button_pressed():
@ -124,3 +137,8 @@ func _on_spin_box_minutes_value_changed(value):
func _on_spin_box_seconds_value_changed(value): func _on_spin_box_seconds_value_changed(value):
default_seconds = $SpinBoxSeconds.value default_seconds = $SpinBoxSeconds.value
reset_remaining_time_label() reset_remaining_time_label()
func _on_volume_slider_drag_ended(value_changed):
background_music.volume_db = $VolumeSlider.value
print($VolumeSlider.value)