Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP : Add screenshots to save games #364

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func close() -> void:
# Idempotent call, no need to check the mode
PopochiuUtils.e.gui.popups_stack.back().show()

# Sleep for 1 frame so the save popup is not visible in the save screenshot
await E.get_tree().process_frame
_close()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ signal slot_selected
const SELECTION_COLOR := Color("edf171")
const OVERWRITE_COLOR := Color("c46c71")

var _current_slot: Button = null
var _current_slot: TextureButton = null
var _slot_name := ""
var _prev_text := ""
var _slot := 0

@onready var slots: VBoxContainer = %Slots
@onready var slots: HBoxContainer = %Slots

const SAVE_SCREENSHOT_PATH := "user://screenshot_%d.png"


#region Godot ######################################################################################
Expand All @@ -24,18 +26,22 @@ func _ready() -> void:

var saves: Dictionary = PopochiuUtils.e.get_saves_descriptions()

for btn: Button in slots.get_children():
for btn_loop: VBoxContainer in slots.get_children():
var btn: TextureButton = btn_loop.get_node("BtnSlot")
btn.set_meta("has_save", false)

if saves.has(btn.get_index() + 1):
btn.text = saves[btn.get_index() + 1]
if saves.has(btn_loop.get_index() + 1):
btn_loop.get_node("Label").text = saves[btn.get_index() + 1]
btn.set_meta("has_save", true)



else:
btn.disabled = true

btn_loop.get_node("Label").text = "Empty Slot"

btn.pressed.connect(_select_slot.bind(btn))


#endregion

#region Virtual ####################################################################################
Expand All @@ -44,7 +50,7 @@ func _open() -> void:
_slot = 0

if _current_slot:
_current_slot.text = _prev_text
_current_slot.get_node("../Label").text = _prev_text
_current_slot.button_pressed = false

_current_slot = null
Expand All @@ -63,13 +69,13 @@ func _close() -> void:


func _on_ok() -> void:
_slot = _current_slot.get_index() + 1
_slot = _current_slot.get_node("..").get_index() + 1

if _slot_name:
_prev_text = _current_slot.text
_prev_text = _current_slot.get_node("../Label").text
_current_slot.set_meta("has_save", true)

close()
# close()


#endregion
Expand All @@ -93,37 +99,94 @@ func _show_save(slot_text := "") -> void:
if _slot_name.is_empty():
_slot_name = _format_date(Time.get_datetime_dict_from_system())

for btn in slots.get_children():
for btn_loop in slots.get_children():
var btn: TextureButton = btn_loop.get_node("BtnSlot")
btn.disabled = false

var screenshot_expected = (btn as TextureButton).get_meta("has_save")
_update_screenshot(btn_loop, screenshot_expected)
open()


func _show_load() -> void:
lbl_title.text = "Choose the slot to load"
_slot_name = ""

for btn in slots.get_children():
btn.disabled = !(btn as Button).get_meta("has_save")

var counter := 1
for btn_loop in slots.get_children():
var btn: TextureButton = btn_loop.get_node("BtnSlot")
btn.disabled = !(btn as TextureButton).get_meta("has_save")
_update_screenshot(btn_loop, !btn.disabled)

open()


func _select_slot(btn: Button) -> void:
## Validate if the file you want to open exists and can be read from
func _file_can_be_opened(filename) -> bool:
if ! FileAccess.file_exists(filename):
return false
var file_opened := FileAccess.open(filename, FileAccess.READ)
if not file_opened:
PopochiuUtils.print_error(
"Could not open the file %s. Error code: %s" % [
filename, file_opened.get_open_error()
]
)
return false
return true


## Update the screenshot slot in the load/save screen with the relevant
## screenshot if possible
func _update_screenshot(btn_loop, screenshot_expected) -> void:
var save_screenshot_name:String
if screenshot_expected == false:
save_screenshot_name = "res://addons/popochiu/icons/empty_slot.png"

