1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/widgets/CArtifactsOfHeroBase.cpp

293 lines
8.0 KiB
C++
Raw Normal View History

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"
#include "../../lib/networkPacks/ArtifactLocation.h"
2023-04-23 14:10:35 +02:00
CArtifactsOfHeroBase::CArtifactsOfHeroBase()
: backpackPos(0),
2023-07-06 21:14:12 +02:00
curHero(nullptr),
putBackPickedArtCallback(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
if(getPickedArtifact())
{
auto slot = ArtifactUtils::getArtAnyPosition(curHero, curHero->artifactsTransitionPos.begin()->artifact->getTypeId());
if(slot == ArtifactPosition::PRE_FIRST)
{
LOCPLINT->cb->eraseArtifactByClient(ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
2023-04-23 14:10:35 +02:00
}
else
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS), ArtifactLocation(curHero->id, slot));
2023-04-23 14:10:35 +02:00
}
}
2023-07-06 21:14:12 +02:00
if(putBackPickedArtCallback)
putBackPickedArtCallback();
}
void CArtifactsOfHeroBase::setPutBackPickedArtifactCallback(PutBackPickedArtCallback callback)
{
putBackPickedArtCallback = callback;
2023-04-23 14:10:35 +02:00
}
void CArtifactsOfHeroBase::init(
CArtPlace::ClickFunctor lClickCallback,
CArtPlace::ClickFunctor showPopupCallback,
2023-04-23 14:10:35 +02:00
const Point & position,
2023-09-17 17:40:14 +02:00
BpackScrollFunctor scrollCallback)
2023-04-23 14:10:35 +02:00
{
// CArtifactsOfHeroBase::init may be transform to CArtifactsOfHeroBase::CArtifactsOfHeroBase if OBJECT_CONSTRUCTION_CAPTURING is removed
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
pos += position;
for(int g = 0; g < ArtifactPosition::BACKPACK_START; g++)
2023-04-23 14:10:35 +02:00
{
artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
}
backpack.clear();
for(int s = 0; s < 5; s++)
{
auto artPlace = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
backpack.push_back(artPlace);
}
for(auto artPlace : artWorn)
{
artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr);
artPlace.second->setClickPressedCallback(lClickCallback);
artPlace.second->setShowPopupCallback(showPopupCallback);
2023-04-23 14:10:35 +02:00
}
for(auto artPlace : backpack)
{
artPlace->setArtifact(nullptr);
artPlace->setClickPressedCallback(lClickCallback);
artPlace->setShowPopupCallback(showPopupCallback);
2023-04-23 14:10:35 +02:00
}
2023-09-17 17:40:14 +02:00
leftBackpackRoll = std::make_shared<CButton>(Point(379, 364), AnimationPath::builtin("hsbtns3.def"), CButton::tooltip(), [scrollCallback]() {scrollCallback(-1);}, EShortcut::MOVE_LEFT);
rightBackpackRoll = std::make_shared<CButton>(Point(632, 364), AnimationPath::builtin("hsbtns5.def"), CButton::tooltip(), [scrollCallback]() {scrollCallback(+1);}, 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
setRedrawParent(true);
2023-04-23 14:10:35 +02:00
}
2023-12-17 16:30:19 +02:00
void CArtifactsOfHeroBase::clickPrassedArtPlace(CArtPlace & artPlace, const Point & cursorPosition)
2023-04-23 14:10:35 +02:00
{
2023-12-17 16:30:19 +02:00
if(clickPressedCallback)
clickPressedCallback(*this, artPlace, cursorPosition);
2023-04-23 14:10:35 +02:00
}
2023-12-17 16:30:19 +02:00
void CArtifactsOfHeroBase::showPopupArtPlace(CArtPlace & artPlace, const Point & cursorPosition)
2023-04-23 14:10:35 +02:00
{
2023-09-17 17:40:14 +02:00
if(showPopupCallback)
showPopupCallback(*this, artPlace, cursorPosition);
2023-04-23 14:10:35 +02:00
}
2023-12-17 16:30:19 +02:00
void CArtifactsOfHeroBase::gestureArtPlace(CArtPlace & artPlace, const Point & cursorPosition)
{
if(gestureCallback)
gestureCallback(*this, artPlace, cursorPosition);
}
2023-04-23 14:10:35 +02:00
void CArtifactsOfHeroBase::setHero(const CGHeroInstance * hero)
{
curHero = hero;
if(curHero->artifactsInBackpack.size() > 0)
backpackPos %= curHero->artifactsInBackpack.size();
else
backpackPos = 0;
for(auto slot : artWorn)
{
setSlotData(slot.second, slot.first, *curHero);
}
2023-07-16 20:16:12 +02:00
scrollBackpack(0);
2023-04-23 14:10:35 +02:00
}
const CGHeroInstance * CArtifactsOfHeroBase::getHero() const
{
return curHero;
}
void CArtifactsOfHeroBase::scrollBackpack(int offset)
{
scrollBackpackForArtSet(offset, *curHero);
2023-09-03 20:41:00 +02:00
redraw();
2023-04-23 14:10:35 +02:00
}
void CArtifactsOfHeroBase::scrollBackpackForArtSet(int offset, const CArtifactSet & artSet)
{
// offset==-1 => to left; offset==1 => to right
using slotInc = std::function<ArtifactPosition(ArtifactPosition&)>;
auto artsInBackpack = static_cast<int>(artSet.artifactsInBackpack.size());
auto scrollingPossible = artsInBackpack > backpack.size();
slotInc inc_straight = [](ArtifactPosition & slot) -> ArtifactPosition
{
return slot + 1;
};
slotInc inc_ring = [artsInBackpack](ArtifactPosition & slot) -> ArtifactPosition
{
return ArtifactPosition::BACKPACK_START + (slot - ArtifactPosition::BACKPACK_START + 1) % artsInBackpack;
2023-04-23 14:10:35 +02:00
};
slotInc inc;
if(scrollingPossible)
inc = inc_ring;
else
inc = inc_straight;
backpackPos += offset;
if(backpackPos < 0)
backpackPos += artsInBackpack;
if(artsInBackpack)
backpackPos %= artsInBackpack;
auto slot = ArtifactPosition(ArtifactPosition::BACKPACK_START + backpackPos);
2023-04-23 14:10:35 +02:00
for(auto artPlace : backpack)
{
setSlotData(artPlace, slot, artSet);
slot = inc(slot);
}
// Blocking scrolling if there is not enough artifacts to scroll
2023-07-06 21:14:12 +02:00
if(leftBackpackRoll)
leftBackpackRoll->block(!scrollingPossible);
if(rightBackpackRoll)
rightBackpackRoll->block(!scrollingPossible);
2023-04-23 14:10:35 +02:00
}
void CArtifactsOfHeroBase::markPossibleSlots(const CArtifactInstance * art, bool assumeDestRemoved)
{
for(auto artPlace : artWorn)
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)
{
if(ArtifactUtils::isSlotEquipment(slot))
{
if(artWorn.find(slot) == artWorn.end())
{
logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
return nullptr;
}
return artWorn[slot];
}
if(ArtifactUtils::isSlotBackpack(slot))
{
for(ArtPlacePtr artPlace : backpack)
if(artPlace->slot == slot)
return artPlace;
return nullptr;
}
else
{
return nullptr;
}
}
void CArtifactsOfHeroBase::updateWornSlots()
{
for(auto place : artWorn)
updateSlot(place.first);
}
void CArtifactsOfHeroBase::updateBackpackSlots()
{
2023-06-06 13:37:48 +02:00
if(curHero->artifactsInBackpack.size() <= backpack.size() && backpackPos != 0)
backpackPos = 0;
2023-07-17 18:42:55 +02:00
scrollBackpack(0);
2023-04-23 14:10:35 +02:00
}
void CArtifactsOfHeroBase::updateSlot(const ArtifactPosition & slot)
{
setSlotData(getArtPlace(slot), slot, *curHero);
}
const CArtifactInstance * CArtifactsOfHeroBase::getPickedArtifact()
{
// Returns only the picked up artifact. Not just highlighted like in the trading window.
if(!curHero || curHero->artifactsTransitionPos.empty())
return nullptr;
else
return curHero->getArt(ArtifactPosition::TRANSITION_POS);
}
2023-12-17 16:30:19 +02:00
void CArtifactsOfHeroBase::addGestureCallback(CArtPlace::ClickFunctor callback)
{
for(auto & artPlace : artWorn)
{
artPlace.second->setGestureCallback(callback);
artPlace.second->addUsedEvents(GESTURE);
}
}
2023-04-23 14:10:35 +02:00
void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot, const CArtifactSet & artSet)
{
// Spurious call from artifactMoved in attempt to update hidden backpack slot
if(!artPlace && ArtifactUtils::isSlotBackpack(slot))
{
return;
}
artPlace->slot = slot;
if(auto slotInfo = artSet.getSlot(slot))
{
artPlace->lockSlot(slotInfo->locked);
artPlace->setArtifact(slotInfo->artifact);
2023-06-29 17:34:07 +02:00
if(!slotInfo->artifact->isCombined())
2023-04-23 14:10:35 +02:00
{
// If the artifact is part of at least one combined artifact, add additional information
std::map<const CArtifact*, int> arts;
2023-07-03 18:15:40 +02:00
for(const auto combinedArt : slotInfo->artifact->artType->getPartOf())
2023-04-23 14:10:35 +02:00
{
arts.insert(std::pair(combinedArt, 0));
2023-07-03 18:15:40 +02:00
for(const auto part : combinedArt->getConstituents())
2023-09-12 17:30:48 +02:00
{
if(artSet.hasArt(part->getId(), false))
2023-04-23 14:10:35 +02:00
arts.at(combinedArt)++;
2023-09-12 17:30:48 +02:00
}
2023-04-23 14:10:35 +02:00
}
artPlace->addCombinedArtInfo(arts);
}
}
else
{
artPlace->setArtifact(nullptr);
}
}