1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-01 00:45:26 +02:00

CWindowWithArtifacts remove std::variant

This commit is contained in:
SoundSSGood
2024-05-20 12:47:25 +03:00
parent 0501073d4c
commit 03aaf849f0
13 changed files with 173 additions and 175 deletions

View File

@ -240,6 +240,11 @@ const CArtifactInstance * CArtifactsOfHeroBase::getArt(const ArtifactPosition &
return curHero ? curHero->getArt(slot) : nullptr; return curHero ? curHero->getArt(slot) : nullptr;
} }
void CArtifactsOfHeroBase::enableKeyboardShortcuts()
{
addUsedEvents(AEventsReceiver::KEYBOARD);
}
void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot) void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot)
{ {
// Spurious call from artifactMoved in attempt to update hidden backpack slot // Spurious call from artifactMoved in attempt to update hidden backpack slot

View File

@ -11,9 +11,11 @@
#include "CArtPlace.h" #include "CArtPlace.h"
#include "../gui/Shortcut.h"
class CButton; class CButton;
class CArtifactsOfHeroBase : virtual public CIntObject class CArtifactsOfHeroBase : virtual public CIntObject, public CKeyShortcut
{ {
protected: protected:
using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>; using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>;
@ -45,6 +47,7 @@ public:
virtual const CArtifactInstance * getPickedArtifact(); virtual const CArtifactInstance * getPickedArtifact();
void addGestureCallback(CArtPlace::ClickFunctor callback); void addGestureCallback(CArtPlace::ClickFunctor callback);
const CArtifactInstance * getArt(const ArtifactPosition & slot) const; const CArtifactInstance * getArt(const ArtifactPosition & slot) const;
void enableKeyboardShortcuts();
protected: protected:
const CGHeroInstance * curHero; const CGHeroInstance * curHero;

View File

@ -33,11 +33,6 @@ CArtifactsOfHeroMain::~CArtifactsOfHeroMain()
CArtifactsOfHeroBase::putBackPickedArtifact(); CArtifactsOfHeroBase::putBackPickedArtifact();
} }
void CArtifactsOfHeroMain::enableArtifactsCostumeSwitcher()
{
addUsedEvents(AEventsReceiver::KEYBOARD);
}
void CArtifactsOfHeroMain::keyPressed(EShortcut key) void CArtifactsOfHeroMain::keyPressed(EShortcut key)
{ {
if(!shortcutPressed) if(!shortcutPressed)

View File

@ -11,14 +11,11 @@
#include "CArtifactsOfHeroBase.h" #include "CArtifactsOfHeroBase.h"
#include "../gui/Shortcut.h" class CArtifactsOfHeroMain : public CArtifactsOfHeroBase
class CArtifactsOfHeroMain : public CArtifactsOfHeroBase, public CKeyShortcut
{ {
public: public:
CArtifactsOfHeroMain(const Point & position); CArtifactsOfHeroMain(const Point & position);
~CArtifactsOfHeroMain() override; ~CArtifactsOfHeroMain() override;
void enableArtifactsCostumeSwitcher();
void keyPressed(EShortcut key) override; void keyPressed(EShortcut key) override;
void keyReleased(EShortcut key) override; void keyReleased(EShortcut key) override;

View File

@ -20,7 +20,10 @@
#include "render/Canvas.h" #include "render/Canvas.h"
#include "CPlayerInterface.h" #include "CPlayerInterface.h"
CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero, const std::vector<ArtifactsOfHeroVar> & artsSets) #include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/networkPacks/ArtifactLocation.h"
CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero, const std::vector<CArtifactsOfHeroPtr> & artsSets)
: CWindowWithArtifacts(&artsSets) : CWindowWithArtifacts(&artsSets)
{ {
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
@ -28,7 +31,15 @@ CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero, const std:
stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0)); stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0));
arts = std::make_shared<CArtifactsOfHeroBackpack>(); arts = std::make_shared<CArtifactsOfHeroBackpack>();
arts->moveBy(Point(windowMargin, windowMargin)); arts->moveBy(Point(windowMargin, windowMargin));
addSetAndCallbacks(arts); arts->clickPressedCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
clickPressedOnArtPlace(arts->getHero(), artPlace.slot, true, false, true);
};
arts->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*arts, artPlace, cursorPosition);
};
addSet(arts);
arts->setHero(hero); arts->setHero(hero);
addCloseCallback(std::bind(&CHeroBackpackWindow::close, this)); addCloseCallback(std::bind(&CHeroBackpackWindow::close, this));
quitButton = std::make_shared<CButton>(Point(), AnimationPath::builtin("IOKAY32.def"), CButton::tooltip(""), quitButton = std::make_shared<CButton>(Point(), AnimationPath::builtin("IOKAY32.def"), CButton::tooltip(""),
@ -55,7 +66,16 @@ CHeroQuickBackpackWindow::CHeroQuickBackpackWindow(const CGHeroInstance * hero,
stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0)); stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0));
arts = std::make_shared<CArtifactsOfHeroQuickBackpack>(targetSlot); arts = std::make_shared<CArtifactsOfHeroQuickBackpack>(targetSlot);
arts->moveBy(Point(windowMargin, windowMargin)); arts->moveBy(Point(windowMargin, windowMargin));
addSetAndCallbacks(static_cast<std::shared_ptr<CArtifactsOfHeroQuickBackpack>>(arts)); arts->clickPressedCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
if(const auto curHero = arts->getHero())
swapArtifactAndClose(*arts, artPlace.slot, ArtifactLocation(curHero->id, arts->getFilterSlot()));
};
arts->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
showArifactInfo(artPlace, cursorPosition);
};
addSet(arts);
arts->setHero(hero); arts->setHero(hero);
addCloseCallback(std::bind(&CHeroQuickBackpackWindow::close, this)); addCloseCallback(std::bind(&CHeroQuickBackpackWindow::close, this));
addUsedEvents(GESTURE); addUsedEvents(GESTURE);

