From 98ac4b9265e27004243e998b0f79a90c4762c805 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 20 Nov 2022 19:49:45 +0100 Subject: [PATCH] Implemented export and export as tools for the PaintProjectToolsPropertyInspector. --- ...paint_project_tools_property_inspector.cpp | 108 ++++++++++++++++-- .../paint_project_tools_property_inspector.h | 9 +- 2 files changed, 106 insertions(+), 11 deletions(-) diff --git a/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.cpp b/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.cpp index 65e64aaf0..45190eecd 100644 --- a/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.cpp +++ b/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.cpp @@ -25,12 +25,41 @@ SOFTWARE. #include "scene/gui/box_container.h" #include "scene/gui/button.h" #include "scene/gui/color_picker.h" +#include "scene/gui/file_dialog.h" #include "scene/gui/flow_container.h" #include "scene/gui/scroll_container.h" +#include "core/os/dir_access.h" + #include "../../nodes/paint_node.h" #include "../../nodes/paint_project.h" +#ifdef TOOLS_ENABLED +#include "core/config/engine.h" +#include "editor/editor_node.h" +#endif + +void PaintProjectToolsPropertyInspector::add_action_button(const String &callback, const String &hint, const String &icon, const String &theme_type) { + Button *button = memnew(Button); + + Ref icon_tex; + +#ifdef TOOLS_ENABLED + if (EditorNode::get_singleton() && Engine::get_singleton()->is_editor_hint()) { + icon_tex = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(icon, theme_type); + } else +#endif + { + icon_tex = get_theme_icon(icon, theme_type); + } + + button->set_icon(icon_tex); + button->set_tooltip(hint); + button->connect("pressed", this, callback); + + _button_contianer->add_child(button); +} + void PaintProjectToolsPropertyInspector::_on_paint_node_selected(Node *p_paint_node) { PaintNode *paint_node = Object::cast_to(p_paint_node); @@ -53,7 +82,6 @@ void PaintProjectToolsPropertyInspector::_on_paint_node_selected(Node *p_paint_n proj->connect("color_presets_changed", this, "_on_project_color_preset_changed"); _current_paint_project = proj->get_instance_id(); - } PaintProjectToolsPropertyInspector::PaintProjectToolsPropertyInspector() { @@ -62,6 +90,19 @@ PaintProjectToolsPropertyInspector::PaintProjectToolsPropertyInspector() { _ignore_preset_changed_event = false; _ignore_color_event = false; + Control *popups = memnew(Control); + add_child(popups); + popups->set_mouse_filter(MOUSE_FILTER_IGNORE); + + _export_as_file_dialog = memnew(FileDialog); + popups->add_child(_export_as_file_dialog); + _export_as_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM); + _export_as_file_dialog->set_resizable(true); + _export_as_file_dialog->set_mode(FileDialog::MODE_SAVE_FILE); + _export_as_file_dialog->set_title("Export Image"); + _export_as_file_dialog->add_filter("*.png"); + _export_as_file_dialog->connect("file_selected", this, "_on_export_as_dialog_file_selected"); + VBoxContainer *main_container = memnew(VBoxContainer); add_child(main_container); @@ -70,22 +111,69 @@ PaintProjectToolsPropertyInspector::PaintProjectToolsPropertyInspector() { //_button_contianer->set_h_size_flags(SIZE_EXPAND_FILL); _button_contianer->set_v_size_flags(SIZE_EXPAND_FILL); - //Button *button = memnew(Button); - //button->set_tooltip(""); - //button->connect("pressed", this, ""); - //_button_contianer->add_child(button); - + add_action_button("_on_export_pressed", "Export Image", "Save", "EditorIcons"); + add_action_button("_on_export_as_pressed", "Export Image As", "Duplicate", "EditorIcons"); } PaintProjectToolsPropertyInspector::~PaintProjectToolsPropertyInspector() { } -void PaintProjectToolsPropertyInspector::_notification(int p_what) { - if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) { - //_add_color_button->set_icon(get_theme_icon("Add", "EditorIcons")); +void PaintProjectToolsPropertyInspector::_on_export_pressed() { + PaintProject *paint_project = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); + + ERR_FAIL_COND(!paint_project); + + String fn = paint_project->get_save_file_name(); + + if (!fn.empty()) { + _on_export_as_dialog_file_selected(fn); + } else { + _on_export_as_pressed(); + } +} +void PaintProjectToolsPropertyInspector::_on_export_as_pressed() { + _export_as_file_dialog->popup_centered_ratio(); +} + +void PaintProjectToolsPropertyInspector::_on_export_as_dialog_file_selected(const String &f) { + PaintProject *paint_project = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); + + ERR_FAIL_COND(!paint_project); + + if (f.empty()) { + return; + } + + Ref img = paint_project->render_image(); + + ERR_FAIL_COND(!img.is_valid()); + + Error err = img->save_png(f); + + ERR_FAIL_COND_MSG(err != OK, "err != OK :" + itos(err)); + + String res_path = DirAccess::get_full_path("res://", DirAccess::ACCESS_FILESYSTEM); + String user_path = DirAccess::get_full_path("user://", DirAccess::ACCESS_FILESYSTEM); + + if (f.begins_with(res_path)) { + paint_project->set_save_file_name(f.replace_first(res_path, "res:/")); + } else if (f.begins_with(user_path)) { + paint_project->set_save_file_name(f.replace_first(res_path, "user:/")); + } else { + paint_project->set_save_file_name(f); } } +//void PaintProjectToolsPropertyInspector::_notification(int p_what) { +//if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) { +//_add_color_button->set_icon(get_theme_icon("Add", "EditorIcons")); +//} +//} + void PaintProjectToolsPropertyInspector::_bind_methods() { - //ClassDB::bind_method(D_METHOD("_on_grid_color_button_changed"), &PaintProjectToolsPropertyInspector::_on_grid_color_button_changed); + ClassDB::bind_method(D_METHOD("add_action_button", "callback", "hint", "icon", "theme_type"), &PaintProjectToolsPropertyInspector::add_action_button); + + ClassDB::bind_method(D_METHOD("_on_export_pressed"), &PaintProjectToolsPropertyInspector::_on_export_pressed); + ClassDB::bind_method(D_METHOD("_on_export_as_pressed"), &PaintProjectToolsPropertyInspector::_on_export_as_pressed); + ClassDB::bind_method(D_METHOD("_on_export_as_dialog_file_selected"), &PaintProjectToolsPropertyInspector::_on_export_as_dialog_file_selected); } diff --git a/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.h b/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.h index c90dad3ef..f9115c1e7 100644 --- a/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.h +++ b/modules/paint/ui/property_inspectors/paint_project_tools_property_inspector.h @@ -33,11 +33,13 @@ class PaintProject; class HFlowContainer; class ColorSelectorButton; class Button; +class FileDialog; class PaintProjectToolsPropertyInspector : public PaintCustomPropertyInspector { GDCLASS(PaintProjectToolsPropertyInspector, PaintCustomPropertyInspector); public: + void add_action_button(const String &callback, const String &hint, const String &icon, const String &theme_type); void _on_paint_node_selected(Node *paint_node); @@ -45,11 +47,16 @@ public: ~PaintProjectToolsPropertyInspector(); protected: + void _on_export_pressed(); + void _on_export_as_pressed(); + void _on_export_as_dialog_file_selected(const String &f); - void _notification(int p_what); + //void _notification(int p_what); static void _bind_methods(); + FileDialog *_export_as_file_dialog; + HFlowContainer *_button_contianer; ObjectID _current_paint_node;