khanat-client/scenes/interfaces/music_manager/music_manager.gd

513 lines
13 KiB
GDScript3
Raw Normal View History

2020-03-21 09:10:26 +00:00
extends WindowDialog
signal mute_pressed
signal musicplayer_pressed
onready var music_player = $music
var playlist_music = { 'player': [], 'global': ["res://assets/musics/sangakanat_ames_voyageuses.ogg"] }
var lastdomain = "global"
var currentmusic = ""
var numberchannelplayeraudio = 1
var cacheplayeraudio = []
var music_play = false
var bus = "master"
var next_id = 0
var pause = false
var disable_execution_mute:bool = false
var playback_position_before_pause = 0
var last_update_playermusic = false
func _ready():
var directory = Directory.new()
get_tree().paused = false
Config.msg_debug("Load MusicManager")
self.last_update_playermusic = Config.get_playermusic()
if not directory.dir_exists( "user://musics/" ):
directory.make_dir_recursive( "user://musics/" )
directory.open( "user://musics/" )
directory.list_dir_begin()
var files = []
while true:
var file = directory.get_next()
if file == "":
break
elif not file.begins_with(".") and not directory.current_is_dir() and not file.ends_with( ".import" ):
files.append(file)
directory.list_dir_end()
# for file in files:
# var button = preload( "res://scenes/interfaces/music_manager/music_button.tscn" ).instance()
# button.music_filename = "res://assets/musics/" + file
# button.text = file
# button.connect( "music_selected", self, "_on_music_pressed" )
# $window_box/scroll_box/musics_box.add_child( button )
for file in Config.queuemusic:
# # Config.msg_debug(str(file))
add_music( file )
update_volume_db()
#musicplayer = AudioStreamPlayer.new()
#self.add_child(music_player)
# update_playermusic()
if Config.mute:
set_mute_on()
else:
set_mute_off()
music_player.connect("finished", self, "_on_stream_finished", [])
func connect_ext( signal_name, target ):
Config.msg_debug("Connect external [signal:" + signal_name + ", func:" + "_on_signal_" + signal_name + "]")
target.connect( signal_name, self, "_on_signal_" + signal_name )
func _on_stream_finished():
# When finished playing a stream, make the player available again.
Config.msg_debug("_on_stream_finished")
music_play = false
func play_music(filename: String):
Config.msg_debug("path:" + str(filename))
music_player.stream = load_music( filename )
music_player.play()
currentmusic = filename
music_play = true
for child in $window_box/scroll_box/musics_box.get_children():
if Config.mute:
child.set_disabled()
elif child.get_music() == filename or not Config.playermusic :
child.set_play()
else:
child.set_unplay()
func load_playlist():
Config.msg_debug("load_playlist")
var domain="global"
var pos = 0
var posmusic = -1
# if Config.mute:
# if music_play:
# Config.msg_debug("mute")
# music_player.set_pause_mode(true)
# return
if music_play:
music_player.set_pause_mode(false)
# music_player.queue_free()
if Config.playermusic:
domain="player"
if domain != lastdomain:
Config.msg_debug("Change domain:" + domain)
currentmusic = ""
posmusic = -1
lastdomain = domain
if len(playlist_music[domain]) == 0:
Config.msg_debug("No musique on" + domain)
music_player.stop()
return
for path in playlist_music[domain]:
if Config.playermusic:
if currentmusic == path.get_music():
posmusic = pos
Config.msg_debug("Change domain:" + domain + " posmusic:" + str(posmusic))
break
else:
if currentmusic == path:
posmusic = pos
Config.msg_debug("Change domain:" + domain + " posmusic:" + str(posmusic))
break
pos += 1
Config.msg_debug("Change domain:" + domain + " posmusic" + str(posmusic))
if posmusic == -1:
posmusic = 0
Config.msg_debug("init:" + str(posmusic) + " / " + str(len(playlist_music[domain])))
else:
posmusic = (posmusic + 1) % len(playlist_music[domain])
Config.msg_debug("next:" + str(posmusic))
var path = playlist_music[domain][posmusic]
if Config.playermusic:
currentmusic = path.get_music()
else:
currentmusic = path
play_music(currentmusic)
# Config.msg_debug("path:" + str(currentmusic))
# music_player.stream = load_music( currentmusic )
# music_player.play()
# play_music = true
#audio_player.add_child()
func _process(_delta):
if not pause and not music_play and not Config.mute:
Config.msg_debug("next")
load_playlist()
# music_player.stream = load_music(queuemusic.pop_front())
# music_player.play()
# music_play = true
func load_external_music(filepath):
var stream = null
Config.msg_debug("load_external_music")
var file = File.new()
file.open(filepath, file.READ)
var ext = filepath.split(".")[-1].to_lower()
var buffer = file.get_buffer(file.get_len())
Config.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
_:
Config.msg_error("Impossible to identify type of file (file:" + filepath + ", ext:" + ext + ")")
return
stream.data = buffer
Config.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 reinitialize():
pass
func play_next_music(stream):
Config.msg_debug("play_next_music")
func update_volume_db():
Config.msg_debug("update_volume_db")
if Config.mute:
music_player.set_volume_db(linear2db(0.0))
music_player.set_pause_mode(true)
#update_playermusic()
return
var lvl = Config.sound_lvl_global * Config.sound_lvl_music / 10000.0
music_player.set_volume_db(linear2db(lvl))
music_player.set_pause_mode(false)
#update_playermusic()
func set_sound_mute(value: bool):
Config.msg_debug("set_sound_mute")
Config.set_sound_mute(value)
update_volume_db()
Config.save_config()
func set_level_global(value: int):
Config.msg_debug("set_level_global")
Config.sound_lvl_global = value
update_volume_db()
func set_level_music(value: int):
Config.msg_debug("set_level_music")
Config.sound_lvl_music = value
update_volume_db()
func set_level_effect(value: int):
Config.msg_debug("set_level_effect")
Config.sound_lvl_effect = value
update_volume_db()
func clear_music():
pass
func load_music_to_config():
Config.msg_debug("load_music_to_config")
Config.queuemusic = []
for child in $window_box/scroll_box/musics_box.get_children():
Config.queuemusic.append( child.get_music() )
Config.save_config()
func add_music(file: String):
var music_box = preload( "res://scenes/interfaces/music_manager/music_button.tscn" ).instance()
music_box.set_music( file, self.next_id )
music_box.connect( "del_pressed", self, "_on_music_box_delete_pressed" )
music_box.connect( "down_pressed", self, "_on_music_box_down_pressed" )
music_box.connect( "up_pressed", self, "_on_music_box_up_pressed" )
$window_box/scroll_box/musics_box.add_child( music_box )
self.next_id += 1
playlist_music['player'] = $window_box/scroll_box/musics_box.get_children()
load_music_to_config()
func _on_music_box_delete_pressed( id ):
Config.msg_debug("_on_music_box_delete_pressed")
for child in $window_box/scroll_box/musics_box.get_children():
if child.id == id:
child.queue_free()
func _on_music_box_down_pressed( id ):
Config.msg_debug("_on_music_box_down_pressed")
var pos = 0
for child in $window_box/scroll_box/musics_box.get_children():
if child.id == id:
break
pos += 1
if pos >= $window_box/scroll_box/musics_box.get_child_count():
Config.msg_error("Out of range : " + "pos:" + str(pos) + " count:" + str($window_box/scroll_box/musics_box.get_child_count()))
return
self.move_child_id(id, pos + 1)
func _on_music_box_up_pressed( id ):
Config.msg_debug("_on_music_box_up_pressed")
var pos = 0
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_id() == id:
break
pos += 1
if pos <= 0:
Config.msg_error("Out of range : " + "pos:" + str(pos))
return
self.move_child_id(id, pos - 1)
func move_child(music_filename, pos):
var ele = null
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_music() == music_filename:
ele = child
break
if ele:
$window_box/scroll_box/musics_box.move_child(ele, pos)
playlist_music['player'] = $window_box/scroll_box/musics_box.get_children()
func move_child_id(id, pos):
var ele = null
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_id() == id:
ele = child
break
if ele:
$window_box/scroll_box/musics_box.move_child(ele, pos)
playlist_music['player'] = $window_box/scroll_box/musics_box.get_children()
load_music_to_config()
2020-03-21 09:10:26 +00:00
func open():
Config.msg_debug("Open")
update_playermusic()
self.popup()
2020-03-21 09:10:26 +00:00
func close():
Config.msg_debug("Close")
self.hide()
2020-03-21 09:10:26 +00:00
func toggle():
Config.msg_debug("toggle")
if self.visible:
self.close()
else:
self.open()
2020-03-21 09:10:26 +00:00
func _on_music_pressed():
Config.msg_debug("Music")
func _on_music_bis_pressed( p_filename ):
Config.msg_debug(p_filename)
func _on_previous_pressed():
Config.msg_debug("Previous")
var previous = null
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_music() == currentmusic:
if previous != null:
pause = false
play_music(previous.get_music())
return
previous = child
func _on_pause_pressed():
Config.msg_debug("Pause")
if pause:
return
pause = true
playback_position_before_pause = music_player.get_playback_position( )
#music_player.stop()
func _on_play_pressed():
Config.msg_debug("Play")
if not pause:
return
music_player.play(playback_position_before_pause)
pause = false
func _on_next_pressed():
Config.msg_debug("Next")
var detected = false
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_music() == currentmusic:
detected = true
break
if detected:
pause = false
play_music(child.get_music())
return
func _on_sound_pressed():
Config.msg_debug("Music")
func _on_soundeffect_pressed():
Config.msg_debug("Sound Effect")
func _on_auto_pressed():
Config.msg_debug("Auto")
Config.set_playermusic(not Config.get_playermusic())
Config.save_config()
emit_signal( "musicplayer_pressed" )
func _on_mute_toggled(button_pressed):
Config.msg_debug("Mute")
MusicManager.set_sound_mute(button_pressed)
emit_signal( "mute_pressed" )
func _on_mute_pressed():
Config.msg_debug("Button JukeBox Mute")
MusicManager.set_sound_mute(not Config.mute)
emit_signal( "mute_pressed" )
func set_playermusic_on():
Config.msg_debug("playermusic on")
$window_box/controls_box/h_box_container_3/auto.texture_normal = load ( "res://assets/interfaces/jukebox/auto_on.png")
func set_playermusic_off():
Config.msg_debug("playermusic off")
$window_box/controls_box/h_box_container_3/auto.texture_normal = load ( "res://assets/interfaces/jukebox/auto_off.png")
Config.msg_debug("playermusic off : End")
func set_mute_on():
Config.msg_debug("mute on")
self.disable_execution_mute = true
$window_box/controls_box/h_box_container/mute.texture_normal = load ( "res://assets/interfaces/jukebox/sound_off.png")
self.disable_execution_mute = false
func set_mute_off():
Config.msg_debug("mute off")
self.disable_execution_mute = true
$window_box/controls_box/h_box_container/mute.texture_normal = load ( "res://assets/interfaces/jukebox/sound_on.png")
self.disable_execution_mute = false
func _on_add_pressed():
Config.msg_debug("")
$file_dialog.show()
func _on_file_dialog_files_selected(paths):
Config.msg_debug("multi files")
#if self.slots_number > 0:
# self.slots[self.slots_number - 1].disable_down()
for path in paths:
var found = false
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_music() == path:
found = true
break
if found:
continue
Config.msg_debug("file: "+ path)
MusicManager.add_music(path)
self.next_id += 1
self.open()
func update_playermusic():
Config.msg_debug("update_playermusic")
if Config.get_playermusic():
#Config.msg_debug("update_playermusic: player music")
set_playermusic_on()
for child in $window_box/scroll_box/musics_box.get_children():
if child.get_music() == currentmusic:
#Config.msg_debug("update_playermusic: player music : on " + child.get_music())
child.set_play()
elif Config.mute:
child.set_disabled()
else:
#Config.msg_debug("update_playermusic: player music : off " + child.get_music())
child.set_unplay()
else:
#Config.msg_debug("update_playermusic: khanat music")
set_playermusic_off()
for child in $window_box/scroll_box/musics_box.get_children():
child.set_disabled()
if last_update_playermusic != Config.get_playermusic():
Config.msg_debug("*** last_update_playermusic different ***")
load_playlist()
last_update_playermusic = Config.get_playermusic()
Config.msg_debug("update_playermusic : End")
func _on_check_button_toggled(button_pressed):
Config.msg_debug("_on_check_button_toggled")
#Config.set_playermusic(not Config.get_playermusic())
Config.set_playermusic(button_pressed)
emit_signal( "musicplayer_toggled" , not Config.playermusic )
func _on_music_manager_musicplayer_pressed():
Config.msg_debug("Signal musicplayer_pressed recieved on MusicManager")
update_playermusic()
func _on_music_manager_mute_pressed():
Config.msg_debug("Signal mute_pressed")
update_playermusic()
if Config.mute:
set_mute_on()
else:
set_mute_off()
func _on_signal_mute_pressed():
Config.msg_debug("<MusicManager> Received signal mute")
update_playermusic()
if Config.mute:
set_mute_on()
else:
set_mute_off()
func _on_signal_musicplayer_pressed():
Config.msg_debug("<MusicManager> Received signal musicplayer")
update_playermusic()