TinyAdventure/ui/start_game_menu_ui.gd

97 lines
3.6 KiB
GDScript

extends Panel
signal start_game(game_path:String)
const GAME_PROFILE_FOLDER:String = "game_profiles"
@onready var game_profile_v_box_container: VBoxContainer = %GameProfileVBoxContainer
@onready var confirm_delete_game_profile_dialog: Popup = %ConfirmDeleteGameProfileDialog
const game_profile_item_scene = preload("res://ui/game_profile_item.tscn")
var game_profile_list:Array[GameProfileResource] = []
var selected_game_profile_item:GameProfileItem = null
# 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_PROFILE_FOLDER)
if not game_profiles_folder:
push_warning("Warning: no " + GAME_PROFILE_FOLDER + " folder found!")
return
for profile_directory in game_profiles_folder.get_directories():
var game_profile:GameProfileResource = 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:GameProfileResource 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))
game_profile_item.delete_button.pressed.connect(_on_game_profile_delete_pressed.bind(game_profile_item))
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_PROFILE_FOLDER + "/" + game_profile_item.game_profile.directory + "/game.tres")
func _on_game_profile_delete_pressed(game_profile_item:GameProfileItem):
var game_profile_label = confirm_delete_game_profile_dialog.find_child("GameProfileNameLabel", true, false) as Label
if game_profile_label == null:
push_error("Error finding game profile label!")
return
game_profile_label.text = game_profile_item.name_label.text
selected_game_profile_item = game_profile_item
confirm_delete_game_profile_dialog.show()
func _on_cancel_delete_game_profile() -> void:
confirm_delete_game_profile_dialog.hide()
func delete_game_profile(game_profile_item:GameProfileItem) -> void:
var game_profiles_folder = DirAccess.open("user://" + GAME_PROFILE_FOLDER)
if not game_profiles_folder:
push_warning("Warning: no " + GAME_PROFILE_FOLDER + " folder found!")
return
# Cannot delete folders recursively, therefore have to cleanup files first.
var game_profile_path:String = "user://" + GAME_PROFILE_FOLDER + "/" + game_profile_item.game_profile.directory
for file in DirAccess.get_files_at(game_profile_path):
DirAccess.remove_absolute(game_profile_path + "/" + file)
game_profiles_folder.remove(game_profile_item.game_profile.directory)
update_game_profile_list()
update_game_profiles_widget()
func _on_confirm_delete_game_profile() -> void:
if selected_game_profile_item == null:
push_error("Error no game profile currently selected!")
return
delete_game_profile(selected_game_profile_item)
selected_game_profile_item = null
confirm_delete_game_profile_dialog.hide()