Files
MusicPlayer/MainWindow.gd

451 lines
15 KiB
GDScript

extends Control
# 601 LOC 11/9 - 2023
# 628 LOC 15/9 - 2023
# 631 LOC 16/9 - 2023
# 554 LOC 30/5 - 2024
# 551 LOC 3/6 - 2024
# 386 LOC 12/6 - 2024
##TODO
# 13. Fix graphics in lists
# 14. Fix layout
# 15. Fix for local play
@onready
var open_button: Button = $Open
@onready
var fileDialog: FileDialog = $FileDialog
@onready
var inspiration_scroll: ScrollContainer = $ScrollContainer
@onready
var inspiration_list: VBoxContainer = $ScrollContainer/VBoxContainer
@onready
var game_label: Label = $VBoxContainer/GameLabel
@onready
var song_label: Label = $VBoxContainer/SongLabel
@onready
var add_player_container: HBoxContainer = $Players/VBoxContainer/AddPlayerContainer
@onready
var add_players_button: TextureButton = $Players/VBoxContainer/HBoxContainer/AddPlayersButton
@onready
var add_player_button: Button = $Players/VBoxContainer/AddPlayerContainer/AddPlayerButton
@onready
var new_player_name_field: TextEdit = $Players/VBoxContainer/AddPlayerContainer/PlayerNameField
@onready
var reset_playlist_button: Button = $ResetPlaylistButton
@onready
var reset_points_button: Button = $ResetPointsButton
@onready
var sound_test_button: Button = $SoundTestButton
@onready
var sync_button: Button = $SyncButton
@onready
var sync_popup: PopupPanel = $SyncPopupPanel
@onready
var settings_button: Button = $SettingsButton
@onready
var settings_popup: PopupPanel = $SettingsPopupPanel
@onready
var statistics_button: Button = $StatisticsButton
@onready
var statistic_popup: PopupPanel = $StatisticsPopupPanel
@onready
var statistic_label: Label = $StatisticsPopupPanel/StatisticsLabel
@onready
var about_button: Button = $AboutButton
@onready
var about_popup: PopupPanel = $AboutPopupPanel
@onready
var show_answer_button: Button = $ShowAnswerButton
@onready
var next_button: Button = $NextButton
@onready
var music_list_scroll: ScrollContainer = $MusicListPanel/ScrollContainer
@onready
var music_list: VBoxContainer = $MusicListPanel/ScrollContainer/MusicList
@onready
var players: VBoxContainer = $Players/VBoxContainer
@onready
var character_select: Control = $CharacterSelect
@onready
var search_button: Button = $SearchButton
@onready
var search_view: Control= $Search
@onready
var version_label: Label = $AboutPopupPanel/VBoxContainer/VersionLabel
@onready
var whats_new_label: Label = $AboutPopupPanel/VBoxContainer/HBoxContainer/NewLabel
@onready
var shortcut_label: Label = $AboutPopupPanel/VBoxContainer/HBoxContainer/ShortcutsLabel
@onready
var coming_label: Label = $AboutPopupPanel/VBoxContainer/HBoxContainer/CommingLabel
@onready
var winner_popup: PopupPanel = $WinnerPopupPanel
@onready
var winner_label: Label = $WinnerPopupPanel/WinnerLabel
@onready
var music_player_container: PanelContainer = $MusicPlayer
var player := preload("res://Player.tscn")
var songs: Array= []
var games: Array = []
var song_list: Array = []
var next_label: Label
var current_player: Node
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var is_debug := OS.has_feature("debug")
var is_mac := OS.has_feature("macos")
if is_debug:
print("is_debug")
Settings.default_path = "http://localhost:8080"
Settings.selected_server = 3
if is_mac:
print("is_mac")
next_button.pressed.connect(next_track)
show_answer_button.pressed.connect(show_answer_pressed)
search_button.pressed.connect(show_search)
sync_button.pressed.connect(sync_games)
sound_test_button.pressed.connect(music_player_container.get_sound_test_song)
statistics_button.pressed.connect(get_statistics)
about_button.pressed.connect(show_about)
settings_button.pressed.connect(show_settings)
reset_playlist_button.pressed.connect(reset_playlist)
reset_points_button.pressed.connect(reset_points)
character_select.connect("character_selected", _on_character_selected)
new_player_name_field.connect("enter_key_pressed", add_player)
music_player_container.connect("add_to_queue", _on_add_to_queue)
music_player_container.connect("show_answer", show_answer_pressed)
add_players_button.pressed.connect(add_players)
add_player_button.pressed.connect(add_player)
open_button.pressed.connect(open)
get_suggestion_list()
fetch_full_music_list_at_start()
func _input(event: InputEvent) -> void:
if event is InputEventKey and event.pressed:
if event.alt_pressed && event.keycode == KEY_UP:
print("Alt + UP pressed")
music_player_container.change_volume(0.05)
if event.alt_pressed && event.keycode == KEY_DOWN:
print("Alt + DOWN pressed")
music_player_container.change_volume(-0.05)
if event.alt_pressed && event.keycode == KEY_LEFT:
print("Alt + LEFT pressed")
music_player_container.seek(-5)
if event.alt_pressed && event.keycode == KEY_RIGHT:
print("Alt + RIGHT pressed")
music_player_container.seek(5)
func next_track() -> void:
next_button.disabled = true
music_player_container.fetch_next_song()
func reset_playlist() -> void:
print("reset_playlist")
Settings.make_request2("/music/reset", fetch_full_music_list_at_start, false)
func reset_points() -> void:
print("reset_points")
var players_to_reset: Array = players.get_children()
for player_to_reset: Node in players_to_reset:
if player_to_reset.has_method("reset_points"):
player_to_reset.reset_points()
func show_about() -> void:
print("show_about")
about_popup.visible = true
version_label.text = Settings.version
whats_new_label.text = Settings.whats_new
shortcut_label.text = Settings.shortcuts
coming_label.text = Settings.whats_left
func show_settings() -> void:
print("show_settings")
settings_popup.visible = true
func get_statistics() -> void:
print("get_statistics")
statistic_popup.visible = true
statistic_label.text = "Total amount of games in the playlist: " + str(games.size())
func _on_add_to_queue() -> void:
print("_on_add_to_queue")
next_button.disabled = false
Settings.make_request2("/music/addQue", fetched, false)
game_label.text = "????????"
song_label.text = "??????"
func sync_games() -> void:
print("sync_games")
sync_button.disabled = true
var games_synced: Callable = func games_synced() -> void:
sync_button.disabled = false
sync_popup.visible = true
print("games_synced")
reset_playlist()
get_suggestion_list()
search_view.get_list_of_games()
Settings.make_request2("/sync", games_synced, false)
func get_suggestion_list() -> void:
print("get_suggestion_list")
var populate_list: Callable = func(array) -> void:
if typeof(array) == TYPE_ARRAY:
games = []
games.append_array(array)
for game: String in games:
var inspiration_label: Label= Label.new()
inspiration_label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
inspiration_label.add_theme_font_size_override("font_size", 20)
inspiration_label.text = game
inspiration_label.autowrap_mode = TextServer.AUTOWRAP_WORD
inspiration_list.add_child(inspiration_label)
inspiration_scroll.scroll_to_bottom()
else:
print("Unexpected data")
Settings.make_request2("/music/all", populate_list, true)
func add_players() -> void:
print("add_players")
add_player_container.visible = !add_player_container.visible
new_player_name_field.grab_focus()
func add_player() -> void:
print("add_player")
var new_player: Node = player.instantiate()
new_player.player_name = new_player_name_field.text
new_player.player_score = new_player_name_field.text + ": 0"
new_player.id = Settings.get_next_player_id()
new_player_name_field.text = ""
players.add_child(new_player)
new_player.connect("change_character_clicked", _on_player_change_character_clicked.bind(new_player))
new_player.connect("first_point_triggerd", music_player_container._on_point_triggered.bind("first"))
new_player.connect("match_point_triggerd", music_player_container._on_point_triggered.bind("match"))
new_player.connect("winner_triggerd", _on_player_won.bind(new_player.player_name))
new_player.connect("player_removed", _on_player_removed.bind(new_player))
func _on_player_won(player_name: String) -> void:
print("_on_player_won")
winner_popup.visible = true
winner_label.text = player_name + " won!!"
music_player_container.play_sound(preload("res://sounds/winning.mp3"))
func show_search() -> void:
print("show_search")
if search_view.visible == false:
search_view.visible = true
else:
search_view.visible = false
func show_answer_pressed() -> void:
print("show_answer_pressed")
var show_fetched: Callable = func(data_received: Dictionary) -> void:
print("show_fetched data_received: ", data_received)
game_label.text = data_received.Game
song_label.text = data_received.Song
var format_string: String = "%d. %s - %s"
var actual_string: String = format_string % [(data_received.SongNo+1), data_received.Game, data_received.Song]
if next_label == null:
next_label = Label.new()
next_label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
next_label.add_theme_font_size_override("font_size", 20)
next_label.text = actual_string
Settings.make_request2("/music/info", show_fetched, true)
func fetch_full_music_list_at_start() -> void:
print("fetch_full_music_list_at_start")
var show_music_list_at_start: Callable = func(data) -> void:
if data == null:
song_list = []
Settings.delete_children(music_list)
return
if typeof(data) == TYPE_ARRAY:
song_list = []
song_list.append_array(data)
print("song_list", song_list)
for song: Dictionary in song_list:
var music_list_label: Label = Label.new()
music_list_label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
music_list_label.add_theme_font_size_override("font_size", 20)
music_list_label.autowrap_mode = TextServer.AUTOWRAP_WORD
var format_string: String = "%d. %s - %s"
var actual_string: String = format_string % [(song.SongNo+1), song.Game, song.Song]
music_list_label.text = actual_string
music_list_label.mouse_filter = Control.MOUSE_FILTER_PASS
music_list_label.gui_input.connect(song_clicked.bind(music_list_label, song.SongNo))
music_list.add_child(music_list_label)
else:
print("Unexpected data")
Settings.make_request2("/music/list", show_music_list_at_start, true)
func fetch_full_music_list(event: InputEvent, song_no: int) -> void:
print("fetch_full_music_list")
if (event is InputEventMouseButton && event.pressed && event.button_index == MOUSE_BUTTON_LEFT):
var show_music_list: Callable = func(data_received) -> void:
if typeof(data_received) == TYPE_ARRAY:
song_list = []
song_list.append_array(data_received)
Settings.delete_children(music_list)
for song: Dictionary in song_list:
var music_label: Label= Label.new()
music_label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
music_label.add_theme_font_size_override("font_size", 20)
game_label.text = song.Game
song_label.text = song.Song
var format_string: String = "%d. %s - %s"
var actual_string: String = format_string % [(song.SongNo+1), song.Game, song.Song]
music_label.text = actual_string
music_label.mouse_filter = Control.MOUSE_FILTER_PASS
music_label.gui_input.connect(song_clicked.bind(music_label, song.SongNo))
print(str(music_list.get_child_count()) + " | " + str(song_list.size() - 1))
if music_list.get_child_count() == song_list.size() - 1:
music_label.add_theme_color_override("font_color", Color(1, 0.5, 0))
music_list.add_child(music_label)
else:
print("Unexpected data")
Settings.make_request2("/music/list", show_music_list, true)
Settings.make_request2("/music?song=" + str(song_no), music_player_container.play_song, true)
Settings.make_request2("/music/info", show_answer, true)
func show_answer(answer: Dictionary) -> void:
print("show_answer1: ", answer)
game_label.text = answer.Game
song_label.text = answer.Song
func fetched() -> void:
print("fetched")
var show_fetched_list: Callable = func(data_received) -> void:
if typeof(data_received) == TYPE_ARRAY:
song_list = []
song_list.append_array(data_received)
Settings.delete_children(music_list)
song_list.remove_at(song_list.size() - 1)
for song: Dictionary in song_list:
var fetched_song_label: Label = Label.new()
fetched_song_label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
fetched_song_label.add_theme_font_size_override("font_size", 20)
var format_string: String = "%d. %s - %s"
var actual_string: String = format_string % [(song.SongNo+1), song.Game, song.Song]
fetched_song_label.text = actual_string
fetched_song_label.mouse_filter = Control.MOUSE_FILTER_PASS
fetched_song_label.gui_input.connect(song_clicked.bind(fetched_song_label, song.SongNo))
music_list.add_child(fetched_song_label)
songs = music_list.get_children()
for song: Label in songs:
song.remove_theme_color_override("font_color")
next_label = Label.new()
next_label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
next_label.add_theme_font_size_override("font_size", 20)
next_label.add_theme_color_override("font_color", Color(1, 0.5, 0))
next_label.text = "??? - ???"
next_label.mouse_filter = Control.MOUSE_FILTER_PASS
next_label.gui_input.connect(fetch_full_music_list.bind(songs.size()))
music_list.add_child(next_label)
else:
print("Unexpected data")
Settings.make_request2("/music/list", show_fetched_list, true)
func song_clicked(event: InputEvent, clicked_song_label: Label, song_no: int) -> void:
print("song_clicked")
if (event is InputEventMouseButton && event.pressed && event.button_index == MOUSE_BUTTON_LEFT):
print("Song Clicked: " + str(song_no))
songs = music_list.get_children()
for song: Label in songs:
song.remove_theme_color_override("font_color")
clicked_song_label.add_theme_color_override("font_color", Color(1, 0.5, 0))
Settings.make_request2("/music?song=" + str(song_no), music_player_container.play_song, true)
Settings.make_request2("/music/info", show_answer, true)
func _on_player_removed(new_player: Node) -> void:
print("_on_player_removed")
players.remove_child(new_player)
func _on_player_change_character_clicked(new_player: Node) -> void:
print("_on_player_change_character_clicked")
current_player = new_player
character_select.visible = true
func _on_character_selected(file_name: String) -> void:
print("_on_character_selected")
character_select.visible = false
current_player._on_control_character_selected_clicked(file_name)
###Local
var local_path: String = '/Users/sebastian/ResilioSync/Sorterat_test/Metal Gear Solid 4 - Guns of the Patriots/2-16 Metal Gear Saga.mp3'
func open() -> void:
fileDialog.popup()
func _on_file_dialog_dir_selected(path: String) -> void:
print(path)
dir_contents(path)
func dir_contents(path: String) -> void:
var dir: DirAccess = DirAccess.open(path)
if dir:
dir.list_dir_begin()
var file_name: String = dir.get_next()
songs.clear()
games = []
while file_name != "":
if dir.current_is_dir():
#print("Found directory: " + file_name)
games.append(file_name)
else:
#print("Found file: " + file_name)
if file_name.ends_with(".mp3"):
songs.append(path + "/" + file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path.")
for game: String in games:
var local_inspiration_label: Label = Label.new()
local_inspiration_label.text = game
inspiration_list.add_child(local_inspiration_label)