Offset items in PopupMenu when Icons and Checks are present

This commit is contained in:
Michael Alexsander Silva Dias 2019-03-18 10:10:19 -03:00
parent df7d3708c5
commit de04003d3f

View file

@ -54,9 +54,11 @@ Size2 PopupMenu::get_minimum_size() const {
Ref<Font> font = get_font("font"); Ref<Font> font = get_font("font");
float max_w = 0; float max_w = 0;
float icon_w = 0;
int font_h = font->get_height(); int font_h = font->get_height();
int check_w = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width()); int check_w = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width()) + hseparation;
int accel_max_w = 0; int accel_max_w = 0;
bool has_check = false;
for (int i = 0; i < items.size(); i++) { for (int i = 0; i < items.size(); i++) {
@ -65,8 +67,7 @@ Size2 PopupMenu::get_minimum_size() const {
Size2 icon_size = items[i].icon->get_size(); Size2 icon_size = items[i].icon->get_size();
size.height = MAX(icon_size.height, font_h); size.height = MAX(icon_size.height, font_h);
size.width += icon_size.width; icon_w = MAX(icon_size.width + hseparation, icon_w);
size.width += hseparation;
} else { } else {
size.height = font_h; size.height = font_h;
@ -74,10 +75,8 @@ Size2 PopupMenu::get_minimum_size() const {
size.width += items[i].h_ofs; size.width += items[i].h_ofs;
if (items[i].checkable_type) { if (items[i].checkable_type)
has_check = true;
size.width += check_w + hseparation;
}
String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text; String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text;
size.width += font->get_string_size(text).width; size.width += font->get_string_size(text).width;
@ -91,13 +90,14 @@ Size2 PopupMenu::get_minimum_size() const {
accel_max_w = MAX(accel_w, accel_max_w); accel_max_w = MAX(accel_w, accel_max_w);
} }
if (items[i].submenu != "") { if (items[i].submenu != "")
size.width += get_icon("submenu")->get_width(); size.width += get_icon("submenu")->get_width();
}
if (has_check)
size.width += check_w;
max_w = MAX(max_w, size.width + icon_w);
minsize.height += size.height; minsize.height += size.height;
max_w = MAX(max_w, size.width);
} }
minsize.width += max_w + accel_max_w; minsize.width += max_w + accel_max_w;
@ -147,10 +147,10 @@ int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
void PopupMenu::_activate_submenu(int over) { void PopupMenu::_activate_submenu(int over) {
Node *n = get_node(items[over].submenu); Node *n = get_node(items[over].submenu);
ERR_EXPLAIN("item subnode does not exist: " + items[over].submenu); ERR_EXPLAIN("Item subnode does not exist: " + items[over].submenu);
ERR_FAIL_COND(!n); ERR_FAIL_COND(!n);
Popup *pm = Object::cast_to<Popup>(n); Popup *pm = Object::cast_to<Popup>(n);
ERR_EXPLAIN("item subnode is not a Popup: " + items[over].submenu); ERR_EXPLAIN("Item subnode is not a Popup: " + items[over].submenu);
ERR_FAIL_COND(!pm); ERR_FAIL_COND(!pm);
if (pm->is_visible_in_tree()) if (pm->is_visible_in_tree())
return; //already visible! return; //already visible!
@ -443,15 +443,31 @@ void PopupMenu::_notification(int p_what) {
Color font_color_hover = get_color("font_color_hover"); Color font_color_hover = get_color("font_color_hover");
float font_h = font->get_height(); float font_h = font->get_height();
// Add the check and the wider icon to the offset of all items.
float icon_ofs = 0.0;
bool has_check = false;
for (int i = 0; i < items.size(); i++) {
if (!items[i].icon.is_null())
icon_ofs = MAX(items[i].icon->get_size().height, icon_ofs);
if (items[i].checkable_type)
has_check = true;
}
if (icon_ofs > 0.0)
icon_ofs += hseparation;
float check_ofs = 0.0;
if (has_check)
check_ofs = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width()) + hseparation;
for (int i = 0; i < items.size(); i++) { for (int i = 0; i < items.size(); i++) {
if (i > 0) if (i > 0)
ofs.y += vseparation; ofs.y += vseparation;
Point2 item_ofs = ofs; Point2 item_ofs = ofs;
float h;
Size2 icon_size; Size2 icon_size;
float h;
Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
if (!items[i].icon.is_null()) { if (!items[i].icon.is_null()) {
@ -489,16 +505,15 @@ void PopupMenu::_notification(int p_what) {
} }
} }
Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
if (items[i].checkable_type) { if (items[i].checkable_type) {
Texture *icon = (items[i].checked ? check[items[i].checkable_type - 1] : uncheck[items[i].checkable_type - 1]).ptr(); Texture *icon = (items[i].checked ? check[items[i].checkable_type - 1] : uncheck[items[i].checkable_type - 1]).ptr();
icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color); icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
item_ofs.x += icon->get_width() + hseparation;
} }
if (!items[i].icon.is_null()) { if (!items[i].icon.is_null()) {
items[i].icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color); items[i].icon->draw(ci, item_ofs + Size2(check_ofs, 0) + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
item_ofs.x += items[i].icon->get_width();
item_ofs.x += hseparation;
} }
if (items[i].submenu != "") { if (items[i].submenu != "") {
@ -514,6 +529,7 @@ void PopupMenu::_notification(int p_what) {
} }
} else { } else {
item_ofs.x += icon_ofs + check_ofs;
font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text, items[i].disabled ? font_color_disabled : (i == mouse_over ? font_color_hover : font_color)); font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text, items[i].disabled ? font_color_disabled : (i == mouse_over ? font_color_hover : font_color));
} }