View File

@ -16,7 +16,7 @@ class CFilledTexture;
class CHeroBackpackWindow : public CStatusbarWindow, public CWindowWithArtifacts class CHeroBackpackWindow : public CStatusbarWindow, public CWindowWithArtifacts
{ {
public: public:
CHeroBackpackWindow(const CGHeroInstance * hero, const std::vector<ArtifactsOfHeroVar> & artsSets); CHeroBackpackWindow(const CGHeroInstance * hero, const std::vector<CArtifactsOfHeroPtr> & artsSets);
protected: protected:
std::shared_ptr<CArtifactsOfHeroBackpack> arts; std::shared_ptr<CArtifactsOfHeroBackpack> arts;

View File

@ -214,8 +214,20 @@ void CHeroWindow::update()
{ {
arts = std::make_shared<CArtifactsOfHeroMain>(Point(-65, -8)); arts = std::make_shared<CArtifactsOfHeroMain>(Point(-65, -8));
arts->setHero(curHero); arts->setHero(curHero);
addSetAndCallbacks(arts); arts->clickPressedCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
enableArtifactsCostumeSwitcher(); {
clickPressedOnArtPlace(arts->getHero(), artPlace.slot, true, false, false);
};
arts->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*arts, artPlace, cursorPosition);
};
arts->gestureCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
showQuickBackpackWindow(arts->getHero(), artPlace.slot, cursorPosition);
};
addSet(arts);
enableKeyboardShortcuts();
} }
int serial = LOCPLINT->cb->getHeroSerial(curHero, false); int serial = LOCPLINT->cb->getHeroSerial(curHero, false);

View File

