| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- /*
- * CWindowWithArtifacts.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "CWindowWithArtifacts.h"
- #include "../gui/CGuiHandler.h"
- #include "../gui/CursorHandler.h"
- #include "../gui/WindowHandler.h"
- #include "../render/IRenderHandler.h"
- #include "../render/CAnimation.h"
- #include "../render/IImage.h"
- #include "../widgets/CComponent.h"
- #include "../windows/CHeroWindow.h"
- #include "../windows/CSpellWindow.h"
- #include "../windows/GUIClasses.h"
- #include "../windows/CHeroBackpackWindow.h"
- #include "../CPlayerInterface.h"
- #include "../CGameInfo.h"
- #include "../../lib/ArtifactUtils.h"
- #include "../../lib/CGeneralTextHandler.h"
- #include "../../lib/networkPacks/ArtifactLocation.h"
- #include "../../lib/CConfigHandler.h"
- #include "../../CCallback.h"
- CWindowWithArtifacts::CWindowWithArtifacts(const std::vector<ArtifactsOfHeroVar> * artSets)
- {
- if(artSets)
- this->artSets.insert(this->artSets.end(), artSets->begin(), artSets->end());
- }
- void CWindowWithArtifacts::addSet(ArtifactsOfHeroVar 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->onClickPrassedArtPlace(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, 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)
- {
- showQuickBackpackWindow(*artSet, artPlace, cursorPosition);
- };
- }
- }, newArtSet);
- }
- void CWindowWithArtifacts::addCloseCallback(const CloseCallback & callback)
- {
- closeCallback = callback;
- }
- const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact()
- {
- const CGHeroInstance * hero = nullptr;
- for(auto & artSet : artSets)
- std::visit([&hero](auto artSetPtr)
- {
- if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
- {
- hero = artSetPtr->getHero();
- return;
- }
- }, artSet);
- return hero;
- }
- const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact()
- {
- const CArtifactInstance * art = nullptr;
- for(auto & artSet : artSets)
- std::visit([&art](auto artSetPtr)
- {
- if(const auto pickedArt = artSetPtr->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
- {
- art = pickedArt;
- return;
- }
- }, artSet);
- return art;
- }
- void CWindowWithArtifacts::clickPressedOnArtPlace(const CGHeroInstance & hero, const ArtifactPosition & slot,
- bool allowExchange, bool altarTrading, bool closeWindow)
- {
- if(!LOCPLINT->makingTurn)
- return;
- if(const auto heroArtOwner = getHeroPickedArtifact())
- {
- if(allowExchange || hero.id == heroArtOwner->id)
- putPickedArtifact(hero, slot);
- }
- else if(auto art = hero.getArt(slot))
- {
- if(hero.getOwner() == LOCPLINT->playerID)
- {
- if(checkSpecialArts(*art, hero, altarTrading))
- {
- assert(hero.getArt(slot));
- auto srcLoc = ArtifactLocation(hero.id, slot);
- auto dstLoc = ArtifactLocation(hero.id, ArtifactPosition::TRANSITION_POS);
- if(GH.isKeyboardCtrlDown())
- {
- for(auto & anotherSet : artSets)
- if(std::holds_alternative<std::shared_ptr<CArtifactsOfHeroMain>>(anotherSet))
- {
- const auto anotherHeroEquipment = std::get<std::shared_ptr<CArtifactsOfHeroMain>>(anotherSet);
- if(hero.id != anotherHeroEquipment->getHero()->id)
- {
- dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
- dstLoc.artHolder = anotherHeroEquipment->getHero()->id;
- break;
- }
- }
- }
- else if(GH.isKeyboardAltDown())
- {
- const auto artId = hero.getArt(slot)->getTypeId();
- if(ArtifactUtils::isSlotEquipment(slot))
- dstLoc.slot = ArtifactUtils::getArtBackpackPosition(&hero, artId);
- else if(ArtifactUtils::isSlotBackpack(slot))
- dstLoc.slot = ArtifactUtils::getArtEquippedPosition(&hero, artId);
- }
- else if(closeWindow && closeCallback)
- {
- closeCallback();
- }
- if(dstLoc.slot != ArtifactPosition::PRE_FIRST)
- LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
- }
- }
- else
- {
- for(const auto & artSlot : ArtifactUtils::unmovableSlots())
- if(slot == artSlot)
- {
- LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
- break;
- }
- }
- }
- }
- void CWindowWithArtifacts::swapArtifactAndClose(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const ArtifactLocation & dstLoc)
- {
- LOCPLINT->cb->swapArtifacts(ArtifactLocation(artsInst.getHero()->id, artPlace.slot), dstLoc);
- if(closeCallback)
- closeCallback();
- }
- void CWindowWithArtifacts::showArtifactAssembling(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition)
- {
- if(artPlace.getArt())
- {
- if(ArtifactUtilsClient::askToDisassemble(artsInst.getHero(), artPlace.slot))
- return;
- if(ArtifactUtilsClient::askToAssemble(artsInst.getHero(), artPlace.slot))
- return;
- if(artPlace.text.size())
- artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
- }
- }
- void CWindowWithArtifacts::showArifactInfo(CArtPlace & artPlace, const Point & cursorPosition)
- {
- if(artPlace.getArt() && artPlace.text.size())
- artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
- }
- void CWindowWithArtifacts::showQuickBackpackWindow(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition)
- {
- if(!settings["general"]["enableUiEnhancements"].Bool())
- return;
- GH.windows().createAndPushWindow<CHeroQuickBackpackWindow>(artsInst.getHero(), artPlace.slot);
- auto backpackWindow = GH.windows().topWindow<CHeroQuickBackpackWindow>();
- backpackWindow->moveTo(cursorPosition - Point(1, 1));
- backpackWindow->fitToScreen(15);
- }
- void CWindowWithArtifacts::activate()
- {
- if(const auto art = getPickedArtifact())
- setCursorAnimation(*art);
- CWindowObject::activate();
- }
- void CWindowWithArtifacts::deactivate()
- {
- CCS->curh->dragAndDropCursor(nullptr);
- CWindowObject::deactivate();
- }
- void CWindowWithArtifacts::enableArtifactsCostumeSwitcher() const
- {
- for(auto & artSet : artSets)
- std::visit(
- [](auto artSetPtr)
- {
- if constexpr(std::is_same_v<decltype(artSetPtr), std::shared_ptr<CArtifactsOfHeroMain>>)
- artSetPtr->enableArtifactsCostumeSwitcher();
- }, artSet);
- }
- void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
- {
- update();
- }
- void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
- {
- auto artifactMovedBody = [this](auto artSetPtr)
- {
- if(const auto pickedArtInst = getPickedArtifact())
- {
- setCursorAnimation(*pickedArtInst);
- }
- else
- {
- artSetPtr->unmarkSlots();
- CCS->curh->dragAndDropCursor(nullptr);
- }
- };
- for(auto & artSet : artSets)
- std::visit(artifactMovedBody, artSet);
- if(withRedraw)
- update();
- }
- void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc)
- {
- update();
- }
- void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc)
- {
- markPossibleSlots();
- update();
- }
- void CWindowWithArtifacts::update()
- {
- for(auto & artSet : artSets)
- std::visit([this](auto artSetPtr)
- {
- artSetPtr->updateWornSlots();
- artSetPtr->updateBackpackSlots();
- // Update arts bonuses on window.
- // TODO rework this part when CHeroWindow and CExchangeWindow are reworked
- if(auto * chw = dynamic_cast<CHeroWindow*>(this))
- {
- chw->update(artSetPtr->getHero(), true);
- }
- else if(auto * cew = dynamic_cast<CExchangeWindow*>(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);
- }
- void CWindowWithArtifacts::markPossibleSlots()
- {
- if(const auto pickedArtInst = getPickedArtifact())
- {
- const auto heroArtOwner = getHeroPickedArtifact();
- auto artifactAssembledBody = [&pickedArtInst, &heroArtOwner](auto artSetPtr)
- {
- if(artSetPtr->isActive())
- {
- const auto hero = artSetPtr->getHero();
- if(heroArtOwner == hero || !std::is_same_v<decltype(artSetPtr), std::shared_ptr<CArtifactsOfHeroKingdom>>)
- artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
- }
- };
- for(auto & artSet : artSets)
- std::visit(artifactAssembledBody, artSet);
- }
- }
- bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance & hero, bool isTrade) const
- {
- const auto artId = artInst.getTypeId();
-
- if(artId == ArtifactID::SPELLBOOK)
- {
- GH.windows().createAndPushWindow<CSpellWindow>(&hero, LOCPLINT, LOCPLINT->battleInt.get());
- return false;
- }
- if(artId == ArtifactID::CATAPULT)
- {
- // The Catapult must be equipped
- LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312],
- std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT))));
- return false;
- }
- if(isTrade && !artInst.artType->isTradable())
- {
- LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21],
- std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, artId)));
- return false;
- }
- return true;
- }
- void CWindowWithArtifacts::setCursorAnimation(const CArtifactInstance & artInst)
- {
- markPossibleSlots();
- if(artInst.isScroll() && settings["general"]["enableUiEnhancements"].Bool())
- {
- assert(artInst.getScrollSpellID().num >= 0);
- const auto animation = GH.renderHandler().loadAnimation(AnimationPath::builtin("spellscr"));
- animation->load(artInst.getScrollSpellID().num);
- CCS->curh->dragAndDropCursor(animation->getImage(artInst.getScrollSpellID().num)->scaleFast(Point(44, 34)));
- }
- else
- {
- CCS->curh->dragAndDropCursor(AnimationPath::builtin("artifact"), artInst.artType->getIconIndex());
- }
- }
- void CWindowWithArtifacts::putPickedArtifact(const CGHeroInstance & curHero, const ArtifactPosition & targetSlot)
- {
- const auto heroArtOwner = getHeroPickedArtifact();
- const auto pickedArt = getPickedArtifact();
- auto srcLoc = ArtifactLocation(heroArtOwner->id, ArtifactPosition::TRANSITION_POS);
- auto dstLoc = ArtifactLocation(curHero.id, targetSlot);
- if(ArtifactUtils::isSlotBackpack(dstLoc.slot))
- {
- if(pickedArt->artType->isBig())
- {
- // War machines cannot go to backpack
- LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % pickedArt->artType->getNameTranslated()));
- }
- else
- {
- if(ArtifactUtils::isBackpackFreeSlots(heroArtOwner))
- LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
- else
- LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
- }
- }
- // Check if artifact transfer is possible
- else if(pickedArt->canBePutAt(&curHero, dstLoc.slot, true) && (!curHero.getArt(targetSlot) || curHero.tempOwner == LOCPLINT->playerID))
- {
- LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
- }
- }
|