From ed1b5168f99a85cf855260f49bd0245a7756fcd8 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Thu, 21 Dec 2023 21:48:11 +0200 Subject: [PATCH] Exporting palettes to png files is now possible --- CHANGELOG.md | 3 +++ src/Palette/EditPaletteDialog.gd | 12 +++++++++++- src/Palette/EditPaletteDialog.tscn | 15 +++++++++++++-- src/Palette/Palette.gd | 11 +++++++++++ src/Palette/PalettePanel.gd | 8 ++++++++ src/Palette/PalettePanel.tscn | 1 + 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a8155689..113318257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ Fayez Akhtar ([@Variable-ind](https://github.com/Variable-ind)) Built using Godot 3.5.2 +### Added +- Exporting palettes to png files is now possible. + ### Changed - When quitting, multiple save confirmation dialogs now appear, each for every project that has changes. - Loop through frames when clicking on go to previous/next frame buttons on the timeline. diff --git a/src/Palette/EditPaletteDialog.gd b/src/Palette/EditPaletteDialog.gd index c652b4c76..8577a4edd 100644 --- a/src/Palette/EditPaletteDialog.gd +++ b/src/Palette/EditPaletteDialog.gd @@ -3,7 +3,9 @@ extends ConfirmationDialog # Emitted when user confirms their changes signal saved(name, comment, width, height) signal deleted +signal exported(path) +const EXPORT_ACTION := "export" const DELETE_ACTION := "delete" const BIN_ACTION := "trash" @@ -22,11 +24,13 @@ onready var path_input := $VBoxContainer/PaletteMetadata/Path onready var size_reduced_warning := $VBoxContainer/SizeReducedWarning onready var already_exists_warning := $VBoxContainer/AlreadyExistsWarning onready var delete_confirmation := $DeleteConfirmation +onready var export_file_dialog: FileDialog = $ExportFileDialog func _ready() -> void: - # Add delete button to edit palette dialog + # Add delete and export buttons to edit palette dialog add_button(tr("Delete"), false, DELETE_ACTION) + add_button(tr("Export"), false, EXPORT_ACTION) delete_confirmation.get_ok().text = tr("Delete Permanently") delete_confirmation.add_button(tr("Move to Trash"), false, BIN_ACTION) @@ -83,6 +87,8 @@ func _on_EditPaletteDialog_confirmed() -> void: func _on_EditPaletteDialog_custom_action(action: String) -> void: if action == DELETE_ACTION: delete_confirmation.popup_centered() + elif action == EXPORT_ACTION: + export_file_dialog.popup_centered() func _on_delete_confirmation_confirmed() -> void: @@ -117,3 +123,7 @@ func _on_Name_text_changed(new_name): # Disable ok button on empty name if new_name == "": get_ok().disabled = true + + +func _on_ExportFileDialog_file_selected(path: String) -> void: + emit_signal("exported", path) diff --git a/src/Palette/EditPaletteDialog.tscn b/src/Palette/EditPaletteDialog.tscn index 03538a37d..ff1b1ba10 100644 --- a/src/Palette/EditPaletteDialog.tscn +++ b/src/Palette/EditPaletteDialog.tscn @@ -140,8 +140,10 @@ __meta__ = { } [node name="DeleteConfirmation" type="ConfirmationDialog" parent="."] -margin_right = 170.0 -margin_bottom = 60.0 +margin_left = 8.0 +margin_top = 8.0 +margin_right = 551.0 +margin_bottom = 499.0 rect_min_size = Vector2( 170, 59.5 ) [node name="Label2" type="Label" parent="DeleteConfirmation"] @@ -155,6 +157,14 @@ text = "Delete Palette?" align = 1 valign = 1 +[node name="ExportFileDialog" type="FileDialog" parent="."] +margin_left = 8.0 +margin_top = 8.0 +margin_right = 551.0 +margin_bottom = 499.0 +access = 2 +filters = PoolStringArray( "*.png ; PNG Image" ) + [connection signal="confirmed" from="." to="." method="_on_EditPaletteDialog_confirmed"] [connection signal="custom_action" from="." to="." method="_on_EditPaletteDialog_custom_action"] [connection signal="popup_hide" from="." to="." method="_on_EditPaletteDialog_popup_hide"] @@ -163,3 +173,4 @@ valign = 1 [connection signal="value_changed" from="VBoxContainer/PaletteMetadata/Height" to="." method="_on_size_value_changed"] [connection signal="confirmed" from="DeleteConfirmation" to="." method="_on_delete_confirmation_confirmed"] [connection signal="custom_action" from="DeleteConfirmation" to="." method="_on_delete_confirmation_custom_action"] +[connection signal="file_selected" from="ExportFileDialog" to="." method="_on_ExportFileDialog_file_selected"] diff --git a/src/Palette/Palette.gd b/src/Palette/Palette.gd index 389ce6dce..78d2a74cc 100644 --- a/src/Palette/Palette.gd +++ b/src/Palette/Palette.gd @@ -205,3 +205,14 @@ static func strip_unvalid_characters(string_to_strip: String) -> String: var regex := RegEx.new() regex.compile("[^a-zA-Z0-9_]+") return regex.sub(string_to_strip, "", true) + + +func convert_to_image() -> Image: + var image := Image.new() + image.create(colors_max, 1, false, Image.FORMAT_RGBA8) + image.lock() + for i in colors_max: + if colors.has(i): + image.set_pixel(i, 0, colors[i].color) + image.unlock() + return image diff --git a/src/Palette/PalettePanel.gd b/src/Palette/PalettePanel.gd index f085d4bf3..5762e3e1d 100644 --- a/src/Palette/PalettePanel.gd +++ b/src/Palette/PalettePanel.gd @@ -223,3 +223,11 @@ func _color_changed(_color: Color, button: int) -> void: Palettes.right_selected_color = -1 palette_grid.unselect_swatch(button, swatch_to_unselect) + + +func _on_EditPaletteDialog_exported(path: String) -> void: + var extension := path.get_extension() + match extension: + "png": + var image: Image = Palettes.current_palette.convert_to_image() + image.save_png(path) diff --git a/src/Palette/PalettePanel.tscn b/src/Palette/PalettePanel.tscn index 8e8bed3b5..65835f3b1 100644 --- a/src/Palette/PalettePanel.tscn +++ b/src/Palette/PalettePanel.tscn @@ -221,6 +221,7 @@ margin_bottom = 111.0 [connection signal="value_changed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/HScrollBar" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_HSlider_value_changed"] [connection signal="value_changed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/MarginContainer/VScrollBar" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_VSlider_value_changed"] [connection signal="deleted" from="EditPaletteDialog" to="." method="_on_EditPaletteDialog_deleted"] +[connection signal="exported" from="EditPaletteDialog" to="." method="_on_EditPaletteDialog_exported"] [connection signal="saved" from="EditPaletteDialog" to="." method="_on_EditPaletteDialog_saved"] [connection signal="saved" from="CreatePaletteDialog" to="." method="_on_CreatePaletteDialog_saved"] [connection signal="color_changed" from="HiddenColorPickerButton" to="." method="_on_ColorPicker_color_changed"]