Make 'EditorPath' into a proper 'MenuButton'

This commit is contained in:
Michael Alexsander Silva Dias 2019-04-25 20:53:58 -03:00
parent cce2e4b07c
commit f275de24ee
2 changed files with 57 additions and 117 deletions

View file

@ -56,122 +56,67 @@ void EditorPath::_add_children_to_popup(Object *p_obj, int p_depth) {
Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
int index = popup->get_item_count();
popup->add_icon_item(icon, E->get().name.capitalize(), objects.size());
popup->set_item_h_offset(index, p_depth * 10 * EDSCALE);
int index = get_popup()->get_item_count();
get_popup()->add_icon_item(icon, E->get().name.capitalize(), objects.size());
get_popup()->set_item_h_offset(index, p_depth * 10 * EDSCALE);
objects.push_back(obj->get_instance_id());
_add_children_to_popup(obj, p_depth + 1);
}
}
void EditorPath::_gui_input(const Ref<InputEvent> &p_event) {
void EditorPath::_about_to_show() {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
if (!obj)
return;
Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
if (!obj)
return;
objects.clear();
popup->clear();
_add_children_to_popup(obj);
popup->set_position(get_global_position() + Vector2(0, get_size().height));
popup->set_size(Size2(get_size().width, 1));
popup->popup();
}
}
void EditorPath::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_MOUSE_ENTER: {
mouse_over = true;
update();
} break;
case NOTIFICATION_MOUSE_EXIT: {
mouse_over = false;
update();
} break;
case NOTIFICATION_DRAW: {
RID ci = get_canvas_item();
Ref<Font> label_font = get_font("font", "Label");
Size2i size = get_size();
Ref<Texture> sn = get_icon("SmallNext", "EditorIcons");
Ref<StyleBox> sb = get_stylebox("pressed", "Button");
int ofs = sb->get_margin(MARGIN_LEFT);
if (mouse_over) {
draw_style_box(sb, Rect2(Point2(), get_size()));
}
for (int i = 0; i < history->get_path_size(); i++) {
Object *obj = ObjectDB::get_instance(history->get_path_object(i));
if (!obj)
continue;
String type = obj->get_class();
Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
if (icon.is_valid()) {
icon->draw(ci, Point2i(ofs, (size.height - icon->get_height()) / 2));
ofs += icon->get_width();
}
if (i == history->get_path_size() - 1) {
//add name
ofs += 4;
int left = size.width - ofs;
if (left < 0)
continue;
String name;
if (Object::cast_to<Resource>(obj)) {
Resource *r = Object::cast_to<Resource>(obj);
if (r->get_path().is_resource_file())
name = r->get_path().get_file();
else
name = r->get_name();
if (name == "")
name = r->get_class();
} else if (obj->is_class("ScriptEditorDebuggerInspectedObject"))
name = obj->call("get_title");
else if (Object::cast_to<Node>(obj))
name = Object::cast_to<Node>(obj)->get_name();
else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "")
name = Object::cast_to<Resource>(obj)->get_name();
else
name = obj->get_class();
set_tooltip(obj->get_class());
label_font->draw(ci, Point2i(ofs, (size.height - label_font->get_height()) / 2 + label_font->get_ascent()), name, get_color("font_color", "Label"), left);
} else {
//add arrow
//sn->draw(ci,Point2i(ofs,(size.height-sn->get_height())/2));
//ofs+=sn->get_width();
ofs += 5; //just looks better! somehow
}
}
} break;
}
objects.clear();
get_popup()->clear();
get_popup()->set_size(Size2(get_size().width, 1));
_add_children_to_popup(obj);
}
void EditorPath::update_path() {
update();
for (int i = 0; i < history->get_path_size(); i++) {
Object *obj = ObjectDB::get_instance(history->get_path_object(i));
if (!obj)
continue;
Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
if (icon.is_valid())
set_icon(icon);
if (i == history->get_path_size() - 1) {
String name;
if (Object::cast_to<Resource>(obj)) {
Resource *r = Object::cast_to<Resource>(obj);
if (r->get_path().is_resource_file())
name = r->get_path().get_file();
else
name = r->get_name();
if (name == "")
name = r->get_class();
} else if (obj->is_class("ScriptEditorDebuggerInspectedObject"))
name = obj->call("get_title");
else if (Object::cast_to<Node>(obj))
name = Object::cast_to<Node>(obj)->get_name();
else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "")
name = Object::cast_to<Resource>(obj)->get_name();
else
name = obj->get_class();
set_text(" " + name); // An extra space so the text is not too close of the icon.
set_tooltip(obj->get_class());
}
}
}
void EditorPath::_popup_select(int p_idx) {
void EditorPath::_id_pressed(int p_idx) {
ERR_FAIL_INDEX(p_idx, objects.size());
@ -184,15 +129,14 @@ void EditorPath::_popup_select(int p_idx) {
void EditorPath::_bind_methods() {
ClassDB::bind_method("_gui_input", &EditorPath::_gui_input);
ClassDB::bind_method("_popup_select", &EditorPath::_popup_select);
ClassDB::bind_method("_about_to_show", &EditorPath::_about_to_show);
ClassDB::bind_method("_id_pressed", &EditorPath::_id_pressed);
}
EditorPath::EditorPath(EditorHistory *p_history) {
history = p_history;
mouse_over = false;
popup = memnew(PopupMenu);
popup->connect("id_pressed", this, "_popup_select");
add_child(popup);
set_text_align(ALIGN_LEFT);
get_popup()->connect("about_to_show", this, "_about_to_show");
get_popup()->connect("id_pressed", this, "_id_pressed");
}

View file

@ -32,27 +32,23 @@
#define EDITOR_PATH_H
#include "editor_data.h"
#include "scene/gui/control.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/menu_button.h"
class EditorPath : public Control {
class EditorPath : public MenuButton {
GDCLASS(EditorPath, Control);
GDCLASS(EditorPath, MenuButton);
EditorHistory *history;
Vector<ObjectID> objects;
PopupMenu *popup;
bool mouse_over;
EditorPath();
void _popup_select(int p_idx);
void _gui_input(const Ref<InputEvent> &p_event);
void _id_pressed(int p_idx);
void _about_to_show();
void _add_children_to_popup(Object *p_obj, int p_depth = 0);
protected:
static void _bind_methods();
void _notification(int p_what);
public:
void update_path();