Add buttons to import/export from/to clipboard

This commit is contained in:
ChaoticByte 2025-08-06 23:34:09 +02:00
parent 83d213e135
commit fd50da7a1a
No known key found for this signature in database
3 changed files with 95 additions and 25 deletions

View file

@ -32,6 +32,15 @@ theme = ExtResource("2_lwwgp")
theme_override_styles/panel = SubResource("StyleBoxTexture_choun") theme_override_styles/panel = SubResource("StyleBoxTexture_choun")
script = ExtResource("2_labj1") script = ExtResource("2_labj1")
[node name="MenuLabel" type="Label" parent="."]
layout_mode = 0
offset_left = 16.0
offset_top = 16.0
offset_right = 104.0
offset_bottom = 39.0
text = "Settings"
label_settings = SubResource("LabelSettings_labj1")
[node name="CloseSettingsButton" type="TextureButton" parent="."] [node name="CloseSettingsButton" type="TextureButton" parent="."]
layout_mode = 1 layout_mode = 1
anchors_preset = 1 anchors_preset = 1
@ -45,21 +54,43 @@ grow_horizontal = 0
texture_normal = ExtResource("2_wswnh") texture_normal = ExtResource("2_wswnh")
stretch_mode = 0 stretch_mode = 0
[node name="Label" type="Label" parent="."] [node name="ImportExportLabel" type="Label" parent="."]
layout_mode = 0 layout_mode = 0
offset_left = 16.0 offset_left = 16.0
offset_top = 16.0 offset_top = 80.0
offset_right = 104.0 offset_right = 144.0
offset_bottom = 39.0 offset_bottom = 114.8
text = "Settings" text = "Import | Export
label_settings = SubResource("LabelSettings_labj1") to/from Clipboard"
[node name="ResetXPButton" type="Button" parent="."] [node name="ImportButton" type="Button" parent="ImportExportLabel"]
layout_mode = 0
offset_top = 48.0
offset_right = 61.0
offset_bottom = 72.0
text = "Import"
[node name="ExportButton" type="Button" parent="ImportExportLabel"]
layout_mode = 0
offset_left = 75.0
offset_top = 48.0
offset_right = 136.0
offset_bottom = 72.0
text = "Export"
[node name="ResetLabel" type="Label" parent="."]
layout_mode = 0 layout_mode = 0
offset_left = 16.0 offset_left = 16.0
offset_top = 72.0 offset_top = 192.0
offset_right = 92.0 offset_right = 91.0
offset_bottom = 96.0 offset_bottom = 207.9
text = "Reset Data"
[node name="ResetXPButton" type="Button" parent="ResetLabel"]
layout_mode = 0
offset_top = 24.0
offset_right = 76.0
offset_bottom = 48.0
text = "Reset XP" text = "Reset XP"
[node name="Info Footer" type="Label" parent="."] [node name="Info Footer" type="Label" parent="."]
@ -84,4 +115,6 @@ text_overrun_behavior = 3
script = ExtResource("5_lwwgp") script = ExtResource("5_lwwgp")
[connection signal="pressed" from="CloseSettingsButton" to="." method="_on_close_settings_button_pressed"] [connection signal="pressed" from="CloseSettingsButton" to="." method="_on_close_settings_button_pressed"]
[connection signal="pressed" from="ResetXPButton" to="." method="_on_reset_xp_button_pressed"] [connection signal="pressed" from="ImportExportLabel/ImportButton" to="." method="_on_import_button_pressed"]
[connection signal="pressed" from="ImportExportLabel/ExportButton" to="." method="_on_export_button_pressed"]
[connection signal="pressed" from="ResetLabel/ResetXPButton" to="." method="_on_reset_xp_button_pressed"]

View file

@ -5,13 +5,33 @@ const SAVEFILE = "user://save.dat"
func _ready() -> void: func _ready() -> void:
load_game() load_game()
func save_game(): func _to_dict() -> Dictionary:
var data: Dictionary = { return {
"player_xp": XpLevelManager.player_xp, "player_xp": XpLevelManager.player_xp,
"phrases": PhrasesManager.phrases, "phrases": PhrasesManager.phrases,
"last_played_phrases": CoreGameplayManager.last_played_phrases "last_played_phrases": CoreGameplayManager.last_played_phrases
} }
var data_json = JSON.stringify(data)
func _from_dict(data: Dictionary) -> int:
var successfully_set = 0
# set variables
if "player_xp" in data:
XpLevelManager.loading = true
XpLevelManager.player_xp = data["player_xp"]
XpLevelManager.loading = false
successfully_set += 1 #!
if "phrases" in data and data["phrases"] is Array:
PhrasesManager.phrases = []
for p in data["phrases"]:
PhrasesManager.phrases.append(p)
successfully_set += 1 #!
if "last_played_phrases" in data and data["last_played_phrases"] is Dictionary:
CoreGameplayManager.last_played_phrases = data["last_played_phrases"]
successfully_set += 1 #!
return successfully_set
func save_game():
var data_json = JSON.stringify(_to_dict())
var f = FileAccess.open(SAVEFILE, FileAccess.WRITE) var f = FileAccess.open(SAVEFILE, FileAccess.WRITE)
f.store_string(data_json) f.store_string(data_json)
f.close() f.close()
@ -20,14 +40,19 @@ func load_game():
if FileAccess.file_exists(SAVEFILE): if FileAccess.file_exists(SAVEFILE):
var data_json = FileAccess.get_file_as_string(SAVEFILE) var data_json = FileAccess.get_file_as_string(SAVEFILE)
var data = JSON.parse_string(data_json) var data = JSON.parse_string(data_json)
# set variables _from_dict(data)
if "player_xp" in data:
XpLevelManager.loading = true func export_to_base64() -> String:
XpLevelManager.player_xp = data["player_xp"] var data_json = JSON.stringify(_to_dict())
XpLevelManager.loading = false return Marshalls.utf8_to_base64(data_json)
if "phrases" in data and data["phrases"] is Array:
PhrasesManager.phrases = [] func import_from_base64(encoded_data: String) -> bool:
for p in data["phrases"]: var data_json = Marshalls.base64_to_utf8(encoded_data)
PhrasesManager.phrases.append(p) var data_dict = JSON.parse_string(data_json)
if "last_played_phrases" in data and data["last_played_phrases"] is Dictionary: if data_dict == null:
CoreGameplayManager.last_played_phrases = data["last_played_phrases"] return false
var n_successful: int = _from_dict(data_dict) > 0
if n_successful > 0:
save_game()
return true
else: return false

View file

@ -10,3 +10,15 @@ func _on_reset_xp_button_pressed() -> void:
XpLevelManager.player_xp = 0 XpLevelManager.player_xp = 0
SaveManager.save_game() SaveManager.save_game()
NotificationQueue.add("Reset XP.") NotificationQueue.add("Reset XP.")
func _on_import_button_pressed() -> void:
var data = DisplayServer.clipboard_get()
if SaveManager.import_from_base64(data):
NotificationQueue.add("Import successful", 4000)
else:
NotificationQueue.add("Import failed", 4000)
func _on_export_button_pressed() -> void:
var data = SaveManager.export_to_base64()
DisplayServer.clipboard_set(data)
NotificationQueue.add("Exported to clipboard", 4000)