Skip to content

Commit

Permalink
Groups UI: Ensure the selected group's page is displayed
Browse files Browse the repository at this point in the history
Including when using keybinds to switch groups
  • Loading branch information
past-due committed Aug 29, 2023
1 parent 8571bcb commit 33e84fc
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/droid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,12 @@ void assignDroidsToGroup(UDWORD playerNumber, UDWORD groupNumber, bool clearGrou
}
if (bAtLeastOne || numCleared > 0)
{
intGroupsChanged();
optional<UBYTE> newSelectedGroup;
if (bAtLeastOne)
{
newSelectedGroup = groupNumber;
}
intGroupsChanged(newSelectedGroup);
}
}

Expand Down Expand Up @@ -1961,7 +1966,7 @@ bool activateGroupAndMove(UDWORD playerNumber, UDWORD groupNumber)

if (selected || numDeselected > 0)
{
intGroupsChanged(true);
intGroupsChanged((selected) ? groupNumber : UBYTE_MAX);
}

return selected;
Expand Down Expand Up @@ -1998,7 +2003,7 @@ bool activateNoGroup(UDWORD playerNumber, const SELECTIONTYPE selectionType, con
psFlagPos->selected = false;
}
}
intGroupsChanged(true);
intGroupsChanged(UBYTE_MAX);
CONPRINTF(ngettext("%u unit selected", "%u units selected", selectionCount), selectionCount);
return selected;
}
Expand Down Expand Up @@ -2044,7 +2049,7 @@ bool activateGroup(UDWORD playerNumber, UDWORD groupNumber)
}
if (selected || numDeselected > 0)
{
intGroupsChanged(true);
intGroupsChanged((selected) ? groupNumber : UBYTE_MAX);
}
return selected;
}
Expand Down
6 changes: 5 additions & 1 deletion src/hci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2030,11 +2030,15 @@ void intAlliedResearchChanged()
}
}

void intGroupsChanged(bool selectionOnly)
void intGroupsChanged(optional<UBYTE> selectedGroup)
{
if (getGroupButtonEnabled())
{
intRefreshGroupsUI();
if (selectedGroup.has_value() && selectedGroup.value() != UBYTE_MAX)
{
groupsUI->updateSelectedGroup(selectedGroup.value());
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/hci.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ typedef std::function<void (const int)> playerCallbackFunc; // callback function

#include "message.h"

#include <nonstd/optional.hpp>
using nonstd::optional;
using nonstd::nullopt;

class MultipleChoiceButton;
class WIDGET;
struct DROID;
Expand Down Expand Up @@ -305,7 +309,7 @@ void intResearchFinished(STRUCTURE *psBuilding);
void intAlliedResearchChanged();

/* Tell the interface that groups have changed */
void intGroupsChanged(bool selectionOnly = false);
void intGroupsChanged(optional<UBYTE> selectedGroup = nullopt);
void intGroupDamaged(UBYTE group, uint64_t additionalDamage, bool unitKilled);

/* Sync the interface to an object */
Expand Down
11 changes: 11 additions & 0 deletions src/hci/groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ void GroupsForum::updateData()
groupsUIController->updateData();
}

void GroupsForum::updateSelectedGroup(size_t group)
{
size_t groupButtonIdx = (group > 0) ? group - 1 : 9;
if (groupButtonIdx >= groupsList->childrenSize())
{
return;
}
// ensure that the page that contains this group is displayed
groupsList->goToChildPage(groupButtonIdx);
}

void GroupsForum::addGroupDamageForCurrentTick(size_t group, uint64_t additionalDamage, bool unitKilled)
{
groupsUIController->addGroupDamageForCurrentTick(group, additionalDamage, unitKilled);
Expand Down
1 change: 1 addition & 0 deletions src/hci/groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class GroupsForum: public IntFormAnimated
return widget;
}
void updateData();
void updateSelectedGroup(size_t group);
void addGroupDamageForCurrentTick(size_t group, uint64_t additionalDamage, bool unitKilled);
private:
std::shared_ptr<GroupButton> makeGroupButton(size_t groupNumber);
Expand Down
2 changes: 1 addition & 1 deletion src/keybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ void kf_SelectGrouping(UDWORD groupNumber)
{
Selected = activateGroup(selectedPlayer, groupNumber);
}
intGroupsChanged(true);
intGroupsChanged(groupNumber);

/* play group audio but only if they weren't already selected - AM */
if (Selected && !bAlreadySelected)
Expand Down

0 comments on commit 33e84fc

Please sign in to comment.