summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYogendra Manawat <yogendramanawat@gmail.com>2023-08-04 22:54:20 +0530
committerYogendra Manawat <yogendramanawat@gmail.com>2023-08-30 23:31:22 +0530
commita16fdb05aee82a52f631ad1e5209e65ab85d6c30 (patch)
treeaf47b12e0897cde716bba87d9cc18f223b0768ba
parenteb4301b941fa211de204e37bd4d701f7e490a945 (diff)
downloadredot-engine-a16fdb05aee82a52f631ad1e5209e65ab85d6c30.tar.gz
Fix scrolling popup_menu On keyboard/controller input
-rw-r--r--scene/gui/popup_menu.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp
index 40db8deaac..3451a75a41 100644
--- a/scene/gui/popup_menu.cpp
+++ b/scene/gui/popup_menu.cpp
@@ -1803,14 +1803,16 @@ int PopupMenu::get_item_count() const {
void PopupMenu::scroll_to_item(int p_idx) {
ERR_FAIL_INDEX(p_idx, items.size());
- // Scroll item into view (upwards).
- if (items[p_idx]._ofs_cache - scroll_container->get_v_scroll() < -control->get_position().y) {
- scroll_container->set_v_scroll(items[p_idx]._ofs_cache + control->get_position().y);
- }
-
- // Scroll item into view (downwards).
- if (items[p_idx]._ofs_cache + items[p_idx]._height_cache - scroll_container->get_v_scroll() > -control->get_position().y + scroll_container->get_size().height) {
- scroll_container->set_v_scroll(items[p_idx]._ofs_cache + items[p_idx]._height_cache + control->get_position().y);
+ // Calculate the position of the item relative to the visible area.
+ int item_y = items[p_idx]._ofs_cache;
+ int visible_height = scroll_container->get_size().height;
+ int relative_y = item_y - scroll_container->get_v_scroll();
+
+ // If item is not fully visible, adjust scroll.
+ if (relative_y < 0) {
+ scroll_container->set_v_scroll(item_y);
+ } else if (relative_y + items[p_idx]._height_cache > visible_height) {
+ scroll_container->set_v_scroll(item_y + items[p_idx]._height_cache - visible_height);
}
}