From 637a60d9ee0068b06223f1ef6e7b66b6f1179026 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 30 Jun 2020 20:25:43 +0300 Subject: [PATCH] Saving .pxo files in HTML5 is now possible Addresses #280 --- src/Autoload/Global.gd | 2 ++ src/Autoload/OpenSave.gd | 10 +++++++++ src/Main.gd | 7 ++++++ src/Main.tscn | 7 +++++- src/UI/Dialogs/ExportDialog.tscn | 4 ++-- src/UI/Dialogs/OpenSprite.tscn | 4 ++-- src/UI/Dialogs/SaveSprite.tscn | 4 ++-- src/UI/Dialogs/SaveSpriteHTML5.tscn | 33 +++++++++++++++++++++++++++++ src/UI/TopMenuContainer.gd | 15 +++++++++---- 9 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 src/UI/Dialogs/SaveSpriteHTML5.tscn diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index d39ef8425..f49380893 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -153,6 +153,7 @@ var zoom_level_label : Label var new_image_dialog : ConfirmationDialog var open_sprites_dialog : FileDialog var save_sprites_dialog : FileDialog +var save_sprites_html5_dialog : ConfirmationDialog var export_dialog : AcceptDialog var preferences_dialog : AcceptDialog var unsaved_changes_dialog : ConfirmationDialog @@ -288,6 +289,7 @@ func _ready() -> void: new_image_dialog = find_node_by_name(root, "CreateNewImage") open_sprites_dialog = find_node_by_name(root, "OpenSprite") save_sprites_dialog = find_node_by_name(root, "SaveSprite") + save_sprites_html5_dialog = find_node_by_name(root, "SaveSpriteHTML5") export_dialog = find_node_by_name(root, "ExportDialog") preferences_dialog = find_node_by_name(root, "PreferencesDialog") unsaved_changes_dialog = find_node_by_name(root, "UnsavedCanvasDialog") diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index c1f91a395..d902e5a32 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -286,6 +286,16 @@ func save_pxo_file(path : String, autosave : bool, project : Project = Global.cu file.close() + if OS.get_name() == "HTML5" and !autosave: + file.open_compressed(path, File.READ, File.COMPRESSION_ZSTD) + if !err: + var file_data = Array(file.get_buffer(file.get_len())) + JavaScript.eval("download('%s', %s, '');" % [path.get_file(), str(file_data)], true) + file.close() + # Remove the .pxo file from memory, as we don't need it anymore + var dir = Directory.new() + dir.remove(path) + if autosave: Global.notification_label("File autosaved") else: diff --git a/src/Main.gd b/src/Main.gd index a56b3dbe9..4c2ea147f 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -145,6 +145,13 @@ func _on_SaveSprite_file_selected(path : String) -> void: _on_QuitDialog_confirmed() +func _on_SaveSpriteHTML5_confirmed() -> void: + var file_name = Global.save_sprites_html5_dialog.get_node("FileNameContainer/FileNameLineEdit").text + file_name += ".pxo" + var path = "user://".plus_file(file_name) + OpenSave.save_pxo_file(path, false) + + func _on_OpenSprite_popup_hide() -> void: if !opensprite_file_selected: _can_draw_true() diff --git a/src/Main.tscn b/src/Main.tscn index 953132ae8..00eef469f 100644 --- a/src/Main.tscn +++ b/src/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=19 format=2] +[gd_scene load_steps=20 format=2] [ext_resource path="res://assets/themes/dark/theme.tres" type="Theme" id=1] [ext_resource path="res://src/Main.gd" type="Script" id=2] @@ -7,6 +7,7 @@ [ext_resource path="res://src/UI/PatternsPopup.tscn" type="PackedScene" id=5] [ext_resource path="res://src/UI/BrushesPopup.tscn" type="PackedScene" id=6] [ext_resource path="res://src/UI/Dialogs/ResizeCanvas.tscn" type="PackedScene" id=7] +[ext_resource path="res://src/UI/Dialogs/SaveSpriteHTML5.tscn" type="PackedScene" id=8] [ext_resource path="res://src/UI/Dialogs/SaveSprite.tscn" type="PackedScene" id=11] [ext_resource path="res://src/UI/Dialogs/OpenSprite.tscn" type="PackedScene" id=12] [ext_resource path="res://src/UI/Dialogs/SplashDialog.tscn" type="PackedScene" id=27] @@ -54,6 +55,8 @@ __meta__ = { [node name="SaveSprite" parent="." instance=ExtResource( 11 )] +[node name="SaveSpriteHTML5" parent="." instance=ExtResource( 8 )] + [node name="ExportDialog" parent="." instance=ExtResource( 39 )] [node name="ScaleImage" parent="." instance=ExtResource( 31 )] @@ -120,6 +123,8 @@ visible = false [connection signal="popup_hide" from="OpenSprite" to="." method="_on_OpenSprite_popup_hide"] [connection signal="file_selected" from="SaveSprite" to="." method="_on_SaveSprite_file_selected"] [connection signal="popup_hide" from="SaveSprite" to="." method="_can_draw_true"] +[connection signal="confirmed" from="SaveSpriteHTML5" to="." method="_on_SaveSpriteHTML5_confirmed"] +[connection signal="popup_hide" from="SaveSpriteHTML5" to="." method="_can_draw_true"] [connection signal="popup_hide" from="ExportDialog" to="." method="_can_draw_true"] [connection signal="popup_hide" from="ScaleImage" to="." method="_can_draw_true"] [connection signal="popup_hide" from="ResizeCanvas" to="." method="_can_draw_true"] diff --git a/src/UI/Dialogs/ExportDialog.tscn b/src/UI/Dialogs/ExportDialog.tscn index 23625dbbf..a5194f25c 100644 --- a/src/UI/Dialogs/ExportDialog.tscn +++ b/src/UI/Dialogs/ExportDialog.tscn @@ -346,8 +346,8 @@ window_title = "Open a Directory" resizable = true mode = 2 access = 2 -current_dir = "C:/Users" -current_path = "C:/Users/" +current_dir = "/Users" +current_path = "/Users/" [node name="PathValidationAlert" type="AcceptDialog" parent="Popups"] margin_left = 8.0 diff --git a/src/UI/Dialogs/OpenSprite.tscn b/src/UI/Dialogs/OpenSprite.tscn index 62bd9e972..c6c513f3c 100644 --- a/src/UI/Dialogs/OpenSprite.tscn +++ b/src/UI/Dialogs/OpenSprite.tscn @@ -8,5 +8,5 @@ resizable = true mode = 0 access = 2 filters = PoolStringArray( "*.pxo ; Pixelorama Project", "*.png ; PNG Image", "*.bmp ; BMP Image", "*.hdr ; Radiance HDR Image", "*.jpg,*.jpeg ; JPEG Image", "*.svg ; SVG Image", "*.tga ; TGA Image", "*.webp ; WebP Image" ) -current_dir = "C:/Users" -current_path = "C:/Users/" +current_dir = "/Users" +current_path = "/Users/" diff --git a/src/UI/Dialogs/SaveSprite.tscn b/src/UI/Dialogs/SaveSprite.tscn index 3bdc19f6f..c2b6f8cb0 100644 --- a/src/UI/Dialogs/SaveSprite.tscn +++ b/src/UI/Dialogs/SaveSprite.tscn @@ -12,5 +12,5 @@ margin_bottom = 48.0 resizable = true access = 2 filters = PoolStringArray( "*.pxo ; Pixelorama Project" ) -current_dir = "C:/Users" -current_path = "C:/Users/" +current_dir = "/Users" +current_path = "/Users/" diff --git a/src/UI/Dialogs/SaveSpriteHTML5.tscn b/src/UI/Dialogs/SaveSpriteHTML5.tscn new file mode 100644 index 000000000..4491d26b9 --- /dev/null +++ b/src/UI/Dialogs/SaveSpriteHTML5.tscn @@ -0,0 +1,33 @@ +[gd_scene format=2] + +[node name="SaveSpriteHTML5" type="ConfirmationDialog"] +visible = true +margin_right = 200.0 +margin_bottom = 70.0 + +[node name="FileNameContainer" type="HBoxContainer" parent="."] +anchor_right = 1.0 +margin_bottom = 41.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="FileNameLabel" type="Label" parent="FileNameContainer"] +margin_top = 13.0 +margin_right = 67.0 +margin_bottom = 27.0 +text = "File name:" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="FileNameLineEdit" type="LineEdit" parent="FileNameContainer"] +margin_left = 71.0 +margin_top = 8.0 +margin_right = 200.0 +margin_bottom = 32.0 +size_flags_horizontal = 3 +size_flags_vertical = 4 +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/src/UI/TopMenuContainer.gd b/src/UI/TopMenuContainer.gd index 39e8fe148..15120b069 100644 --- a/src/UI/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer.gd @@ -162,16 +162,23 @@ func on_open_last_project_file_menu_option_pressed() -> void: func save_project_file() -> void: Global.control.is_quitting_on_save = false - if OpenSave.current_save_paths[Global.current_project_index] == "": - Global.save_sprites_dialog.popup_centered() + var path = OpenSave.current_save_paths[Global.current_project_index] + if path == "": + if OS.get_name() == "HTML5": + Global.save_sprites_html5_dialog.popup_centered() + else: + Global.save_sprites_dialog.popup_centered() Global.dialog_open(true) else: - Global.control._on_SaveSprite_file_selected(OpenSave.current_save_paths[Global.current_project_index]) + Global.control._on_SaveSprite_file_selected(path) func save_project_file_as() -> void: Global.control.is_quitting_on_save = false - Global.save_sprites_dialog.popup_centered() + if OS.get_name() == "HTML5": + Global.save_sprites_html5_dialog.popup_centered() + else: + Global.save_sprites_dialog.popup_centered() Global.dialog_open(true)