258 lines
8.1 KiB
GDScript
258 lines
8.1 KiB
GDScript
extends Control
|
|
|
|
|
|
@onready var music_player:AudioStreamPlayer = $Music
|
|
var official_music:Dictionary = {}
|
|
var default_domain:String = "Les Chemins du Khanat"
|
|
var current_domain:String = ""
|
|
var current_music:int = 0
|
|
var music_play:bool = false
|
|
var select_rand:bool = true
|
|
var list_music:Array = []
|
|
|
|
|
|
func _ready() -> void:
|
|
var directory = DirAccess.open("res://music/")
|
|
var files = []
|
|
get_tree().paused = false
|
|
Common.msg_info("Load MusicManager")
|
|
if not directory.dir_exists( "res://music/" ):
|
|
Common.msg_info("No Music")
|
|
return
|
|
#directory.open( "res://music/" )
|
|
directory = null
|
|
directory = DirAccess.open( "res://music/" )
|
|
for dir in directory.get_directories():
|
|
var fullpath: String = "res://music/" + dir
|
|
var dir2:DirAccess = DirAccess.open(fullpath)
|
|
Common.msg_debug("Dir: " + str(dir))
|
|
for file in dir2.get_files():
|
|
if file == "":
|
|
break
|
|
elif not file.begins_with(".") and not dir2.current_is_dir() and (file.ends_with( ".ogg" ) or file.ends_with( ".mp3" )):
|
|
if ! official_music.has(dir):
|
|
official_music[dir] = []
|
|
official_music[dir].append(fullpath + "/" + file)
|
|
files.append(file)
|
|
#music_player.finished.connect(_on_stream_finished.bind())
|
|
_on_random_pressed()
|
|
select_domain( default_domain )
|
|
current_music = official_music[current_domain].size()
|
|
|
|
# Get Mute state
|
|
Common.msg_debug("mute:" + str(user_settings.get_sound_mute()))
|
|
AudioServer.set_bus_mute(AudioServer.get_bus_index($Music.get_bus()), user_settings.get_sound_mute())
|
|
$Window/VBox/HBoxContainer/Mute.set_pressed(user_settings.get_sound_mute())
|
|
|
|
$Window/VBox/Tab/Music/HBox/Play.set_pressed(user_settings.get_sound_music_play())
|
|
$Window/VBox/Tab/Music/HBox/Random.set_pressed(user_settings.get_sound_music_random())
|
|
# Get Volume
|
|
# Common.msg_debug("volume :" + str($Music.get_volume_db()))
|
|
# var t = linear2db(0.0)
|
|
# var u = db2linear($Music.get_volume_db())
|
|
# Common.msg_debug("volume :" + str(u))
|
|
var music_volume = user_settings.get_sound_music_volume()
|
|
$Music.set_volume_db(linear_to_db(music_volume/100.0))
|
|
$Window/VBox/Tab/Mixer/MusicLevel/music.set_value( int(db_to_linear($Music.get_volume_db()) * 100))
|
|
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db_to_linear($Music.get_volume_db()) * 100)))
|
|
var bus_name = $Music.get_bus()
|
|
Common.msg_debug("bus_name: " + str(bus_name))
|
|
var bus_id = AudioServer.get_bus_index(bus_name)
|
|
Common.msg_debug("bus_id: " + str(bus_id))
|
|
var glb_volume = AudioServer.get_bus_volume_db(bus_id)
|
|
Common.msg_debug("GLB Volume: " + str(glb_volume))
|
|
|
|
var global_volume = user_settings.get_sound_global_volume()
|
|
AudioServer.set_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()), linear_to_db(global_volume/100.0))
|
|
# AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))
|
|
$Window/VBox/Tab/Mixer/GlobalLevel/global.set_value( int(db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))) * 100.0))
|
|
$Window/VBox/Tab/Mixer/GlobalLevel/Value.set_text(str(int(db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))) * 100.0)))
|
|
|
|
|
|
func select_domain(new_domain:String) -> void:
|
|
var found:bool = false
|
|
for key in official_music.keys():
|
|
if new_domain == key:
|
|
found = true
|
|
if found == false:
|
|
return
|
|
current_domain = new_domain
|
|
reinitialize_jukebox()
|
|
|
|
|
|
func reinitialize_jukebox() -> void:
|
|
list_music.clear()
|
|
if select_rand:
|
|
var temp:Array = []
|
|
for i in range(0, official_music[current_domain].size()):
|
|
temp.append(i)
|
|
for i in range(0, official_music[current_domain].size()):
|
|
var pos = randi() % temp.size()
|
|
list_music.append(temp[pos])
|
|
temp.remove_at(pos)
|
|
else:
|
|
for i in range(0, official_music[current_domain].size()):
|
|
list_music.append(i)
|
|
|
|
|
|
func next_music() -> void:
|
|
#var pos:float
|
|
current_music += 1
|
|
if current_music >= list_music.size():
|
|
reinitialize_jukebox()
|
|
play_music(0)
|
|
return
|
|
play_music(current_music)
|
|
|
|
|
|
func load_external_music(filepath):
|
|
var stream = null
|
|
Common.msg_debug("load_external_music")
|
|
#var file = FileAccess.new()
|
|
#file.open(filepath, file.READ)
|
|
var file = FileAccess.open(filepath, FileAccess.READ)
|
|
var ext = filepath.split(".")[-1].to_lower()
|
|
var buffer = file.get_buffer(file.get_len())
|
|
Common.msg_debug(ext)
|
|
match ext:
|
|
"ogg":
|
|
stream = AudioStreamOggVorbis.new()
|
|
"mp3":
|
|
stream = AudioStreamMP3.new()
|
|
#"wav":
|
|
# # We need decode message to get format (Stero/size bit/ ...)
|
|
# stream = AudioStreamSample.new()
|
|
# stream.format = AudioStreamSample.FORMAT_16_BITS
|
|
# #stream.stereo = true
|
|
_:
|
|
Common.msg_error("Impossible to identify type of file (file:" + filepath + ", ext:" + ext + ")")
|
|
return
|
|
stream.data = buffer
|
|
Common.msg_debug("load_external_music : End")
|
|
return stream
|
|
|
|
|
|
func load_music(filepath: String):
|
|
if filepath.left(5) == "res:/":
|
|
return load( filepath )
|
|
return load_external_music(filepath)
|
|
|
|
|
|
func _on_stream_finished() -> void:
|
|
Common.msg_debug("_on_stream_finished")
|
|
music_player.stop()
|
|
music_play = false
|
|
|
|
|
|
func show_list_music() -> void:
|
|
for child in $Window/VBox/Tab/Music/ScrollContainer/ListMusic.get_children():
|
|
child.queue_free()
|
|
for pos in range(0, list_music.size()):
|
|
var filename:String = official_music[current_domain][list_music[pos]]
|
|
var music_box = preload( "res://scenes/music/music_field.tscn" ).instantiate()
|
|
music_box.set_music( filename, filename.get_file().get_basename() , pos, pos == current_music)
|
|
music_box.connect("selected_music", force_music.bind())
|
|
$Window/VBox/Tab/Music/ScrollContainer/ListMusic.add_child( music_box )
|
|
|
|
|
|
func force_music(pos:int) -> void:
|
|
Common.msg_debug("Force: " + str(pos))
|
|
play_music(pos)
|
|
|
|
|
|
func play_music(pos: int) -> void:
|
|
if pos >= list_music.size():
|
|
return
|
|
var filename:String = official_music[current_domain][list_music[pos]]
|
|
music_play = true
|
|
music_player.set_stream(load_music( filename ))
|
|
music_player.play()
|
|
music_player.get_stream().set_loop(false)
|
|
var timer = music_player.get_stream().get_length()
|
|
$EndMusic.start(timer)
|
|
music_player.connect("finished", _on_stream_finished.bind())
|
|
current_music = pos
|
|
show_list_music()
|
|
|
|
|
|
func _process(_delta) -> void:
|
|
if ! music_play and $Window/VBox/Tab/Music/HBox/Play.is_pressed():
|
|
next_music()
|
|
|
|
|
|
func _on_music_finished() -> void:
|
|
Common.msg_debug("_on_music_finished")
|
|
music_player.stop()
|
|
music_play = false
|
|
|
|
|
|
func _on_end_music_timeout() -> void:
|
|
# Timer end - sometimes, signal finished is not sent
|
|
Common.msg_debug("_on_end_music_timeout")
|
|
music_play = false
|
|
|
|
|
|
func show_config() -> void:
|
|
#$Window.popup_centered()
|
|
$Window.visible = true
|
|
|
|
|
|
func _on_button_pressed() -> void:
|
|
$Window.hide()
|
|
|
|
|
|
func _on_play_pressed() -> void:
|
|
var current_time = $EndMusic.get_time_left()
|
|
Common.msg_debug("_on_play_pressed : " + str(current_time))
|
|
if $Window/VBox/Tab/Music/HBox/Play.is_pressed():
|
|
$Music.set_stream_paused(false)
|
|
$EndMusic.set_paused(false)
|
|
user_settings.set_sound_music_play(true)
|
|
else:
|
|
$Music.set_stream_paused(true)
|
|
$EndMusic.set_paused(true)
|
|
user_settings.set_sound_music_play(false)
|
|
|
|
|
|
func _on_random_pressed() -> void:
|
|
if $Window/VBox/Tab/Music/HBox/Random.is_pressed():
|
|
select_rand = true
|
|
user_settings.set_sound_music_random(true)
|
|
else:
|
|
select_rand = false
|
|
user_settings.set_sound_music_random(false)
|
|
|
|
|
|
func _on_reinit_pressed() -> void:
|
|
reinitialize_jukebox()
|
|
play_music(0)
|
|
|
|
|
|
func _on_mute_toggled(button_pressed) -> void:
|
|
AudioServer.set_bus_mute(AudioServer.get_bus_index($Music.get_bus()), button_pressed)
|
|
user_settings.set_sound_mute(button_pressed)
|
|
|
|
|
|
func _on_effect_value_changed(value) -> void:
|
|
$Window/VBox/Tab/Mixer/EffectLevel/Value.set_text(str(value))
|
|
|
|
|
|
func _on_music_value_changed(value) -> void:
|
|
$Music.set_volume_db(linear_to_db(value/100.0))
|
|
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(value))
|
|
user_settings.set_sound_music_volume(value)
|
|
|
|
func _on_global_value_changed(value) -> void:
|
|
AudioServer.set_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()), linear_to_db(value/100.0))
|
|
$Window/VBox/Tab/Mixer/GlobalLevel/Value.set_text(str(value))
|
|
user_settings.set_sound_global_volume(value)
|
|
|
|
|
|
func _on_window_close_requested() -> void:
|
|
$Window.hide()
|
|
|
|
|
|
func _on_window_visibility_changed():
|
|
if $Window.visible:
|
|
$Window/VBox/HBoxContainer/Quit.grab_focus()
|
|
|