From e38dfb7c405567beac466749dd5b3c57c7f5a944 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 20 Aug 2022 20:24:40 +0200 Subject: [PATCH] Set up WebEditorPlugin to hide / show the web editor tab in a way that feels right. The tab itself is empty though at the moment. --- modules/web/editor/web_editor_plugin.cpp | 97 +++++++++++++++++++++--- modules/web/editor/web_editor_plugin.h | 9 ++- 2 files changed, 96 insertions(+), 10 deletions(-) diff --git a/modules/web/editor/web_editor_plugin.cpp b/modules/web/editor/web_editor_plugin.cpp index 08da808ab..03490a870 100644 --- a/modules/web/editor/web_editor_plugin.cpp +++ b/modules/web/editor/web_editor_plugin.cpp @@ -27,7 +27,14 @@ SOFTWARE. #include "editor/editor_node.h" -void WebEditorPlugin::make_visible(const bool visible) { +void WebEditorPlugin::make_visible(bool visible) { + if (visible) { + if (is_inside_tree()) { + _scene_has_webnode = true; + editor->editor_set_visible_by_name("Web", true); + } + } + window->set_visible(visible); } @@ -44,26 +51,94 @@ bool WebEditorPlugin::has_main_screen() const { } void WebEditorPlugin::edit(Object *p_object) { - //WebNode *wn = Object::cast_to(p_object); + WebNode *wn = Object::cast_to(p_object); - //if (wn) { - // window->set_node(wn); - //} + if (wn) { + // window->set_node(wn); + + get_editor_interface()->set_main_screen_editor("Web"); + } } bool WebEditorPlugin::handles(Object *p_object) const { return p_object->is_class("WebNode"); } +void WebEditorPlugin::edited_scene_changed() { + Node *edited_scene = editor->get_edited_scene(); + + _scene_has_webnode = scene_has_webnode(edited_scene); + editor->editor_set_visible_by_name("Web", _scene_has_webnode); +} + +bool WebEditorPlugin::scene_has_webnode(Node *p_node) { + if (!p_node) { + return false; + } + + if (Object::cast_to(p_node)) { + return true; + } + + for (int i = 0; i < p_node->get_child_count(); ++i) { + Node *n = p_node->get_child(i); + + if (scene_has_webnode(n)) { + return true; + } + } + + return false; +} + +bool WebEditorPlugin::scene_has_webnode_skip(Node *p_node, Node *skip) { + if (!p_node) { + return false; + } + + if (p_node == skip) { + return false; + } + + if (Object::cast_to(p_node)) { + return true; + } + + for (int i = 0; i < p_node->get_child_count(); ++i) { + Node *n = p_node->get_child(i); + + if (scene_has_webnode_skip(n, skip)) { + return true; + } + } + + return false; +} + +void WebEditorPlugin::on_node_removed(Node *p_node) { + if (!_scene_has_webnode) { + return; + } + + if (Object::cast_to(p_node)) { + if (!scene_has_webnode_skip(editor->get_edited_scene(), p_node)) { + editor->editor_set_visible_by_name("Web", false); + _scene_has_webnode = false; + } + } +} + WebEditorPlugin::WebEditorPlugin(EditorNode *p_node) { editor = p_node; window = memnew(WebEditor); - get_editor_interface()->get_editor_viewport()->add_child(window); + editor->get_viewport()->add_child(window); _icon = get_editor_interface()->get_base_control()->get_theme_icon("WebNodeEditor", "EditorIcons"); make_visible(false); + + _scene_has_webnode = false; } WebEditorPlugin::~WebEditorPlugin() { @@ -71,12 +146,16 @@ WebEditorPlugin::~WebEditorPlugin() { void WebEditorPlugin::_notification(int p_what) { switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - get_editor_interface()->set_main_screen_editor_tab_button_visible("Web", false); + case NOTIFICATION_POST_ENTER_TREE: { + editor->editor_set_visible_by_name("Web", false); + get_tree()->connect("node_removed", this, "on_node_removed"); + } break; + case NOTIFICATION_EXIT_TREE: { + get_tree()->disconnect("node_removed", this, "on_node_removed"); } break; } } void WebEditorPlugin::_bind_methods() { - //ClassDB::bind_method(D_METHOD(""), &WebEditorPlugin::); + ClassDB::bind_method(D_METHOD("on_node_removed", "node"), &WebEditorPlugin::on_node_removed); } diff --git a/modules/web/editor/web_editor_plugin.h b/modules/web/editor/web_editor_plugin.h index bb7ea780e..5ef724172 100644 --- a/modules/web/editor/web_editor_plugin.h +++ b/modules/web/editor/web_editor_plugin.h @@ -34,12 +34,16 @@ class WebEditorPlugin : public EditorPlugin { GDCLASS(WebEditorPlugin, EditorPlugin); public: - void make_visible(const bool visible); + void make_visible(bool visible); const Ref get_icon() const; bool has_main_screen() const; String get_name() const; void edit(Object *p_object); bool handles(Object *p_object) const; + void edited_scene_changed(); + + bool scene_has_webnode(Node *p_node); + bool scene_has_webnode_skip(Node *p_node, Node *skip); WebEditorPlugin(EditorNode *p_node); ~WebEditorPlugin(); @@ -49,11 +53,14 @@ public: WebEditor *window; protected: + void on_node_removed(Node *p_child); + void _notification(int p_what); static void _bind_methods(); Ref _icon; + bool _scene_has_webnode; }; #endif