From 25d2b468c37c3c4269c0e88e6b21bf74cdb203cd Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:49:35 +0300 Subject: [PATCH 1/3] CWindowWithArtifacts preparations --- client/CMakeLists.txt | 4 ++-- client/CPlayerInterface.cpp | 8 ++++---- .../{CArtifactHolder.cpp => CArtPlace.cpp} | 4 ++-- .../widgets/{CArtifactHolder.h => CArtPlace.h} | 18 +----------------- client/widgets/CArtifactsOfHeroBase.h | 2 +- client/widgets/CComponent.cpp | 1 - client/widgets/CExchangeController.h | 3 +-- client/widgets/CWindowWithArtifacts.h | 11 +++++------ client/windows/CCreatureWindow.cpp | 2 +- client/windows/CKingdomInterface.cpp | 10 +++++----- client/windows/CKingdomInterface.h | 4 ++-- client/windows/GUIClasses.h | 1 + 12 files changed, 25 insertions(+), 43 deletions(-) rename client/widgets/{CArtifactHolder.cpp => CArtPlace.cpp} (99%) rename client/widgets/{CArtifactHolder.h => CArtPlace.h} (80%) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 35d23822b..632601cc2 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -103,7 +103,7 @@ set(client_SRCS globalLobby/GlobalLobbyWindow.cpp widgets/Buttons.cpp - widgets/CArtifactHolder.cpp + widgets/CArtPlace.cpp widgets/CComponent.cpp widgets/CExchangeController.cpp widgets/CGarrisonInt.cpp @@ -293,7 +293,7 @@ set(client_HEADERS globalLobby/GlobalLobbyWindow.h widgets/Buttons.h - widgets/CArtifactHolder.h + widgets/CArtPlace.h widgets/CComponent.h widgets/CExchangeController.h widgets/CGarrisonInt.h diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index ded74dfc2..551072e2a 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -1745,7 +1745,7 @@ void CPlayerInterface::artifactRemoved(const ArtifactLocation &al) EVENT_HANDLER_CALLED_BY_CLIENT; adventureInt->onHeroChanged(cb->getHero(al.artHolder)); - for(auto artWin : GH.windows().findWindows()) + for(auto artWin : GH.windows().findWindows()) artWin->artifactRemoved(al); waitWhileDialog(); @@ -1765,7 +1765,7 @@ void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const Artifact redraw = false; } - for(auto artWin : GH.windows().findWindows()) + for(auto artWin : GH.windows().findWindows()) artWin->artifactMoved(src, dst, redraw); waitWhileDialog(); @@ -1781,7 +1781,7 @@ void CPlayerInterface::artifactAssembled(const ArtifactLocation &al) EVENT_HANDLER_CALLED_BY_CLIENT; adventureInt->onHeroChanged(cb->getHero(al.artHolder)); - for(auto artWin : GH.windows().findWindows()) + for(auto artWin : GH.windows().findWindows()) artWin->artifactAssembled(al); } @@ -1790,7 +1790,7 @@ void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al) EVENT_HANDLER_CALLED_BY_CLIENT; adventureInt->onHeroChanged(cb->getHero(al.artHolder)); - for(auto artWin : GH.windows().findWindows()) + for(auto artWin : GH.windows().findWindows()) artWin->artifactDisassembled(al); } diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtPlace.cpp similarity index 99% rename from client/widgets/CArtifactHolder.cpp rename to client/widgets/CArtPlace.cpp index 14a90796d..3e709599b 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtPlace.cpp @@ -1,5 +1,5 @@ /* - * CArtifactHolder.cpp, part of VCMI engine + * CArtPlace.cpp, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * @@ -8,7 +8,7 @@ * */ #include "StdInc.h" -#include "CArtifactHolder.h" +#include "CArtPlace.h" #include "../gui/CGuiHandler.h" #include "../gui/Shortcut.h" diff --git a/client/widgets/CArtifactHolder.h b/client/widgets/CArtPlace.h similarity index 80% rename from client/widgets/CArtifactHolder.h rename to client/widgets/CArtPlace.h index c39cbdca7..eb0752af5 100644 --- a/client/widgets/CArtifactHolder.h +++ b/client/widgets/CArtPlace.h @@ -1,5 +1,5 @@ /* - * CArtifactHolder.h, part of VCMI engine + * CArtPlace.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * @@ -11,24 +11,8 @@ #include "MiscWidgets.h" -VCMI_LIB_NAMESPACE_BEGIN - -struct ArtifactLocation; -class CArtifactSet; - -VCMI_LIB_NAMESPACE_END - class CAnimImage; -class CArtifactHolder -{ -public: - virtual void artifactRemoved(const ArtifactLocation & artLoc)=0; - virtual void artifactMoved(const ArtifactLocation & artLoc, const ArtifactLocation & destLoc, bool withRedraw)=0; - virtual void artifactDisassembled(const ArtifactLocation & artLoc)=0; - virtual void artifactAssembled(const ArtifactLocation & artLoc)=0; -}; - class CArtPlace : public SelectableSlot { public: diff --git a/client/widgets/CArtifactsOfHeroBase.h b/client/widgets/CArtifactsOfHeroBase.h index 61a16580f..db5eef332 100644 --- a/client/widgets/CArtifactsOfHeroBase.h +++ b/client/widgets/CArtifactsOfHeroBase.h @@ -9,7 +9,7 @@ */ #pragma once -#include "CArtifactHolder.h" +#include "CArtPlace.h" class CButton; diff --git a/client/widgets/CComponent.cpp b/client/widgets/CComponent.cpp index d8e7a7d97..2d31f76eb 100644 --- a/client/widgets/CComponent.cpp +++ b/client/widgets/CComponent.cpp @@ -10,7 +10,6 @@ #include "StdInc.h" #include "CComponent.h" -#include "CArtifactHolder.h" #include "Images.h" #include diff --git a/client/widgets/CExchangeController.h b/client/widgets/CExchangeController.h index e5aa53b8c..1348002a4 100644 --- a/client/widgets/CExchangeController.h +++ b/client/widgets/CExchangeController.h @@ -9,8 +9,7 @@ */ #pragma once -#include "../windows/CWindowObject.h" -#include "CWindowWithArtifacts.h" +#include "../lib/mapObjects/CGHeroInstance.h" class CCallback; diff --git a/client/widgets/CWindowWithArtifacts.h b/client/widgets/CWindowWithArtifacts.h index 6ba216ba8..23b287465 100644 --- a/client/widgets/CWindowWithArtifacts.h +++ b/client/widgets/CWindowWithArtifacts.h @@ -9,14 +9,13 @@ */ #pragma once -#include "CArtifactHolder.h" #include "CArtifactsOfHeroMain.h" #include "CArtifactsOfHeroKingdom.h" #include "CArtifactsOfHeroAltar.h" #include "CArtifactsOfHeroMarket.h" #include "CArtifactsOfHeroBackpack.h" -class CWindowWithArtifacts : public CArtifactHolder +class CWindowWithArtifacts { public: using CArtifactsOfHeroPtr = std::variant< @@ -38,10 +37,10 @@ public: void showPopupArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); void gestureArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); - void artifactRemoved(const ArtifactLocation & artLoc) override; - void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw) override; - void artifactDisassembled(const ArtifactLocation & artLoc) override; - void artifactAssembled(const ArtifactLocation & artLoc) override; + virtual void artifactRemoved(const ArtifactLocation & artLoc); + virtual void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw); + virtual void artifactDisassembled(const ArtifactLocation & artLoc); + virtual void artifactAssembled(const ArtifactLocation & artLoc); protected: std::vector artSets; diff --git a/client/windows/CCreatureWindow.cpp b/client/windows/CCreatureWindow.cpp index 65b457733..ee2c75471 100644 --- a/client/windows/CCreatureWindow.cpp +++ b/client/windows/CCreatureWindow.cpp @@ -17,7 +17,7 @@ #include "../CPlayerInterface.h" #include "../render/Canvas.h" #include "../widgets/Buttons.h" -#include "../widgets/CArtifactHolder.h" +#include "../widgets/CArtPlace.h" #include "../widgets/CComponent.h" #include "../widgets/Images.h" #include "../widgets/TextControls.h" diff --git a/client/windows/CKingdomInterface.cpp b/client/windows/CKingdomInterface.cpp index b1466c40d..eb09b6209 100644 --- a/client/windows/CKingdomInterface.cpp +++ b/client/windows/CKingdomInterface.cpp @@ -676,25 +676,25 @@ bool CKingdomInterface::holdsGarrison(const CArmedInstance * army) void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc) { - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) + if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) arts->artifactAssembled(artLoc); } void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc) { - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) + if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) arts->artifactDisassembled(artLoc); } void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc, bool withRedraw) { - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) + if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) arts->artifactMoved(artLoc, destLoc, withRedraw); } void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc) { - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) + if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) arts->artifactRemoved(artLoc); } @@ -745,7 +745,7 @@ std::shared_ptr CKingdHeroList::createHeroItem(size_t index) if(index < heroesList.size()) { auto hero = std::make_shared(heroesList[index]); - addSetAndCallbacks(hero->heroArts); + //addSetAndCallbacks(hero->heroArts); return hero; } else diff --git a/client/windows/CKingdomInterface.h b/client/windows/CKingdomInterface.h index ba2291379..5823e2298 100644 --- a/client/windows/CKingdomInterface.h +++ b/client/windows/CKingdomInterface.h @@ -200,7 +200,7 @@ public: }; /// Class which holds all parts of kingdom overview window -class CKingdomInterface : public CWindowObject, public IGarrisonHolder, public CArtifactHolder, public ITownHolder +class CKingdomInterface : public CWindowObject, public IGarrisonHolder, public CWindowWithArtifacts, public ITownHolder { private: struct OwnedObjectInfo @@ -330,7 +330,7 @@ public: }; /// Tab with all hero-specific data -class CKingdHeroList : public CIntObject, public IGarrisonHolder, public CWindowWithArtifacts +class CKingdHeroList : public CIntObject, public IGarrisonHolder { private: std::shared_ptr heroes; diff --git a/client/windows/GUIClasses.h b/client/windows/GUIClasses.h index 6048ef3ca..85ebd3c69 100644 --- a/client/windows/GUIClasses.h +++ b/client/windows/GUIClasses.h @@ -13,6 +13,7 @@ #include "../widgets/CExchangeController.h" #include "../widgets/CWindowWithArtifacts.h" #include "../widgets/Images.h" +#include "../windows/CWindowObject.h" VCMI_LIB_NAMESPACE_BEGIN From 20471c9c2f95fdc8c24b85aa73f577676c56ba5d Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Tue, 23 Apr 2024 16:21:45 +0300 Subject: [PATCH 2/3] virtual CWindowObject --- client/CMakeLists.txt | 4 +- client/windows/CCastleInterface.cpp | 12 ++-- client/windows/CHeroBackpackWindow.cpp | 11 +-- client/windows/CHeroBackpackWindow.h | 6 +- client/windows/CHeroWindow.cpp | 9 +-- client/windows/CHeroWindow.h | 4 +- client/windows/CKingdomInterface.cpp | 72 +++++-------------- client/windows/CKingdomInterface.h | 14 ++-- client/windows/CMarketWindow.cpp | 9 +-- client/windows/CMarketWindow.h | 4 +- client/windows/CWindowObject.cpp | 8 --- client/windows/CWindowObject.h | 7 +- .../CWindowWithArtifacts.cpp | 15 +++- .../CWindowWithArtifacts.h | 15 ++-- client/windows/GUIClasses.cpp | 24 +++---- client/windows/GUIClasses.h | 4 +- lib/constants/NumericConstants.h | 1 + 17 files changed, 74 insertions(+), 145 deletions(-) rename client/{widgets => windows}/CWindowWithArtifacts.cpp (98%) rename client/{widgets => windows}/CWindowWithArtifacts.h (84%) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 632601cc2..4f04eefb8 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -122,7 +122,6 @@ set(client_SRCS widgets/CArtifactsOfHeroAltar.cpp widgets/CArtifactsOfHeroMarket.cpp widgets/CArtifactsOfHeroBackpack.cpp - widgets/CWindowWithArtifacts.cpp widgets/RadialMenu.cpp widgets/markets/CAltarArtifacts.cpp widgets/markets/CAltarCreatures.cpp @@ -152,6 +151,7 @@ set(client_SRCS windows/InfoWindows.cpp windows/QuickRecruitmentWindow.cpp windows/CHeroBackpackWindow.cpp + windows/CWindowWithArtifacts.cpp windows/settings/GeneralOptionsTab.cpp windows/settings/OtherOptionsTab.cpp windows/settings/SettingsMainWindow.cpp @@ -312,7 +312,6 @@ set(client_HEADERS widgets/CArtifactsOfHeroAltar.h widgets/CArtifactsOfHeroMarket.h widgets/CArtifactsOfHeroBackpack.h - widgets/CWindowWithArtifacts.h widgets/RadialMenu.h widgets/markets/CAltarArtifacts.h widgets/markets/CAltarCreatures.h @@ -342,6 +341,7 @@ set(client_HEADERS windows/InfoWindows.h windows/QuickRecruitmentWindow.h windows/CHeroBackpackWindow.h + windows/CWindowWithArtifacts.h windows/settings/GeneralOptionsTab.h windows/settings/OtherOptionsTab.h windows/settings/SettingsMainWindow.h diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 50be7f9ce..806b11cd7 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -1183,7 +1183,7 @@ void CTownInfo::showPopupWindow(const Point & cursorPosition) } CCastleInterface::CCastleInterface(const CGTownInstance * Town, const CGTownInstance * from): - CStatusbarWindow(PLAYER_COLORED | BORDERED), + CWindowObject(PLAYER_COLORED | BORDERED), town(Town) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); @@ -1459,7 +1459,7 @@ void CHallInterface::CBuildingBox::showPopupWindow(const Point & cursorPosition) } CHallInterface::CHallInterface(const CGTownInstance * Town): - CStatusbarWindow(PLAYER_COLORED | BORDERED, Town->town->clientInfo.hallBackground), + CWindowObject(PLAYER_COLORED | BORDERED, Town->town->clientInfo.hallBackground), town(Town) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); @@ -1507,7 +1507,7 @@ CHallInterface::CHallInterface(const CGTownInstance * Town): } CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, EBuildingState state, bool rightClick): - CStatusbarWindow(PLAYER_COLORED | (rightClick ? RCLICK_POPUP : 0), ImagePath::builtin("TPUBUILD")), + CWindowObject(PLAYER_COLORED | (rightClick ? RCLICK_POPUP : 0), ImagePath::builtin("TPUBUILD")), town(Town), building(Building) { @@ -1660,7 +1660,7 @@ void LabeledValue::hover(bool on) } CFortScreen::CFortScreen(const CGTownInstance * town): - CStatusbarWindow(PLAYER_COLORED | BORDERED, getBgName(town)) + CWindowObject(PLAYER_COLORED | BORDERED, getBgName(town)) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); ui32 fortSize = static_cast(town->creatures.size()); @@ -1849,7 +1849,7 @@ void CFortScreen::RecruitArea::showPopupWindow(const Point & cursorPosition) } CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner, const ImagePath & imagename) - : CStatusbarWindow(BORDERED, imagename) + : CWindowObject(BORDERED, imagename) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); @@ -1918,7 +1918,7 @@ void CMageGuildScreen::Scroll::hover(bool on) } CBlacksmithDialog::CBlacksmithDialog(bool possible, CreatureID creMachineID, ArtifactID aid, ObjectInstanceID hid): - CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("TPSMITH")) + CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPSMITH")) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); diff --git a/client/windows/CHeroBackpackWindow.cpp b/client/windows/CHeroBackpackWindow.cpp index 8d69f0f8b..369cc73af 100644 --- a/client/windows/CHeroBackpackWindow.cpp +++ b/client/windows/CHeroBackpackWindow.cpp @@ -21,8 +21,7 @@ #include "CPlayerInterface.h" CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero, const std::vector & artsSets) - : CStatusbarWindow(0) - , CWindowWithArtifacts(&artsSets) + : CWindowWithArtifacts(&artsSets) { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); @@ -49,15 +48,7 @@ void CHeroBackpackWindow::showAll(Canvas & to) CMessage::drawBorder(PlayerColor(LOCPLINT->playerID), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15); } -void CHeroBackpackWindow::activate() -{ - if(const auto art = getPickedArtifact()) - setCursorAnimation(*art); - CIntObject::activate(); -} - CHeroQuickBackpackWindow::CHeroQuickBackpackWindow(const CGHeroInstance * hero, ArtifactPosition targetSlot) - : CWindowObject(0) { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); diff --git a/client/windows/CHeroBackpackWindow.h b/client/windows/CHeroBackpackWindow.h index 23ba611e9..aff8cbca9 100644 --- a/client/windows/CHeroBackpackWindow.h +++ b/client/windows/CHeroBackpackWindow.h @@ -9,8 +9,7 @@ */ #pragma once -#include "../widgets/CWindowWithArtifacts.h" -#include "CWindowObject.h" +#include "CWindowWithArtifacts.h" class CFilledTexture; @@ -26,10 +25,9 @@ protected: const int windowMargin = 5; void showAll(Canvas & to) override; - void activate() override; }; -class CHeroQuickBackpackWindow : public CWindowObject, public CWindowWithArtifacts +class CHeroQuickBackpackWindow : public CWindowWithArtifacts { public: CHeroQuickBackpackWindow(const CGHeroInstance * hero, ArtifactPosition targetSlot); diff --git a/client/windows/CHeroWindow.cpp b/client/windows/CHeroWindow.cpp index f3b2abd5e..080e9e09a 100644 --- a/client/windows/CHeroWindow.cpp +++ b/client/windows/CHeroWindow.cpp @@ -19,7 +19,6 @@ #include "../CPlayerInterface.h" #include "../gui/CGuiHandler.h" -#include "../gui/CursorHandler.h" #include "../gui/TextAlignment.h" #include "../gui/Shortcut.h" #include "../gui/WindowHandler.h" @@ -71,7 +70,7 @@ CHeroSwitcher::CHeroSwitcher(CHeroWindow * owner_, Point pos_, const CGHeroInsta } CHeroWindow::CHeroWindow(const CGHeroInstance * hero) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("HeroScr4")) + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("HeroScr4")) { auto & heroscrn = CGI->generaltexth->heroscrn; @@ -360,9 +359,3 @@ bool CHeroWindow::holdsGarrison(const CArmedInstance * army) { return army == curHero; } - -void CHeroWindow::deactivate() -{ - CCS->curh->dragAndDropCursor(nullptr); - CIntObject::deactivate(); -} diff --git a/client/windows/CHeroWindow.h b/client/windows/CHeroWindow.h index 11448c816..dfebf45d2 100644 --- a/client/windows/CHeroWindow.h +++ b/client/windows/CHeroWindow.h @@ -9,8 +9,7 @@ */ #pragma once -#include "../widgets/CWindowWithArtifacts.h" -#include "CWindowObject.h" +#include "CWindowWithArtifacts.h" #include "../../lib/bonuses/IBonusBearer.h" @@ -108,7 +107,6 @@ public: void switchHero(); //changes displayed hero void updateGarrisons() override; bool holdsGarrison(const CArmedInstance * army) override; - void deactivate() override; void createBackpackWindow(); //friends diff --git a/client/windows/CKingdomInterface.cpp b/client/windows/CKingdomInterface.cpp index eb09b6209..fe6a1876b 100644 --- a/client/windows/CKingdomInterface.cpp +++ b/client/windows/CKingdomInterface.cpp @@ -19,7 +19,6 @@ #include "../PlayerLocalState.h" #include "../adventureMap/CResDataBar.h" #include "../gui/CGuiHandler.h" -#include "../gui/CursorHandler.h" #include "../gui/Shortcut.h" #include "../gui/WindowHandler.h" #include "../widgets/CComponent.h" @@ -549,7 +548,10 @@ std::shared_ptr CKingdomInterface::createMainTab(size_t index) switch(index) { case 0: - return std::make_shared(size); + return std::make_shared(size, [this](const CWindowWithArtifacts::CArtifactsOfHeroPtr & newHeroSet) + { + addSetAndCallbacks(newHeroSet); + }); case 1: return std::make_shared(size); default: @@ -674,31 +676,7 @@ bool CKingdomInterface::holdsGarrison(const CArmedInstance * army) return army->getOwner() == LOCPLINT->playerID; } -void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc) -{ - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) - arts->artifactAssembled(artLoc); -} - -void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc) -{ - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) - arts->artifactDisassembled(artLoc); -} - -void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc, bool withRedraw) -{ - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) - arts->artifactMoved(artLoc, destLoc, withRedraw); -} - -void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc) -{ - if(auto arts = std::dynamic_pointer_cast(tabArea->getItem())) - arts->artifactRemoved(artLoc); -} - -CKingdHeroList::CKingdHeroList(size_t maxSize) +CKingdHeroList::CKingdHeroList(size_t maxSize, const CreateHeroItemFunctor & onCreateHeroItemCallback) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); title = std::make_shared(ImagePath::builtin("OVTITLE"),16,0); @@ -708,8 +686,20 @@ CKingdHeroList::CKingdHeroList(size_t maxSize) ui32 townCount = LOCPLINT->cb->howManyHeroes(false); ui32 size = OVERVIEW_SIZE*116 + 19; - heroes = std::make_shared(std::bind(&CKingdHeroList::createHeroItem, this, _1), - Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size)); + heroes = std::make_shared([onCreateHeroItemCallback](size_t idx) -> std::shared_ptr + { + auto heroesList = LOCPLINT->localState->getWanderingHeroes(); + if(idx < heroesList.size()) + { + auto hero = std::make_shared(heroesList[idx]); + onCreateHeroItemCallback(hero->heroArts); + return hero; + } + else + { + return std::make_shared(AnimationPath::builtin("OVSLOT"), (idx - 2) % GameConstants::KINGDOM_WINDOW_HEROES_SLOTS); + } + }, Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size)); } void CKingdHeroList::updateGarrisons() @@ -730,30 +720,6 @@ bool CKingdHeroList::holdsGarrison(const CArmedInstance * army) return false; } -void CKingdHeroList::deactivate() -{ - CCS->curh->dragAndDropCursor(nullptr); - CIntObject::deactivate(); -} - -std::shared_ptr CKingdHeroList::createHeroItem(size_t index) -{ - ui32 picCount = 4; // OVSLOT contains 4 images - - auto heroesList = LOCPLINT->localState->getWanderingHeroes(); - - if(index < heroesList.size()) - { - auto hero = std::make_shared(heroesList[index]); - //addSetAndCallbacks(hero->heroArts); - return hero; - } - else - { - return std::make_shared(AnimationPath::builtin("OVSLOT"), (index-2) % picCount ); - } -} - CKingdTownList::CKingdTownList(size_t maxSize) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); diff --git a/client/windows/CKingdomInterface.h b/client/windows/CKingdomInterface.h index 5823e2298..50fbd1038 100644 --- a/client/windows/CKingdomInterface.h +++ b/client/windows/CKingdomInterface.h @@ -9,8 +9,7 @@ */ #pragma once -#include "../widgets/CWindowWithArtifacts.h" -#include "CWindowObject.h" +#include "CWindowWithArtifacts.h" VCMI_LIB_NAMESPACE_BEGIN class CGObjectInstance; @@ -200,7 +199,7 @@ public: }; /// Class which holds all parts of kingdom overview window -class CKingdomInterface : public CWindowObject, public IGarrisonHolder, public CWindowWithArtifacts, public ITownHolder +class CKingdomInterface : public IGarrisonHolder, public CWindowWithArtifacts, public ITownHolder { private: struct OwnedObjectInfo @@ -254,10 +253,6 @@ public: void heroRemoved(); void updateGarrisons() override; bool holdsGarrison(const CArmedInstance * army) override; - void artifactRemoved(const ArtifactLocation &artLoc) override; - void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw) override; - void artifactDisassembled(const ArtifactLocation &artLoc) override; - void artifactAssembled(const ArtifactLocation &artLoc) override; void buildChanged() override; }; @@ -338,13 +333,12 @@ private: std::shared_ptr heroLabel; std::shared_ptr skillsLabel; - std::shared_ptr createHeroItem(size_t index); public: - CKingdHeroList(size_t maxSize); + using CreateHeroItemFunctor = std::function; + CKingdHeroList(size_t maxSize, const CreateHeroItemFunctor & onCreateHeroItemCallback); void updateGarrisons() override; bool holdsGarrison(const CArmedInstance * army) override; - void deactivate() override; }; /// Tab with all town-specific data diff --git a/client/windows/CMarketWindow.cpp b/client/windows/CMarketWindow.cpp index 3bcd37242..1c19ea899 100644 --- a/client/windows/CMarketWindow.cpp +++ b/client/windows/CMarketWindow.cpp @@ -12,7 +12,6 @@ #include "CMarketWindow.h" #include "../gui/CGuiHandler.h" -#include "../gui/CursorHandler.h" #include "../gui/Shortcut.h" #include "../widgets/Buttons.h" @@ -34,7 +33,7 @@ #include "../../lib/mapObjects/CGHeroInstance.h" CMarketWindow::CMarketWindow(const IMarket * market, const CGHeroInstance * hero, const std::function & onWindowClosed, EMarketMode mode) - : CStatusbarWindow(PLAYER_COLORED) + : CWindowObject(PLAYER_COLORED) , windowClosedCallback(onWindowClosed) { assert(mode == EMarketMode::RESOURCE_RESOURCE || mode == EMarketMode::RESOURCE_PLAYER || mode == EMarketMode::CREATURE_RESOURCE || @@ -262,9 +261,3 @@ void CMarketWindow::createAltarCreatures(const IMarket * market, const CGHeroIns initWidgetInternals(EMarketMode::CREATURE_EXP, CGI->generaltexth->zelp[568]); updateHero(); } - -void CMarketWindow::deactivate() -{ - CCS->curh->dragAndDropCursor(nullptr); - CIntObject::deactivate(); -} diff --git a/client/windows/CMarketWindow.h b/client/windows/CMarketWindow.h index cafdc449f..e6b57f2a7 100644 --- a/client/windows/CMarketWindow.h +++ b/client/windows/CMarketWindow.h @@ -10,8 +10,7 @@ #pragma once #include "../widgets/markets/CMarketBase.h" -#include "../widgets/CWindowWithArtifacts.h" -#include "CWindowObject.h" +#include "CWindowWithArtifacts.h" class CMarketWindow : public CStatusbarWindow, public CWindowWithArtifacts, public IGarrisonHolder { @@ -25,7 +24,6 @@ public: bool holdsGarrison(const CArmedInstance * army) override; void artifactRemoved(const ArtifactLocation & artLoc) override; void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw) override; - void deactivate() override; private: void createChangeModeButtons(EMarketMode currentMode, const IMarket * market, const CGHeroInstance * hero); diff --git a/client/windows/CWindowObject.cpp b/client/windows/CWindowObject.cpp index bf6845d07..0403c8c91 100644 --- a/client/windows/CWindowObject.cpp +++ b/client/windows/CWindowObject.cpp @@ -239,11 +239,3 @@ bool CWindowObject::isPopupWindow() const { return options & RCLICK_POPUP; } - -CStatusbarWindow::CStatusbarWindow(int options, const ImagePath & imageName, Point centerAt) : CWindowObject(options, imageName, centerAt) -{ -} - -CStatusbarWindow::CStatusbarWindow(int options, const ImagePath & imageName) : CWindowObject(options, imageName) -{ -} diff --git a/client/windows/CWindowObject.h b/client/windows/CWindowObject.h index 52111aee6..feecc1243 100644 --- a/client/windows/CWindowObject.h +++ b/client/windows/CWindowObject.h @@ -47,17 +47,14 @@ public: * centerAt - position of window center. Default - center of the screen */ CWindowObject(int options, const ImagePath & imageName, Point centerAt); - CWindowObject(int options, const ImagePath & imageName = {}); + CWindowObject(int options = 0, const ImagePath & imageName = {}); ~CWindowObject(); void showAll(Canvas & to) override; }; -class CStatusbarWindow : public CWindowObject +class CStatusbarWindow : virtual public CWindowObject { -public: - CStatusbarWindow(int options, const ImagePath & imageName, Point centerAt); - CStatusbarWindow(int options, const ImagePath & imageName = {}); protected: std::shared_ptr statusbar; }; diff --git a/client/widgets/CWindowWithArtifacts.cpp b/client/windows/CWindowWithArtifacts.cpp similarity index 98% rename from client/widgets/CWindowWithArtifacts.cpp rename to client/windows/CWindowWithArtifacts.cpp index 22e2f841b..77f0a08d7 100644 --- a/client/widgets/CWindowWithArtifacts.cpp +++ b/client/windows/CWindowWithArtifacts.cpp @@ -18,7 +18,7 @@ #include "../render/CAnimation.h" #include "../render/IImage.h" -#include "CComponent.h" +#include "../widgets/CComponent.h" #include "../windows/CHeroWindow.h" #include "../windows/CSpellWindow.h" @@ -333,6 +333,19 @@ void CWindowWithArtifacts::gestureArtPlaceHero(CArtifactsOfHeroBase & artsInst, }, artSetWeak.value()); } +void CWindowWithArtifacts::activate() +{ + if(const auto art = getPickedArtifact()) + setCursorAnimation(*art); + CWindowObject::activate(); +} + +void CWindowWithArtifacts::deactivate() +{ + CCS->curh->dragAndDropCursor(nullptr); + CWindowObject::deactivate(); +} + void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc) { updateSlots(); diff --git a/client/widgets/CWindowWithArtifacts.h b/client/windows/CWindowWithArtifacts.h similarity index 84% rename from client/widgets/CWindowWithArtifacts.h rename to client/windows/CWindowWithArtifacts.h index 23b287465..93c6c12c3 100644 --- a/client/widgets/CWindowWithArtifacts.h +++ b/client/windows/CWindowWithArtifacts.h @@ -9,13 +9,14 @@ */ #pragma once -#include "CArtifactsOfHeroMain.h" -#include "CArtifactsOfHeroKingdom.h" -#include "CArtifactsOfHeroAltar.h" -#include "CArtifactsOfHeroMarket.h" -#include "CArtifactsOfHeroBackpack.h" +#include "../widgets/CArtifactsOfHeroMain.h" +#include "../widgets/CArtifactsOfHeroKingdom.h" +#include "../widgets/CArtifactsOfHeroAltar.h" +#include "../widgets/CArtifactsOfHeroMarket.h" +#include "../widgets/CArtifactsOfHeroBackpack.h" +#include "CWindowObject.h" -class CWindowWithArtifacts +class CWindowWithArtifacts : virtual public CWindowObject { public: using CArtifactsOfHeroPtr = std::variant< @@ -36,6 +37,8 @@ public: void clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); void showPopupArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); void gestureArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); + void activate() override; + void deactivate() override; virtual void artifactRemoved(const ArtifactLocation & artLoc); virtual void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw); diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 8b2032ca1..f1e37e6f1 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -204,7 +204,7 @@ void CRecruitmentWindow::showAll(Canvas & to) } CRecruitmentWindow::CRecruitmentWindow(const CGDwelling * Dwelling, int Level, const CArmedInstance * Dst, const std::function & Recruit, const std::function & onClose, int y_offset): - CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("TPRCRT")), + CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPRCRT")), onRecruit(Recruit), onClose(onClose), level(Level), @@ -445,7 +445,7 @@ CLevelWindow::~CLevelWindow() } CTavernWindow::CTavernWindow(const CGObjectInstance * TavernObj, const std::function & onWindowClosed) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("TPTAVERN")), + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPTAVERN")), onWindowClosed(onWindowClosed), tavernObj(TavernObj), heroToInvite(nullptr) @@ -692,7 +692,7 @@ static bool isQuickExchangeLayoutAvailable() } CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID) - : CStatusbarWindow(PLAYER_COLORED | BORDERED, ImagePath::builtin(isQuickExchangeLayoutAvailable() ? QUICK_EXCHANGE_BG : "TRADE2")), + : CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin(isQuickExchangeLayoutAvailable() ? QUICK_EXCHANGE_BG : "TRADE2")), controller(hero1, hero2), moveStackLeftButtons(), moveStackRightButtons() @@ -934,12 +934,6 @@ bool CExchangeWindow::holdsGarrison(const CArmedInstance * army) return garr->upperArmy() == army || garr->lowerArmy() == army; } -void CExchangeWindow::deactivate() -{ - CCS->curh->dragAndDropCursor(nullptr); - CIntObject::deactivate(); -} - void CExchangeWindow::questlog(int whichHero) { CCS->curh->dragAndDropCursor(nullptr); @@ -975,7 +969,7 @@ void CExchangeWindow::updateWidgets() } CShipyardWindow::CShipyardWindow(const TResources & cost, int state, BoatId boatType, const std::function & onBuy) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("TPSHIP")) + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPSHIP")) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); @@ -1093,7 +1087,7 @@ bool CTransformerWindow::holdsGarrison(const CArmedInstance * army) } CTransformerWindow::CTransformerWindow(const IMarket * _market, const CGHeroInstance * _hero, const std::function & onWindowClosed) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("SKTRNBK")), + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("SKTRNBK")), hero(_hero), onWindowClosed(onWindowClosed), market(_market) @@ -1192,7 +1186,7 @@ void CUniversityWindow::CItem::showAll(Canvas & to) } CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market, const std::function & onWindowClosed) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("UNIVERS1")), + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("UNIVERS1")), hero(_hero), onWindowClosed(onWindowClosed), market(_market) @@ -1254,7 +1248,7 @@ void CUniversityWindow::makeDeal(SecondarySkill skill) CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * owner_, SecondarySkill SKILL, bool available) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("UNIVERS2.PCX")), + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("UNIVERS2.PCX")), owner(owner_) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); @@ -1341,7 +1335,7 @@ bool CGarrisonWindow::holdsGarrison(const CArmedInstance * army) } CHillFortWindow::CHillFortWindow(const CGHeroInstance * visitor, const CGObjectInstance * object) - : CStatusbarWindow(PLAYER_COLORED, ImagePath::builtin("APHLFTBK")), + : CWindowObject(PLAYER_COLORED, ImagePath::builtin("APHLFTBK")), fort(object), hero(visitor) { @@ -1545,7 +1539,7 @@ int CHillFortWindow::getState(SlotID slot) } CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner): - CStatusbarWindow(PLAYER_COLORED | BORDERED, ImagePath::builtin("TpRank")), + CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin("TpRank")), owner(_owner) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); diff --git a/client/windows/GUIClasses.h b/client/windows/GUIClasses.h index 85ebd3c69..6cce1a0ea 100644 --- a/client/windows/GUIClasses.h +++ b/client/windows/GUIClasses.h @@ -11,9 +11,8 @@ #include "../lib/ResourceSet.h" #include "../widgets/CExchangeController.h" -#include "../widgets/CWindowWithArtifacts.h" #include "../widgets/Images.h" -#include "../windows/CWindowObject.h" +#include "CWindowWithArtifacts.h" VCMI_LIB_NAMESPACE_BEGIN @@ -326,7 +325,6 @@ public: void updateGarrisons() override; bool holdsGarrison(const CArmedInstance * army) override; - void deactivate() override; void questlog(int whichHero); //questlog button callback; whichHero: 0 - left, 1 - right diff --git a/lib/constants/NumericConstants.h b/lib/constants/NumericConstants.h index e4ef37939..b0cda4270 100644 --- a/lib/constants/NumericConstants.h +++ b/lib/constants/NumericConstants.h @@ -53,6 +53,7 @@ namespace GameConstants constexpr int64_t PLAYER_RESOURCES_CAP = 1000 * 1000 * 1000; constexpr int ALTAR_ARTIFACTS_SLOTS = 22; constexpr int TOURNAMENT_RULES_DD_MAP_TILES_THRESHOLD = 144*144*2; //map tiles count threshold for 2 dimension door casts with tournament rules + constexpr int KINGDOM_WINDOW_HEROES_SLOTS = 4; } VCMI_LIB_NAMESPACE_END From c9a8f8e85548cee9dd993b94a2ee4fff9fba3499 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Tue, 23 Apr 2024 20:26:21 +0300 Subject: [PATCH 3/3] Sonarcloud warnings --- client/widgets/CArtPlace.cpp | 20 ++++---- client/widgets/CArtPlace.h | 21 ++++---- client/windows/CWindowWithArtifacts.cpp | 66 +++++++++++-------------- client/windows/CWindowWithArtifacts.h | 24 ++++----- 4 files changed, 63 insertions(+), 68 deletions(-) diff --git a/client/widgets/CArtPlace.cpp b/client/widgets/CArtPlace.cpp index 3e709599b..6b9b88a59 100644 --- a/client/widgets/CArtPlace.cpp +++ b/client/widgets/CArtPlace.cpp @@ -92,7 +92,7 @@ CArtPlace::CArtPlace(Point position, const CArtifactInstance * art) image->disable(); } -const CArtifactInstance * CArtPlace::getArt() +const CArtifactInstance * CArtPlace::getArt() const { return ourArt; } @@ -108,7 +108,7 @@ CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * co void CCommanderArtPlace::returnArtToHeroCallback() { ArtifactPosition artifactPos = commanderSlotID; - ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId()); + ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArt()->getTypeId()); if(freeSlot == ArtifactPosition::PRE_FIRST) { LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152")); @@ -119,7 +119,7 @@ void CCommanderArtPlace::returnArtToHeroCallback() src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER; ArtifactLocation dst(commanderOwner->id, freeSlot); - if(ourArt->canBePutAt(commanderOwner, freeSlot, true)) + if(getArt()->canBePutAt(commanderOwner, freeSlot, true)) { LOCPLINT->cb->swapArtifacts(src, dst); setArtifact(nullptr); @@ -130,13 +130,13 @@ void CCommanderArtPlace::returnArtToHeroCallback() void CCommanderArtPlace::clickPressed(const Point & cursorPosition) { - if(ourArt && text.size()) + if(getArt() && text.size()) LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {}); } -void CCommanderArtPlace::showPopupWindow(const Point& cursorPosition) +void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition) { - if(ourArt && text.size()) + if(getArt() && text.size()) CArtPlace::showPopupWindow(cursorPosition); } @@ -204,22 +204,22 @@ void CArtPlace::setArtifact(const CArtifactInstance * art) } } -void CArtPlace::setClickPressedCallback(ClickFunctor callback) +void CArtPlace::setClickPressedCallback(const ClickFunctor & callback) { clickPressedCallback = callback; } -void CArtPlace::setShowPopupCallback(ClickFunctor callback) +void CArtPlace::setShowPopupCallback(const ClickFunctor & callback) { showPopupCallback = callback; } -void CArtPlace::setGestureCallback(ClickFunctor callback) +void CArtPlace::setGestureCallback(const ClickFunctor & callback) { gestureCallback = callback; } -void CHeroArtPlace::addCombinedArtInfo(std::map & arts) +void CHeroArtPlace::addCombinedArtInfo(const std::map & arts) { for(const auto & combinedArt : arts) { diff --git a/client/widgets/CArtPlace.h b/client/widgets/CArtPlace.h index eb0752af5..3a3b83601 100644 --- a/client/widgets/CArtPlace.h +++ b/client/widgets/CArtPlace.h @@ -19,34 +19,35 @@ public: using ClickFunctor = std::function; ArtifactPosition slot; - + CArtPlace(Point position, const CArtifactInstance * art = nullptr); - const CArtifactInstance * getArt(); + const CArtifactInstance * getArt() const; void lockSlot(bool on); bool isLocked() const; void setArtifact(const CArtifactInstance * art); - void setClickPressedCallback(ClickFunctor callback); - void setShowPopupCallback(ClickFunctor callback); - void setGestureCallback(ClickFunctor callback); + void setClickPressedCallback(const ClickFunctor & callback); + void setShowPopupCallback(const ClickFunctor & callback); + void setGestureCallback(const ClickFunctor & callback); void clickPressed(const Point & cursorPosition) override; void showPopupWindow(const Point & cursorPosition) override; void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override; -protected: - std::shared_ptr image; +private: const CArtifactInstance * ourArt; - int imageIndex; bool locked; + int imageIndex; + std::shared_ptr image; ClickFunctor clickPressedCallback; ClickFunctor showPopupCallback; ClickFunctor gestureCallback; +protected: void setInternals(const CArtifactInstance * artInst); }; class CCommanderArtPlace : public CArtPlace { -protected: +private: const CGHeroInstance * commanderOwner; ArtifactPosition commanderSlotID; @@ -62,7 +63,7 @@ class CHeroArtPlace: public CArtPlace { public: CHeroArtPlace(Point position, const CArtifactInstance * art = nullptr); - void addCombinedArtInfo(std::map & arts); + void addCombinedArtInfo(const std::map & arts); }; namespace ArtifactUtilsClient diff --git a/client/windows/CWindowWithArtifacts.cpp b/client/windows/CWindowWithArtifacts.cpp index 77f0a08d7..a1bfdcf73 100644 --- a/client/windows/CWindowWithArtifacts.cpp +++ b/client/windows/CWindowWithArtifacts.cpp @@ -41,24 +41,24 @@ CWindowWithArtifacts::CWindowWithArtifacts(const std::vectorartSets.insert(this->artSets.end(), artSets->begin(), artSets->end()); } -void CWindowWithArtifacts::addSet(CArtifactsOfHeroPtr artSet) +void CWindowWithArtifacts::addSet(CArtifactsOfHeroPtr newArtSet) { - artSets.emplace_back(artSet); + artSets.emplace_back(newArtSet); } -void CWindowWithArtifacts::addSetAndCallbacks(CArtifactsOfHeroPtr artSet) +void CWindowWithArtifacts::addSetAndCallbacks(CArtifactsOfHeroPtr newArtSet) { - addSet(artSet); + addSet(newArtSet); std::visit([this](auto artSetWeak) { auto artSet = artSetWeak.lock(); artSet->clickPressedCallback = std::bind(&CWindowWithArtifacts::clickPressedArtPlaceHero, this, _1, _2, _3); artSet->showPopupCallback = std::bind(&CWindowWithArtifacts::showPopupArtPlaceHero, this, _1, _2, _3); artSet->gestureCallback = std::bind(&CWindowWithArtifacts::gestureArtPlaceHero, this, _1, _2, _3); - }, artSet); + }, newArtSet); } -void CWindowWithArtifacts::addCloseCallback(CloseCallback callback) +void CWindowWithArtifacts::addCloseCallback(const CloseCallback & callback) { closeCallback = callback; } @@ -81,10 +81,10 @@ const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact() return nullptr; } -void CWindowWithArtifacts::clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) +void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) { - const auto artSet = findAOHbyRef(artsInst); - assert(artSet.has_value()); + const auto currentArtSet = findAOHbyRef(artsInst); + assert(currentArtSet.has_value()); if(artPlace.isLocked()) return; @@ -222,11 +222,8 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsI if constexpr(std::is_same_v>) { - if(!isTransferAllowed && artPlace.getArt()) - { - if(closeCallback) - closeCallback(); - } + if(!isTransferAllowed && artPlace.getArt() && closeCallback) + closeCallback(); } else { @@ -259,13 +256,13 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsI if(closeCallback) closeCallback(); } - }, artSet.value()); + }, currentArtSet.value()); } -void CWindowWithArtifacts::showPopupArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) +void CWindowWithArtifacts::showPopupArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) { - const auto artSetWeak = findAOHbyRef(artsInst); - assert(artSetWeak.has_value()); + const auto currentArtSet = findAOHbyRef(artsInst); + assert(currentArtSet.has_value()); if(artPlace.isLocked()) return; @@ -304,13 +301,13 @@ void CWindowWithArtifacts::showPopupArtPlaceHero(CArtifactsOfHeroBase & artsInst if(artPlace.getArt() && artPlace.text.size()) artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition); } - }, artSetWeak.value()); + }, currentArtSet.value()); } -void CWindowWithArtifacts::gestureArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) +void CWindowWithArtifacts::gestureArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) { - const auto artSetWeak = findAOHbyRef(artsInst); - assert(artSetWeak.has_value()); + const auto currentArtSet = findAOHbyRef(artsInst); + assert(currentArtSet.has_value()); if(artPlace.isLocked()) return; @@ -330,7 +327,7 @@ void CWindowWithArtifacts::gestureArtPlaceHero(CArtifactsOfHeroBase & artsInst, backpackWindow->moveTo(cursorPosition - Point(1, 1)); backpackWindow->fitToScreen(15); } - }, artSetWeak.value()); + }, currentArtSet.value()); } void CWindowWithArtifacts::activate() @@ -348,7 +345,7 @@ void CWindowWithArtifacts::deactivate() void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc) { - updateSlots(); + update(); } void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw) @@ -407,16 +404,16 @@ void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc) { - updateSlots(); + update(); } void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc) { markPossibleSlots(); - updateSlots(); + update(); } -void CWindowWithArtifacts::updateSlots() +void CWindowWithArtifacts::update() const { auto updateSlotBody = [](auto artSetWeak) -> void { @@ -468,7 +465,7 @@ std::optional> CWind return std::make_tuple(pickedCnt.begin()->first, artInst); } -std::optional CWindowWithArtifacts::findAOHbyRef(CArtifactsOfHeroBase & artsInst) +std::optional CWindowWithArtifacts::findAOHbyRef(const CArtifactsOfHeroBase & artsInst) { std::optional res; @@ -510,7 +507,7 @@ void CWindowWithArtifacts::markPossibleSlots() } } -bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade) +bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade) const { const auto artId = artInst.getTypeId(); @@ -526,14 +523,11 @@ bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, c std::vector>(1, std::make_shared(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT)))); return false; } - if(isTrade) + if(isTrade && !artInst.artType->isTradable()) { - if(!artInst.artType->isTradable()) - { - LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21], - std::vector>(1, std::make_shared(ComponentType::ARTIFACT, artId))); - return false; - } + LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21], + std::vector>(1, std::make_shared(ComponentType::ARTIFACT, artId))); + return false; } return true; } diff --git a/client/windows/CWindowWithArtifacts.h b/client/windows/CWindowWithArtifacts.h index 93c6c12c3..311e6a74c 100644 --- a/client/windows/CWindowWithArtifacts.h +++ b/client/windows/CWindowWithArtifacts.h @@ -28,15 +28,18 @@ public: std::weak_ptr>; using CloseCallback = std::function; + std::vector artSets; + CloseCallback closeCallback; + explicit CWindowWithArtifacts(const std::vector * artSets = nullptr); - void addSet(CArtifactsOfHeroPtr artSet); - void addSetAndCallbacks(CArtifactsOfHeroPtr artSet); - void addCloseCallback(CloseCallback callback); + void addSet(CArtifactsOfHeroPtr newArtSet); + void addSetAndCallbacks(CArtifactsOfHeroPtr newArtSet); + void addCloseCallback(const CloseCallback & callback); const CGHeroInstance * getHeroPickedArtifact(); const CArtifactInstance * getPickedArtifact(); - void clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); - void showPopupArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); - void gestureArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); + void clickPressedArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); + void showPopupArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); + void gestureArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition); void activate() override; void deactivate() override; @@ -46,13 +49,10 @@ public: virtual void artifactAssembled(const ArtifactLocation & artLoc); protected: - std::vector artSets; - CloseCallback closeCallback; - - void updateSlots(); + void update() const; std::optional> getState(); - std::optional findAOHbyRef(CArtifactsOfHeroBase & artsInst); + std::optional findAOHbyRef(const CArtifactsOfHeroBase & artsInst); void markPossibleSlots(); - bool checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade); + bool checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade) const; void setCursorAnimation(const CArtifactInstance & artInst); };