test-client-godot/gui_scene/GUI/MusicControls/Music.gd

84 lines
No EOL
2.3 KiB
GDScript

extends VBoxContainer
var songs_list = []
var current_song = ""
var current_position = 0.0
var popup
var musics_path = 'res://assets/musics/'
func list_music_files_in_directory(path):
var files = []
var dir = Directory.new()
dir.open(path)
dir.list_dir_begin()
while true:
var file = dir.get_next()
if file == "":
break
elif not file.begins_with(".") and file.ends_with( '.ogg' ):
files.append(file.left(file.length()-4))
dir.list_dir_end()
return files
func _ready():
var music_files = list_music_files_in_directory( musics_path )
popup = $SongsSelector.get_popup()
for song in music_files:
var stream = AudioStreamPlayer.new()
stream.name = song
stream.stream = load( musics_path+song+".ogg" )
stream.connect( "finished", self, "_on_song_finished" )
$Songs.add_child( stream )
popup.add_item( song )
$SongsSelector.text = current_song
func set_play():
if not current_song == "" and get_node( "Songs" ).get_node( current_song ):
get_node( "Songs" ).get_node( current_song ).play( current_position )
$Buttons/Pause.text = "Pause"
$Title.text = "Musiques (Played):"
func set_pause():
if not current_song == "" and get_node( "Songs" ).get_node( current_song ):
current_position = get_node( "Songs" ).get_node( current_song ).get_playback_position()
get_node( "Songs" ).get_node( current_song ).stop()
else:
current_position = 0.0
$Buttons/Pause.text = "Play"
$Title.text = "Musiques (Paused):"
func set_stop():
if not current_song == "" and get_node( "Songs" ).get_node( current_song ):
get_node( "Songs" ).get_node( current_song ).stop()
current_position = 0.0
$Buttons/Pause.pressed = false
$Buttons/Pause.text = "Play"
$Title.text = "Musiques (Stopped):"
func _on_Pause_toggled(button_pressed):
if button_pressed:
set_play()
else:
set_pause()
func _on_Stop_pressed():
set_stop()
func _on_SongsSelector_item_selected(ID):
var new_song = popup.get_item_text(ID)
if not current_song == new_song:
set_stop()
current_song = popup.get_item_text(ID)
func _on_song_finished():
print("test")
set_stop()