From 6a1a6b6864c9d09f4ce3eb63ed1d0875f462d970 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Wed, 1 May 2024 21:18:36 +0300 Subject: [PATCH] weak_ptr removed --- client/widgets/CArtifactsOfHeroBase.cpp | 15 ++ client/widgets/CArtifactsOfHeroBase.h | 1 + client/windows/CHeroBackpackWindow.cpp | 2 +- client/windows/CWindowWithArtifacts.cpp | 237 ++++++++++-------------- client/windows/CWindowWithArtifacts.h | 14 +- 5 files changed, 122 insertions(+), 147 deletions(-) diff --git a/client/widgets/CArtifactsOfHeroBase.cpp b/client/widgets/CArtifactsOfHeroBase.cpp index 57b44e62d..e3ae2d924 100644 --- a/client/widgets/CArtifactsOfHeroBase.cpp +++ b/client/widgets/CArtifactsOfHeroBase.cpp @@ -166,6 +166,21 @@ CArtifactsOfHeroBase::ArtPlacePtr CArtifactsOfHeroBase::getArtPlace(const Artifa } } +CArtifactsOfHeroBase::ArtPlacePtr CArtifactsOfHeroBase::getArtPlace(const Point & cursorPosition) +{ + for(const auto & [slot, artPlace] : artWorn) + { + if(artPlace->pos.isInside(cursorPosition)) + return artPlace; + } + for(const auto & artPlace : backpack) + { + if(artPlace->pos.isInside(cursorPosition)) + return artPlace; + } + return nullptr; +} + void CArtifactsOfHeroBase::updateWornSlots() { for(auto place : artWorn) diff --git a/client/widgets/CArtifactsOfHeroBase.h b/client/widgets/CArtifactsOfHeroBase.h index cece27267..2c67f1b4e 100644 --- a/client/widgets/CArtifactsOfHeroBase.h +++ b/client/widgets/CArtifactsOfHeroBase.h @@ -38,6 +38,7 @@ public: virtual void markPossibleSlots(const CArtifactInstance * art, bool assumeDestRemoved = true); virtual void unmarkSlots(); virtual ArtPlacePtr getArtPlace(const ArtifactPosition & slot); + virtual ArtPlacePtr getArtPlace(const Point & cursorPosition); virtual void updateWornSlots(); virtual void updateBackpackSlots(); virtual void updateSlot(const ArtifactPosition & slot); diff --git a/client/windows/CHeroBackpackWindow.cpp b/client/windows/CHeroBackpackWindow.cpp index 72c365279..328a2bdb8 100644 --- a/client/windows/CHeroBackpackWindow.cpp +++ b/client/windows/CHeroBackpackWindow.cpp @@ -55,7 +55,7 @@ CHeroQuickBackpackWindow::CHeroQuickBackpackWindow(const CGHeroInstance * hero, stretchedBackground = std::make_shared(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0)); arts = std::make_shared(targetSlot); arts->moveBy(Point(windowMargin, windowMargin)); - addSetAndCallbacks(static_cast>(arts)); + addSetAndCallbacks(static_cast>(arts)); arts->setHero(hero); addCloseCallback(std::bind(&CHeroQuickBackpackWindow::close, this)); addUsedEvents(GESTURE); diff --git a/client/windows/CWindowWithArtifacts.cpp b/client/windows/CWindowWithArtifacts.cpp index 368afdb00..25a0d6e7f 100644 --- a/client/windows/CWindowWithArtifacts.cpp +++ b/client/windows/CWindowWithArtifacts.cpp @@ -49,9 +49,8 @@ void CWindowWithArtifacts::addSet(ArtifactsOfHeroVar newArtSet) void CWindowWithArtifacts::addSetAndCallbacks(ArtifactsOfHeroVar newArtSet) { addSet(newArtSet); - std::visit([this](auto artSetWeak) + std::visit([this](auto artSet) { - 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); @@ -67,16 +66,15 @@ const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact() { const CGHeroInstance * hero = nullptr; - for(auto artSetWeak : artSets) - std::visit([&hero](auto artSetWeak) + for(auto & artSet : artSets) + std::visit([&hero](auto artSetPtr) { - if(auto artSetPtr = artSetWeak.lock()) - if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS)) - { - hero = artSetPtr->getHero(); - return; - } - }, artSetWeak); + if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS)) + { + hero = artSetPtr->getHero(); + return; + } + }, artSet); return hero; } @@ -84,16 +82,15 @@ const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact() { const CArtifactInstance * art = nullptr; - for (auto artSetWeak : artSets) - std::visit([&art](auto artSetWeak) + for(auto & artSet : artSets) + std::visit([&art](auto artSetPtr) { - if(auto artSetPtr = artSetWeak.lock()) - if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS)) - { - art = pickedArt; - return; - } - }, artSetWeak); + if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS)) + { + art = pickedArt; + return; + } + }, artSet); return art; } @@ -105,20 +102,18 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & if(artPlace.isLocked()) return; - if (!LOCPLINT->makingTurn) + if(!LOCPLINT->makingTurn) return; std::visit( - [this, &artPlace](auto artSetWeak) -> void + [this, &artPlace](auto artSetPtr) { - const auto artSetPtr = artSetWeak.lock(); - // Hero(Main, Exchange) window, Kingdom window, Altar window, Backpack window left click handler if constexpr( - std::is_same_v> || - std::is_same_v> || - std::is_same_v> || - std::is_same_v>) + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v>) { const auto pickedArtInst = getPickedArtifact(); const auto heroPickedArt = getHeroPickedArtifact(); @@ -151,7 +146,7 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & { isTransferAllowed = true; } - if constexpr(std::is_same_v>) + if constexpr(std::is_same_v>) { if(hero != heroPickedArt) isTransferAllowed = false; @@ -163,35 +158,26 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & { if(artSetPtr->getHero()->getOwner() == LOCPLINT->playerID) { - if(checkSpecialArts(*art, hero, std::is_same_v> ? true : false)) + if(checkSpecialArts(*art, hero, std::is_same_v> ? true : false)) { assert(artSetPtr->getHero()->getSlotByInstance(art) != ArtifactPosition::PRE_FIRST); - auto srcLoc = ArtifactLocation(artSetPtr->getHero()->id, artPlace.slot); - auto dstLoc = ArtifactLocation(artSetPtr->getHero()->id, ArtifactPosition::TRANSITION_POS); + auto srcLoc = ArtifactLocation(hero->id, artPlace.slot); + auto dstLoc = ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS); if(GH.isKeyboardCtrlDown()) { - std::shared_ptr anotherArtSet = nullptr; - - for(auto set : artSets) - { - if(std::holds_alternative>(set)) + for(auto & anotherSet : artSets) + if(std::holds_alternative>(anotherSet)) { - std::shared_ptr heroEquipmentPointer = std::get>(set).lock(); - if(heroEquipmentPointer->getHero()->id != artSetPtr->getHero()->id) + auto anotherHeroEquipment = std::get>(anotherSet); + if(hero->id != anotherHeroEquipment->getHero()->id) { - anotherArtSet = heroEquipmentPointer; + dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE; + dstLoc.artHolder = anotherHeroEquipment->getHero()->id; break; } } - } - - if(anotherArtSet != nullptr) - { - dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE; - dstLoc.artHolder = anotherArtSet->getHero()->id; - } } else if(GH.isKeyboardAltDown()) { @@ -215,7 +201,7 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & } } - if constexpr(std::is_same_v>) + if constexpr(std::is_same_v>) { if(!isTransferAllowed && artPlace.getArt() && !GH.isKeyboardCtrlDown() && !GH.isKeyboardAltDown() && closeCallback) closeCallback(); @@ -227,7 +213,7 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & } } // Market window left click handler - else if constexpr(std::is_same_v>) + else if constexpr(std::is_same_v>) { if(artSetPtr->selectArtCallback && artPlace.getArt()) { @@ -244,7 +230,7 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(const CArtifactsOfHeroBase & } } } - else if constexpr(std::is_same_v>) + else if constexpr(std::is_same_v>) { const auto hero = artSetPtr->getHero(); LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero->id, artPlace.slot), ArtifactLocation(hero->id, artSetPtr->getFilterSlot())); @@ -263,16 +249,14 @@ void CWindowWithArtifacts::showPopupArtPlaceHero(const CArtifactsOfHeroBase & ar return; std::visit( - [&artPlace, &cursorPosition](auto artSetWeak) -> void + [&artPlace, &cursorPosition](auto artSetPtr) { - const auto artSetPtr = artSetWeak.lock(); - // Hero (Main, Exchange) window, Kingdom window, Backpack window right click handler if constexpr( - std::is_same_v> || - std::is_same_v> || - std::is_same_v> || - std::is_same_v>) + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v>) { if(artPlace.getArt()) { @@ -290,8 +274,8 @@ void CWindowWithArtifacts::showPopupArtPlaceHero(const CArtifactsOfHeroBase & ar } // Altar window, Market window right click handler else if constexpr( - std::is_same_v> || - std::is_same_v>) + std::is_same_v> || + std::is_same_v>) { if(artPlace.getArt() && artPlace.text.size()) artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition); @@ -307,12 +291,11 @@ void CWindowWithArtifacts::gestureArtPlaceHero(const CArtifactsOfHeroBase & arts return; std::visit( - [&artPlace, cursorPosition](auto artSetWeak) -> void + [&artPlace, cursorPosition](auto artSetPtr) { - const auto artSetPtr = artSetWeak.lock(); if constexpr( - std::is_same_v> || - std::is_same_v>) + std::is_same_v> || + std::is_same_v>) { if(!settings["general"]["enableUiEnhancements"].Bool()) return; @@ -340,15 +323,12 @@ void CWindowWithArtifacts::deactivate() void CWindowWithArtifacts::enableArtifactsCostumeSwitcher() const { - for(auto artSet : artSets) + for(auto & artSet : artSets) std::visit( - [](auto artSetWeak) + [](auto artSetPtr) { - if constexpr(std::is_same_v>) - { - if(auto artSetPtr = artSetWeak.lock()) - artSetPtr->enableArtifactsCostumeSwitcher(); - } + if constexpr(std::is_same_v>) + artSetPtr->enableArtifactsCostumeSwitcher(); }, artSet); } @@ -359,50 +339,24 @@ void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc) void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw) { - const auto pickedArtInst = getPickedArtifact(); - auto artifactMovedBody = [this, withRedraw, &destLoc, &pickedArtInst](auto artSetWeak) -> void + auto artifactMovedBody = [this](auto artSetPtr) { - if(auto artSetPtr = artSetWeak.lock()) + if(const auto pickedArtInst = getPickedArtifact()) { - const auto hero = artSetPtr->getHero(); - if(pickedArtInst) - { - setCursorAnimation(*pickedArtInst); - } - else - { - artSetPtr->unmarkSlots(); - CCS->curh->dragAndDropCursor(nullptr); - } - if(withRedraw) - { - artSetPtr->updateWornSlots(); - artSetPtr->updateBackpackSlots(); - - // Update arts bonuses on window. - // TODO rework this part when CHeroWindow and CExchangeWindow are reworked - if(auto * chw = dynamic_cast(this)) - { - chw->update(hero, true); - } - else if(auto * cew = dynamic_cast(this)) - { - cew->updateWidgets(); - } - artSetPtr->redraw(); - } - - // Make sure the status bar is updated so it does not display old text - if(destLoc.artHolder == hero->id) - { - if(auto artPlace = artSetPtr->getArtPlace(destLoc.slot)) - artPlace->hover(true); - } + setCursorAnimation(*pickedArtInst); + } + else + { + artSetPtr->unmarkSlots(); + CCS->curh->dragAndDropCursor(nullptr); } }; - for(auto artSetWeak : artSets) - std::visit(artifactMovedBody, artSetWeak); + for(auto & artSet : artSets) + std::visit(artifactMovedBody, artSet); + + if(withRedraw) + update(); } void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc) @@ -416,35 +370,43 @@ void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc) update(); } -void CWindowWithArtifacts::update() const +void CWindowWithArtifacts::update() { - auto updateSlotBody = [](auto artSetWeak) -> void - { - if(const auto artSetPtr = artSetWeak.lock()) - { - artSetPtr->updateWornSlots(); - artSetPtr->updateBackpackSlots(); - artSetPtr->redraw(); - } - }; + for(auto & artSet : artSets) + std::visit([this](auto artSetPtr) + { + artSetPtr->updateWornSlots(); + artSetPtr->updateBackpackSlots(); - for(auto artSetWeak : artSets) - std::visit(updateSlotBody, artSetWeak); + // Update arts bonuses on window. + // TODO rework this part when CHeroWindow and CExchangeWindow are reworked + if(auto * chw = dynamic_cast(this)) + { + chw->update(artSetPtr->getHero(), true); + } + else if(auto * cew = dynamic_cast(this)) + { + cew->updateWidgets(); + } + + // Make sure the status bar is updated so it does not display old text + if(auto artPlace = artSetPtr->getArtPlace(GH.getCursorPosition())) + artPlace->hover(true); + + artSetPtr->redraw(); + }, artSet); } std::optional CWindowWithArtifacts::findAOHbyRef(const CArtifactsOfHeroBase & artsInst) { std::optional res; - - auto findAOHBody = [&res, &artsInst](auto & artSetWeak) -> void + for(auto & artSet : artSets) { - if(&artsInst == artSetWeak.lock().get()) - res = artSetWeak; - }; - - for(auto artSetWeak : artSets) - { - std::visit(findAOHBody, artSetWeak); + std::visit([&res, &artsInst](auto & artSetPtr) + { + if(&artsInst == artSetPtr.get()) + res = artSetPtr; + }, artSet); if(res.has_value()) return res; } @@ -456,20 +418,17 @@ void CWindowWithArtifacts::markPossibleSlots() if(const auto pickedArtInst = getPickedArtifact()) { const auto heroArtOwner = getHeroPickedArtifact(); - auto artifactAssembledBody = [&pickedArtInst, &heroArtOwner](auto artSetWeak) -> void + auto artifactAssembledBody = [&pickedArtInst, &heroArtOwner](auto artSetPtr) { - if(auto artSetPtr = artSetWeak.lock()) + if(artSetPtr->isActive()) { - if(artSetPtr->isActive()) - { - const auto hero = artSetPtr->getHero(); - if(heroArtOwner == hero || !std::is_same_v>) - artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID); - } + const auto hero = artSetPtr->getHero(); + if(heroArtOwner == hero || !std::is_same_v>) + artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID); } }; - for(auto artSetWeak : artSets) + for(auto & artSetWeak : artSets) std::visit(artifactAssembledBody, artSetWeak); } } diff --git a/client/windows/CWindowWithArtifacts.h b/client/windows/CWindowWithArtifacts.h index 31e871336..808b56607 100644 --- a/client/windows/CWindowWithArtifacts.h +++ b/client/windows/CWindowWithArtifacts.h @@ -20,12 +20,12 @@ class CWindowWithArtifacts : virtual public CWindowObject { public: using ArtifactsOfHeroVar = std::variant< - std::weak_ptr, - std::weak_ptr, - std::weak_ptr, - std::weak_ptr, - std::weak_ptr, - std::weak_ptr>; + std::shared_ptr, + std::shared_ptr, + std::shared_ptr, + std::shared_ptr, + std::shared_ptr, + std::shared_ptr>; using CloseCallback = std::function; std::vector artSets; @@ -50,7 +50,7 @@ public: virtual void artifactAssembled(const ArtifactLocation & artLoc); protected: - void update() const; + void update(); std::optional findAOHbyRef(const CArtifactsOfHeroBase & artsInst); void markPossibleSlots(); bool checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade) const;