From 54f6334f40c197030473e05801176f76c76663a6 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Wed, 23 Oct 2024 11:07:49 +0800 Subject: [PATCH] Fix `PopupMenu` margin and separation calculations --- scene/gui/popup_menu.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 27a66d84a..470f8c62e 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -35,8 +35,6 @@ #include "core/input/shortcut.h" #include "core/os/keyboard.h" #include "core/os/os.h" -#include "core/string/print_string.h" -#include "core/string/translation.h" #include "scene/gui/margin_container.h" #include "scene/gui/scroll_bar.h" #include "scene/gui/scroll_container.h" @@ -144,8 +142,7 @@ int PopupMenu::_get_items_total_height() const { 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; + return items_total_height; } void PopupMenu::_scroll_to_item(int p_item) { @@ -174,16 +171,12 @@ int PopupMenu::_get_mouse_over(const Point2 &p_over) const { int vseparation = get_theme_constant("vseparation"); float font_h = get_theme_font("font")->get_height(); - Point2 ofs = style->get_offset() + Point2(0, vseparation / 2); + real_t ofs = style->get_margin(MARGIN_TOP) + control->get_position().y; for (int i = 0; i < items.size(); i++) { - if (i > 0) { - ofs.y += vseparation; - } + ofs += MAX(items[i].get_icon_size().height, font_h) + vseparation; - ofs.y += MAX(items[i].get_icon_size().height, font_h); - - if (p_over.y - control->get_position().y < ofs.y) { + if (p_over.y < ofs) { return i; } } @@ -354,7 +347,7 @@ void PopupMenu::_gui_input(const Ref &p_event) { // Make an area which does not include v scrollbar, so that items are not activated when dragging scrollbar. Transform2D xform = get_global_transform_with_canvas(); - Point2 item_origin = xform.get_origin(); + Point2 item_origin = scroll_container->get_global_position(); float scroll_width = scroll_container->get_v_scrollbar()->is_visible_in_tree() ? scroll_container->get_v_scrollbar()->get_size().width : 0; Size2 item_size = (control->get_global_rect().get_size() - Vector2(scroll_width, 0)) * xform.get_scale(); Rect2 item_clickable_area = Rect2(item_origin, item_size); @@ -493,9 +486,9 @@ void PopupMenu::_draw_items() { margin_size.width = margin_container->get_theme_constant("margin_right") + margin_container->get_theme_constant("margin_left"); margin_size.height = margin_container->get_theme_constant("margin_top") + margin_container->get_theme_constant("margin_bottom"); - Ref style = get_theme_stylebox("panel"); Ref hover = get_theme_stylebox("hover"); Ref font = get_theme_font("font"); + select_font(font); // In Item::checkable_type enum order (less the non-checkable member) @@ -536,7 +529,7 @@ void PopupMenu::_draw_items() { check_ofs = MAX(get_theme_icon("checked")->get_width(), get_theme_icon("radio_checked")->get_width()) + hseparation; } - Point2 ofs = Point2(); + Point2 ofs = Point2(0, vseparation / 2); // Loop through all items and draw each. for (int i = 0; i < items.size(); i++) {