From c683e675e16c8af2f20c2ec749ce6ddf68b49368 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 16 Nov 2022 15:25:27 +0100 Subject: [PATCH] Now PaintNodes can handle input events coming from the editor plugin. --- modules/paint/nodes/paint_node.cpp | 11 +++++++++++ modules/paint/nodes/paint_node.h | 3 +++ modules/paint/paint_editor_plugin.cpp | 28 +++++++++++++++++++++++++-- modules/paint/paint_editor_plugin.h | 4 ++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/modules/paint/nodes/paint_node.cpp b/modules/paint/nodes/paint_node.cpp index 4e8748baf..9bce07bec 100644 --- a/modules/paint/nodes/paint_node.cpp +++ b/modules/paint/nodes/paint_node.cpp @@ -34,6 +34,13 @@ Color PaintNode::util_random_color_alt() { return PaintUtilities::random_color_alt(); } +bool PaintNode::forward_canvas_gui_input(const Ref &p_event) { + return call("_forward_canvas_gui_input", p_event); +} +bool PaintNode::_forward_canvas_gui_input(const Ref &p_event) { + return false; +} + PaintProject *PaintNode::get_paint_project() { PaintNode *p = this; @@ -87,5 +94,9 @@ void PaintNode::_bind_methods() { ClassDB::bind_method(D_METHOD("util_random_color"), &PaintNode::util_random_color); ClassDB::bind_method(D_METHOD("util_random_color_alt"), &PaintNode::util_random_color_alt); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_forward_canvas_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); + ClassDB::bind_method(D_METHOD("forward_canvas_gui_input", "event"), &PaintNode::forward_canvas_gui_input); + ClassDB::bind_method(D_METHOD("_forward_canvas_gui_input", "event"), &PaintNode::_forward_canvas_gui_input); + ClassDB::bind_method(D_METHOD("get_paint_project"), &PaintNode::get_paint_project); } diff --git a/modules/paint/nodes/paint_node.h b/modules/paint/nodes/paint_node.h index cd21cf77d..7391883c4 100644 --- a/modules/paint/nodes/paint_node.h +++ b/modules/paint/nodes/paint_node.h @@ -24,6 +24,9 @@ public: Color util_random_color(); Color util_random_color_alt(); + bool forward_canvas_gui_input(const Ref &p_event); + bool _forward_canvas_gui_input(const Ref &p_event); + PaintProject *get_paint_project(); String get_configuration_warning() const; diff --git a/modules/paint/paint_editor_plugin.cpp b/modules/paint/paint_editor_plugin.cpp index ff5fd92f1..1db054eef 100644 --- a/modules/paint/paint_editor_plugin.cpp +++ b/modules/paint/paint_editor_plugin.cpp @@ -44,7 +44,9 @@ String PaintEditorPlugin::get_name() const { } void PaintEditorPlugin::edit(Object *p_object) { - _sidebar->on_paint_node_selected(Object::cast_to(p_object)); + _active_node = Object::cast_to(p_object); + + _sidebar->on_paint_node_selected(_active_node); } bool PaintEditorPlugin::handles(Object *p_object) const { return p_object->is_class("PaintNode"); @@ -53,7 +55,16 @@ void PaintEditorPlugin::edited_scene_changed() { } bool PaintEditorPlugin::forward_canvas_gui_input(const Ref &p_event) { - return false; + if (!_active_node) { + return false; + } + + if (!ObjectDB::instance_validate(_active_node)) { + _active_node = NULL; + return false; + } + + return _active_node->forward_canvas_gui_input(p_event); } void PaintEditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) { } @@ -61,6 +72,8 @@ void PaintEditorPlugin::forward_canvas_force_draw_over_viewport(Control *p_overl } PaintEditorPlugin::PaintEditorPlugin(EditorNode *p_node) { + _active_node = NULL; + editor = p_node; EDITOR_DEF("editors/paint/editor_side", 0); @@ -88,6 +101,12 @@ PaintEditorPlugin::~PaintEditorPlugin() { Engine::get_singleton()->remove_global("PaintEditorPlugin"); } +void PaintEditorPlugin::on_node_removed(Node *node) { + if (_active_node == node) { + _active_node = NULL; + } +} + void PaintEditorPlugin::_notification(int p_what) { if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { switch ((int)EditorSettings::get_singleton()->get("editors/paint/editor_side")) { @@ -98,9 +117,14 @@ void PaintEditorPlugin::_notification(int p_what) { CanvasItemEditor::get_singleton()->move_control_to_right_panel(_sidebar); } break; } + } else if (p_what == NOTIFICATION_READY) { + if (!get_tree()->is_connected("node_removed", this, "on_node_removed")) { + get_tree()->connect("node_removed", this, "on_node_removed"); + } } } void PaintEditorPlugin::_bind_methods() { ClassDB::bind_method(D_METHOD("get_sidebar"), &PaintEditorPlugin::get_sidebar); + ClassDB::bind_method(D_METHOD("on_node_removed", "node"), &PaintEditorPlugin::on_node_removed); } diff --git a/modules/paint/paint_editor_plugin.h b/modules/paint/paint_editor_plugin.h index 7ae58f3bb..b15873be8 100644 --- a/modules/paint/paint_editor_plugin.h +++ b/modules/paint/paint_editor_plugin.h @@ -29,6 +29,7 @@ SOFTWARE. class PaintWindow; class Texture; class PaintSidebar; +class PaintNode; class PaintEditorPlugin : public EditorPlugin { GDCLASS(PaintEditorPlugin, EditorPlugin); @@ -53,11 +54,14 @@ public: EditorNode *editor; protected: + void on_node_removed(Node *node); + void _notification(int p_what); static void _bind_methods(); PaintSidebar *_sidebar; + PaintNode *_active_node; }; #endif