@ -548,9 +548,21 @@ std::shared_ptr<CIntObject> CKingdomInterface::createMainTab(size_t index)
switch(index) switch(index)
{ {
case 0: case 0:
return std::make_shared<CKingdHeroList>(size, [this](const CWindowWithArtifacts::ArtifactsOfHeroVar & newHeroSet) return std::make_shared<CKingdHeroList>(size, [this](const CWindowWithArtifacts::CArtifactsOfHeroPtr & newHeroSet)
{ {
addSetAndCallbacks(newHeroSet); newHeroSet->clickPressedCallback = [this, newHeroSet](CArtPlace & artPlace, const Point & cursorPosition)
{
clickPressedOnArtPlace(newHeroSet->getHero(), artPlace.slot, false, false, false);
};
newHeroSet->showPopupCallback = [this, newHeroSet](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*newHeroSet, artPlace, cursorPosition);
};
newHeroSet->gestureCallback = [this, newHeroSet](CArtPlace & artPlace, const Point & cursorPosition)
{
showQuickBackpackWindow(newHeroSet->getHero(), artPlace.slot, cursorPosition);
};
addSet(newHeroSet);
}); });
case 1: case 1:
return std::make_shared<CKingdTownList>(size); return std::make_shared<CKingdTownList>(size);

View File

@ -334,7 +334,7 @@ private:
std::shared_ptr<CLabel> skillsLabel; std::shared_ptr<CLabel> skillsLabel;
public: public:
using CreateHeroItemFunctor = std::function<void(const CWindowWithArtifacts::ArtifactsOfHeroVar)>; using CreateHeroItemFunctor = std::function<void(const CWindowWithArtifacts::CArtifactsOfHeroPtr)>;
CKingdHeroList(size_t maxSize, const CreateHeroItemFunctor & onCreateHeroItemCallback); CKingdHeroList(size_t maxSize, const CreateHeroItemFunctor & onCreateHeroItemCallback);
void updateGarrisons() override; void updateGarrisons() override;

View File

@ -194,7 +194,16 @@ void CMarketWindow::createArtifactsSelling(const IMarket * market, const CGHeroI
artSlotBack->moveTo(pos.topLeft() + Point(18, 339)); artSlotBack->moveTo(pos.topLeft() + Point(18, 339));
auto artsSellingMarket = std::make_shared<CArtifactsSelling>(market, hero); auto artsSellingMarket = std::make_shared<CArtifactsSelling>(market, hero);
artSets.clear(); artSets.clear();
addSetAndCallbacks(artsSellingMarket->getAOHset()); const auto heroArts = artsSellingMarket->getAOHset();
heroArts->clickPressedCallback = [heroArts](CArtPlace & artPlace, const Point & cursorPosition)
{
heroArts->onClickPressedArtPlace(artPlace);
};
heroArts->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
showArifactInfo(artPlace, cursorPosition);
};
addSet(heroArts);
marketWidget = artsSellingMarket; marketWidget = artsSellingMarket;
initWidgetInternals(EMarketMode::ARTIFACT_RESOURCE, CGI->generaltexth->zelp[600]); initWidgetInternals(EMarketMode::ARTIFACT_RESOURCE, CGI->generaltexth->zelp[600]);
} }
@ -234,7 +243,20 @@ void CMarketWindow::createAltarArtifacts(const IMarket * market, const CGHeroIns
auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero); auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero);
marketWidget = altarArtifacts; marketWidget = altarArtifacts;
artSets.clear(); artSets.clear();
addSetAndCallbacks(altarArtifacts->getAOHset()); const auto heroArts = altarArtifacts->getAOHset();
heroArts->clickPressedCallback = [this, heroArts](CArtPlace & artPlace, const Point & cursorPosition)
{
clickPressedOnArtPlace(heroArts->getHero(), artPlace.slot, true, true, false);
};
heroArts->showPopupCallback = [this, heroArts](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*heroArts, artPlace, cursorPosition);
};
heroArts->gestureCallback = [this, heroArts](CArtPlace & artPlace, const Point & cursorPosition)
{
showQuickBackpackWindow(heroArts->getHero(), artPlace.slot, cursorPosition);
};
addSet(heroArts);
initWidgetInternals(EMarketMode::ARTIFACT_EXP, CGI->generaltexth->zelp[568]); initWidgetInternals(EMarketMode::ARTIFACT_EXP, CGI->generaltexth->zelp[568]);
updateHero(); updateHero();
quitButton->addCallback([altarArtifacts](){altarArtifacts->putBackArtifacts();}); quitButton->addCallback([altarArtifacts](){altarArtifacts->putBackArtifacts();});

