diff --git a/client/gui/CIntObject.cpp b/client/gui/CIntObject.cpp index debc01927..3f136151e 100644 --- a/client/gui/CIntObject.cpp +++ b/client/gui/CIntObject.cpp @@ -314,20 +314,12 @@ CKeyShortcut::CKeyShortcut(EShortcut key) { } -CKeyShortcut::CKeyShortcut(const EShortcut & key, const KeyPressedFunctor & keyPressedCallback) - : CKeyShortcut(key) -{ - this->keyPressedCallback = keyPressedCallback; -} - void CKeyShortcut::keyPressed(EShortcut key) { if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed) { shortcutPressed = true; clickPressed(GH.getCursorPosition()); - if(keyPressedCallback) - keyPressedCallback(); } } diff --git a/client/gui/CIntObject.h b/client/gui/CIntObject.h index 61224a122..bde3c1f81 100644 --- a/client/gui/CIntObject.h +++ b/client/gui/CIntObject.h @@ -134,18 +134,12 @@ public: class CKeyShortcut : public virtual CIntObject { public: - using KeyPressedFunctor = std::function; - + bool shortcutPressed; EShortcut assignedKey; CKeyShortcut(); CKeyShortcut(EShortcut key); - CKeyShortcut(const EShortcut & key, const KeyPressedFunctor & keyPressedCallback); void keyPressed(EShortcut key) override; void keyReleased(EShortcut key) override; - -private: - bool shortcutPressed; - KeyPressedFunctor keyPressedCallback; }; class WindowBase : public CIntObject diff --git a/client/widgets/CArtifactsOfHeroBase.h b/client/widgets/CArtifactsOfHeroBase.h index db5eef332..cece27267 100644 --- a/client/widgets/CArtifactsOfHeroBase.h +++ b/client/widgets/CArtifactsOfHeroBase.h @@ -13,7 +13,7 @@ class CButton; -class CArtifactsOfHeroBase : public CIntObject +class CArtifactsOfHeroBase : virtual public CIntObject { protected: using ArtPlacePtr = std::shared_ptr; diff --git a/client/widgets/CArtifactsOfHeroMain.cpp b/client/widgets/CArtifactsOfHeroMain.cpp index 991ff6b68..e6953cb07 100644 --- a/client/widgets/CArtifactsOfHeroMain.cpp +++ b/client/widgets/CArtifactsOfHeroMain.cpp @@ -35,15 +35,56 @@ CArtifactsOfHeroMain::~CArtifactsOfHeroMain() void CArtifactsOfHeroMain::enableArtifactsCostumeSwitcher() { - size_t costumeIdx = 0; - for(const auto & hotkey : costumesSwitcherHotkeys) + addUsedEvents(AEventsReceiver::KEYBOARD); +} + +void CArtifactsOfHeroMain::keyPressed(EShortcut key) +{ + if(!shortcutPressed) { - auto keyProc = costumesSwitcherProcessors.emplace_back(std::make_shared(hotkey, - [this, costumeIdx]() - { - LOCPLINT->cb->manageHeroCostume(getHero()->id, costumeIdx, GH.isKeyboardCtrlDown()); - })); - keyProc->addUsedEvents(AEventsReceiver::KEYBOARD); - costumeIdx++; + uint32_t costumeIdx; + switch(key) + { + case EShortcut::HERO_COSTUME_0: + costumeIdx = 0; + break; + case EShortcut::HERO_COSTUME_1: + costumeIdx = 1; + break; + case EShortcut::HERO_COSTUME_2: + costumeIdx = 2; + break; + case EShortcut::HERO_COSTUME_3: + costumeIdx = 3; + break; + case EShortcut::HERO_COSTUME_4: + costumeIdx = 4; + break; + case EShortcut::HERO_COSTUME_5: + costumeIdx = 5; + break; + case EShortcut::HERO_COSTUME_6: + costumeIdx = 6; + break; + case EShortcut::HERO_COSTUME_7: + costumeIdx = 7; + break; + case EShortcut::HERO_COSTUME_8: + costumeIdx = 8; + break; + case EShortcut::HERO_COSTUME_9: + costumeIdx = 9; + break; + default: + return; + } + shortcutPressed = true; + LOCPLINT->cb->manageHeroCostume(getHero()->id, costumeIdx, GH.isKeyboardCtrlDown()); } } + +void CArtifactsOfHeroMain::keyReleased(EShortcut key) +{ + if(vstd::contains(costumesSwitcherHotkeys, key)) + shortcutPressed = false; +} diff --git a/client/widgets/CArtifactsOfHeroMain.h b/client/widgets/CArtifactsOfHeroMain.h index ee28ace28..c3aa837e9 100644 --- a/client/widgets/CArtifactsOfHeroMain.h +++ b/client/widgets/CArtifactsOfHeroMain.h @@ -13,12 +13,14 @@ #include "../gui/Shortcut.h" -class CArtifactsOfHeroMain : public CArtifactsOfHeroBase +class CArtifactsOfHeroMain : public CArtifactsOfHeroBase, public CKeyShortcut { public: CArtifactsOfHeroMain(const Point & position); ~CArtifactsOfHeroMain() override; void enableArtifactsCostumeSwitcher(); + void keyPressed(EShortcut key) override; + void keyReleased(EShortcut key) override; private: const std::vector costumesSwitcherHotkeys = @@ -34,5 +36,4 @@ private: EShortcut::HERO_COSTUME_8, EShortcut::HERO_COSTUME_9 }; - std::vector> costumesSwitcherProcessors; }; diff --git a/client/windows/CWindowWithArtifacts.cpp b/client/windows/CWindowWithArtifacts.cpp index b8ce160a9..92ce7a737 100644 --- a/client/windows/CWindowWithArtifacts.cpp +++ b/client/windows/CWindowWithArtifacts.cpp @@ -343,7 +343,7 @@ void CWindowWithArtifacts::deactivate() CWindowObject::deactivate(); } -void CWindowWithArtifacts::enableArtifactsCostumeSwitcher() +void CWindowWithArtifacts::enableArtifactsCostumeSwitcher() const { for(auto artSet : artSets) std::visit( diff --git a/client/windows/CWindowWithArtifacts.h b/client/windows/CWindowWithArtifacts.h index e8d98673d..41d960de3 100644 --- a/client/windows/CWindowWithArtifacts.h +++ b/client/windows/CWindowWithArtifacts.h @@ -42,7 +42,7 @@ public: void gestureArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); void activate() override; void deactivate() override; - void enableArtifactsCostumeSwitcher(); + void enableArtifactsCostumeSwitcher() const; virtual void artifactRemoved(const ArtifactLocation & artLoc); virtual void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw); diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 1453325a6..c840e8d45 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -249,7 +249,7 @@ class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet { public: CArtifactFittingSet(ArtBearer::ArtBearer Bearer); - CArtifactFittingSet(const CArtifactSet & artSet); + explicit CArtifactFittingSet(const CArtifactSet & artSet); ArtBearer::ArtBearer bearerType() const override; protected: diff --git a/lib/networkPacks/NetPacksLib.cpp b/lib/networkPacks/NetPacksLib.cpp index 6035faa2d..bb236e583 100644 --- a/lib/networkPacks/NetPacksLib.cpp +++ b/lib/networkPacks/NetPacksLib.cpp @@ -1078,7 +1078,7 @@ void ChangeArtifactsCostume::applyGs(CGameState * gs) const if(const auto & costume = allCostumes.find(costumeIdx); costume != allCostumes.end()) costume->second = costumeSet; else - allCostumes.emplace(costumeIdx, costumeSet); + allCostumes.try_emplace(costumeIdx, costumeSet); } void PlayerEndsGame::applyGs(CGameState * gs) const diff --git a/lib/networkPacks/PacksForClient.h b/lib/networkPacks/PacksForClient.h index ce22f1c34..d7d74d909 100644 --- a/lib/networkPacks/PacksForClient.h +++ b/lib/networkPacks/PacksForClient.h @@ -1291,14 +1291,14 @@ struct DLL_LINKAGE ChangeObjectVisitors : public CPackForClient struct DLL_LINKAGE ChangeArtifactsCostume : public CPackForClient { std::map costumeSet; - size_t costumeIdx = 0; + uint32_t costumeIdx = 0; const PlayerColor player = PlayerColor::NEUTRAL; void applyGs(CGameState * gs) const; void visitTyped(ICPackVisitor & visitor) override; ChangeArtifactsCostume() = default; - ChangeArtifactsCostume(const PlayerColor & player, const size_t costumeIdx) + ChangeArtifactsCostume(const PlayerColor & player, const uint32_t costumeIdx) : costumeIdx(costumeIdx) , player(player) { diff --git a/lib/networkPacks/PacksForServer.h b/lib/networkPacks/PacksForServer.h index be4e71249..9db178053 100644 --- a/lib/networkPacks/PacksForServer.h +++ b/lib/networkPacks/PacksForServer.h @@ -432,7 +432,7 @@ struct DLL_LINKAGE ManageBackpackArtifacts : public CPackForServer struct DLL_LINKAGE ManageEquippedArtifacts : public CPackForServer { ManageEquippedArtifacts() = default; - ManageEquippedArtifacts(const ObjectInstanceID & artHolder, const size_t costumeIdx, bool saveCostume = false) + ManageEquippedArtifacts(const ObjectInstanceID & artHolder, const uint32_t costumeIdx, bool saveCostume = false) : artHolder(artHolder) , costumeIdx(costumeIdx) , saveCostume(saveCostume) @@ -440,7 +440,7 @@ struct DLL_LINKAGE ManageEquippedArtifacts : public CPackForServer } ObjectInstanceID artHolder; - size_t costumeIdx; + uint32_t costumeIdx; bool saveCostume; void visitTyped(ICPackVisitor & visitor) override; diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 24ba4e5ba..cad7f0d0a 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -2892,7 +2892,7 @@ bool CGameHandler::scrollBackpackArtifacts(const PlayerColor & player, const Obj return true; } -bool CGameHandler::saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx) +bool CGameHandler::saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx) { auto artSet = getArtSet(heroID); COMPLAIN_RET_FALSE_IF(artSet == nullptr, "saveArtifactsCostume: wrong hero's ID"); @@ -2908,7 +2908,7 @@ bool CGameHandler::saveArtifactsCostume(const PlayerColor & player, const Object return true; } -bool CGameHandler::switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx) +bool CGameHandler::switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx) { const auto artSet = getArtSet(heroID); COMPLAIN_RET_FALSE_IF(artSet == nullptr, "switchArtifactsCostume: wrong hero's ID"); diff --git a/server/CGameHandler.h b/server/CGameHandler.h index 199da23c8..8f591d44a 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -130,8 +130,8 @@ public: bool moveArtifact(const PlayerColor & player, const ArtifactLocation & src, const ArtifactLocation & dst) override; bool bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceID srcId, ObjectInstanceID dstId, bool swap, bool equipped, bool backpack); bool scrollBackpackArtifacts(const PlayerColor & player, const ObjectInstanceID heroID, bool left); - bool saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx); - bool switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx); + bool saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx); + bool switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx); bool eraseArtifactByClient(const ArtifactLocation & al); void synchronizeArtifactHandlerLists();