Add a API to add/remove items to the "Tools" menu

This commit is contained in:
Franklin Sobrinho 2016-08-07 11:01:22 -03:00 committed by Rémi Verschelde
parent e0faf8a51b
commit b24b52d56b
4 changed files with 158 additions and 6 deletions

View file

@ -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();

View file

@ -197,7 +197,9 @@ private:
IMPORT_PLUGIN_BASE=100,
OBJECT_METHOD_BASE=500
OBJECT_METHOD_BASE=500,
TOOL_MENU_BASE=1000
};
@ -593,6 +595,22 @@ private:
void _call_build();
static int build_callback_count;
static EditorBuildCallback build_callbacks[MAX_BUILD_CALLBACKS];
bool _initializing_tool_menu;
struct ToolMenuItem {
String name;
String submenu;
Variant ud;
ObjectID handler;
String callback;
};
Vector<ToolMenuItem> tool_menu_items;
void _tool_menu_insert_item(const ToolMenuItem& p_item);
void _rebuild_tool_menu() const;
protected:
void _notification(int p_what);
static void _bind_methods();
@ -755,6 +773,9 @@ public:
Variant drag_files(const Vector<String>& p_files,Control* p_from);
Variant drag_files_and_dirs(const Vector<String>& p_files,Control* p_from);
void add_tool_menu_item(const String& p_name, Object *p_handler, const String& p_callback, const Variant& p_ud = Variant());
void add_tool_submenu_item(const String& p_name, PopupMenu *p_submenu);
void remove_tool_menu_item(const String& p_name);
EditorNode();
~EditorNode();

View file

@ -27,6 +27,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "editor_plugin.h"
#include "scene/gui/popup_menu.h"
#include "scene/3d/camera.h"
#include "plugins/canvas_item_editor_plugin.h"
#include "plugins/spatial_editor_plugin.h"
@ -132,6 +133,24 @@ void EditorPlugin::add_control_to_container(CustomControlContainer p_location,Co
}
}
void EditorPlugin::add_tool_menu_item(const String& p_name, Object *p_handler, const String& p_callback, const Variant& p_ud) {
EditorNode::get_singleton()->add_tool_menu_item(p_name, p_handler, p_callback, p_ud);
}
void EditorPlugin::add_tool_submenu_item(const String& p_name, Object *p_submenu) {
ERR_FAIL_NULL(p_submenu);
PopupMenu *submenu = p_submenu->cast_to<PopupMenu>();
ERR_FAIL_NULL(submenu);
EditorNode::get_singleton()->add_tool_submenu_item(p_name, submenu);
}
void EditorPlugin::remove_tool_menu_item(const String& p_name) {
EditorNode::get_singleton()->remove_tool_menu_item(p_name);
}
Ref<SpatialEditorGizmo> EditorPlugin::create_spatial_gizmo(Spatial* p_spatial) {
//??
if (get_script_instance() && get_script_instance()->has_method("create_spatial_gizmo")) {
@ -351,6 +370,9 @@ void EditorPlugin::_bind_methods() {
ClassDB::bind_method(_MD("add_control_to_dock","slot","control:Control"),&EditorPlugin::add_control_to_dock);
ClassDB::bind_method(_MD("remove_control_from_docks","control:Control"),&EditorPlugin::remove_control_from_docks);
ClassDB::bind_method(_MD("remove_control_from_bottom_panel","control:Control"),&EditorPlugin::remove_control_from_bottom_panel);
ClassDB::bind_method(_MD("add_tool_menu_item", "name", "handler", "callback", "ud"),&EditorPlugin::add_tool_menu_item,DEFVAL(Variant()));
ClassDB::bind_method(_MD("add_tool_submenu_item", "name", "submenu:PopupMenu"),&EditorPlugin::add_tool_submenu_item);
ClassDB::bind_method(_MD("remove_tool_menu_item", "name"),&EditorPlugin::remove_tool_menu_item);
ClassDB::bind_method(_MD("add_custom_type","type","base","script:Script","icon:Texture"),&EditorPlugin::add_custom_type);
ClassDB::bind_method(_MD("remove_custom_type","type"),&EditorPlugin::remove_custom_type);
ClassDB::bind_method(_MD("get_editor_viewport:Control"), &EditorPlugin::get_editor_viewport);

View file

@ -103,6 +103,10 @@ public:
Control* get_editor_viewport();
void edit_resource(const Ref<Resource>& p_resource);
void add_tool_menu_item(const String& p_name, Object *p_handler, const String& p_callback, const Variant& p_ud = Variant());
void add_tool_submenu_item(const String& p_name, Object *p_submenu);
void remove_tool_menu_item(const String& p_name);
virtual Ref<SpatialEditorGizmo> create_spatial_gizmo(Spatial* p_spatial);
virtual bool forward_canvas_gui_input(const Transform2D& p_canvas_xform, const InputEvent& p_event);
virtual void forward_draw_over_canvas(const Transform2D& p_canvas_xform,Control *p_canvas);