Fixes a crash on draw_hover

This commit is contained in:
groud 2018-06-12 11:01:24 +02:00
parent d5bb6faac7
commit 71cea6b515
2 changed files with 57 additions and 39 deletions

View file

@ -1983,32 +1983,53 @@ bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseMotion> m = p_event;
if (m.is_valid()) {
if (drag_type == DRAG_NONE && tool == TOOL_SELECT) {
Point2 click = transform.affine_inverse().xform(m->get_position());
Point2 click = transform.affine_inverse().xform(m->get_position());
//Checks if the hovered items changed, update the viewport if so
Vector<_SelectResult> hovering_results_tmp;
_get_canvas_items_at_pos(click, hovering_results_tmp);
hovering_results_tmp.sort();
bool changed = false;
if (hovering_results.size() == hovering_results_tmp.size()) {
for (int i = 0; i < hovering_results.size(); i++) {
if (hovering_results[i].item != hovering_results_tmp[i].item) {
changed = true;
break;
}
}
} else {
changed = true;
}
// Checks if the hovered items changed, update the viewport if so
Vector<_SelectResult> hovering_results_items;
_get_canvas_items_at_pos(click, hovering_results_items);
hovering_results_items.sort();
if (changed) {
hovering_results = hovering_results_tmp;
viewport->update();
}
// Compute the nodes names and icon position
Vector<_HoverResult> hovering_results_tmp;
for (int i = 0; i < hovering_results_items.size(); i++) {
CanvasItem *canvas_item = hovering_results_items[i].item;
return true;
if (canvas_item->_edit_use_rect())
continue;
_HoverResult hover_result;
hover_result.position = canvas_item->get_global_transform_with_canvas().get_origin();
if (has_icon(canvas_item->get_class(), "EditorIcons"))
hover_result.icon = get_icon(canvas_item->get_class(), "EditorIcons");
else
hover_result.icon = get_icon("Object", "EditorIcons");
hover_result.name = canvas_item->get_name();
hovering_results_tmp.push_back(hover_result);
}
// Check if changed, if so, update.
bool changed = false;
if (hovering_results_tmp.size() == hovering_results.size()) {
for (int i = 0; i < hovering_results_tmp.size(); i++) {
_HoverResult a = hovering_results_tmp[i];
_HoverResult b = hovering_results[i];
if (a.icon != b.icon || a.name != b.name || a.position != b.position) {
changed = true;
break;
}
}
} else {
changed = true;
}
if (changed) {
hovering_results = hovering_results_tmp;
viewport->update();
}
return true;
}
return false;
@ -2769,26 +2790,15 @@ void CanvasItemEditor::_draw_hover() {
List<Rect2> previous_rects;
for (int i = 0; i < hovering_results.size(); i++) {
// Draw the node's name and icon
CanvasItem *canvas_item = hovering_results[i].item;
if (canvas_item->_edit_use_rect())
continue;
Ref<Texture> node_icon = hovering_results[i].icon;
String node_name = hovering_results[i].name;
Transform2D xform = transform * canvas_item->get_global_transform_with_canvas();
// Get the resources
Ref<Texture> node_icon;
if (has_icon(canvas_item->get_class(), "EditorIcons"))
node_icon = get_icon(canvas_item->get_class(), "EditorIcons");
else
node_icon = get_icon("Object", "EditorIcons");
Ref<Font> font = get_font("font", "Label");
String node_name = canvas_item->get_name();
Size2 node_name_size = font->get_string_size(node_name);
Size2 item_size = Size2(node_icon->get_size().x + 4 + node_name_size.x, MAX(node_icon->get_size().y, node_name_size.y - 3));
Point2 pos = xform.get_origin() - Point2(0, item_size.y) + (Point2(node_icon->get_size().x, -node_icon->get_size().y) / 4);
Point2 pos = transform.xform(hovering_results[i].position) - Point2(0, item_size.y) + (Point2(node_icon->get_size().x, -node_icon->get_size().y) / 4);
// Rectify the position to avoid overlaping items
for (List<Rect2>::Element *E = previous_rects.front(); E; E = E->next()) {
if (E->get().intersects(Rect2(pos, item_size))) {
@ -2798,8 +2808,10 @@ void CanvasItemEditor::_draw_hover() {
previous_rects.push_back(Rect2(pos, item_size));
// Draw the node icon and name
// Draw icon
viewport->draw_texture(node_icon, pos, Color(1.0, 1.0, 1.0, 0.5));
// Draw name
viewport->draw_string(font, pos + Point2(node_icon->get_size().x + 4, item_size.y - 3), node_name, Color(1.0, 1.0, 1.0, 0.5));
}
}

View file

@ -257,9 +257,15 @@ class CanvasItemEditor : public VBoxContainer {
return has_z && p_rr.has_z ? p_rr.z_index < z_index : p_rr.has_z;
}
};
Vector<_SelectResult> selection_results;
Vector<_SelectResult> hovering_results;
struct _HoverResult {
Point2 position;
Ref<Texture> icon;
String name;
};
Vector<_HoverResult> hovering_results;
struct BoneList {