diff options
Diffstat (limited to 'tools/editor/editor_node.cpp')
-rw-r--r-- | tools/editor/editor_node.cpp | 115 |
1 files changed, 110 insertions, 5 deletions
diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index bb33f19a2b..d08d62385f 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -2934,7 +2934,16 @@ void EditorNode::_menu_option_confirm(int p_option,bool p_confirmed) { default: { - if (p_option>=OBJECT_METHOD_BASE) { + if (p_option>=TOOL_MENU_BASE) { + int idx = p_option - TOOL_MENU_BASE; + + if (tool_menu_items[idx].submenu != "") + break; + + Object *handler = ObjectDB::get_instance(tool_menu_items[idx].handler); + ERR_FAIL_COND(!handler); + handler->call(tool_menu_items[idx].callback, tool_menu_items[idx].ud); + } else if (p_option>=OBJECT_METHOD_BASE) { ERR_FAIL_COND(!current); @@ -5269,6 +5278,100 @@ void EditorNode::add_plugin_init_callback(EditorPluginInitializeCallback p_callb EditorPluginInitializeCallback EditorNode::plugin_init_callbacks[EditorNode::MAX_INIT_CALLBACKS]; +void EditorNode::_tool_menu_insert_item(const ToolMenuItem& p_item) { + + int idx = tool_menu_items.size(); + + String cat; + if (p_item.name.find("/") >= 0) { + cat = p_item.name.get_slice("/", 0); + } else { + idx = 0; + cat = ""; + } + + for (int i = tool_menu_items.size() - 1; i >= 0; i--) { + String name = tool_menu_items[i].name; + + if (name.begins_with(cat) && (cat != "" || name.find("/") < 0)) { + idx = i + 1; + break; + } + } + + tool_menu_items.insert(idx, p_item); +} + +void EditorNode::_rebuild_tool_menu() const { + + if (_initializing_tool_menu) + return; + + PopupMenu *menu = tool_menu->get_popup(); + menu->clear(); + + for (int i = 0; i < tool_menu_items.size(); i++) { + menu->add_item(tool_menu_items[i].name.get_slice("/", 1), TOOL_MENU_BASE + i); + + if (tool_menu_items[i].submenu != "") + menu->set_item_submenu(i, tool_menu_items[i].submenu); + } +} + +void EditorNode::add_tool_menu_item(const String& p_name, Object *p_handler, const String& p_callback, const Variant& p_ud) { + + ERR_FAIL_COND(!p_handler); + + ToolMenuItem tmi; + tmi.name = p_name; + tmi.submenu = ""; + tmi.ud = p_ud; + tmi.handler = p_handler->get_instance_ID(); + tmi.callback = p_callback; + _tool_menu_insert_item(tmi); + + _rebuild_tool_menu(); +} + +void EditorNode::add_tool_submenu_item(const String& p_name, PopupMenu *p_submenu) { + + ERR_FAIL_COND(!p_submenu); + ERR_FAIL_COND(p_submenu->get_parent() != NULL); + + ToolMenuItem tmi; + tmi.name = p_name; + tmi.submenu = p_submenu->get_name(); + tmi.ud = Variant(); + tmi.handler = -1; + tmi.callback = ""; + _tool_menu_insert_item(tmi); + + tool_menu->get_popup()->add_child(p_submenu); + + _rebuild_tool_menu(); +} + +void EditorNode::remove_tool_menu_item(const String& p_name) { + + for (int i = 0; i < tool_menu_items.size(); i++) { + if (tool_menu_items[i].name == p_name) { + String submenu = tool_menu_items[i].submenu; + + if (submenu != "") { + Node *n = tool_menu->get_popup()->get_node(submenu); + + if (n) { + tool_menu->get_popup()->remove_child(n); + memdelete(n); + } + } + + tool_menu_items.remove(i); + } + } + + _rebuild_tool_menu(); +} int EditorNode::build_callback_count=0; @@ -5412,6 +5515,8 @@ EditorNode::EditorNode() { docks_visible = true; + _initializing_tool_menu = true; + FileAccess::set_backup_save(true); PathRemap::get_singleton()->clear_remaps();; //editor uses no remaps @@ -5871,10 +5976,9 @@ EditorNode::EditorNode() { //tool_menu->set_icon(gui_base->get_icon("Save","EditorIcons")); left_menu_hb->add_child( tool_menu ); + tool_menu->get_popup()->connect("id_pressed", this, "_menu_option"); - p=tool_menu->get_popup(); - p->connect("id_pressed",this,"_menu_option"); - p->add_item(TTR("Orphan Resource Explorer"),TOOLS_ORPHAN_RESOURCES); + add_tool_menu_item(TTR("Orphan Resource Explorer"), this, "_menu_option", TOOLS_ORPHAN_RESOURCES); export_button = memnew( ToolButton ); export_button->set_tooltip(TTR("Export the project to many platforms.")); @@ -6755,7 +6859,8 @@ EditorNode::EditorNode() { _initializing_addons=false; } - + _initializing_tool_menu = false; + _rebuild_tool_menu(); _load_docks(); |