From 43ee35431eb2074fc81f11bd8394a0dbe43985ff Mon Sep 17 00:00:00 2001 From: Yuri Roubinski Date: Mon, 5 Aug 2019 17:13:02 +0300 Subject: [PATCH] Fix opening of sub-resource properties in visual shaders --- .../plugins/visual_shader_editor_plugin.cpp | 51 ++++++++++++++++--- editor/plugins/visual_shader_editor_plugin.h | 4 +- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 4f7b9f9815..c956409966 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -42,17 +42,17 @@ #include "scene/main/viewport.h" #include "scene/resources/visual_shader_nodes.h" -Control *VisualShaderNodePlugin::create_editor(const Ref &p_node) { +Control *VisualShaderNodePlugin::create_editor(const Ref &p_parent_resource, const Ref &p_node) { if (get_script_instance()) { - return get_script_instance()->call("create_editor", p_node); + return get_script_instance()->call("create_editor", p_parent_resource, p_node); } return NULL; } void VisualShaderNodePlugin::_bind_methods() { - BIND_VMETHOD(MethodInfo(Variant::OBJECT, "create_editor", PropertyInfo(Variant::OBJECT, "for_node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode"))); + BIND_VMETHOD(MethodInfo(Variant::OBJECT, "create_editor", PropertyInfo(Variant::OBJECT, "parent_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::OBJECT, "for_node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode"))); } /////////////////// @@ -462,7 +462,7 @@ void VisualShaderEditor::_update_graph() { } for (int i = 0; i < plugins.size(); i++) { - custom_editor = plugins.write[i]->create_editor(vsnode); + custom_editor = plugins.write[i]->create_editor(visual_shader, vsnode); if (custom_editor) { break; } @@ -2590,6 +2590,7 @@ public: class VisualShaderNodePluginDefaultEditor : public VBoxContainer { GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer); + Ref parent_resource; public: void _property_changed(const String &prop, const Variant &p_value, const String &p_field, bool p_changing = false) { @@ -2603,7 +2604,27 @@ public: undo_redo->create_action(TTR("Edit Visual Property") + ": " + prop, UndoRedo::MERGE_ENDS); undo_redo->add_do_property(node.ptr(), prop, p_value); undo_redo->add_undo_property(node.ptr(), prop, node->get(prop)); + + if (p_value.get_type() == Variant::OBJECT) { + + RES prev_res = node->get(prop); + RES curr_res = p_value; + + if (curr_res.is_null()) { + undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr()); + } else { + undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr()); + } + if (!prev_res.is_null()) { + undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr()); + } else { + undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr()); + } + undo_redo->add_do_method(this, "_refresh_request"); + undo_redo->add_undo_method(this, "_refresh_request"); + } undo_redo->commit_action(); + updating = false; } @@ -2619,11 +2640,20 @@ public: VisualShaderEditor::get_singleton()->call_deferred("_update_graph"); } + void _resource_selected(const String &p_path, RES p_resource) { + _open_inspector(p_resource); + } + + void _open_inspector(RES p_resource) { + EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr()); + } + bool updating; Ref node; Vector properties; - void setup(Vector p_properties, const Vector &p_names, Ref p_node) { + void setup(Ref p_parent_resource, Vector p_properties, const Vector &p_names, Ref p_node) { + parent_resource = p_parent_resource; updating = false; node = p_node; properties = p_properties; @@ -2632,6 +2662,11 @@ public: add_child(p_properties[i]); + bool res_prop = Object::cast_to(p_properties[i]); + if (res_prop) { + p_properties[i]->connect("resource_selected", this, "_resource_selected"); + } + properties[i]->connect("property_changed", this, "_property_changed"); properties[i]->set_object_and_property(node.ptr(), p_names[i]); properties[i]->update_property(); @@ -2645,10 +2680,12 @@ public: ClassDB::bind_method("_property_changed", &VisualShaderNodePluginDefaultEditor::_property_changed, DEFVAL(String()), DEFVAL(false)); ClassDB::bind_method("_node_changed", &VisualShaderNodePluginDefaultEditor::_node_changed); ClassDB::bind_method("_refresh_request", &VisualShaderNodePluginDefaultEditor::_refresh_request); + ClassDB::bind_method("_resource_selected", &VisualShaderNodePluginDefaultEditor::_resource_selected); + ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector); } }; -Control *VisualShaderNodePluginDefault::create_editor(const Ref &p_node) { +Control *VisualShaderNodePluginDefault::create_editor(const Ref &p_parent_resource, const Ref &p_node) { if (p_node->is_class("VisualShaderNodeInput")) { //create input @@ -2706,7 +2743,7 @@ Control *VisualShaderNodePluginDefault::create_editor(const Refsetup(editors, properties, p_node); + editor->setup(p_parent_resource, editors, properties, p_node); return editor; } diff --git a/editor/plugins/visual_shader_editor_plugin.h b/editor/plugins/visual_shader_editor_plugin.h index d396243bf3..315ef1788e 100644 --- a/editor/plugins/visual_shader_editor_plugin.h +++ b/editor/plugins/visual_shader_editor_plugin.h @@ -48,7 +48,7 @@ protected: static void _bind_methods(); public: - virtual Control *create_editor(const Ref &p_node); + virtual Control *create_editor(const Ref &p_parent_resource, const Ref &p_node); }; class VisualShaderEditor : public VBoxContainer { @@ -272,7 +272,7 @@ class VisualShaderNodePluginDefault : public VisualShaderNodePlugin { GDCLASS(VisualShaderNodePluginDefault, VisualShaderNodePlugin); public: - virtual Control *create_editor(const Ref &p_node); + virtual Control *create_editor(const Ref &p_parent_resource, const Ref &p_node); }; class EditorPropertyShaderMode : public EditorProperty {