diff --git a/doc/classes/PopupMenu.xml b/doc/classes/PopupMenu.xml
index bea1e787382..95629ab815e 100644
--- a/doc/classes/PopupMenu.xml
+++ b/doc/classes/PopupMenu.xml
@@ -478,6 +478,9 @@
If [code]true[/code], hides the [PopupMenu] when a state item is selected.
+
+ If non-zero, the [code]PopupMenu[/code] will be resized vertically to that maximum value, showing a scrollbar if the content doesn't fit.
+
Sets the delay time in seconds for the submenu item to popup on mouse hovering. If the popup menu is added as a child of another (acting as a submenu), it will inherit the delay time of the parent menu item.
diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp
index 7e7bc92dd10..4662e5edf0f 100644
--- a/scene/gui/popup_menu.cpp
+++ b/scene/gui/popup_menu.cpp
@@ -50,7 +50,8 @@ Size2 PopupMenu::get_minimum_size() const {
int vseparation = get_constant("vseparation");
int hseparation = get_constant("hseparation");
- Size2 minsize = get_stylebox("panel")->get_minimum_size();
+ Size2 minsize = get_stylebox("panel")->get_minimum_size(); // Accounts for margin in the margin container
+ minsize.x += scroll_container->get_v_scrollbar()->get_size().width * 2; // Adds a buffer so that the scrollbar does not render over the top of content
Ref font = get_font("font");
Ref font_separator = get_font("font_separator");
@@ -65,13 +66,10 @@ Size2 PopupMenu::get_minimum_size() const {
int font_h = items[i].separator && text != String() ? font_separator->get_height() : font->get_height();
Size2 size;
- if (!items[i].icon.is_null()) {
- Size2 icon_size = items[i].icon->get_size();
- size.height = MAX(icon_size.height, font_h);
- icon_w = MAX(icon_size.width + hseparation, icon_w);
- } else {
- size.height = font_h;
- }
+
+ Size2 icon_size = items[i].get_icon_size();
+ size.height = MAX(icon_size.height, font_h);
+ icon_w = MAX(icon_size.width, icon_w);
size.width += items[i].h_ofs;
@@ -102,42 +100,63 @@ Size2 PopupMenu::get_minimum_size() const {
minsize.width += check_w;
}
+ if (max_height > 0 && minsize.height > max_height) {
+ minsize.height = max_height;
+ }
+
return minsize;
}
+int PopupMenu::_get_items_total_height() const {
+ int font_height = get_font("font")->get_height();
+ int vsep = get_constant("vseparation");
+
+ // Get total height of all items by taking max of icon height and font height
+ int items_total_height = 0;
+ for (int i = 0; i < items.size(); i++) {
+ items_total_height += MAX(items[i].get_icon_size().height, font_height) + vsep;
+ }
+
+ // Subtract a separator which is not needed for the last item.
+ return items_total_height - vsep;
+}
+
+void PopupMenu::_scroll_to_item(int p_item) {
+ ERR_FAIL_INDEX(p_item, items.size());
+
+ // Scroll item into view (upwards)
+ if (items[p_item]._ofs_cache < -control->get_position().y) {
+ int amnt_over = items[p_item]._ofs_cache + control->get_position().y;
+ scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over);
+ }
+
+ // Scroll item into view (downwards)
+ if (items[p_item]._ofs_cache + items[p_item]._height_cache > -control->get_position().y + scroll_container->get_size().height) {
+ int amnt_over = items[p_item]._ofs_cache + items[p_item]._height_cache + control->get_position().y - scroll_container->get_size().height;
+ scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over);
+ }
+}
+
int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
if (p_over.x < 0 || p_over.x >= get_size().width) {
return -1;
}
- Ref style = get_stylebox("panel");
+ Ref style = get_stylebox("panel"); // Accounts for margin in the margin container
- Point2 ofs = style->get_offset();
-
- if (ofs.y > p_over.y) {
- return -1;
- }
-
- Ref font = get_font("font");
- Ref font_separator = get_font("font_separator");
int vseparation = get_constant("vseparation");
+ float font_h = get_font("font")->get_height();
+
+ Point2 ofs = style->get_offset() + Point2(0, vseparation / 2);
for (int i = 0; i < items.size(); i++) {
- String text = items[i].xl_text;
- float font_h = items[i].separator && text != String() ? font_separator->get_height() : font->get_height();
-
- ofs.y += vseparation;
- float h;
-
- if (!items[i].icon.is_null()) {
- Size2 icon_size = items[i].icon->get_size();
- h = MAX(icon_size.height, font_h);
- } else {
- h = font_h;
+ if (i > 0) {
+ ofs.y += vseparation;
}
- ofs.y += h;
- if (p_over.y < ofs.y) {
+ ofs.y += MAX(items[i].get_icon_size().height, font_h);
+
+ if (p_over.y - control->get_position().y < ofs.y) {
return i;
}
}
@@ -148,46 +167,58 @@ int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
void PopupMenu::_activate_submenu(int over, bool p_by_keyboard) {
Node *n = get_node(items[over].submenu);
ERR_FAIL_COND_MSG(!n, "Item subnode does not exist: " + items[over].submenu + ".");
- Popup *pm = Object::cast_to(n);
- ERR_FAIL_COND_MSG(!pm, "Item subnode is not a Popup: " + items[over].submenu + ".");
- if (pm->is_visible_in_tree()) {
+ Popup *submenu_popup = Object::cast_to(n);
+ ERR_FAIL_COND_MSG(!submenu_popup, "Item subnode is not a Popup: " + items[over].submenu + ".");
+ if (submenu_popup->is_visible_in_tree()) {
return; // Already visible.
}
- Point2 p = get_global_position();
- Rect2 pr(p, get_size());
Ref style = get_stylebox("panel");
+ int vsep = get_constant("vseparation");
- Point2 pos = p + Point2(get_size().width, items[over]._ofs_cache - style->get_offset().y) * get_global_transform().get_scale();
- Size2 size = pm->get_size();
- // Fix pos.
- if (pos.x + size.width > get_viewport_rect().size.width) {
- pos.x = p.x - size.width;
+ Point2 this_pos = get_position();
+ Rect2 this_rect(this_pos, get_size());
+
+ float scroll_offset = control->get_position().y;
+
+ Point2 submenu_pos = this_pos + Point2(this_rect.size.width, items[over]._ofs_cache + scroll_offset);
+ Size2 submenu_size = submenu_popup->get_size();
+
+ // Fix pos if going outside parent rect
+ if (submenu_pos.x + submenu_size.width > get_viewport_rect().size.width) {
+ submenu_pos.x = this_pos.x - submenu_size.width;
}
- pm->set_position(pos);
- pm->set_scale(get_global_transform().get_scale());
- pm->popup();
+ submenu_popup->set_position(submenu_pos);
+ submenu_popup->set_as_minsize(); // Shrink the popup size to it's contents.
+ submenu_popup->popup();
+
+ // Set autohide areas
+ PopupMenu *submenu_pum = Object::cast_to(submenu_popup);
+ if (submenu_pum) {
+ submenu_pum->activated_by_keyboard = p_by_keyboard;
- PopupMenu *pum = Object::cast_to(pm);
- if (pum) {
// If not triggered by the mouse, start the popup with its first enabled item focused.
if (p_by_keyboard) {
- for (int i = 0; i < pum->get_item_count(); i++) {
- if (!pum->is_item_disabled(i)) {
- pum->set_current_index(i);
+ for (int i = 0; i < submenu_pum->get_item_count(); i++) {
+ if (!submenu_pum->is_item_disabled(i)) {
+ submenu_pum->set_current_index(i);
break;
}
}
}
- // Setting autohide areas *must* be done after `popup()` which can move the popup (to fit it into the viewport).
- pr.position -= pum->get_global_position();
- pum->clear_autohide_areas();
- pum->add_autohide_area(Rect2(pr.position.x, pr.position.y, pr.size.x, items[over]._ofs_cache));
- if (over < items.size() - 1) {
- int from = items[over + 1]._ofs_cache;
- pum->add_autohide_area(Rect2(pr.position.x, pr.position.y + from, pr.size.x, pr.size.y - from));
+ // Make the position of the parent popup relative to submenu popup
+ this_rect.position = this_rect.position - submenu_pum->get_position();
+
+ // Autohide area above the submenu item
+ submenu_pum->clear_autohide_areas();
+ submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y, this_rect.size.x, items[over]._ofs_cache + scroll_offset + style->get_offset().height - vsep / 2));
+
+ // If there is an area below the submenu item, add an autohide area there.
+ if (items[over]._ofs_cache + items[over]._height_cache + scroll_offset <= control->get_size().height) {
+ int from = items[over]._ofs_cache + items[over]._height_cache + scroll_offset + vsep / 2 + style->get_offset().height;
+ submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + from, this_rect.size.x, this_rect.size.y - from));
}
}
}
@@ -202,34 +233,11 @@ void PopupMenu::_submenu_timeout() {
submenu_over = -1;
}
-void PopupMenu::_scroll(float p_factor, const Point2 &p_over) {
- int vseparation = get_constant("vseparation");
- Ref font = get_font("font");
-
- float dy = (vseparation + font->get_height()) * 3 * p_factor * get_global_transform().get_scale().y;
- if (dy > 0) {
- const float global_top = get_global_position().y;
- const float limit = global_top < 0 ? -global_top : 0;
- dy = MIN(dy, limit);
- } else if (dy < 0) {
- const float global_bottom = get_global_position().y + get_size().y * get_global_transform().get_scale().y;
- const float viewport_height = get_viewport_rect().size.y;
- const float limit = global_bottom > viewport_height ? global_bottom - viewport_height : 0;
- dy = -MIN(-dy, limit);
- }
- set_position(get_position() + Vector2(0, dy));
-
- Ref ie;
- ie.instance();
- ie->set_position(p_over - Vector2(0, dy));
- _gui_input(ie);
-}
-
void PopupMenu::_gui_input(const Ref &p_event) {
ERR_FAIL_COND(p_event.is_null());
if (!items.empty()) {
- if (p_event->is_action("ui_down") && p_event->is_pressed()) {
+ if (p_event->is_action("ui_down") && p_event->is_pressed() && mouse_over != items.size() - 1) {
int search_from = mouse_over + 1;
if (search_from >= items.size()) {
search_from = 0;
@@ -237,15 +245,12 @@ void PopupMenu::_gui_input(const Ref &p_event) {
bool match_found = false;
for (int i = search_from; i < items.size(); i++) {
- if (i < 0 || i >= items.size()) {
- continue;
- }
-
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal("id_focused", i);
- update();
- accept_event();
+ _scroll_to_item(i);
+ control->update();
+ control->accept_event();
match_found = true;
break;
}
@@ -257,13 +262,14 @@ void PopupMenu::_gui_input(const Ref &p_event) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal("id_focused", i);
- update();
- accept_event();
+ _scroll_to_item(i);
+ control->update();
+ control->accept_event();
break;
}
}
}
- } else if (p_event->is_action("ui_up") && p_event->is_pressed()) {
+ } else if (p_event->is_action("ui_up") && p_event->is_pressed() && mouse_over != 0) {
int search_from = mouse_over - 1;
if (search_from < 0) {
search_from = items.size() - 1;
@@ -271,15 +277,12 @@ void PopupMenu::_gui_input(const Ref &p_event) {
bool match_found = false;
for (int i = search_from; i >= 0; i--) {
- if (i >= items.size()) {
- continue;
- }
-
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal("id_focused", i);
- update();
- accept_event();
+ _scroll_to_item(i);
+ control->update();
+ control->accept_event();
match_found = true;
break;
}
@@ -291,8 +294,9 @@ void PopupMenu::_gui_input(const Ref &p_event) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal("id_focused", i);
- update();
- accept_event();
+ _scroll_to_item(i);
+ control->update();
+ control->accept_event();
break;
}
}
@@ -301,12 +305,12 @@ void PopupMenu::_gui_input(const Ref &p_event) {
Node *n = get_parent();
if (n && Object::cast_to(n)) {
hide();
- accept_event();
+ control->accept_event();
}
} else if (p_event->is_action("ui_right") && p_event->is_pressed()) {
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu != "" && submenu_over != mouse_over) {
_activate_submenu(mouse_over, true);
- accept_event();
+ control->accept_event();
}
} else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
@@ -315,64 +319,56 @@ void PopupMenu::_gui_input(const Ref &p_event) {
} else {
activate_item(mouse_over);
}
- accept_event();
+ control->accept_event();
}
}
}
+ // Make an area which does not include v scrollbar, so that items are not activated when dragging scrollbar.
+ Rect2 item_clickable_area = control->get_global_rect();
+ float scroll_width = scroll_container->get_v_scrollbar()->is_visible_in_tree() ? scroll_container->get_v_scrollbar()->get_size().width : 0;
+ item_clickable_area.set_size(Size2(item_clickable_area.size.width - scroll_width, item_clickable_area.size.height));
+
Ref b = p_event;
if (b.is_valid()) {
- if (b->is_pressed()) {
+ if (!item_clickable_area.has_point(b->get_global_position())) {
return;
}
int button_idx = b->get_button_index();
- switch (button_idx) {
- case BUTTON_WHEEL_DOWN: {
- if (get_global_position().y + get_size().y * get_global_transform().get_scale().y > get_viewport_rect().size.y) {
- _scroll(-b->get_factor(), b->get_position());
+ if (!b->is_pressed()) {
+ // Allow activating item by releasing the LMB or any that was down when the popup appeared.
+ // However, if button was not held when opening menu, do not allow release to activate item.
+ if (button_idx == BUTTON_LEFT || (initial_button_mask & (1 << (button_idx - 1)))) {
+ bool was_during_grabbed_click = during_grabbed_click;
+ during_grabbed_click = false;
+ initial_button_mask = 0;
+
+ int over = _get_mouse_over(b->get_position());
+
+ if (invalidated_click) {
+ invalidated_click = false;
+ return;
}
- } break;
- case BUTTON_WHEEL_UP: {
- if (get_global_position().y < 0) {
- _scroll(b->get_factor(), b->get_position());
+ if (over < 0) {
+ if (!was_during_grabbed_click) {
+ hide();
+ }
+ return;
}
- } break;
- default: {
- // Allow activating item by releasing the LMB or any that was down when the popup appeared
- if (button_idx == BUTTON_LEFT || (initial_button_mask & (1 << (button_idx - 1)))) {
- bool was_during_grabbed_click = during_grabbed_click;
- during_grabbed_click = false;
- initial_button_mask = 0;
- int over = _get_mouse_over(b->get_position());
-
- if (invalidated_click) {
- invalidated_click = false;
- break;
- }
- if (over < 0) {
- if (!was_during_grabbed_click) {
- hide();
- }
- break; //non-activable
- }
-
- if (items[over].separator || items[over].disabled) {
- break;
- }
-
- if (items[over].submenu != "") {
- _activate_submenu(over);
- return;
- }
- activate_item(over);
+ if (items[over].separator || items[over].disabled) {
+ return;
}
+
+ if (items[over].submenu != "") {
+ _activate_submenu(over);
+ return;
+ }
+ activate_item(over);
}
}
-
- //update();
}
Ref m = p_event;
@@ -392,12 +388,16 @@ void PopupMenu::_gui_input(const Ref &p_event) {
}
}
+ if (!item_clickable_area.has_point(m->get_global_position())) {
+ return;
+ }
+
int over = _get_mouse_over(m->get_position());
int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].id >= 0 ? items[over].id : over);
if (id < 0) {
mouse_over = -1;
- update();
+ control->update();
return;
}
@@ -408,14 +408,7 @@ void PopupMenu::_gui_input(const Ref &p_event) {
if (over != mouse_over) {
mouse_over = over;
- update();
- }
- }
-
- Ref pan_gesture = p_event;
- if (pan_gesture.is_valid()) {
- if (get_global_position().y + get_size().y > get_viewport_rect().size.y || get_global_position().y < 0) {
- _scroll(-pan_gesture->get_delta().y, pan_gesture->get_position());
+ control->update();
}
}
@@ -451,14 +444,151 @@ void PopupMenu::_gui_input(const Ref &p_event) {
if (items[i].text.findn(search_string) == 0) {
mouse_over = i;
emit_signal("id_focused", i);
- update();
- accept_event();
+ _scroll_to_item(i);
+ control->update();
+ control->accept_event();
break;
}
}
}
}
+void PopupMenu::_draw_items() {
+ control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
+ RID ci = control->get_canvas_item();
+
+ Size2 margin_size;
+ margin_size.width = margin_container->get_constant("margin_right") + margin_container->get_constant("margin_left");
+ margin_size.height = margin_container->get_constant("margin_top") + margin_container->get_constant("margin_bottom");
+
+ Ref style = get_stylebox("panel");
+ Ref hover = get_stylebox("hover");
+ Ref font = get_font("font");
+ // In Item::checkable_type enum order (less the non-checkable member)
+ Ref check[] = { get_icon("checked"), get_icon("radio_checked") };
+ Ref uncheck[] = { get_icon("unchecked"), get_icon("radio_unchecked") };
+ Ref submenu = get_icon("submenu");
+ Ref separator = get_stylebox("separator");
+ Ref labeled_separator_left = get_stylebox("labeled_separator_left");
+ Ref labeled_separator_right = get_stylebox("labeled_separator_right");
+
+ int vseparation = get_constant("vseparation");
+ int hseparation = get_constant("hseparation");
+ Color font_color = get_color("font_color");
+ Color font_color_disabled = get_color("font_color_disabled");
+ Color font_color_accel = get_color("font_color_accel");
+ Color font_color_hover = get_color("font_color_hover");
+ float font_h = font->get_height();
+
+ float scroll_width = scroll_container->get_v_scrollbar()->is_visible_in_tree() ? scroll_container->get_v_scrollbar()->get_size().width : 0;
+ float display_width = control->get_size().width - scroll_width;
+
+ // Find the widest icon and whether any items have a checkbox, and store the offsets for each.
+ float icon_ofs = 0.0;
+ bool has_check = false;
+ for (int i = 0; i < items.size(); i++) {
+ icon_ofs = MAX(items[i].get_icon_size().width, icon_ofs);
+
+ if (items[i].checkable_type) {
+ has_check = true;
+ }
+ }
+
+ float check_ofs = 0.0;
+ if (has_check) {
+ check_ofs = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width()) + hseparation;
+ }
+
+ Point2 ofs = Point2();
+
+ // Loop through all items and draw each.
+ for (int i = 0; i < items.size(); i++) {
+ // If not the first item, add the separation space between items.
+ if (i > 0) {
+ ofs.y += vseparation;
+ }
+
+ Point2 item_ofs = ofs;
+ Size2 icon_size = items[i].get_icon_size();
+ float h = MAX(icon_size.height, font_h);
+
+ if (i == mouse_over) {
+ hover->draw(ci, Rect2(item_ofs + Point2(-hseparation, -vseparation / 2), Size2(display_width + hseparation * 2, h + vseparation)));
+ }
+
+ String text = items[i].xl_text;
+
+ // Separator
+ item_ofs.x += items[i].h_ofs;
+ if (items[i].separator) {
+ int sep_h = separator->get_center_size().height + separator->get_minimum_size().height;
+ if (text != String()) {
+ int text_size = font->get_string_size(text).width;
+ int text_center = display_width / 2;
+ int text_left = text_center - text_size / 2;
+ int text_right = text_center + text_size / 2;
+ if (text_left > item_ofs.x) {
+ labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, text_left - item_ofs.x), sep_h)));
+ }
+ if (text_right < display_width) {
+ labeled_separator_right->draw(ci, Rect2(Point2(text_right, item_ofs.y + Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, display_width - text_right), sep_h)));
+ }
+ } else {
+ separator->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(display_width, sep_h)));
+ }
+ }
+
+ Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
+
+ // Checkboxes
+ if (items[i].checkable_type) {
+ 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
+ if (!items[i].icon.is_null()) {
+ items[i].icon->draw(ci, item_ofs + Size2(check_ofs, 0) + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
+ }
+
+ // Submenu arrow on right hand side
+ if (items[i].submenu != "") {
+ submenu->draw(ci, Point2(display_width - submenu->get_width(), item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
+ }
+
+ // Text
+ item_ofs.y += font->get_ascent();
+ if (items[i].separator) {
+ if (text != String()) {
+ int center = (display_width - font->get_string_size(text).width) / 2;
+ font->draw(ci, Point2(center, item_ofs.y + Math::floor((h - font_h) / 2.0)), text, font_color_disabled);
+ }
+ } 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));
+ }
+
+ // Accelerator / Shortcut
+ if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) {
+ String sc_text = _get_accel_text(i);
+ item_ofs.x = display_width - font->get_string_size(sc_text).width;
+ font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), sc_text, i == mouse_over ? font_color_hover : font_color_accel);
+ }
+
+ // Cache the item vertical offset from the first item and the height
+ items.write[i]._ofs_cache = ofs.y;
+ items.write[i]._height_cache = h;
+
+ ofs.y += h;
+ }
+}
+
+void PopupMenu::_draw_background() {
+ Ref style = get_stylebox("panel");
+ RID ci2 = margin_container->get_canvas_item();
+ style->draw(ci2, Rect2(Point2(), margin_container->get_size()));
+}
+
bool PopupMenu::has_point(const Point2 &p_point) const {
if (parent_rect.has_point(p_point)) {
return true;
@@ -488,138 +618,7 @@ void PopupMenu::_notification(int p_what) {
}
minimum_size_changed();
- update();
- } break;
- case NOTIFICATION_DRAW: {
- RID ci = get_canvas_item();
- Size2 size = get_size();
-
- Ref style = get_stylebox("panel");
- Ref hover = get_stylebox("hover");
- Ref font = get_font("font");
- Ref font_separator = get_font("font_separator");
- // In Item::checkable_type enum order (less the non-checkable member)
- Ref check[] = { get_icon("checked"), get_icon("radio_checked") };
- Ref uncheck[] = { get_icon("unchecked"), get_icon("radio_unchecked") };
- Ref submenu = get_icon("submenu");
- Ref separator = get_stylebox("separator");
- Ref labeled_separator_left = get_stylebox("labeled_separator_left");
- Ref labeled_separator_right = get_stylebox("labeled_separator_right");
-
- style->draw(ci, Rect2(Point2(), size));
- Point2 ofs = style->get_offset();
- int vseparation = get_constant("vseparation");
- int hseparation = get_constant("hseparation");
- Color font_color = get_color("font_color");
- Color font_color_disabled = get_color("font_color_disabled");
- Color font_color_accel = get_color("font_color_accel");
- Color font_color_hover = get_color("font_color_hover");
- Color font_color_separator = get_color("font_color_separator");
-
- // 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().width, 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++) {
- String text = items[i].xl_text;
- float font_h = items[i].separator && text != String() ? font_separator->get_height() : font->get_height();
-
- if (i == 0) {
- ofs.y += vseparation / 2;
- } else {
- ofs.y += vseparation;
- }
- Point2 item_ofs = ofs;
- Size2 icon_size;
- float h;
-
- if (!items[i].icon.is_null()) {
- icon_size = items[i].icon->get_size();
- h = MAX(icon_size.height, font_h);
- } else {
- h = font_h;
- }
-
- if (i == mouse_over) {
- hover->draw(ci, Rect2(item_ofs + Point2(0, -vseparation / 2), Size2(size.width - style->get_minimum_size().width, h + vseparation)));
- }
-
- item_ofs.x += items[i].h_ofs;
- if (items[i].separator) {
- if (text != String()) {
- int ss = font_separator->get_string_size(text).width / 2;
- int center = get_size().width / 2;
- int l = center - ss;
- int r = center + ss;
- if (l > item_ofs.x) {
- int sep_h = labeled_separator_left->get_center_size().height + labeled_separator_left->get_minimum_size().height;
- labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, l - item_ofs.x), sep_h)));
- }
- if (r < get_size().width - style->get_margin(MARGIN_RIGHT)) {
- int sep_h = labeled_separator_right->get_center_size().height + labeled_separator_right->get_minimum_size().height;
- labeled_separator_right->draw(ci, Rect2(Point2(r, item_ofs.y + Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, get_size().width - style->get_margin(MARGIN_RIGHT) - r), sep_h)));
- }
- } else {
- int sep_h = separator->get_center_size().height + separator->get_minimum_size().height;
- separator->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(get_size().width - style->get_minimum_size().width, sep_h)));
- }
- }
-
- Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
-
- if (items[i].checkable_type) {
- 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);
- }
-
- if (!items[i].icon.is_null()) {
- items[i].icon->draw(ci, item_ofs + Size2(check_ofs, 0) + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
- }
-
- if (items[i].submenu != "") {
- submenu->draw(ci, Point2(size.width - style->get_margin(MARGIN_RIGHT) - submenu->get_width(), item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
- }
-
- if (items[i].separator) {
- if (text != String()) {
- item_ofs.y += font_separator->get_ascent();
- int center = (get_size().width - font_separator->get_string_size(text).width) / 2;
- font_separator->draw(ci, Point2(center, item_ofs.y + Math::floor((h - font_h) / 2.0)), text, font_color_separator);
- }
- } else {
- item_ofs.x += icon_ofs + check_ofs;
- item_ofs.y += font->get_ascent();
- 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));
- }
-
- if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) {
- //accelerator
- String text2 = _get_accel_text(i);
- item_ofs.x = size.width - style->get_margin(MARGIN_RIGHT) - font->get_string_size(text2).width;
- font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text2, i == mouse_over ? font_color_hover : font_color_accel);
- }
-
- items.write[i]._ofs_cache = ofs.y;
-
- ofs.y += h;
- }
+ control->update();
} break;
case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
if (hide_on_window_lose_focus) {
@@ -632,17 +631,23 @@ void PopupMenu::_notification(int p_what) {
case NOTIFICATION_MOUSE_EXIT: {
if (mouse_over >= 0 && (items[mouse_over].submenu == "" || submenu_over != -1)) {
mouse_over = -1;
- update();
+ control->update();
}
} break;
case NOTIFICATION_POST_POPUP: {
initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
during_grabbed_click = (bool)initial_button_mask;
+ // Set margin on the margin container
+ Ref panel_style = get_stylebox("panel");
+ margin_container->add_constant_override("margin_top", panel_style->get_margin(Margin::MARGIN_TOP));
+ margin_container->add_constant_override("margin_bottom", panel_style->get_margin(Margin::MARGIN_BOTTOM));
+ margin_container->add_constant_override("margin_left", panel_style->get_margin(Margin::MARGIN_LEFT));
+ margin_container->add_constant_override("margin_right", panel_style->get_margin(Margin::MARGIN_RIGHT));
} break;
case NOTIFICATION_POPUP_HIDE: {
if (mouse_over >= 0) {
mouse_over = -1;
- update();
+ control->update();
}
for (int i = 0; i < items.size(); i++) {
@@ -680,7 +685,7 @@ void PopupMenu::add_item(const String &p_label, int p_id, uint32_t p_accel) {
Item item;
ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -689,7 +694,7 @@ void PopupMenu::add_icon_item(const Ref &p_icon, const String &p_label,
ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
item.icon = p_icon;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -698,7 +703,7 @@ void PopupMenu::add_check_item(const String &p_label, int p_id, uint32_t p_accel
ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -708,7 +713,7 @@ void PopupMenu::add_icon_check_item(const Ref &p_icon, const String &p_
item.icon = p_icon;
item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -717,7 +722,7 @@ void PopupMenu::add_radio_check_item(const String &p_label, int p_id, uint32_t p
ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -727,7 +732,7 @@ void PopupMenu::add_icon_radio_check_item(const Ref &p_icon, const Stri
item.icon = p_icon;
item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -737,7 +742,7 @@ void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int
item.max_states = p_max_states;
item.state = p_default_state;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -754,7 +759,7 @@ void PopupMenu::add_shortcut(const Ref &p_shortcut, int p_id, bool p_g
Item item;
ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -763,7 +768,7 @@ void PopupMenu::add_icon_shortcut(const Ref &p_icon, const Refupdate();
minimum_size_changed();
}
@@ -772,7 +777,7 @@ void PopupMenu::add_check_shortcut(const Ref &p_shortcut, int p_id, bo
ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -782,7 +787,7 @@ void PopupMenu::add_icon_check_shortcut(const Ref &p_icon, const Refupdate();
minimum_size_changed();
}
@@ -791,7 +796,7 @@ void PopupMenu::add_radio_check_shortcut(const Ref &p_shortcut, int p_
ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -801,7 +806,7 @@ void PopupMenu::add_icon_radio_check_shortcut(const Ref &p_icon, const
item.icon = p_icon;
item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -812,7 +817,7 @@ void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu,
item.id = p_id == -1 ? items.size() : p_id;
item.submenu = p_submenu;
items.push_back(item);
- update();
+ control->update();
minimum_size_changed();
}
@@ -826,14 +831,14 @@ void PopupMenu::set_item_text(int p_idx, const String &p_text) {
items.write[p_idx].text = p_text;
items.write[p_idx].xl_text = tr(p_text);
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_icon(int p_idx, const Ref &p_icon) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].icon = p_icon;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
@@ -841,14 +846,14 @@ void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
items.write[p_idx].checked = p_checked;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_id(int p_idx, int p_id) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].id = p_id;
- update();
+ control->update();
minimum_size_changed();
}
@@ -856,35 +861,35 @@ void PopupMenu::set_item_accelerator(int p_idx, uint32_t p_accel) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].accel = p_accel;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].metadata = p_meta;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].disabled = p_disabled;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].submenu = p_submenu;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::toggle_item_checked(int p_idx) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].checked = !items[p_idx].checked;
- update();
+ control->update();
minimum_size_changed();
}
@@ -966,7 +971,7 @@ int PopupMenu::get_item_state(int p_idx) const {
void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].separator = p_separator;
- update();
+ control->update();
}
bool PopupMenu::is_item_separator(int p_idx) const {
@@ -977,19 +982,19 @@ bool PopupMenu::is_item_separator(int p_idx) const {
void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
- update();
+ control->update();
}
void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
- update();
+ control->update();
}
void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].tooltip = p_tooltip;
- update();
+ control->update();
}
void PopupMenu::set_item_shortcut(int p_idx, const Ref &p_shortcut, bool p_global) {
@@ -1004,26 +1009,26 @@ void PopupMenu::set_item_shortcut(int p_idx, const Ref &p_shortcut, bo
_ref_shortcut(items[p_idx].shortcut);
}
- update();
+ control->update();
}
void PopupMenu::set_item_h_offset(int p_idx, int p_offset) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].h_ofs = p_offset;
- update();
+ control->update();
minimum_size_changed();
}
void PopupMenu::set_item_multistate(int p_idx, int p_state) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].state = p_state;
- update();
+ control->update();
}
void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
ERR_FAIL_INDEX(p_idx, items.size());
items.write[p_idx].shortcut_is_disabled = p_disabled;
- update();
+ control->update();
}
void PopupMenu::toggle_item_multistate(int p_idx) {
@@ -1037,7 +1042,7 @@ void PopupMenu::toggle_item_multistate(int p_idx) {
items.write[p_idx].state = 0;
}
- update();
+ control->update();
}
bool PopupMenu::is_item_checkable(int p_idx) const {
@@ -1065,7 +1070,7 @@ void PopupMenu::set_current_index(int p_idx) {
}
mouse_over = p_idx;
- update();
+ control->update();
}
int PopupMenu::get_current_index() const {
@@ -1195,7 +1200,7 @@ void PopupMenu::remove_item(int p_idx) {
}
items.remove(p_idx);
- update();
+ control->update();
minimum_size_changed();
}
@@ -1208,7 +1213,7 @@ void PopupMenu::add_separator(const String &p_text, int p_id) {
sep.xl_text = tr(p_text);
}
items.push_back(sep);
- update();
+ control->update();
}
void PopupMenu::clear() {
@@ -1219,7 +1224,7 @@ void PopupMenu::clear() {
}
items.clear();
mouse_over = -1;
- update();
+ control->update();
minimum_size_changed();
}
@@ -1346,6 +1351,15 @@ bool PopupMenu::get_allow_search() const {
return allow_search;
}
+void PopupMenu::set_max_height(real_t p_max_height) {
+ max_height = p_max_height;
+ minimum_size_changed();
+}
+
+real_t PopupMenu::get_max_height() const {
+ return max_height;
+}
+
void PopupMenu::set_hide_on_window_lose_focus(bool p_enabled) {
hide_on_window_lose_focus = p_enabled;
}
@@ -1470,12 +1484,19 @@ void PopupMenu::_bind_methods() {
ClassDB::bind_method(D_METHOD("_submenu_timeout"), &PopupMenu::_submenu_timeout);
+ ClassDB::bind_method(D_METHOD("_draw_items"), &PopupMenu::_draw_items);
+ ClassDB::bind_method(D_METHOD("_draw_background"), &PopupMenu::_draw_background);
+
+ ClassDB::bind_method(D_METHOD("set_max_height", "max_height"), &PopupMenu::set_max_height);
+ ClassDB::bind_method(D_METHOD("get_max_height"), &PopupMenu::get_max_height);
+
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "submenu_popup_delay"), "set_submenu_popup_delay", "get_submenu_popup_delay");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
+ ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_height"), "set_max_height", "get_max_height");
ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id")));
ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id")));
@@ -1492,6 +1513,29 @@ void PopupMenu::popup(const Rect2 &p_bounds) {
PopupMenu::PopupMenu() {
activated_by_keyboard = false;
+ // Margin Container
+ margin_container = memnew(MarginContainer);
+ margin_container->set_anchors_and_margins_preset(Control::PRESET_WIDE);
+ margin_container->set_mouse_filter(MouseFilter::MOUSE_FILTER_IGNORE);
+ add_child(margin_container);
+ margin_container->connect("draw", this, "_draw_background");
+
+ // Scroll Container
+ scroll_container = memnew(ScrollContainer);
+ scroll_container->set_clip_contents(true);
+ scroll_container->set_mouse_filter(MouseFilter::MOUSE_FILTER_PASS);
+ margin_container->add_child(scroll_container);
+
+ // The control which will display the items
+ control = memnew(Control);
+ control->set_clip_contents(false);
+ control->set_anchors_and_margins_preset(Control::PRESET_WIDE);
+ control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+ control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
+ control->set_mouse_filter(MouseFilter::MOUSE_FILTER_IGNORE);
+ scroll_container->add_child(control);
+ control->connect("draw", this, "_draw_items");
+
mouse_over = -1;
submenu_over = -1;
initial_button_mask = 0;
@@ -1501,6 +1545,8 @@ PopupMenu::PopupMenu() {
search_time_msec = 0;
search_string = "";
+ max_height = 0;
+
set_focus_mode(FOCUS_ALL);
set_as_toplevel(true);
set_hide_on_item_selection(true);
diff --git a/scene/gui/popup_menu.h b/scene/gui/popup_menu.h
index c10c7e7952a..ab7f0310142 100644
--- a/scene/gui/popup_menu.h
+++ b/scene/gui/popup_menu.h
@@ -31,7 +31,10 @@
#ifndef POPUP_MENU_H
#define POPUP_MENU_H
+#include "scene/gui/margin_container.h"
#include "scene/gui/popup.h"
+#include "scene/gui/scroll_container.h"
+#include "scene/gui/shortcut.h"
class PopupMenu : public Popup {
GDCLASS(PopupMenu, Popup);
@@ -56,11 +59,17 @@ class PopupMenu : public Popup {
String tooltip;
uint32_t accel;
int _ofs_cache;
+ int _height_cache;
int h_ofs;
Ref shortcut;
bool shortcut_is_global;
bool shortcut_is_disabled;
+ // Returns (0,0) if icon is null.
+ Size2 get_icon_size() const {
+ return icon.is_null() ? Size2() : icon->get_size();
+ }
+
Item() {
checked = false;
checkable_type = CHECKABLE_TYPE_NONE;
@@ -70,6 +79,7 @@ class PopupMenu : public Popup {
accel = 0;
disabled = false;
_ofs_cache = 0;
+ _height_cache = 0;
h_ofs = 0;
shortcut_is_global = false;
shortcut_is_disabled = false;
@@ -89,7 +99,10 @@ class PopupMenu : public Popup {
String _get_accel_text(int p_item) const;
int _get_mouse_over(const Point2 &p_over) const;
virtual Size2 get_minimum_size() const;
- void _scroll(float p_factor, const Point2 &p_over);
+
+ int _get_items_total_height() const;
+ void _scroll_to_item(int p_item);
+
void _gui_input(const Ref &p_event);
void _activate_submenu(int over, bool p_by_keyboard = false);
void _submenu_timeout();
@@ -113,6 +126,14 @@ class PopupMenu : public Popup {
uint64_t search_time_msec;
String search_string;
+ MarginContainer *margin_container;
+ ScrollContainer *scroll_container;
+ Control *control;
+ real_t max_height;
+
+ void _draw_items();
+ void _draw_background();
+
protected:
virtual bool has_point(const Point2 &p_point) const;
@@ -215,6 +236,9 @@ public:
void set_allow_search(bool p_allow);
bool get_allow_search() const;
+ void set_max_height(real_t p_max_height);
+ real_t get_max_height() const;
+
virtual void popup(const Rect2 &p_bounds = Rect2());
void set_hide_on_window_lose_focus(bool p_enabled);