2023-04-23 14:10:35 +02:00
|
|
|
/*
|
|
|
|
* CArtifactsOfHeroBase.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 "CArtifactsOfHeroBase.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-05-01 00:25:48 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2023-04-23 14:10:35 +02:00
|
|
|
|
|
|
|
#include "Buttons.h"
|
|
|
|
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
2023-05-17 15:52:16 +02:00
|
|
|
#include "../../lib/ArtifactUtils.h"
|
2023-04-23 14:10:35 +02:00
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2023-10-23 15:38:05 +02:00
|
|
|
#include "../../lib/networkPacks/ArtifactLocation.h"
|
2023-04-23 14:10:35 +02:00
|
|
|
|
|
|
|
CArtifactsOfHeroBase::CArtifactsOfHeroBase()
|
2024-04-16 16:45:31 +02:00
|
|
|
: curHero(nullptr)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:14:12 +02:00
|
|
|
void CArtifactsOfHeroBase::putBackPickedArtifact()
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
|
|
|
// Artifact located in artifactsTransitionPos should be returned
|
2024-04-30 11:39:20 +02:00
|
|
|
if(const auto art = getPickedArtifact())
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-04-30 11:39:20 +02:00
|
|
|
auto slot = ArtifactUtils::getArtAnyPosition(curHero, art->getTypeId());
|
2023-04-23 14:10:35 +02:00
|
|
|
if(slot == ArtifactPosition::PRE_FIRST)
|
|
|
|
{
|
2023-10-14 21:00:39 +02:00
|
|
|
LOCPLINT->cb->eraseArtifactByClient(ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-14 21:00:39 +02:00
|
|
|
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS), ArtifactLocation(curHero->id, slot));
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBase::init(
|
|
|
|
const Point & position,
|
2024-03-05 17:59:56 +02:00
|
|
|
const BpackScrollFunctor & scrollCallback)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
// CArtifactsOfHeroBase::init may be transform to CArtifactsOfHeroBase::CArtifactsOfHeroBase if OBJECT_CONSTRUCTION is removed
|
|
|
|
OBJECT_CONSTRUCTION;
|
2023-04-23 14:10:35 +02:00
|
|
|
pos += position;
|
2023-08-19 23:22:31 +02:00
|
|
|
for(int g = 0; g < ArtifactPosition::BACKPACK_START; g++)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-05-21 19:00:13 +02:00
|
|
|
artWorn[ArtifactPosition(g)] = std::make_shared<CArtPlace>(slotPos[g]);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
backpack.clear();
|
|
|
|
for(int s = 0; s < 5; s++)
|
|
|
|
{
|
2024-05-21 19:00:13 +02:00
|
|
|
auto artPlace = std::make_shared<CArtPlace>(Point(403 + 46 * s, 365));
|
2023-04-23 14:10:35 +02:00
|
|
|
backpack.push_back(artPlace);
|
|
|
|
}
|
2024-10-21 20:49:22 +02:00
|
|
|
for(auto & artPlace : artWorn)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
|
|
|
artPlace.second->slot = artPlace.first;
|
2024-10-19 15:25:26 +02:00
|
|
|
artPlace.second->setArtifact(ArtifactID(ArtifactID::NONE));
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
2024-10-21 20:49:22 +02:00
|
|
|
for(const auto & artPlace : backpack)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-10-19 15:25:26 +02:00
|
|
|
artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
2024-03-05 17:59:56 +02:00
|
|
|
leftBackpackRoll = std::make_shared<CButton>(Point(379, 364), AnimationPath::builtin("hsbtns3.def"), CButton::tooltip(),
|
2024-03-06 15:16:35 +02:00
|
|
|
[scrollCallback](){scrollCallback(true);}, EShortcut::MOVE_LEFT);
|
2024-03-05 17:59:56 +02:00
|
|
|
rightBackpackRoll = std::make_shared<CButton>(Point(632, 364), AnimationPath::builtin("hsbtns5.def"), CButton::tooltip(),
|
2024-03-06 15:16:35 +02:00
|
|
|
[scrollCallback](){scrollCallback(false);}, EShortcut::MOVE_RIGHT);
|
2023-04-23 14:10:35 +02:00
|
|
|
leftBackpackRoll->block(true);
|
|
|
|
rightBackpackRoll->block(true);
|
2023-09-03 20:41:00 +02:00
|
|
|
|
2024-06-11 18:00:51 +02:00
|
|
|
backpackScroller = std::make_shared<BackpackScroller>(this, Rect(380, 30, 278, 382));
|
|
|
|
backpackScroller->setScrollingEnabled(false);
|
|
|
|
|
2023-09-03 20:41:00 +02:00
|
|
|
setRedrawParent(true);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-10-28 14:30:19 +02:00
|
|
|
void CArtifactsOfHeroBase::setClickPressedArtPlacesCallback(const CArtPlace::ClickFunctor & callback) const
|
2024-10-21 20:49:22 +02:00
|
|
|
{
|
|
|
|
for(const auto & [slot, artPlace] : artWorn)
|
|
|
|
artPlace->setClickPressedCallback(callback);
|
|
|
|
for(const auto & artPlace : backpack)
|
|
|
|
artPlace->setClickPressedCallback(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBase::setShowPopupArtPlacesCallback(const CArtPlace::ClickFunctor & callback) const
|
|
|
|
{
|
|
|
|
for(const auto & [slot, artPlace] : artWorn)
|
|
|
|
artPlace->setShowPopupCallback(callback);
|
|
|
|
for(const auto & artPlace : backpack)
|
|
|
|
artPlace->setShowPopupCallback(callback);
|
|
|
|
}
|
|
|
|
|
2024-10-28 14:30:19 +02:00
|
|
|
void CArtifactsOfHeroBase::clickPressedArtPlace(CComponentHolder & artPlace, const Point & cursorPosition)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-11-13 06:24:00 +02:00
|
|
|
if(auto ownedPlace = getArtPlace(cursorPosition))
|
|
|
|
{
|
|
|
|
if(ownedPlace->isLocked())
|
|
|
|
return;
|
2024-05-02 00:49:17 +02:00
|
|
|
|
2024-11-13 06:24:00 +02:00
|
|
|
if(clickPressedCallback)
|
|
|
|
clickPressedCallback(*ownedPlace, cursorPosition);
|
|
|
|
}
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:27:21 +02:00
|
|
|
void CArtifactsOfHeroBase::showPopupArtPlace(CComponentHolder & artPlace, const Point & cursorPosition)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-11-13 06:24:00 +02:00
|
|
|
if(auto ownedPlace = getArtPlace(cursorPosition))
|
|
|
|
{
|
|
|
|
if(ownedPlace->isLocked())
|
|
|
|
return;
|
2024-05-02 00:49:17 +02:00
|
|
|
|
2024-11-13 06:24:00 +02:00
|
|
|
if(showPopupCallback)
|
|
|
|
showPopupCallback(*ownedPlace, cursorPosition);
|
|
|
|
}
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:27:21 +02:00
|
|
|
void CArtifactsOfHeroBase::gestureArtPlace(CComponentHolder & artPlace, const Point & cursorPosition)
|
2023-12-17 16:30:19 +02:00
|
|
|
{
|
2024-11-13 06:24:00 +02:00
|
|
|
if(auto ownedPlace = getArtPlace(cursorPosition))
|
|
|
|
{
|
|
|
|
if(ownedPlace->isLocked())
|
|
|
|
return;
|
2024-05-02 00:49:17 +02:00
|
|
|
|
2024-11-13 06:24:00 +02:00
|
|
|
if(gestureCallback)
|
|
|
|
gestureCallback(*ownedPlace, cursorPosition);
|
|
|
|
}
|
2023-12-17 16:30:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 14:10:35 +02:00
|
|
|
void CArtifactsOfHeroBase::setHero(const CGHeroInstance * hero)
|
|
|
|
{
|
|
|
|
curHero = hero;
|
2024-11-25 19:11:09 +02:00
|
|
|
if (!hero)
|
|
|
|
return;
|
2023-04-23 14:10:35 +02:00
|
|
|
|
|
|
|
for(auto slot : artWorn)
|
|
|
|
{
|
2024-01-27 23:48:11 +02:00
|
|
|
setSlotData(slot.second, slot.first);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
2024-03-05 17:59:56 +02:00
|
|
|
updateBackpackSlots();
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * CArtifactsOfHeroBase::getHero() const
|
|
|
|
{
|
|
|
|
return curHero;
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:16:35 +02:00
|
|
|
void CArtifactsOfHeroBase::scrollBackpack(bool left)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-03-06 15:16:35 +02:00
|
|
|
LOCPLINT->cb->scrollBackpackArtifacts(curHero->id, left);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 16:59:40 +02:00
|
|
|
void CArtifactsOfHeroBase::markPossibleSlots(const CArtifact * art, bool assumeDestRemoved)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-09-06 16:59:40 +02:00
|
|
|
for(const auto & artPlace : artWorn)
|
2023-10-14 21:00:39 +02:00
|
|
|
artPlace.second->selectSlot(art->canBePutAt(curHero, artPlace.second->slot, assumeDestRemoved));
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBase::unmarkSlots()
|
|
|
|
{
|
|
|
|
for(auto & artPlace : artWorn)
|
|
|
|
artPlace.second->selectSlot(false);
|
|
|
|
|
|
|
|
for(auto & artPlace : backpack)
|
|
|
|
artPlace->selectSlot(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
CArtifactsOfHeroBase::ArtPlacePtr CArtifactsOfHeroBase::getArtPlace(const ArtifactPosition & slot)
|
|
|
|
{
|
2024-10-21 20:49:22 +02:00
|
|
|
if(ArtifactUtils::isSlotEquipment(slot) && artWorn.find(slot) != artWorn.end())
|
|
|
|
return artWorn[slot];
|
|
|
|
if(ArtifactUtils::isSlotBackpack(slot) && slot - ArtifactPosition::BACKPACK_START < backpack.size())
|
|
|
|
return(backpack[slot - ArtifactPosition::BACKPACK_START]);
|
2024-10-20 14:27:21 +02:00
|
|
|
logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
|
|
|
|
return nullptr;
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-05-01 20:18:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-23 14:10:35 +02:00
|
|
|
void CArtifactsOfHeroBase::updateWornSlots()
|
|
|
|
{
|
|
|
|
for(auto place : artWorn)
|
|
|
|
updateSlot(place.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBase::updateBackpackSlots()
|
|
|
|
{
|
2024-03-06 15:16:35 +02:00
|
|
|
ArtifactPosition slot = ArtifactPosition::BACKPACK_START;
|
2024-03-08 14:21:29 +02:00
|
|
|
for(const auto & artPlace : backpack)
|
2024-03-06 15:16:35 +02:00
|
|
|
{
|
|
|
|
setSlotData(artPlace, slot);
|
|
|
|
slot = slot + 1;
|
|
|
|
}
|
2024-03-05 17:59:56 +02:00
|
|
|
auto scrollingPossible = static_cast<int>(curHero->artifactsInBackpack.size()) > backpack.size();
|
|
|
|
// Blocking scrolling if there is not enough artifacts to scroll
|
|
|
|
if(leftBackpackRoll)
|
|
|
|
leftBackpackRoll->block(!scrollingPossible);
|
|
|
|
if(rightBackpackRoll)
|
|
|
|
rightBackpackRoll->block(!scrollingPossible);
|
2024-06-11 18:00:51 +02:00
|
|
|
if (backpackScroller)
|
|
|
|
backpackScroller->setScrollingEnabled(scrollingPossible);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBase::updateSlot(const ArtifactPosition & slot)
|
|
|
|
{
|
2024-01-27 23:48:11 +02:00
|
|
|
setSlotData(getArtPlace(slot), slot);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const CArtifactInstance * CArtifactsOfHeroBase::getPickedArtifact()
|
|
|
|
{
|
|
|
|
// Returns only the picked up artifact. Not just highlighted like in the trading window.
|
2024-04-30 11:39:20 +02:00
|
|
|
if(curHero)
|
2023-04-23 14:10:35 +02:00
|
|
|
return curHero->getArt(ArtifactPosition::TRANSITION_POS);
|
2024-04-30 11:39:20 +02:00
|
|
|
else
|
|
|
|
return nullptr;
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:31:07 +02:00
|
|
|
void CArtifactsOfHeroBase::enableGesture()
|
2023-12-17 16:30:19 +02:00
|
|
|
{
|
|
|
|
for(auto & artPlace : artWorn)
|
|
|
|
{
|
2024-05-20 14:31:07 +02:00
|
|
|
artPlace.second->setGestureCallback(std::bind(&CArtifactsOfHeroBase::gestureArtPlace, this, _1, _2));
|
2023-12-17 16:30:19 +02:00
|
|
|
artPlace.second->addUsedEvents(GESTURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-08 13:16:16 +02:00
|
|
|
const CArtifactInstance * CArtifactsOfHeroBase::getArt(const ArtifactPosition & slot) const
|
2024-05-02 00:49:17 +02:00
|
|
|
{
|
|
|
|
return curHero ? curHero->getArt(slot) : nullptr;
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:47:25 +02:00
|
|
|
void CArtifactsOfHeroBase::enableKeyboardShortcuts()
|
|
|
|
{
|
|
|
|
addUsedEvents(AEventsReceiver::KEYBOARD);
|
|
|
|
}
|
|
|
|
|
2024-01-27 23:48:11 +02:00
|
|
|
void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot)
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
|
|
|
// Spurious call from artifactMoved in attempt to update hidden backpack slot
|
|
|
|
if(!artPlace && ArtifactUtils::isSlotBackpack(slot))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
artPlace->slot = slot;
|
2024-01-27 23:48:11 +02:00
|
|
|
if(auto slotInfo = curHero->getSlot(slot))
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
|
|
|
artPlace->lockSlot(slotInfo->locked);
|
2024-10-19 15:25:26 +02:00
|
|
|
artPlace->setArtifact(slotInfo->artifact->getTypeId(), slotInfo->artifact->getScrollSpellID());
|
2024-05-21 19:00:13 +02:00
|
|
|
if(slotInfo->locked || slotInfo->artifact->isCombined())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the artifact is part of at least one combined artifact, add additional information
|
|
|
|
std::map<const ArtifactID, std::vector<ArtifactID>> arts;
|
2024-10-12 10:41:59 +02:00
|
|
|
for(const auto combinedArt : slotInfo->artifact->getType()->getPartOf())
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-10-12 20:20:29 +02:00
|
|
|
assert(combinedArt->isCombined());
|
|
|
|
arts.try_emplace(combinedArt->getId());
|
|
|
|
CArtifactFittingSet fittingSet(*curHero);
|
2024-05-21 19:00:13 +02:00
|
|
|
for(const auto part : combinedArt->getConstituents())
|
2023-04-23 14:10:35 +02:00
|
|
|
{
|
2024-10-12 20:20:29 +02:00
|
|
|
const auto partSlot = fittingSet.getArtPos(part->getId(), false, false);
|
|
|
|
if(partSlot != ArtifactPosition::PRE_FIRST)
|
|
|
|
{
|
2024-05-21 19:00:13 +02:00
|
|
|
arts.at(combinedArt->getId()).emplace_back(part->getId());
|
2024-10-12 20:20:29 +02:00
|
|
|
fittingSet.lockSlot(partSlot);
|
|
|
|
}
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-21 19:00:13 +02:00
|
|
|
artPlace->addCombinedArtInfo(arts);
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-10-19 15:25:26 +02:00
|
|
|
artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
|
2023-04-23 14:10:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-11 18:00:51 +02:00
|
|
|
|
|
|
|
BackpackScroller::BackpackScroller(CArtifactsOfHeroBase * owner, const Rect & dimensions)
|
|
|
|
: Scrollable(0, Point(), Orientation::HORIZONTAL)
|
|
|
|
, owner(owner)
|
|
|
|
{
|
|
|
|
pos = dimensions + pos.topLeft();
|
|
|
|
setPanningStep(46);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackpackScroller::scrollBy(int distance)
|
|
|
|
{
|
|
|
|
if (distance != 0)
|
|
|
|
owner->scrollBackpack(distance < 0);
|
|
|
|
}
|