From 92754e22f00cac59c4bf02de3d412df6a5d8f731 Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sun, 22 Jan 2023 20:03:11 +0100 Subject: [PATCH] Highlight stack on queue icon hovered --- client/battle/BattleInterfaceClasses.cpp | 22 +++++++++++++++++++++- client/battle/BattleInterfaceClasses.h | 3 +++ client/battle/BattleStacksController.cpp | 6 ++++++ client/battle/BattleStacksController.h | 2 +- client/battle/BattleWindow.cpp | 5 +++++ client/battle/BattleWindow.h | 3 +++ lib/battle/CBattleInfoEssentials.cpp | 15 +++++++++++++++ lib/battle/CBattleInfoEssentials.h | 1 + 8 files changed, 55 insertions(+), 2 deletions(-) diff --git a/client/battle/BattleInterfaceClasses.cpp b/client/battle/BattleInterfaceClasses.cpp index 599d3c5ca..5ecbb2e70 100644 --- a/client/battle/BattleInterfaceClasses.cpp +++ b/client/battle/BattleInterfaceClasses.cpp @@ -807,8 +807,21 @@ int32_t StackQueue::getSiegeShooterIconID() return owner.siegeController->getSiegedTown()->town->faction->getIndex(); } +boost::optional StackQueue::getHoveredUnitIdIfAny() const +{ + for(const auto & stackBox : stackBoxes) + { + if(stackBox->hovered) + { + return stackBox->getBoundUnitID(); + } + } + + return boost::none; +} + StackQueue::StackBox::StackBox(StackQueue * owner): - owner(owner) + CIntObject(HOVER), owner(owner) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); background = std::make_shared(owner->embedded ? "StackQueueSmall" : "StackQueueLarge"); @@ -838,6 +851,7 @@ void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn) { if(unit) { + boundUnitID = unit->unitId(); background->colorize(unit->unitOwner()); icon->visible = true; @@ -872,6 +886,7 @@ void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn) } else { + boundUnitID = boost::none; background->colorize(PlayerColor::NEUTRAL); icon->visible = false; icon->setFrame(0); @@ -881,3 +896,8 @@ void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn) stateIcon->visible = false; } } + +boost::optional StackQueue::StackBox::getBoundUnitID() const +{ + return boundUnitID; +} diff --git a/client/battle/BattleInterfaceClasses.h b/client/battle/BattleInterfaceClasses.h index 5413a1399..ba6ebe4f8 100644 --- a/client/battle/BattleInterfaceClasses.h +++ b/client/battle/BattleInterfaceClasses.h @@ -196,6 +196,7 @@ class StackQueue : public CIntObject class StackBox : public CIntObject { StackQueue * owner; + boost::optional boundUnitID; public: std::shared_ptr background; std::shared_ptr icon; @@ -204,6 +205,7 @@ class StackQueue : public CIntObject void setUnit(const battle::Unit * unit, size_t turn = 0); StackBox(StackQueue * owner); + boost::optional getBoundUnitID() const; }; static const int QUEUE_SIZE = 10; @@ -220,6 +222,7 @@ public: StackQueue(bool Embedded, BattleInterface & owner); void update(); + boost::optional getHoveredUnitIdIfAny() const; void show(SDL_Surface * to) override; }; diff --git a/client/battle/BattleStacksController.cpp b/client/battle/BattleStacksController.cpp index 8ef6b8107..dabd2e6e3 100644 --- a/client/battle/BattleStacksController.cpp +++ b/client/battle/BattleStacksController.cpp @@ -897,6 +897,12 @@ std::vector BattleStacksController::selectHoveredStacks() if(owner.getAnimationCondition(EAnimationEvents::ACTION) == true) return {}; + auto hoveredQueueUnitId = owner.windowObject->getQueueHoveredUnitId(); + if(hoveredQueueUnitId.has_value()) + { + return { owner.curInt->cb->battleGetStackByUnitId(hoveredQueueUnitId.value(), true) }; + } + auto hoveredHex = owner.fieldController->getHoveredHex(); if (!hoveredHex.isValid()) diff --git a/client/battle/BattleStacksController.h b/client/battle/BattleStacksController.h index 92ffdaae0..1ccf7ceef 100644 --- a/client/battle/BattleStacksController.h +++ b/client/battle/BattleStacksController.h @@ -70,7 +70,7 @@ class BattleStacksController /// currently active stack; nullptr - no one const CStack *activeStack; - /// stacks below mouse pointer (multiple stacks possible while spellcasting), used for border animation + /// stacks or their battle queue images below mouse pointer (multiple stacks possible while spellcasting), used for border animation std::vector mouseHoveredStacks; ///when animation is playing, we should wait till the end to make the next stack active; nullptr of none diff --git a/client/battle/BattleWindow.cpp b/client/battle/BattleWindow.cpp index f47a559ac..a780c1e49 100644 --- a/client/battle/BattleWindow.cpp +++ b/client/battle/BattleWindow.cpp @@ -550,6 +550,11 @@ void BattleWindow::blockUI(bool on) } } +boost::optional BattleWindow::getQueueHoveredUnitId() +{ + return queue->getHoveredUnitIdIfAny(); +} + void BattleWindow::showAll(SDL_Surface *to) { CIntObject::showAll(to); diff --git a/client/battle/BattleWindow.h b/client/battle/BattleWindow.h index 696f92a85..dd80585c4 100644 --- a/client/battle/BattleWindow.h +++ b/client/battle/BattleWindow.h @@ -74,6 +74,9 @@ public: /// Refresh queue after turn order changes void updateQueue(); + /// Get mouse-hovered battle queue unit ID if any found + boost::optional getQueueHoveredUnitId(); + void activate() override; void deactivate() override; void keyPressed(const SDL_KeyboardEvent & key) override; diff --git a/lib/battle/CBattleInfoEssentials.cpp b/lib/battle/CBattleInfoEssentials.cpp index e7e0062aa..e72ed608f 100644 --- a/lib/battle/CBattleInfoEssentials.cpp +++ b/lib/battle/CBattleInfoEssentials.cpp @@ -185,6 +185,21 @@ const CStack* CBattleInfoEssentials::battleGetStackByID(int ID, bool onlyAlive) return stacks[0]; } +const CStack* CBattleInfoEssentials::battleGetStackByUnitId(int unitId, bool onlyAlive) const +{ + RETURN_IF_NOT_BATTLE(nullptr); + + auto stacks = battleGetStacksIf([=](const CStack * s) + { + return s->unitId() == unitId && (!onlyAlive || s->alive()); + }); + + if(stacks.empty()) + return nullptr; + else + return stacks[0]; +} + bool CBattleInfoEssentials::battleDoWeKnowAbout(ui8 side) const { RETURN_IF_NOT_BATTLE(false); diff --git a/lib/battle/CBattleInfoEssentials.h b/lib/battle/CBattleInfoEssentials.h index 117e352b3..f2ee32893 100644 --- a/lib/battle/CBattleInfoEssentials.h +++ b/lib/battle/CBattleInfoEssentials.h @@ -102,6 +102,7 @@ public: TStacks battleGetAllStacks(bool includeTurrets = false) const; const CStack * battleGetStackByID(int ID, bool onlyAlive = true) const; //returns stack info by given ID + const CStack * battleGetStackByUnitId(int unitId, bool onlyAlive = true) const; //returns stack info by given ID bool battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const; ///returns player that controls given stack; mind control included