if not _file_can_be_opened(save_screenshot_name):
return
else:
save_screenshot_name = SAVE_SCREENSHOT_PATH % (btn_loop.get_index() + 1)

if not _file_can_be_opened(save_screenshot_name):
save_screenshot_name = "res://addons/popochiu/icons/missing_image.png"
if not _file_can_be_opened(save_screenshot_name):
return

var savegame_texture := _get_image_from_file(save_screenshot_name)
btn_loop.get_node("BtnSlot").texture_normal = savegame_texture


## Return the screenshot content from a save game image file
func _get_image_from_file(filename) -> ImageTexture:
var file:FileAccess = FileAccess.open(filename, FileAccess.READ)
if FileAccess.get_open_error() != OK:
print(str("Could not load screenshot image : ",filename))
return null
var img_buffer = file.get_buffer(file.get_length())
var savegame_img = Image.new()
var load_error := savegame_img.load_png_from_buffer(img_buffer)
if load_error != OK:
print(str("Error loading image : ",filename," with error: ",load_error))
return null
var savegame_texture = ImageTexture.create_from_image(savegame_img)
return savegame_texture


func _select_slot(btn: TextureButton) -> void:
if _slot_name:
if _current_slot:
_current_slot.text = _prev_text
_current_slot.get_node("../Label").text = _prev_text
_current_slot.button_pressed = false

_current_slot = btn
_prev_text = _current_slot.text
_current_slot.text = _slot_name
_prev_text = _current_slot.get_node("../Label").text
_current_slot.get_node("../Label").text = _slot_name
else:
if _current_slot:
_current_slot.button_pressed = false

_current_slot = btn
_prev_text = _current_slot.text
_prev_text = _current_slot.get_node("../Label").text

btn_ok.disabled = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=7 format=3 uid="uid://cndputybyj57n"]
[gd_scene load_steps=8 format=3 uid="uid://c7ehvcne5j4vq"]

