diff --git a/scenes/settings_menu.tscn b/scenes/settings_menu.tscn index 7886b5e..c278ba2 100644 --- a/scenes/settings_menu.tscn +++ b/scenes/settings_menu.tscn @@ -32,6 +32,15 @@ theme = ExtResource("2_lwwgp") theme_override_styles/panel = SubResource("StyleBoxTexture_choun") 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="."] layout_mode = 1 anchors_preset = 1 @@ -45,21 +54,43 @@ grow_horizontal = 0 texture_normal = ExtResource("2_wswnh") stretch_mode = 0 -[node name="Label" type="Label" parent="."] +[node name="ImportExportLabel" 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") +offset_top = 80.0 +offset_right = 144.0 +offset_bottom = 114.8 +text = "Import | Export +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 offset_left = 16.0 -offset_top = 72.0 -offset_right = 92.0 -offset_bottom = 96.0 +offset_top = 192.0 +offset_right = 91.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" [node name="Info Footer" type="Label" parent="."] @@ -84,4 +115,6 @@ text_overrun_behavior = 3 script = ExtResource("5_lwwgp") [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"] diff --git a/src/global/SaveManager.gd b/src/global/SaveManager.gd index e8b979c..e32a652 100644 --- a/src/global/SaveManager.gd +++ b/src/global/SaveManager.gd @@ -5,13 +5,33 @@ const SAVEFILE = "user://save.dat" func _ready() -> void: load_game() -func save_game(): - var data: Dictionary = { +func _to_dict() -> Dictionary: + return { "player_xp": XpLevelManager.player_xp, "phrases": PhrasesManager.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) f.store_string(data_json) f.close() @@ -20,14 +40,19 @@ func load_game(): if FileAccess.file_exists(SAVEFILE): var data_json = FileAccess.get_file_as_string(SAVEFILE) var data = JSON.parse_string(data_json) - # set variables - if "player_xp" in data: - XpLevelManager.loading = true - XpLevelManager.player_xp = data["player_xp"] - XpLevelManager.loading = false - if "phrases" in data and data["phrases"] is Array: - PhrasesManager.phrases = [] - for p in data["phrases"]: - PhrasesManager.phrases.append(p) - if "last_played_phrases" in data and data["last_played_phrases"] is Dictionary: - CoreGameplayManager.last_played_phrases = data["last_played_phrases"] + _from_dict(data) + +func export_to_base64() -> String: + var data_json = JSON.stringify(_to_dict()) + return Marshalls.utf8_to_base64(data_json) + +func import_from_base64(encoded_data: String) -> bool: + var data_json = Marshalls.base64_to_utf8(encoded_data) + var data_dict = JSON.parse_string(data_json) + if data_dict == null: + return false + var n_successful: int = _from_dict(data_dict) > 0 + if n_successful > 0: + save_game() + return true + else: return false diff --git a/src/ui/settings_menu.gd b/src/ui/settings_menu.gd index aa45e01..1fe2728 100644 --- a/src/ui/settings_menu.gd +++ b/src/ui/settings_menu.gd @@ -10,3 +10,15 @@ func _on_reset_xp_button_pressed() -> void: XpLevelManager.player_xp = 0 SaveManager.save_game() 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)