2023-07-06 21:14:12 +02:00
|
|
|
/*
|
|
|
|
* CArtifactsOfHeroBackpack.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 "CArtifactsOfHeroBackpack.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
|
2023-08-20 18:57:53 +02:00
|
|
|
#include "Images.h"
|
2024-08-31 13:00:36 +02:00
|
|
|
#include "IGameSettings.h"
|
2023-07-17 18:42:55 +02:00
|
|
|
#include "ObjectLists.h"
|
2023-07-06 21:14:12 +02:00
|
|
|
|
|
|
|
#include "../CPlayerInterface.h"
|
2023-11-07 14:17:34 +02:00
|
|
|
#include "../../lib/ArtifactUtils.h"
|
2023-07-16 20:16:12 +02:00
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2023-10-23 15:38:05 +02:00
|
|
|
#include "../../lib/networkPacks/ArtifactLocation.h"
|
2023-07-06 21:14:12 +02:00
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
2023-12-17 16:30:19 +02:00
|
|
|
CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack(size_t slotsColumnsMax, size_t slotsRowsMax)
|
|
|
|
: slotsColumnsMax(slotsColumnsMax)
|
|
|
|
, slotsRowsMax(slotsRowsMax)
|
2024-03-05 17:59:56 +02:00
|
|
|
, backpackPos(0)
|
2023-11-07 14:17:34 +02:00
|
|
|
{
|
|
|
|
setRedrawParent(true);
|
|
|
|
}
|
|
|
|
|
2023-12-17 16:30:19 +02:00
|
|
|
CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack()
|
|
|
|
: CArtifactsOfHeroBackpack(8, 8)
|
2023-07-06 21:14:12 +02:00
|
|
|
{
|
2024-08-31 13:00:36 +02:00
|
|
|
const auto backpackCap = LOCPLINT->cb->getSettings().getInteger(EGameSettings::HEROES_BACKPACK_CAP);
|
2023-12-17 16:30:19 +02:00
|
|
|
auto visibleCapacityMax = slotsRowsMax * slotsColumnsMax;
|
2023-07-06 21:14:12 +02:00
|
|
|
if(backpackCap >= 0)
|
2023-08-20 18:57:53 +02:00
|
|
|
visibleCapacityMax = visibleCapacityMax > backpackCap ? backpackCap : visibleCapacityMax;
|
2023-07-06 21:14:12 +02:00
|
|
|
|
2023-11-07 14:17:34 +02:00
|
|
|
initAOHbackpack(visibleCapacityMax, backpackCap < 0 || visibleCapacityMax < backpackCap);
|
2024-10-28 14:30:19 +02:00
|
|
|
setClickPressedArtPlacesCallback(std::bind(&CArtifactsOfHeroBase::clickPressedArtPlace, this, _1, _2));
|
2024-10-21 20:49:22 +02:00
|
|
|
setShowPopupArtPlacesCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
|
2023-11-07 14:17:34 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:52:50 +02:00
|
|
|
void CArtifactsOfHeroBackpack::onSliderMoved(int newVal)
|
2024-03-29 12:29:16 +02:00
|
|
|
{
|
|
|
|
backpackPos += newVal;
|
|
|
|
updateBackpackSlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBackpack::updateBackpackSlots()
|
2023-11-07 14:17:34 +02:00
|
|
|
{
|
|
|
|
if(backpackListBox)
|
|
|
|
backpackListBox->resize(getActiveSlotRowsNum());
|
|
|
|
auto slot = ArtifactPosition::BACKPACK_START + backpackPos;
|
2024-03-29 12:29:16 +02:00
|
|
|
for(const auto & artPlace : backpack)
|
2023-11-07 14:17:34 +02:00
|
|
|
{
|
2024-01-27 23:48:11 +02:00
|
|
|
setSlotData(artPlace, slot);
|
2023-11-07 14:17:34 +02:00
|
|
|
slot = slot + 1;
|
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CArtifactsOfHeroBackpack::getActiveSlotRowsNum()
|
|
|
|
{
|
2023-12-17 16:30:19 +02:00
|
|
|
return (curHero->artifactsInBackpack.size() + slotsColumnsMax - 1) / slotsColumnsMax;
|
2023-11-07 14:17:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CArtifactsOfHeroBackpack::getSlotsNum()
|
|
|
|
{
|
|
|
|
return backpack.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBackpack::initAOHbackpack(size_t slots, bool slider)
|
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-11-07 14:17:34 +02:00
|
|
|
|
|
|
|
backpack.resize(slots);
|
2023-07-06 21:14:12 +02:00
|
|
|
size_t artPlaceIdx = 0;
|
|
|
|
for(auto & artPlace : backpack)
|
|
|
|
{
|
2023-12-17 16:30:19 +02:00
|
|
|
const auto pos = Point(slotSizeWithMargin * (artPlaceIdx % slotsColumnsMax),
|
|
|
|
slotSizeWithMargin * (artPlaceIdx / slotsColumnsMax));
|
2023-09-08 17:49:06 +02:00
|
|
|
backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos));
|
2024-05-21 19:00:13 +02:00
|
|
|
artPlace = std::make_shared<CArtPlace>(pos);
|
2024-10-19 15:25:26 +02:00
|
|
|
artPlace->setArtifact(ArtifactID(ArtifactID::NONE));
|
2023-07-06 21:14:12 +02:00
|
|
|
artPlaceIdx++;
|
|
|
|
}
|
2023-07-17 18:42:55 +02:00
|
|
|
|
2023-11-07 14:17:34 +02:00
|
|
|
if(slider)
|
2023-07-06 21:14:12 +02:00
|
|
|
{
|
2023-07-17 18:42:55 +02:00
|
|
|
auto onCreate = [](size_t index) -> std::shared_ptr<CIntObject>
|
|
|
|
{
|
|
|
|
return std::make_shared<CIntObject>();
|
|
|
|
};
|
|
|
|
CListBoxWithCallback::MovedPosCallback posMoved = [this](size_t pos) -> void
|
|
|
|
{
|
2024-03-07 16:52:50 +02:00
|
|
|
onSliderMoved(static_cast<int>(pos) * slotsColumnsMax - backpackPos);
|
2023-07-17 18:42:55 +02:00
|
|
|
};
|
|
|
|
backpackListBox = std::make_shared<CListBoxWithCallback>(
|
2023-12-17 16:30:19 +02:00
|
|
|
posMoved, onCreate, Point(0, 0), Point(0, 0), slotsRowsMax, 0, 0, 1,
|
|
|
|
Rect(slotsColumnsMax * slotSizeWithMargin + sliderPosOffsetX, 0, slotsRowsMax * slotSizeWithMargin - 2, 0));
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
2023-08-31 21:27:32 +02:00
|
|
|
|
2023-12-17 16:30:19 +02:00
|
|
|
pos.w = slots > slotsColumnsMax ? slotsColumnsMax : slots;
|
2023-08-31 21:27:32 +02:00
|
|
|
pos.w *= slotSizeWithMargin;
|
2023-11-07 14:17:34 +02:00
|
|
|
if(slider)
|
2023-08-31 21:27:32 +02:00
|
|
|
pos.w += sliderPosOffsetX + 16; // 16 is slider width. TODO: get it from CListBox directly;
|
2023-12-17 16:30:19 +02:00
|
|
|
pos.h = calcRows(slots) * slotSizeWithMargin;
|
|
|
|
}
|
2023-08-31 21:27:32 +02:00
|
|
|
|
2023-12-17 16:30:19 +02:00
|
|
|
size_t CArtifactsOfHeroBackpack::calcRows(size_t slots)
|
|
|
|
{
|
|
|
|
size_t rows = 0;
|
|
|
|
if(slotsColumnsMax != 0)
|
|
|
|
{
|
|
|
|
rows = slots / slotsColumnsMax;
|
|
|
|
if(slots % slotsColumnsMax != 0)
|
|
|
|
rows += 1;
|
|
|
|
}
|
|
|
|
return rows;
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
|
|
|
|
2023-12-17 16:30:19 +02:00
|
|
|
CArtifactsOfHeroQuickBackpack::CArtifactsOfHeroQuickBackpack(const ArtifactPosition filterBySlot)
|
|
|
|
: CArtifactsOfHeroBackpack(0, 0)
|
2023-07-06 21:14:12 +02:00
|
|
|
{
|
2023-11-07 14:17:34 +02:00
|
|
|
if(!ArtifactUtils::isSlotEquipment(filterBySlot))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->filterBySlot = filterBySlot;
|
2024-10-21 20:49:22 +02:00
|
|
|
setShowPopupArtPlacesCallback(std::bind(&CArtifactsOfHeroBase::showPopupArtPlace, this, _1, _2));
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-07 14:17:34 +02:00
|
|
|
void CArtifactsOfHeroQuickBackpack::setHero(const CGHeroInstance * hero)
|
2023-07-06 21:14:12 +02:00
|
|
|
{
|
2023-11-07 14:17:34 +02:00
|
|
|
if(curHero == hero)
|
|
|
|
return;
|
|
|
|
|
|
|
|
curHero = hero;
|
|
|
|
if(curHero)
|
2023-07-16 20:16:12 +02:00
|
|
|
{
|
2023-11-13 23:16:02 +02:00
|
|
|
ArtifactID artInSlotId = ArtifactID::NONE;
|
|
|
|
SpellID scrollInSlotSpellId = SpellID::NONE;
|
|
|
|
if(auto artInSlot = curHero->getArt(filterBySlot))
|
|
|
|
{
|
|
|
|
artInSlotId = artInSlot->getTypeId();
|
|
|
|
scrollInSlotSpellId = artInSlot->getScrollSpellID();
|
|
|
|
}
|
|
|
|
|
2023-12-17 16:30:19 +02:00
|
|
|
std::map<const ArtifactID, const CArtifactInstance*> filteredArts;
|
2023-11-07 14:17:34 +02:00
|
|
|
for(auto & slotInfo : curHero->artifactsInBackpack)
|
2023-11-13 23:16:02 +02:00
|
|
|
if(slotInfo.artifact->getTypeId() != artInSlotId && !slotInfo.artifact->isScroll() &&
|
2024-10-12 10:41:59 +02:00
|
|
|
slotInfo.artifact->getType()->canBePutAt(curHero, filterBySlot, true))
|
2023-11-13 23:16:02 +02:00
|
|
|
{
|
2023-12-17 16:30:19 +02:00
|
|
|
filteredArts.insert(std::pair(slotInfo.artifact->getTypeId(), slotInfo.artifact));
|
2023-11-13 23:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<const SpellID, const CArtifactInstance*> filteredScrolls;
|
|
|
|
if(filterBySlot == ArtifactPosition::MISC1 || filterBySlot == ArtifactPosition::MISC2 || filterBySlot == ArtifactPosition::MISC3 ||
|
|
|
|
filterBySlot == ArtifactPosition::MISC4 || filterBySlot == ArtifactPosition::MISC5)
|
|
|
|
{
|
|
|
|
for(auto & slotInfo : curHero->artifactsInBackpack)
|
|
|
|
{
|
|
|
|
if(slotInfo.artifact->isScroll() && slotInfo.artifact->getScrollSpellID() != scrollInSlotSpellId)
|
|
|
|
filteredScrolls.insert(std::pair(slotInfo.artifact->getScrollSpellID(), slotInfo.artifact));
|
|
|
|
}
|
2023-11-07 14:17:34 +02:00
|
|
|
}
|
2023-11-13 23:16:02 +02:00
|
|
|
|
2023-11-07 14:17:34 +02:00
|
|
|
backpack.clear();
|
2023-12-17 16:30:19 +02:00
|
|
|
auto requiredSlots = filteredArts.size() + filteredScrolls.size();
|
|
|
|
slotsColumnsMax = ceilf(sqrtf(requiredSlots));
|
|
|
|
slotsRowsMax = calcRows(requiredSlots);
|
|
|
|
initAOHbackpack(requiredSlots, false);
|
2024-10-28 14:30:19 +02:00
|
|
|
setClickPressedArtPlacesCallback(std::bind(&CArtifactsOfHeroBase::clickPressedArtPlace, this, _1, _2));
|
2023-11-07 14:17:34 +02:00
|
|
|
auto artPlace = backpack.begin();
|
|
|
|
for(auto & art : filteredArts)
|
2024-03-28 16:16:10 +02:00
|
|
|
setSlotData(*artPlace++, curHero->getArtPos(art.second));
|
2023-11-13 23:16:02 +02:00
|
|
|
for(auto & art : filteredScrolls)
|
2024-03-28 16:16:10 +02:00
|
|
|
setSlotData(*artPlace++, curHero->getArtPos(art.second));
|
2023-07-16 20:16:12 +02:00
|
|
|
}
|
2023-07-17 18:42:55 +02:00
|
|
|
}
|
|
|
|
|
2023-11-07 14:17:34 +02:00
|
|
|
ArtifactPosition CArtifactsOfHeroQuickBackpack::getFilterSlot()
|
2023-07-17 18:42:55 +02:00
|
|
|
{
|
2023-11-07 14:17:34 +02:00
|
|
|
return filterBySlot;
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
2023-12-17 16:30:19 +02:00
|
|
|
|
|
|
|
void CArtifactsOfHeroQuickBackpack::selectSlotAt(const Point & position)
|
|
|
|
{
|
|
|
|
for(auto & artPlace : backpack)
|
|
|
|
artPlace->selectSlot(artPlace->pos.isInside(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroQuickBackpack::swapSelected()
|
|
|
|
{
|
|
|
|
ArtifactLocation backpackLoc(curHero->id, ArtifactPosition::PRE_FIRST);
|
|
|
|
for(auto & artPlace : backpack)
|
|
|
|
if(artPlace->isSelected())
|
|
|
|
{
|
|
|
|
backpackLoc.slot = artPlace->slot;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(backpackLoc.slot != ArtifactPosition::PRE_FIRST && filterBySlot != ArtifactPosition::PRE_FIRST && curHero)
|
2024-01-27 23:48:11 +02:00
|
|
|
LOCPLINT->cb->swapArtifacts(backpackLoc, ArtifactLocation(curHero->id, filterBySlot));
|
2024-08-31 13:00:36 +02:00
|
|
|
}
|