From 67631ac64876c12e1ecc8d492aa6e6acb5d161f3 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 28 Dec 2019 20:30:33 +0200 Subject: [PATCH] Changed Export PNG settings, moved code from Main.gd to ExportSprites.gd And made ExportSprites.tscn a scene of its own --- .gitignore | 5 +- Main.tscn | 41 ++++------- Prefabs/Dialogs/ExportSprites.tscn | 28 ++++++++ Prefabs/Dialogs/ImportSprites.tscn | 13 ++-- Scripts/Dialogs/ExportSprites.gd | 104 +++++++++++++++++++++++++++ Scripts/Dialogs/ImportSprites.gd | 6 +- Scripts/Main.gd | 109 ++--------------------------- 7 files changed, 162 insertions(+), 144 deletions(-) create mode 100644 Prefabs/Dialogs/ExportSprites.tscn create mode 100644 Scripts/Dialogs/ExportSprites.gd diff --git a/.gitignore b/.gitignore index 7ed288076..04a6a11c2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,5 @@ .import/ export_presets.cfg -# Imported translations -# Godot imports them automatically when editing the project for the first time. -*.translation - +Translations/Update Translations.bat Scripts/Old/ diff --git a/Main.tscn b/Main.tscn index 951fe610d..55f793a91 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=89 format=2] +[gd_scene load_steps=90 format=2] [ext_resource path="res://Themes & Styles/Dark Theme/Dark Theme.tres" type="Theme" id=1] [ext_resource path="res://Scripts/Main.gd" type="Script" id=2] @@ -61,11 +61,12 @@ [ext_resource path="res://Assets/Graphics/Canvas_split.png" type="Texture" id=59] [ext_resource path="res://Assets/Graphics/Canvas_unsplit.png" type="Texture" id=60] [ext_resource path="res://Prefabs/Dialogs/ImportSprites.tscn" type="PackedScene" id=61] -[ext_resource path="res://Prefabs/Dialogs/PreferencesDialog.tscn" type="PackedScene" id=62] -[ext_resource path="res://Scripts/Dialogs/AboutDialog.gd" type="Script" id=63] -[ext_resource path="res://Prefabs/EditPalettePopup.tscn" type="PackedScene" id=64] -[ext_resource path="res://Prefabs/NewPaletteDialog.tscn" type="PackedScene" id=65] -[ext_resource path="res://Prefabs/PaletteImportFileDialog.tscn" type="PackedScene" id=66] +[ext_resource path="res://Prefabs/Dialogs/ExportSprites.tscn" type="PackedScene" id=62] +[ext_resource path="res://Prefabs/Dialogs/PreferencesDialog.tscn" type="PackedScene" id=63] +[ext_resource path="res://Scripts/Dialogs/AboutDialog.gd" type="Script" id=64] +[ext_resource path="res://Prefabs/EditPalettePopup.tscn" type="PackedScene" id=65] +[ext_resource path="res://Prefabs/NewPaletteDialog.tscn" type="PackedScene" id=66] +[ext_resource path="res://Prefabs/PaletteImportFileDialog.tscn" type="PackedScene" id=67] [sub_resource type="InputEventKey" id=1] scancode = 88 @@ -778,6 +779,7 @@ size_flags_vertical = 1 min_value = 1.0 value = 1.0 allow_greater = true +ticks_on_borders = true [node name="RightBrushSizeEdit" type="SpinBox" parent="MenuAndUI/UI/ToolPanel/Tools/ColorAndToolOptions/RightToolOptions/RightBrushSize"] margin_left = 154.0 @@ -1732,21 +1734,7 @@ current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρι [node name="ImportSprites" parent="." instance=ExtResource( 61 )] -[node name="ExportSprites" type="FileDialog" parent="."] -anchor_left = 0.5 -anchor_top = 0.5 -anchor_right = 0.5 -anchor_bottom = 0.5 -margin_left = -512.0 -margin_top = -300.0 -margin_right = 3.0 -margin_bottom = 48.0 -window_title = "Export Sprite" -resizable = true -access = 2 -filters = PoolStringArray( "*.png ; PNG Image" ) -current_dir = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama" -current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama/" +[node name="ExportSprites" parent="." instance=ExtResource( 62 )] [node name="ScaleImage" type="ConfirmationDialog" parent="."] editor/display_folded = true @@ -1817,7 +1805,7 @@ text = "Nearest" items = [ "Nearest", null, false, 0, null, "Bilinear", null, false, 1, null, "Cubic", null, false, 2, null, "Trilinear", null, false, 3, null, "Lanczos", null, true, 4, null ] selected = 0 -[node name="PreferencesDialog" parent="." instance=ExtResource( 62 )] +[node name="PreferencesDialog" parent="." instance=ExtResource( 63 )] [node name="OutlineDialog" type="ConfirmationDialog" parent="."] editor/display_folded = true @@ -1877,7 +1865,7 @@ editor/display_folded = true margin_right = 284.0 margin_bottom = 186.0 window_title = "About Pixelorama" -script = ExtResource( 63 ) +script = ExtResource( 64 ) [node name="AboutUI" type="VBoxContainer" parent="AboutDialog"] margin_left = 8.0 @@ -1948,12 +1936,12 @@ margin_bottom = 60.0 window_title = "Error!" dialog_text = "This is an error message!" -[node name="EditPalettePopup" parent="." instance=ExtResource( 64 )] +[node name="EditPalettePopup" parent="." instance=ExtResource( 65 )] visible = false -[node name="NewPaletteDialog" parent="." instance=ExtResource( 65 )] +[node name="NewPaletteDialog" parent="." instance=ExtResource( 66 )] -[node name="PaletteImportFileDialog" parent="." instance=ExtResource( 66 )] +[node name="PaletteImportFileDialog" parent="." instance=ExtResource( 67 )] filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library" ) current_dir = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama" current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama/" @@ -2036,7 +2024,6 @@ visible = false [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="popup_hide" from="ImportSprites" to="." method="_can_draw_true"] -[connection signal="file_selected" from="ExportSprites" to="." method="_on_ExportSprites_file_selected"] [connection signal="popup_hide" from="ExportSprites" to="." method="_can_draw_true"] [connection signal="confirmed" from="ScaleImage" to="." method="_on_ScaleImage_confirmed"] [connection signal="popup_hide" from="ScaleImage" to="." method="_can_draw_true"] diff --git a/Prefabs/Dialogs/ExportSprites.tscn b/Prefabs/Dialogs/ExportSprites.tscn new file mode 100644 index 000000000..ad9fbc766 --- /dev/null +++ b/Prefabs/Dialogs/ExportSprites.tscn @@ -0,0 +1,28 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Scripts/Dialogs/ExportSprites.gd" type="Script" id=1] + +[node name="ExportSprites" type="FileDialog"] +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +margin_left = -512.0 +margin_top = -300.0 +margin_right = 3.0 +margin_bottom = 48.0 +resizable = true +access = 2 +filters = PoolStringArray( "*.png ; PNG Image" ) +current_dir = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama" +current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama/" +script = ExtResource( 1 ) + +[node name="ExportOption" type="OptionButton" parent="."] +margin_right = 41.0 +margin_bottom = 20.0 +text = "Export current frame" +items = [ "Export current frame", null, false, 0, null, "Export all frames as multiple files", null, false, 1, null, "Export all frames as a horizontal spritesheet (single file)", null, false, 2, null, "Export all frames as a vertical spritesheet (single file)", null, false, 3, null ] +selected = 0 +[connection signal="file_selected" from="." to="." method="_on_ExportSprites_file_selected"] +[connection signal="item_selected" from="ExportOption" to="." method="_on_ExportOption_item_selected"] diff --git a/Prefabs/Dialogs/ImportSprites.tscn b/Prefabs/Dialogs/ImportSprites.tscn index 4a6c19f99..5b580a4e7 100644 --- a/Prefabs/Dialogs/ImportSprites.tscn +++ b/Prefabs/Dialogs/ImportSprites.tscn @@ -3,7 +3,6 @@ [ext_resource path="res://Scripts/Dialogs/ImportSprites.gd" type="Script" id=1] [node name="ImportSprites" type="FileDialog"] -editor/display_folded = true margin_right = 515.0 margin_bottom = 348.0 window_title = "Open File(s)" @@ -16,8 +15,10 @@ current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρι script = ExtResource( 1 ) [node name="HBoxContainer2" type="HBoxContainer" parent="."] -margin_right = 40.0 -margin_bottom = 40.0 +margin_left = 8.0 +margin_top = 8.0 +margin_right = 507.0 +margin_bottom = 312.0 [node name="ImportAsNewFrame" type="CheckBox" parent="HBoxContainer2"] margin_right = 130.0 @@ -32,8 +33,10 @@ text = "Import as spritesheet" [node name="Spritesheet" type="HBoxContainer" parent="."] visible = false -margin_right = 306.0 -margin_bottom = 17.0 +margin_left = 8.0 +margin_top = 8.0 +margin_right = 507.0 +margin_bottom = 312.0 [node name="Label" type="Label" parent="Spritesheet"] margin_top = 1.0 diff --git a/Scripts/Dialogs/ExportSprites.gd b/Scripts/Dialogs/ExportSprites.gd new file mode 100644 index 000000000..e7178401e --- /dev/null +++ b/Scripts/Dialogs/ExportSprites.gd @@ -0,0 +1,104 @@ +extends FileDialog + +var current_export_path := "" +var export_option := 0 + +func _ready() -> void: + var children := [] + for i in range(get_child_count()): + if i > 7: + children.append(get_child(i)) + + for child in children: + remove_child(child) + get_vbox().add_child(child) + +func _on_ExportOption_item_selected(ID : int) -> void: + export_option = ID + +func _on_ExportSprites_file_selected(path : String) -> void: + current_export_path = path + Global.file_menu.get_popup().set_item_text(5, tr("Export") + " %s" % path.get_file()) + export_project() + +func export_project() -> void: + if export_option == 0: # Export current frame + save_sprite(Global.canvas, current_export_path) + elif export_option == 1: # Export all frames as multiple files + var i := 1 + for canvas in Global.canvases: + var path := "%s_%s" % [current_export_path, str(i)] + path = path.replace(".png", "") + path = "%s.png" % path + save_sprite(canvas, path) + i += 1 + else: # Export all frames as a spritesheet (single file) + save_spritesheet(export_option == 2) + + Global.notification_label("File exported") + +func save_sprite(canvas : Canvas, path : String) -> void: + var whole_image := Image.new() + whole_image.create(canvas.size.x, canvas.size.y, false, Image.FORMAT_RGBA8) + whole_image.lock() + for layer in canvas.layers: + var img : Image = layer[0] + img.lock() + if layer[4] < 1: # If we have layer transparency + for xx in img.get_size().x: + for yy in img.get_size().y: + var pixel_color := img.get_pixel(xx, yy) + var alpha : float = pixel_color.a * layer[4] + img.set_pixel(xx, yy, Color(pixel_color.r, pixel_color.g, pixel_color.b, alpha)) + + canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), Vector2.ZERO) + layer[0].lock() + var err = whole_image.save_png(path) + if err != OK: + OS.alert("Can't save file") + +func save_spritesheet(horizontal : bool) -> void: + var width + var height + if horizontal: # Horizontal spritesheet + width = 0 + height = Global.canvas.size.y + for canvas in Global.canvases: + width += canvas.size.x + if canvas.size.y > height: + height = canvas.size.y + else: # Vertical spritesheet + width = Global.canvas.size.x + height = 0 + for canvas in Global.canvases: + height += canvas.size.y + if canvas.size.x > width: + width = canvas.size.x + + var whole_image := Image.new() + whole_image.create(width, height, false, Image.FORMAT_RGBA8) + whole_image.lock() + var dst := Vector2.ZERO + for canvas in Global.canvases: + for layer in canvas.layers: + var img : Image = layer[0] + img.lock() + if layer[4] < 1: # If we have layer transparency + for xx in img.get_size().x: + for yy in img.get_size().y: + var pixel_color := img.get_pixel(xx, yy) + var alpha : float = pixel_color.a * layer[4] + img.set_pixel(xx, yy, Color(pixel_color.r, pixel_color.g, pixel_color.b, alpha)) + + canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), dst) + layer[0].lock() + + if horizontal: + dst += Vector2(canvas.size.x, 0) + else: + dst += Vector2(0, canvas.size.y) + + var err = whole_image.save_png(current_export_path) + if err != OK: + OS.alert("Can't save file") + diff --git a/Scripts/Dialogs/ImportSprites.gd b/Scripts/Dialogs/ImportSprites.gd index 012f26e09..f9585f6b5 100644 --- a/Scripts/Dialogs/ImportSprites.gd +++ b/Scripts/Dialogs/ImportSprites.gd @@ -5,7 +5,7 @@ var import_spritesheet := false var spritesheet_horizontal := 1 var spritesheet_vertical := 1 -func _ready(): +func _ready() -> void: var children := [] for i in range(get_child_count()): if i > 7: @@ -44,7 +44,7 @@ func _on_ImportSprites_files_selected(paths : PoolStringArray) -> void: for path in paths: var image := Image.new() var err := image.load(path) - if err != OK: #An error occured + if err != OK: # An error occured OS.alert("Can't load file") continue @@ -74,7 +74,7 @@ func _on_ImportSprites_files_selected(paths : PoolStringArray) -> void: else: var image := Image.new() var err := image.load(first_path) - if err != OK: #An error occured + if err != OK: # An error occured OS.alert("Can't load file") return diff --git a/Scripts/Main.gd b/Scripts/Main.gd index 3e6d2bc07..22aced27a 100644 --- a/Scripts/Main.gd +++ b/Scripts/Main.gd @@ -1,14 +1,11 @@ extends Control var current_save_path := "" -var current_export_path := "" var opensprite_file_selected := false var file_menu : PopupMenu var view_menu : PopupMenu var tools := [] -var export_all_frames : CheckBox -var export_as_single_file : CheckBox -var export_vertical_spritesheet : CheckBox + var redone := false var fps := 6.0 var animation_loop := 0 # 0 is no loop, 1 is cycle loop, 2 is ping-pong loop @@ -155,20 +152,6 @@ func _ready() -> void: Global.left_color_picker.get_picker().move_child(Global.left_color_picker.get_picker().get_child(0), 1) Global.right_color_picker.get_picker().move_child(Global.right_color_picker.get_picker().get_child(0), 1) - # Options for Export - var export_project_hbox := HBoxContainer.new() - export_all_frames = CheckBox.new() - export_all_frames.text = tr("EXPORT_ALLFRAMES_LABEL") - export_as_single_file = CheckBox.new() - export_as_single_file.text = tr("EXPORT_FRAMES_ASFILE_LABEL") - export_vertical_spritesheet = CheckBox.new() - export_vertical_spritesheet.text = tr("EXPORT_VERTICAL_SPRITESHEET_LABEL") - - $ExportSprites.get_vbox().add_child(export_project_hbox) - export_project_hbox.add_child(export_all_frames) - export_project_hbox.add_child(export_as_single_file) - export_project_hbox.add_child(export_vertical_spritesheet) - Import.import_brushes("Brushes") func _input(event : InputEvent) -> void: @@ -219,11 +202,11 @@ func file_menu_id_pressed(id : int) -> void: Global.can_draw = false opensprite_file_selected = false 5: # Export - if current_export_path == "": + if $ExportSprites.current_export_path == "": $ExportSprites.popup_centered() Global.can_draw = false else: - export_project() + $ExportSprites.export_project() 6: # Export as $ExportSprites.popup_centered() Global.can_draw = false @@ -535,93 +518,9 @@ func clear_canvases() -> void: child.queue_free() Global.canvases.clear() current_save_path = "" - current_export_path = "" + $ExportSprites.current_export_path = "" file_menu.set_item_text(5, "Export PNG...") -func _on_ExportSprites_file_selected(path : String) -> void: - current_export_path = path - file_menu.set_item_text(5, tr("Export") + " %s" % path.get_file()) - export_project() - -func export_project() -> void: - if export_all_frames.pressed: - if !export_as_single_file.pressed: - var i := 1 - for canvas in Global.canvases: - var path := "%s_%s" % [current_export_path, str(i)] - path = path.replace(".png", "") - path = "%s.png" % path - save_sprite(canvas, path) - i += 1 - else: - save_spritesheet() - else: - save_sprite(Global.canvas, current_export_path) - Global.notification_label("File exported") - -func save_sprite(canvas : Canvas, path : String) -> void: - var whole_image := Image.new() - whole_image.create(canvas.size.x, canvas.size.y, false, Image.FORMAT_RGBA8) - whole_image.lock() - for layer in canvas.layers: - var img : Image = layer[0] - img.lock() - if layer[4] < 1: # If we have layer transparency - for xx in img.get_size().x: - for yy in img.get_size().y: - var pixel_color := img.get_pixel(xx, yy) - var alpha : float = pixel_color.a * layer[4] - img.set_pixel(xx, yy, Color(pixel_color.r, pixel_color.g, pixel_color.b, alpha)) - - canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), Vector2.ZERO) - layer[0].lock() - var err = whole_image.save_png(path) - if err != OK: - OS.alert("Can't save file") - -func save_spritesheet() -> void: - var width - var height - if export_vertical_spritesheet.pressed: #Vertical spritesheet - width = Global.canvas.size.x - height = 0 - for canvas in Global.canvases: - height += canvas.size.y - if canvas.size.x > width: - width = canvas.size.x - else: #Horizontal spritesheet - width = 0 - height = Global.canvas.size.y - for canvas in Global.canvases: - width += canvas.size.x - if canvas.size.y > height: - height = canvas.size.y - var whole_image := Image.new() - whole_image.create(width, height, false, Image.FORMAT_RGBA8) - whole_image.lock() - var dst := Vector2.ZERO - for canvas in Global.canvases: - for layer in canvas.layers: - var img : Image = layer[0] - img.lock() - if layer[4] < 1: # If we have layer transparency - for xx in img.get_size().x: - for yy in img.get_size().y: - var pixel_color := img.get_pixel(xx, yy) - var alpha : float = pixel_color.a * layer[4] - img.set_pixel(xx, yy, Color(pixel_color.r, pixel_color.g, pixel_color.b, alpha)) - - canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), dst) - layer[0].lock() - - if export_vertical_spritesheet.pressed: - dst += Vector2(0, canvas.size.y) - else: - dst += Vector2(canvas.size.x, 0) - - var err = whole_image.save_png(current_export_path) - if err != OK: - OS.alert("Can't save file") func _on_ScaleImage_confirmed() -> void: var width : int = $ScaleImage/VBoxContainer/OptionsContainer/WidthValue.value