TinyAdventure/ui/start_game_menu_ui.gd

51 lines
1.9 KiB
GDScript

extends Panel
signal start_game(game_path:String)
@onready var game_profile_v_box_container: VBoxContainer = %GameProfileVBoxContainer
const game_profile_item_scene = preload("res://ui/game_profile_item.tscn")
var game_profile_list:Array[GameProfile] = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
connect("visibility_changed", _on_visibility_changed)
func update_game_profile_list() -> void:
game_profile_list = []
var game_profiles_folder = DirAccess.open("user://game_profiles")
if not game_profiles_folder:
push_warning("Warning: no game_profiles folder found!")
return
for profile_directory in game_profiles_folder.get_directories():
var game_profile:GameProfile = ResourceLoader.load("user://game_profiles/" + profile_directory + "/game.tres")
if not game_profile:
push_warning("Warning: could not open game profile " + profile_directory)
continue
game_profile.directory = profile_directory
game_profile_list.append(game_profile)
func update_game_profiles_widget() -> void:
for profile_entry in game_profile_v_box_container.get_children():
game_profile_v_box_container.remove_child(profile_entry)
profile_entry.queue_free()
for profile:GameProfile in game_profile_list:
var game_profile_item:GameProfileItem = game_profile_item_scene.instantiate()
game_profile_item.game_profile = profile
game_profile_v_box_container.add_child(game_profile_item)
game_profile_item.start_button.pressed.connect(_on_game_profile_start_pressed.bind(game_profile_item))
#connect("pressed", bind("_on_game_profile_start_pressed", game_profile))
func _on_visibility_changed() -> void:
update_game_profile_list()
update_game_profiles_widget()
func _on_game_profile_start_pressed(game_profile_item:GameProfileItem):
start_game.emit("user://game_profiles/" + game_profile_item.game_profile.directory + "/game.tres")