Skip to content

Commit

Permalink
Fix #5290
Browse files Browse the repository at this point in the history
  • Loading branch information
Alayan-stk-2 committed Feb 3, 2025
1 parent 3cca6a7 commit b4d6253
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/guiengine/event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,10 @@ void EventHandler::navigate(const NavigationDirection nav, const int playerID)
assert(ribbon != NULL);
if (ribbon->getRibbonType() == GUIEngine::RibbonType::RIBBON_VERTICAL_TABS)
{
int new_selection = (nav == NAV_UP) ?
ribbon->getActiveChildrenNumber(playerID) - 1 : 0;
ribbon->setSelection(new_selection, playerID);
// Some of the tabs may be disabled. So we navigate until an active tab is found
(nav == NAV_UP) ? ribbon->setLastSelection(playerID)
: ribbon->setFirstSelection(playerID);

// The tab selection triggers an action
sendEventToUser(ribbon, ribbon->m_properties[PROP_ID], playerID);
}
Expand Down
24 changes: 24 additions & 0 deletions src/guiengine/widgets/ribbon_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,30 @@ void RibbonWidget::removeChildNamed(const char* name)
}
}

// ----------------------------------------------------------------------------

/** Sets the first active item within the ribbon as selected,
* or the last one if 'reverse_order' is true. */
void RibbonWidget::setFirstSelection(const int playerID, bool reverse_order)
{
// Despite the name, "active children" includes elements that have been created
// and disabled with setActive(false)
// TODO: rename it to be clearer
int id = (reverse_order) ? getActiveChildrenNumber(playerID) - 1 : 0;
while (id >= 0 && id <= (getActiveChildrenNumber(playerID) - 1))
{
if (m_active_children[id].isActivated())
break;

id = (reverse_order) ? id - 1 : id + 1;
}

// If every single element within the ribbon is desactivated, we will pick
// the last (first) even if disabled, but this case should not arise.
m_selection[playerID] = id;
updateSelection();
} // setFirstSelection

// ----------------------------------------------------------------------------

void RibbonWidget::select(std::string item, const int mousePlayerID)
Expand Down
7 changes: 7 additions & 0 deletions src/guiengine/widgets/ribbon_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ namespace GUIEngine
/** Sets the ID of the selected item within the ribbon */
void setSelection(const int i, const int playerID)
{ m_selection[playerID] = i; updateSelection(); }

/** Sets the first active item within the ribbon as selected */
void setFirstSelection(const int playerID, bool reverse_order = false);

/** Sets the last active item within the ribbon as selected */
void setLastSelection(const int playerID)
{ setFirstSelection(playerID, true); }

/** Select an item in the ribbon by its internal name */
void select(std::string item, const int playerID);
Expand Down

0 comments on commit b4d6253

Please sign in to comment.