frame-of-mind/src/ui/menu_main/save_game_list.gd

118 lines
2.9 KiB
GDScript3
Raw Normal View History

class_name SaveGameList
extends Control
signal _picked(save_game: SaveGame)
@export var saves: Array[SaveGame]
var save_buttons: Array[SaveGameDisplay]
@export var update_display: bool:
set(value):
_load_games()
@onready var list_container: VBoxContainer = %ListContainer
2025-03-25 21:34:13 +00:00
func _validate_property(property: Dictionary) -> void:
if property.name == "saves":
property.usage = PROPERTY_USAGE_READ_ONLY + PROPERTY_USAGE_SCRIPT_VARIABLE + PROPERTY_USAGE_EDITOR
func _ready() -> void:
_load_games()
hide()
set_process_input(false)
func _ensure_directory() -> void:
var dir := DirAccess.open(State.user_saves_path)
# Create dir if needed.
2024-10-16 10:24:38 +00:00
if DirAccess.get_open_error() == ERR_INVALID_PARAMETER:
dir = DirAccess.open("user://")
dir.make_dir_recursive(State.user_saves_path)
if DirAccess.get_open_error() != OK:
printerr("Error while opening User Save Directory: %s" % error_string(DirAccess.get_open_error()))
func _load_games():
_ensure_directory()
2025-03-25 21:34:13 +00:00
saves = []
var save_game_dir := DirAccess.open(State.user_saves_path)
var filepaths: PackedStringArray = save_game_dir.get_files()
for path in filepaths:
if path.ends_with(".json"):
2025-03-25 21:34:13 +00:00
saves.append(SaveGame.new("%s/%s" % [State.user_saves_path, path.get_basename()]))
_sort_saves()
_rebuild_buttons()
func _sort_saves() -> void:
saves.sort_custom(func(a: SaveGame, b: SaveGame) -> int:
if a.last_saved > b.last_saved:
return -1
elif a.last_saved < b.last_saved:
return 1
else:
return 0
)
func _rebuild_buttons() -> void:
save_buttons = []
for child in list_container.get_children():
child.queue_free()
2025-03-25 21:34:13 +00:00
var save_box := VBoxContainer.new()
save_box.add_theme_constant_override("separation", 16)
list_container.add_child(save_box)
for i in range(saves.size()):
var new_button := SaveGameDisplay.new(saves[i], i+1)
2025-03-25 21:34:13 +00:00
save_box.add_child(new_button)
save_buttons.append(new_button)
new_button.pressed.connect(_on_game_picked.bind(i))
func _on_game_picked(id: int) -> void:
hide()
#_picked.emit(saves[id])
func get_most_recent_save() -> SaveGame:
_sort_saves()
return saves[0] if saves.size() > 0 else SaveGame.new()
2025-03-25 21:34:13 +00:00
func has_existing_saves() -> bool:
return saves.size() > 1
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_cancel"):
cancel()
func cancel()->void:
_picked.emit(State.save_game)
# This function is called when the user us supposed to choose a slot to load or create a new game.
func pick_save_slot() -> SaveGame:
await open()
var result = await _picked
await close()
return result
# TODO: ugh, godot tweens are the wurst
func open() -> void:
show()
set_process_input(true)
save_buttons[0].grab_focus()
modulate = Color.TRANSPARENT
var tween := get_tree().create_tween()
tween.tween_property(self, "modulate", Color.WHITE, 0.5)
await tween.finished
func close() -> void:
set_process_input(false)
var tween := get_tree().create_tween()
tween.tween_property(self, "modulate", Color.TRANSPARENT, 0.5)
await tween.finished
hide()