From 785701b093f7185041d74157ba3741051133a0bc Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 6 Apr 2024 03:30:50 +0300 Subject: [PATCH] Add an option to export a JSON file with the project data Probably temporary, it might be a good idea to add a third "Data" tab in the export dialog, if we add stuff like exporting to Godot resources, such as AnimatedSprite, AnimationPlayer etc. --- Translations/Translations.pot | 4 ++++ src/Autoload/Export.gd | 12 +++++++++++- src/Main.gd | 10 +++++++--- src/UI/Dialogs/ExportDialog.gd | 4 ++++ src/UI/Dialogs/ExportDialog.tscn | 8 +++++++- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Translations/Translations.pot b/Translations/Translations.pot index 8ef6a6991..a81abe98f 100644 --- a/Translations/Translations.pot +++ b/Translations/Translations.pot @@ -2378,6 +2378,10 @@ msgstr "" msgid "minute(s)" msgstr "" +#. A setting found in the export dialog. When enabled, a JSON file containing the project's data is also being exported. JSON refers to this https://en.wikipedia.org/wiki/JSON +msgid "Export JSON data" +msgstr "" + #. A setting found in the export dialog. When enabled, each layer is being exported as a different file, or as different frames if the target format is gif/apng/video. msgid "Split layers" msgstr "" diff --git a/src/Autoload/Export.gd b/src/Autoload/Export.gd index fd134f964..96278a4cd 100644 --- a/src/Autoload/Export.gd +++ b/src/Autoload/Export.gd @@ -46,6 +46,7 @@ var custom_exporter_generators := {} var current_tab := ExportTab.IMAGE ## All frames and their layers processed/blended into images var processed_images: Array[ProcessedImage] = [] +var export_json := false var split_layers := false # Spritesheet options @@ -351,7 +352,16 @@ func export_processed_images( return false _scale_processed_images() - + if export_json: + var json := JSON.stringify(project.serialize()) + var json_file_name := project.name + ".json" + if OS.has_feature("web"): + var json_buffer := json.to_utf8_buffer() + JavaScriptBridge.download_buffer(json_buffer, json_file_name, "application/json") + else: + var json_path := project.export_directory_path.path_join(json_file_name) + var json_file := FileAccess.open(json_path, FileAccess.WRITE) + json_file.store_string(json) # override if a custom export is chosen if project.file_format in custom_exporter_generators.keys(): # Divert the path to the custom exporter instead diff --git a/src/Main.gd b/src/Main.gd index dd3920cdd..c93377ef3 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -24,7 +24,7 @@ var splash_dialog: AcceptDialog: class CLI: - static var args_list = { + static var args_list := { ["--version", "--pixelorama-version"]: [CLI.print_version, "Prints current Pixelorama version"], ["--size"]: [CLI.print_project_size, "Prints size of the given project"], @@ -36,8 +36,9 @@ class CLI: ["--scale"]: [CLI.set_export_scale, "[integer] Scales up the export image by a number"], ["--frames", "-f"]: [CLI.set_frames, "[integer-integer] Used to specify frame range"], ["--direction", "-d"]: [CLI.set_direction, "[0, 1, 2] Specifies direction"], + ["--json"]: [CLI.set_json, "Export the JSON data of the project"], ["--split-layers"]: [CLI.set_split_layers, "Each layer exports separately"], - ["--help", "-h"]: [CLI.generate_help, "Use to print HELP"] + ["--help", "-h", "-?"]: [CLI.generate_help, "Displays this help page"] } static func generate_help(_project: Project, _next_arg: String): @@ -59,7 +60,7 @@ some useful [SYSTEM OPTIONS] are: [USER OPTIONS]:\n -(The terms in [ ] reflects the valid type for corresponding argument). +(The terms in [ ] reflect the valid type for corresponding argument). """ % OS.get_executable_path().get_file() @@ -142,6 +143,9 @@ some useful [SYSTEM OPTIONS] are: else: print(Export.AnimationDirection.keys()[Export.direction]) + static func set_json(_project: Project, _next_arg: String) -> void: + Export.export_json = true + static func set_split_layers(_project: Project, _next_arg: String) -> void: Export.split_layers = true diff --git a/src/UI/Dialogs/ExportDialog.gd b/src/UI/Dialogs/ExportDialog.gd index 04a594518..128b16107 100644 --- a/src/UI/Dialogs/ExportDialog.gd +++ b/src/UI/Dialogs/ExportDialog.gd @@ -419,6 +419,10 @@ func _on_ExportDialog_popup_hide() -> void: frame_timer.stop() +func _on_export_json_toggled(toggled_on: bool) -> void: + Export.export_json = toggled_on + + func _on_split_layers_toggled(toggled_on: bool) -> void: Export.split_layers = toggled_on Export.process_data() diff --git a/src/UI/Dialogs/ExportDialog.tscn b/src/UI/Dialogs/ExportDialog.tscn index 14b481860..613c67f94 100644 --- a/src/UI/Dialogs/ExportDialog.tscn +++ b/src/UI/Dialogs/ExportDialog.tscn @@ -281,7 +281,12 @@ layout_mode = 2 tooltip_text = "The character(s) that separate the file name and the frame number" text = "_" -[node name="SplitLayers" type="CheckBox" parent="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer" groups=["ExportImageOptions"]] +[node name="ExportJSON" type="CheckBox" parent="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer"] +layout_mode = 2 +mouse_default_cursor_shape = 2 +text = "Export JSON data" + +[node name="SplitLayers" type="CheckBox" parent="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer"] layout_mode = 2 mouse_default_cursor_shape = 2 text = "Split layers" @@ -348,6 +353,7 @@ size_flags_horizontal = 3 [connection signal="item_selected" from="VBoxContainer/VSplitContainer/VBoxContainer/FilePath/FileFormat" to="." method="_on_FileFormat_item_selected"] [connection signal="item_selected" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/Interpolation" to="." method="_on_Interpolation_item_selected"] [connection signal="text_changed" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/SeparatorCharacter" to="." method="_on_SeparatorCharacter_text_changed"] +[connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/ExportJSON" to="." method="_on_export_json_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/SplitLayers" to="." method="_on_split_layers_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/IncludeTagsInFilename" to="." method="_on_IncludeTagsInFilename_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/MultipleAnimationsDirectories" to="." method="_on_MultipleAnimationsDirectories_toggled"]