Skip to content

Commit

Permalink
Lock prev/next button when those buttons would do nothing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Apr 4, 2024
1 parent 59e7789 commit e47236f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/lib/game/data/gui/unitselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ bool cUnitSelection::canSelect (const cUnit* unit) const
return unit && !(unit->isABuilding() && static_cast<const cBuilding*> (unit)->isRubble());
}

//------------------------------------------------------------------------------
bool cUnitSelection::canSelectNextUnit (const cPlayer& player, const std::vector<unsigned int>& doneList) const
{
const auto currentSelected = getSelectedUnit();
const auto next = getNextUnit (player, doneList, currentSelected);
return next != nullptr && next != currentSelected;
}

//------------------------------------------------------------------------------
bool cUnitSelection::selectNextUnit (const cPlayer& player, const std::vector<unsigned int>& doneList)
{
Expand Down
2 changes: 2 additions & 0 deletions src/lib/game/data/gui/unitselection.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class cUnitSelection

bool canSelect (const cUnit*) const;

bool canSelectNextUnit (const cPlayer&, const std::vector<unsigned int>& doneList) const;

mutable cSignal<void()> selectionChanged;
mutable cSignal<void()> mainSelectionChanged;
mutable cSignal<void()> groupSelectionChanged;
Expand Down
15 changes: 14 additions & 1 deletion src/ui/graphical/game/control/gameguicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,18 @@ void cGameGuiController::connectGuiStaticCommands()

signalConnectionManager.connect (gameGui->getHud().nextClicked, [this]() { selectNextUnit(); });
signalConnectionManager.connect (gameGui->getHud().prevClicked, [this]() { selectPreviousUnit(); });
signalConnectionManager.connect (gameGui->getHud().doneClicked, [this]() {
const auto updatePrevNextButtons = [this]() {
const auto player = getActivePlayer();
if (!player)
{
gameGui->getHud().setCanSelectNextUnit (false);
return;
}
const auto& unitSelection = gameGui->getGameMap().getUnitSelection();
const bool canSelectNextUnit = unitSelection.canSelectNextUnit (*player, playerGameGuiStates[player->getId()].doneList);
gameGui->getHud().setCanSelectNextUnit (canSelectNextUnit);
};
signalConnectionManager.connect (gameGui->getHud().doneClicked, [=]() {
auto keyboard = application.getActiveKeyboard();
if (keyboard && keyboard->isAnyModifierActive (toEnumFlag (eKeyModifierType::Ctrl)))
{
Expand All @@ -583,8 +594,10 @@ void cGameGuiController::connectGuiStaticCommands()
else
{
markSelectedUnitAsDone();
updatePrevNextButtons();
}
});
signalConnectionManager.connect (gameGui->getGameMap().getUnitSelection().selectionChanged, updatePrevNextButtons);

signalConnectionManager.connect (gameGui->getHud().reportsClicked, [this]() { showReportsWindow(); });

Expand Down
19 changes: 17 additions & 2 deletions src/ui/graphical/game/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ cHud::cHud (std::shared_ptr<cAnimationTimer> animationTimer)
chatButton = emplaceChild<cCheckBox> (cPosition (51, 252), lngPack.i18n ("Others~Chat"), eUnicodeFontType::LatinSmallWhite, eCheckBoxTextAnchor::Left, eCheckBoxType::HudChat);
signalConnectionManager.connect (chatButton->toggled, [this]() { chatToggled(); });

auto nextButton = emplaceChild<cPushButton> (cPosition (124, 227), ePushButtonType::HudNext, ">>");
nextButton = emplaceChild<cPushButton> (cPosition (124, 227), ePushButtonType::HudNext, ">>");
nextButton->addClickShortcut (KeysList.keyUnitNext);
signalConnectionManager.connect (nextButton->clicked, [this]() { nextClicked(); });
auto prevButton = emplaceChild<cPushButton> (cPosition (60, 227), ePushButtonType::HudPrev, "<<");
prevButton = emplaceChild<cPushButton> (cPosition (60, 227), ePushButtonType::HudPrev, "<<");
prevButton->addClickShortcut (KeysList.keyUnitPrev);
signalConnectionManager.connect (prevButton->clicked, [this]() { prevClicked(); });
doneButton = emplaceChild<cPushButton> (cPosition (99, 227), ePushButtonType::HudDone, lngPack.i18n ("Others~Proceed_4"));
Expand Down Expand Up @@ -598,3 +598,18 @@ void cHud::setActiveUnit (const cUnit* unit)
unitVideo->setUnit (unit);
unitDetails->setUnit (unit);
}

//------------------------------------------------------------------------------
void cHud::setCanSelectNextUnit(bool value)
{
if (value)
{
prevButton->unlock();
nextButton->unlock();
}
else
{
prevButton->lock();
nextButton->lock();
}
}
5 changes: 4 additions & 1 deletion src/ui/graphical/game/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class cHud : public cWidget
void activateShortcuts();
void deactivateShortcuts();

void setCanSelectNextUnit (bool);

mutable cSignal<void()> zoomChanged;

mutable cSignal<void()> surveyToggled;
Expand Down Expand Up @@ -163,7 +165,6 @@ class cHud : public cWidget
private:
void handleZoomPlusClicked();
void handleZoomMinusClicked();
void handlePreferencesClicked();

private:
UniqueSurface surface;
Expand Down Expand Up @@ -194,7 +195,9 @@ class cHud : public cWidget
cCheckBox* chatButton = nullptr;

cPushButton* reportsButton = nullptr;
cPushButton* prevButton = nullptr;
cPushButton* doneButton = nullptr;
cPushButton* nextButton = nullptr;

cShortcut* surveyShortcut = nullptr;
cShortcut* hitsShortcut = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/graphical/game/widgets/gamemapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ cGameMapWidget::cGameMapWidget (const cBox<cPosition>& area, std::shared_ptr<con
const auto selectedUnit = unitSelection.getSelectedUnit();
if (!selectedUnit) return;

const auto shortcutsUpdater = [this]() { updateActiveUnitCommandShortcuts(); };
const auto shortcutsUpdater = [this]() { updateActiveUnitCommandShortcuts(); unitSelection.selectionChanged(); };
selectedUnitSignalConnectionManager.connect (selectedUnit->data.shotsChanged, shortcutsUpdater);
selectedUnitSignalConnectionManager.connect (selectedUnit->storedResourcesChanged, shortcutsUpdater);
selectedUnitSignalConnectionManager.connect (selectedUnit->positionChanged, shortcutsUpdater);
Expand Down

0 comments on commit e47236f

Please sign in to comment.