[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="1_llqb8"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/popups/save_and_load_popup/save_and_load_popup.gd" id="2_m1ot6"]
[ext_resource type="StyleBox" uid="uid://dbajakvkltfaj" path="res://addons/popochiu/engine/objects/gui/components/popups/popochiu_popup_panel_container.tres" id="3_inruf"]
[ext_resource type="Texture2D" uid="uid://cmxrewai8t2lm" path="res://addons/popochiu/engine/objects/gui/resources/images/close.png" id="3_n4oyj"]
[ext_resource type="Texture2D" uid="uid://p32i25numi5e" path="res://addons/popochiu/engine/objects/gui/resources/images/close_highlight.png" id="4_jqolx"]
[ext_resource type="Theme" uid="uid://rg3ulk68vghu" path="res://game/gui/resources/gui_theme.tres" id="1_75y11"]
[ext_resource type="Script" path="res://game/gui/components/popups/save_and_load_popup/save_and_load_popup_custom.gd" id="2_pqr4b"]
[ext_resource type="StyleBox" uid="uid://bqnp8jblfcqe" path="res://game/gui/components/popups/popochiu_popup_panel_container.tres" id="3_30jnn"]
[ext_resource type="Texture2D" uid="uid://cmxrewai8t2lm" path="res://addons/popochiu/engine/objects/gui/resources/images/close.png" id="4_qsvch"]
[ext_resource type="Texture2D" uid="uid://p32i25numi5e" path="res://addons/popochiu/engine/objects/gui/resources/images/close_highlight.png" id="5_r5kvn"]
[ext_resource type="Texture2D" uid="uid://c2uf3fq2o5o4y" path="res://addons/popochiu/icons/empty_slot.png" id="6_md14g"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_yg86r"]

Expand All @@ -15,8 +16,8 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = ExtResource("1_llqb8")
script = ExtResource("2_m1ot6")
theme = ExtResource("1_75y11")
script = ExtResource("2_pqr4b")
script_name = &"SaveAndLoadPopup"

[node name="Overlay" type="PanelContainer" parent="."]
Expand All @@ -33,7 +34,7 @@ custom_minimum_size = Vector2(192, 0)
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 4
theme_override_styles/panel = ExtResource("3_inruf")
theme_override_styles/panel = ExtResource("3_30jnn")

[node name="VBoxContainer" type="VBoxContainer" parent="Overlay/PanelContainer"]
layout_mode = 2
Expand All @@ -54,35 +55,67 @@ unique_name_in_owner = true
texture_filter = 1
layout_mode = 2
size_flags_vertical = 4
texture_normal = ExtResource("3_n4oyj")
texture_pressed = ExtResource("4_jqolx")
texture_hover = ExtResource("4_jqolx")
texture_normal = ExtResource("4_qsvch")
texture_pressed = ExtResource("5_r5kvn")
texture_hover = ExtResource("5_r5kvn")

[node name="Slots" type="VBoxContainer" parent="Overlay/PanelContainer/VBoxContainer"]
[node name="Slots" type="HBoxContainer" parent="Overlay/PanelContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 3
alignment = 1

[node name="BtnSlot1" type="Button" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
[node name="VBoxContainer" type="VBoxContainer" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
layout_mode = 2

[node name="BtnSlot" type="TextureButton" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer"]
layout_mode = 2
toggle_mode = true
text = "slot 1"
texture_normal = ExtResource("6_md14g")

[node name="Label" type="Label" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer"]
layout_mode = 2
text = "Empty Slot"
horizontal_alignment = 1

[node name="BtnSlot2" type="Button" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
[node name="VBoxContainer2" type="VBoxContainer" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
layout_mode = 2

[node name="BtnSlot" type="TextureButton" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer2"]
layout_mode = 2
toggle_mode = true
text = "slot 2"
texture_normal = ExtResource("6_md14g")

[node name="Label" type="Label" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer2"]
layout_mode = 2
text = "Empty Slot"
horizontal_alignment = 1

[node name="BtnSlot3" type="Button" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
[node name="VBoxContainer3" type="VBoxContainer" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
layout_mode = 2

[node name="BtnSlot" type="TextureButton" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer3"]
layout_mode = 2
toggle_mode = true
text = "slot 3"
texture_normal = ExtResource("6_md14g")

[node name="Label" type="Label" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer3"]
layout_mode = 2
text = "Empty Slot"
horizontal_alignment = 1

[node name="BtnSlot4" type="Button" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
[node name="VBoxContainer4" type="VBoxContainer" parent="Overlay/PanelContainer/VBoxContainer/Slots"]
layout_mode = 2

[node name="BtnSlot" type="TextureButton" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer4"]
layout_mode = 2
toggle_mode = true
text = "slot 4"
texture_normal = ExtResource("6_md14g")

[node name="Label" type="Label" parent="Overlay/PanelContainer/VBoxContainer/Slots/VBoxContainer4"]
layout_mode = 2
text = "Empty Slot"
horizontal_alignment = 1

[node name="FooterContainer" type="HBoxContainer" parent="Overlay/PanelContainer/VBoxContainer"]
layout_mode = 2
Expand Down
9 changes: 7 additions & 2 deletions addons/popochiu/engine/others/popochiu_save_load.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func get_saves_descriptions() -> Dictionary:
]
)
return {}

var content := opened.get_as_text()
opened.close()

Expand All @@ -45,7 +44,7 @@ func get_saves_descriptions() -> Dictionary:
var loaded_data: Dictionary = test_json_conv.data

saves[i] = loaded_data.description

return saves


Expand All @@ -58,6 +57,12 @@ func save_game(slot := 1, description := "") -> bool:
]
)
return false

# Take save game screenshot
var capture := E.get_viewport().get_texture().get_image()
capture.resize(100, 65, Image.INTERPOLATE_LANCZOS)
var save_filename := "user://screenshot_%s.png" %slot
var result := capture.save_png(save_filename)

var data := {
description = description,
Expand Down
Binary file added addons/popochiu/icons/empty_slot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added addons/popochiu/icons/missing_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.