From f67bbba375ebe4ad48cebc9359c475487d37dfd0 Mon Sep 17 00:00:00 2001 From: theludovyc Date: Sun, 15 Dec 2024 17:07:22 +0100 Subject: [PATCH] [wip] prepare save ui --- Scene/Menu/MainMenu.tscn | 39 ++++++++++- Scene/RGT/loadSaveMenu.tscn | 21 ++++++ Scene/RGT/savePanelContainer.tscn | 49 +++++++++++++ Script/Gui/Menu/MainMenu.gd | 18 +++++ Script/RGT/LoadSaveMenu/load_save_menu.gd | 2 +- Script/RGT/MainMenu.gd | 46 ++++++------ Script/RGT/load_save_menu.gd | 85 +++++++++++++++++++++++ Script/RGT/save_panel_container.gd | 22 ++++++ 8 files changed, 256 insertions(+), 26 deletions(-) create mode 100644 Scene/RGT/loadSaveMenu.tscn create mode 100644 Scene/RGT/savePanelContainer.tscn create mode 100644 Script/RGT/load_save_menu.gd create mode 100644 Script/RGT/save_panel_container.gd diff --git a/Scene/Menu/MainMenu.tscn b/Scene/Menu/MainMenu.tscn index 6bc6f495..528c3a8b 100644 --- a/Scene/Menu/MainMenu.tscn +++ b/Scene/Menu/MainMenu.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=3 uid="uid://dytw0n3b07ux4"] +[gd_scene load_steps=19 format=3 uid="uid://dytw0n3b07ux4"] [ext_resource type="Script" path="res://Script/Gui/Menu/MainMenu.gd" id="1"] [ext_resource type="Theme" uid="uid://lyx8kydf373j" path="res://Art/Theme/MenuTheme.tres" id="1_i4hjr"] @@ -14,6 +14,7 @@ [ext_resource type="Texture2D" uid="uid://fnwe2ccpoxu7" path="res://Art/Image/Gui/Icons/MainMenu/multiplayer_bw.png" id="11_ulff8"] [ext_resource type="Texture2D" uid="uid://dx3wfk7nhepo7" path="res://Art/Image/Gui/Logos/uh_no_text.svg" id="12_q4rhy"] [ext_resource type="Texture2D" uid="uid://lxvd4qr8d35m" path="res://Art/Image/Gui/Logos/uh_only_text.svg" id="13_yw4on"] +[ext_resource type="PackedScene" uid="uid://choevp6ilq78t" path="res://Scene/RGT/loadSaveMenu.tscn" id="15_fufc5"] [sub_resource type="Animation" id="Animation_ft716"] length = 0.001 @@ -202,7 +203,6 @@ offset_left = -158.0 offset_top = -160.0 offset_right = -58.0 offset_bottom = -60.0 -disabled = true text = "Load Game" texture = ExtResource("6_6uh4k") @@ -347,6 +347,39 @@ texture = ExtResource("13_yw4on") expand_mode = 1 stretch_mode = 4 +[node name="PanelMenu" type="Panel" parent="."] +unique_name_in_owner = true +visible = false +layout_mode = 1 +anchors_preset = -1 +anchor_left = 0.1 +anchor_top = 0.1 +anchor_right = 0.9 +anchor_bottom = 0.9 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="LoadSaveMenu" parent="PanelMenu" instance=ExtResource("15_fufc5")] +unique_name_in_owner = true +visible = false +layout_mode = 1 +metadata/_edit_use_anchors_ = true + +[node name="BackButton" type="Button" parent="PanelMenu"] +layout_mode = 1 +anchors_preset = 7 +anchor_left = 0.5 +anchor_top = 1.0 +anchor_right = 0.5 +anchor_bottom = 1.0 +offset_left = -34.0 +offset_top = 16.0 +offset_right = 37.0 +offset_bottom = 61.0 +grow_horizontal = 2 +grow_vertical = 0 +text = "Back" + [node name="VersionNumber" type="Label" parent="."] unique_name_in_owner = true layout_mode = 1 @@ -371,4 +404,6 @@ unique_name_in_owner = true dialog_text = "Are you sure you want to quit ?" [connection signal="pressed" from="Decoration/MenuItems/PlayButton" to="." method="_on_play_button_pressed"] +[connection signal="pressed" from="Decoration/MenuItems/LoadButton" to="." method="_on_load_button_pressed"] [connection signal="pressed" from="Decoration/MenuItems/ExitButton" to="." method="_on_exit_button_pressed"] +[connection signal="pressed" from="PanelMenu/BackButton" to="." method="_on_back_button_pressed"] diff --git a/Scene/RGT/loadSaveMenu.tscn b/Scene/RGT/loadSaveMenu.tscn new file mode 100644 index 00000000..7f418df5 --- /dev/null +++ b/Scene/RGT/loadSaveMenu.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=2 format=3 uid="uid://choevp6ilq78t"] + +[ext_resource type="Script" path="res://Script/RGT/LoadSaveMenu/load_save_menu.gd" id="1_nyk5l"] + +[node name="LoadSaveMenu" type="ScrollContainer"] +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +horizontal_scroll_mode = 0 +script = ExtResource("1_nyk5l") + +[node name="VBoxContainer" type="VBoxContainer" parent="."] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="ConfirmationDialog" type="ConfirmationDialog" parent="."] +unique_name_in_owner = true + +[connection signal="confirmed" from="ConfirmationDialog" to="." method="_on_confirmation_dialog_confirmed"] diff --git a/Scene/RGT/savePanelContainer.tscn b/Scene/RGT/savePanelContainer.tscn new file mode 100644 index 00000000..fcbd1f8f --- /dev/null +++ b/Scene/RGT/savePanelContainer.tscn @@ -0,0 +1,49 @@ +[gd_scene load_steps=2 format=3 uid="uid://wal8k0gos06y"] + +[ext_resource type="Script" path="res://Script/RGT/LoadSaveMenu/save_panel_container.gd" id="1_np60c"] + +[node name="SavePanelContainer" type="PanelContainer"] +script = ExtResource("1_np60c") + +[node name="MarginContainer" type="MarginContainer" parent="."] +layout_mode = 2 +theme_override_constants/margin_left = 12 +theme_override_constants/margin_top = 12 +theme_override_constants/margin_right = 12 +theme_override_constants/margin_bottom = 12 + +[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer"] +layout_mode = 2 +theme_override_constants/separation = 8 + +[node name="SaveTextureRect" type="TextureRect" parent="MarginContainer/HBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 4 +expand_mode = 5 +stretch_mode = 5 + +[node name="NameLabel" type="Label" parent="MarginContainer/HBoxContainer"] +unique_name_in_owner = true +custom_minimum_size = Vector2(8, 8) +layout_mode = 2 +size_flags_horizontal = 3 +horizontal_alignment = 1 +vertical_alignment = 1 +autowrap_mode = 1 + +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/HBoxContainer"] +layout_mode = 2 +size_flags_vertical = 4 +alignment = 1 + +[node name="LoadButton" type="Button" parent="MarginContainer/HBoxContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +text = "Load" + +[node name="DeleteButton" type="Button" parent="MarginContainer/HBoxContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +text = "Delete" diff --git a/Script/Gui/Menu/MainMenu.gd b/Script/Gui/Menu/MainMenu.gd index 8425abf4..fffffefe 100644 --- a/Script/Gui/Menu/MainMenu.gd +++ b/Script/Gui/Menu/MainMenu.gd @@ -1,5 +1,7 @@ extends MainMenu +@onready var panel_menu = %PanelMenu + # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): if Input.is_anything_pressed(): @@ -8,3 +10,19 @@ func _process(_delta): animation_player.seek(animation_player.current_animation_length) set_process(false) + + +func _open_sub_menu(menu : Control): + panel_menu.visible = true + + sub_menu = menu + sub_menu.show() + +func _close_sub_menu(): + if sub_menu == null: + return + + panel_menu.visible = false + + sub_menu.hide() + sub_menu = null diff --git a/Script/RGT/LoadSaveMenu/load_save_menu.gd b/Script/RGT/LoadSaveMenu/load_save_menu.gd index 46aa67ad..abf2f73c 100644 --- a/Script/RGT/LoadSaveMenu/load_save_menu.gd +++ b/Script/RGT/LoadSaveMenu/load_save_menu.gd @@ -5,7 +5,7 @@ signal no_save_to_load const confirm_load = "Are you sure you want to load this save?\n" const confirm_delete = "Are you sure you want to delete this save ?\n" -var SavePanel = preload("res://scenes/LoadSaveMenu/savePanelContainer.tscn") +var SavePanel = preload("res://Scene/RGT/savePanelContainer.tscn") @onready var vbox_container = $VBoxContainer diff --git a/Script/RGT/MainMenu.gd b/Script/RGT/MainMenu.gd index 02361678..c217cb98 100644 --- a/Script/RGT/MainMenu.gd +++ b/Script/RGT/MainMenu.gd @@ -1,7 +1,7 @@ class_name MainMenu extends Control -#var sub_menu +var sub_menu #@onready var continue_button = %ContinueButton @onready var play_button = %PlayButton @@ -18,27 +18,27 @@ extends Control exit_button ] -#@onready var load_save_menu = %LoadSaveMenu +@onready var load_save_menu = %LoadSaveMenu #@onready var option_menu = %OptionsMenu #@onready var credit_menu = %CreditsContainer @onready var confirm_popup = %ConfirmationDialog -#func _open_sub_menu(menu : Control): - #if sub_menu == menu: - #sub_menu.visible = !sub_menu.visible - #return - #if sub_menu != null: - #sub_menu.hide() - #sub_menu = menu - #sub_menu.show() +func _open_sub_menu(menu : Control): + if sub_menu == menu: + sub_menu.visible = !sub_menu.visible + return + if sub_menu != null: + sub_menu.hide() + sub_menu = menu + sub_menu.show() -#func _close_sub_menu(): - #if sub_menu == null: - #return - #sub_menu.hide() - #sub_menu = null +func _close_sub_menu(): + if sub_menu == null: + return + sub_menu.hide() + sub_menu = null #func _event_is_mouse_button_released(event : InputEvent): @@ -86,8 +86,8 @@ func _on_play_button_pressed(): SceneLoader.change_scene(RGT_Globals.first_game_scene_setting) -#func _on_load_button_pressed() -> void: - #_open_sub_menu(load_save_menu) +func _on_load_button_pressed() -> void: + _open_sub_menu(load_save_menu) # #func _on_options_button_pressed(): #_open_sub_menu(option_menu) @@ -101,11 +101,11 @@ func _on_exit_button_pressed(): func _on_exit_confirmed(): get_tree().quit() -#func _on_back_button_pressed(): - #_close_sub_menu() +func _on_back_button_pressed(): + _close_sub_menu() -#func _on_load_save_menu_no_save_to_load() -> void: +func _on_load_save_menu_no_save_to_load() -> void: #continue_button.hide() - #load_button.hide() - # - #_close_sub_menu() + load_button.hide() + + _close_sub_menu() diff --git a/Script/RGT/load_save_menu.gd b/Script/RGT/load_save_menu.gd new file mode 100644 index 00000000..46aa67ad --- /dev/null +++ b/Script/RGT/load_save_menu.gd @@ -0,0 +1,85 @@ +extends ScrollContainer + +signal no_save_to_load + +const confirm_load = "Are you sure you want to load this save?\n" +const confirm_delete = "Are you sure you want to delete this save ?\n" + +var SavePanel = preload("res://scenes/LoadSaveMenu/savePanelContainer.tscn") + +@onready var vbox_container = $VBoxContainer + +@onready var confirm_dialog = %ConfirmationDialog + +enum Modes{ + Loading, + Deleting +} + +var popup_mode = Modes.Loading +var current_save_file_name:String = "" +var current_save_panel:Node = null + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + if SaveHelper.save_file_names.is_empty(): + SaveHelper.update_save_file_names() + + if SaveHelper.save_file_names.is_empty(): + push_warning("No save to load") + pass + + for save_file_name in SaveHelper.save_file_names: + var save_panel = SavePanel.instantiate() + + vbox_container.add_child(save_panel) + + save_panel.init(save_file_name) + + save_panel.load_button.pressed.connect(_on_load_button_pressed.bind(save_file_name)) + save_panel.delete_button.pressed.connect( + _on_delete_button_pressed.bind(save_panel, save_file_name)) + +func _on_load_button_pressed(save_file_name:String): + popup_mode = Modes.Loading + + current_save_file_name = save_file_name + + confirm_dialog.dialog_text = confirm_load + save_file_name + + confirm_dialog.popup_centered() + +func _on_delete_button_pressed(save_panel:Node, save_file_name:String): + popup_mode = Modes.Deleting + + current_save_file_name = save_file_name + + current_save_panel = save_panel + + confirm_dialog.dialog_text = confirm_delete + save_file_name + + confirm_dialog.popup_centered() + +func _on_confirmation_dialog_confirmed() -> void: + match(popup_mode): + Modes.Loading: + SaveHelper.save_file_name_to_load = current_save_file_name + + SceneLoader.change_scene(RGT_Globals.first_game_scene_setting) + + Modes.Deleting: + SaveHelper.delete(current_save_file_name) + + # move to trash the screenshot + OS.move_to_trash( + ProjectSettings.globalize_path( + SaveHelper.get_save_file_path_without_extension(current_save_file_name) + ".png" + )) + + SaveHelper.update_save_file_names() + + if SaveHelper.save_file_names.is_empty(): + no_save_to_load.emit() + + current_save_panel.queue_free() + diff --git a/Script/RGT/save_panel_container.gd b/Script/RGT/save_panel_container.gd new file mode 100644 index 00000000..4dc6ae59 --- /dev/null +++ b/Script/RGT/save_panel_container.gd @@ -0,0 +1,22 @@ +extends PanelContainer + +@onready var name_label = %NameLabel +@onready var save_texture = %SaveTextureRect +@onready var load_button = %LoadButton +@onready var delete_button = %DeleteButton + +func init(save_name:String): + name_label.text = save_name + + var image_path := SaveHelper.save_dir_path + "/" + save_name + ".png" + + if not FileAccess.file_exists(image_path): + return + + var image := Image.load_from_file(image_path) + + if image == null: + return + + save_texture.texture = ImageTexture.create_from_image(image) +