Skip to content

Commit

Permalink
Merge pull request #80271 from Ymanawat/optionmenu-scroll-test
Browse files Browse the repository at this point in the history
Fix scrolling on keyboard/controller input
  • Loading branch information
akien-mga committed Oct 9, 2023
2 parents c6635b4 + a16fdb0 commit c1fed53
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions scene/gui/popup_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2170,14 +2170,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);
}
}

Expand Down

0 comments on commit c1fed53

Please sign in to comment.