View File

@ -33,85 +33,17 @@
#include "../../CCallback.h" #include "../../CCallback.h"
CWindowWithArtifacts::CWindowWithArtifacts(const std::vector<ArtifactsOfHeroVar> * artSets) CWindowWithArtifacts::CWindowWithArtifacts(const std::vector<CArtifactsOfHeroPtr> * artSets)
{ {
if(artSets) if(artSets)
this->artSets.insert(this->artSets.end(), artSets->begin(), artSets->end()); this->artSets.insert(this->artSets.end(), artSets->begin(), artSets->end());
} }
void CWindowWithArtifacts::addSet(ArtifactsOfHeroVar newArtSet) void CWindowWithArtifacts::addSet(const std::shared_ptr<CArtifactsOfHeroBase> & newArtSet)
{ {
artSets.emplace_back(newArtSet); artSets.emplace_back(newArtSet);
} }
void CWindowWithArtifacts::addSetAndCallbacks(ArtifactsOfHeroVar newArtSet)
{
addSet(newArtSet);
std::visit([this](auto artSet)
{
if constexpr(std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroMarket>>)
{
artSet->clickPressedCallback = [artSet](CArtPlace & artPlace, const Point & cursorPosition)
{
artSet->onClickPressedArtPlace(artPlace);
};
}
if constexpr(std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroQuickBackpack>>)
{
artSet->clickPressedCallback = [this, artSet](CArtPlace & artPlace, const Point & cursorPosition)
{
if(const auto curHero = artSet->getHero())
swapArtifactAndClose(*artSet, artPlace.slot, ArtifactLocation(curHero->id, artSet->getFilterSlot()));
};
}
if constexpr(
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroMain>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroKingdom>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroAltar>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroBackpack>>)
{
artSet->clickPressedCallback = [this, artSet](CArtPlace & artPlace, const Point & cursorPosition)
{
if(const auto curHero = artSet->getHero())
clickPressedOnArtPlace(*curHero, artPlace.slot,
!std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroKingdom>>,
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroAltar>>,
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroBackpack>>);
};
}
if constexpr(
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroMarket>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroQuickBackpack>>)
{
artSet->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
{
showArifactInfo(artPlace, cursorPosition);
};
}
if constexpr(
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroMain>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroKingdom>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroAltar>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroBackpack>>)
{
artSet->showPopupCallback = [this, artSet](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*artSet, artPlace, cursorPosition);
};
}
if constexpr(
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroMain>> ||
std::is_same_v<decltype(artSet), std::shared_ptr<CArtifactsOfHeroKingdom>>)
{
artSet->gestureCallback = [this, artSet](CArtPlace & artPlace, const Point & cursorPosition)
{
if(const auto curHero = artSet->getHero())
showQuickBackpackWindow(*curHero, artPlace.slot, cursorPosition);
};
}
}, newArtSet);
}
void CWindowWithArtifacts::addCloseCallback(const CloseCallback & callback) void CWindowWithArtifacts::addCloseCallback(const CloseCallback & callback)
{ {
closeCallback = callback; closeCallback = callback;
@ -122,14 +54,11 @@ const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact()
const CGHeroInstance * hero = nullptr; const CGHeroInstance * hero = nullptr;
for(auto & artSet : artSets) for(auto & artSet : artSets)
std::visit([&hero](auto artSetPtr) if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
{ {
if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS)) hero = artSet->getHero();
{ break;
hero = artSetPtr->getHero();
return;
} }
}, artSet);
return hero; return hero;
} }
@ -138,34 +67,33 @@ const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact()
const CArtifactInstance * art = nullptr; const CArtifactInstance * art = nullptr;
for(auto & artSet : artSets) for(auto & artSet : artSets)
std::visit([&art](auto artSetPtr) if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
{
if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
{ {
art = pickedArt; art = pickedArt;
return; break;
} }
}, artSet);
return art; return art;
} }
void CWindowWithArtifacts::clickPressedOnArtPlace(const CGHeroInstance & hero, const ArtifactPosition & slot, void CWindowWithArtifacts::clickPressedOnArtPlace(const CGHeroInstance * hero, const ArtifactPosition & slot,
bool allowExchange, bool altarTrading, bool closeWindow) bool allowExchange, bool altarTrading, bool closeWindow)
{ {
if(!LOCPLINT->makingTurn) if(!LOCPLINT->makingTurn)
return; return;
if(hero == nullptr)
return;
if(const auto heroArtOwner = getHeroPickedArtifact()) if(const auto heroArtOwner = getHeroPickedArtifact())
{ {
if(allowExchange || hero.id == heroArtOwner->id) if(allowExchange || hero->id == heroArtOwner->id)
putPickedArtifact(hero, slot); putPickedArtifact(*hero, slot);
} }
else if(auto art = hero.getArt(slot)) else if(auto art = hero->getArt(slot))
{ {
if(hero.getOwner() == LOCPLINT->playerID) if(hero->getOwner() == LOCPLINT->playerID)
{ {
if(checkSpecialArts(*art, hero, altarTrading)) if(checkSpecialArts(*art, *hero, altarTrading))
onClickPressedCommonArtifact(hero, slot, closeWindow); onClickPressedCommonArtifact(*hero, slot, closeWindow);
} }
else else
{ {
@ -207,13 +135,13 @@ void CWindowWithArtifacts::showArifactInfo(CArtPlace & artPlace, const Point & c
artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition); artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
} }
void CWindowWithArtifacts::showQuickBackpackWindow(const CGHeroInstance & hero, const ArtifactPosition & slot, void CWindowWithArtifacts::showQuickBackpackWindow(const CGHeroInstance * hero, const ArtifactPosition & slot,
const Point & cursorPosition) const const Point & cursorPosition) const
{ {
if(!settings["general"]["enableUiEnhancements"].Bool()) if(!settings["general"]["enableUiEnhancements"].Bool())
return; return;
GH.windows().createAndPushWindow<CHeroQuickBackpackWindow>(&hero, slot); GH.windows().createAndPushWindow<CHeroQuickBackpackWindow>(hero, slot);
auto backpackWindow = GH.windows().topWindow<CHeroQuickBackpackWindow>(); auto backpackWindow = GH.windows().topWindow<CHeroQuickBackpackWindow>();
backpackWindow->moveTo(cursorPosition - Point(1, 1)); backpackWindow->moveTo(cursorPosition - Point(1, 1));
backpackWindow->fitToScreen(15); backpackWindow->fitToScreen(15);
@ -235,15 +163,10 @@ void CWindowWithArtifacts::deactivate()
CWindowObject::deactivate(); CWindowObject::deactivate();
} }
void CWindowWithArtifacts::enableArtifactsCostumeSwitcher() const void CWindowWithArtifacts::enableKeyboardShortcuts() const
{ {
for(auto & artSet : artSets) for(auto & artSet : artSets)
std::visit( artSet->enableKeyboardShortcuts();
[](auto artSetPtr)
{
if constexpr(std::is_same_v<decltype(artSetPtr), std::shared_ptr<CArtifactsOfHeroMain>>)
artSetPtr->enableArtifactsCostumeSwitcher();
}, artSet);
} }
void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc) void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
@ -254,8 +177,6 @@ void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc) void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc)
{ {
for(auto & artSet : artSets) for(auto & artSet : artSets)
std::visit([this](auto artSetPtr)
{
if(const auto pickedArtInst = getPickedArtifact()) if(const auto pickedArtInst = getPickedArtifact())
{ {
markPossibleSlots(); markPossibleSlots();
@ -263,10 +184,9 @@ void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const
} }
else else
{ {
artSetPtr->unmarkSlots(); artSet->unmarkSlots();
CCS->curh->dragAndDropCursor(nullptr); CCS->curh->dragAndDropCursor(nullptr);
} }
}, artSet);
} }
void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc) void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc)
@ -283,15 +203,14 @@ void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc)
void CWindowWithArtifacts::update() void CWindowWithArtifacts::update()
{ {
for(auto & artSet : artSets) for(auto & artSet : artSets)
std::visit([](auto artSetPtr)
{ {
artSetPtr->updateWornSlots(); artSet->updateWornSlots();
artSetPtr->updateBackpackSlots(); artSet->updateBackpackSlots();
// Make sure the status bar is updated so it does not display old text // Make sure the status bar is updated so it does not display old text
if(auto artPlace = artSetPtr->getArtPlace(GH.getCursorPosition())) if(auto artPlace = artSet->getArtPlace(GH.getCursorPosition()))
artPlace->hover(true); artPlace->hover(true);
}, artSet); }
} }
void CWindowWithArtifacts::markPossibleSlots() void CWindowWithArtifacts::markPossibleSlots()
@ -299,18 +218,15 @@ void CWindowWithArtifacts::markPossibleSlots()
if(const auto pickedArtInst = getPickedArtifact()) if(const auto pickedArtInst = getPickedArtifact())
{ {
const auto heroArtOwner = getHeroPickedArtifact(); const auto heroArtOwner = getHeroPickedArtifact();
auto artifactAssembledBody = [&pickedArtInst, &heroArtOwner](auto artSetPtr) for(auto & artSet : artSets)
{ {
if(artSetPtr->isActive()) if(artSet->isActive())
{ {
const auto hero = artSetPtr->getHero(); const auto hero = artSet->getHero();
if(heroArtOwner == hero || !std::is_same_v<decltype(artSetPtr), std::shared_ptr<CArtifactsOfHeroKingdom>>) if(heroArtOwner == hero || !std::dynamic_pointer_cast<CArtifactsOfHeroKingdom>(artSet))
artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID); artSet->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
} }
}; };
for(auto & artSet : artSets)
std::visit(artifactAssembledBody, artSet);
} }
} }
@ -393,21 +309,19 @@ void CWindowWithArtifacts::onClickPressedCommonArtifact(const CGHeroInstance & c
{ {
for(auto & anotherSet : artSets) for(auto & anotherSet : artSets)
{ {
if(std::holds_alternative<std::shared_ptr<CArtifactsOfHeroMain>>(anotherSet)) if(std::dynamic_pointer_cast<CArtifactsOfHeroMain>(anotherSet))
{ {
const auto anotherHeroEquipment = std::get<std::shared_ptr<CArtifactsOfHeroMain>>(anotherSet); if(curHero.id != anotherSet->getHero()->id)
if(curHero.id != anotherHeroEquipment->getHero()->id)
{ {
dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE; dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
dstLoc.artHolder = anotherHeroEquipment->getHero()->id; dstLoc.artHolder = anotherSet->getHero()->id;
break; break;
} }
} }
if(std::holds_alternative<std::shared_ptr<CArtifactsOfHeroAltar>>(anotherSet)) if(const auto heroSetAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(anotherSet))
{ {
const auto heroEquipment = std::get<std::shared_ptr<CArtifactsOfHeroAltar>>(anotherSet);
dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE; dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
dstLoc.artHolder = heroEquipment->altarId; dstLoc.artHolder = heroSetAltar->altarId;
break; break;
} }
} }

View File

@ -19,33 +19,26 @@
class CWindowWithArtifacts : virtual public CWindowObject class CWindowWithArtifacts : virtual public CWindowObject
{ {
public: public:
using ArtifactsOfHeroVar = std::variant< using CArtifactsOfHeroPtr = std::shared_ptr<CArtifactsOfHeroBase>;
std::shared_ptr<CArtifactsOfHeroMarket>,
std::shared_ptr<CArtifactsOfHeroAltar>,
std::shared_ptr<CArtifactsOfHeroKingdom>,
std::shared_ptr<CArtifactsOfHeroMain>,
std::shared_ptr<CArtifactsOfHeroBackpack>,
std::shared_ptr<CArtifactsOfHeroQuickBackpack>>;
using CloseCallback = std::function<void()>; using CloseCallback = std::function<void()>;
std::vector<ArtifactsOfHeroVar> artSets; std::vector<CArtifactsOfHeroPtr> artSets;
CloseCallback closeCallback; CloseCallback closeCallback;
explicit CWindowWithArtifacts(const std::vector<ArtifactsOfHeroVar> * artSets = nullptr); explicit CWindowWithArtifacts(const std::vector<CArtifactsOfHeroPtr> * artSets = nullptr);
void addSet(ArtifactsOfHeroVar newArtSet); void addSet(const std::shared_ptr<CArtifactsOfHeroBase> & newArtSet);
void addSetAndCallbacks(ArtifactsOfHeroVar newArtSet);
void addCloseCallback(const CloseCallback & callback); void addCloseCallback(const CloseCallback & callback);
const CGHeroInstance * getHeroPickedArtifact(); const CGHeroInstance * getHeroPickedArtifact();
const CArtifactInstance * getPickedArtifact(); const CArtifactInstance * getPickedArtifact();
void clickPressedOnArtPlace(const CGHeroInstance & hero, const ArtifactPosition & slot, void clickPressedOnArtPlace(const CGHeroInstance * hero, const ArtifactPosition & slot,
bool allowExchange, bool altarTrading, bool closeWindow); bool allowExchange, bool altarTrading, bool closeWindow);
void swapArtifactAndClose(const CArtifactsOfHeroBase & artsInst, const ArtifactPosition & slot, const ArtifactLocation & dstLoc) const; void swapArtifactAndClose(const CArtifactsOfHeroBase & artsInst, const ArtifactPosition & slot, const ArtifactLocation & dstLoc) const;
void showArtifactAssembling(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) const; void showArtifactAssembling(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) const;
void showArifactInfo(CArtPlace & artPlace, const Point & cursorPosition) const; void showArifactInfo(CArtPlace & artPlace, const Point & cursorPosition) const;
void showQuickBackpackWindow(const CGHeroInstance & hero, const ArtifactPosition & slot, const Point & cursorPosition) const; void showQuickBackpackWindow(const CGHeroInstance * hero, const ArtifactPosition & slot, const Point & cursorPosition) const;
void activate() override; void activate() override;
void deactivate() override; void deactivate() override;
void enableArtifactsCostumeSwitcher() const; void enableKeyboardShortcuts() const;
virtual void artifactRemoved(const ArtifactLocation & artLoc); virtual void artifactRemoved(const ArtifactLocation & artLoc);
virtual void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc); virtual void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc);

View File

@ -763,8 +763,33 @@ CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2,
artifs[1] = std::make_shared<CArtifactsOfHeroMain>(Point(98, 151)); artifs[1] = std::make_shared<CArtifactsOfHeroMain>(Point(98, 151));
artifs[1]->setHero(heroInst[1]); artifs[1]->setHero(heroInst[1]);
addSetAndCallbacks(artifs[0]); artifs[0]->clickPressedCallback = [this, heroArts = artifs[0]](CArtPlace & artPlace, const Point & cursorPosition)
addSetAndCallbacks(artifs[1]); {
clickPressedOnArtPlace(heroArts->getHero(), artPlace.slot, true, false, false);
};
artifs[0]->showPopupCallback = [this, heroArts = artifs[0]](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*heroArts, artPlace, cursorPosition);
};
artifs[0]->gestureCallback = [this, heroArts = artifs[0]](CArtPlace & artPlace, const Point & cursorPosition)
{
showQuickBackpackWindow(heroArts->getHero(), artPlace.slot, cursorPosition);
};
artifs[1]->clickPressedCallback = [this, heroArts = artifs[1]](CArtPlace & artPlace, const Point & cursorPosition)
{
clickPressedOnArtPlace(heroArts->getHero(), artPlace.slot, true, false, false);
};
artifs[1]->showPopupCallback = [this, heroArts = artifs[1]](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*heroArts, artPlace, cursorPosition);
};
artifs[1]->gestureCallback = [this, heroArts = artifs[1]](CArtPlace & artPlace, const Point & cursorPosition)
{
showQuickBackpackWindow(heroArts->getHero(), artPlace.slot, cursorPosition);
};
addSet(artifs[0]);
addSet(artifs[1]);
for(int g=0; g<4; ++g) for(int g=0; g<4; ++g)
{ {