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"
|
2023-07-06 21:14:12 +02:00
|
|
|
#include "GameSettings.h"
|
2023-07-17 18:42:55 +02:00
|
|
|
#include "ObjectLists.h"
|
2023-07-06 21:14:12 +02:00
|
|
|
|
|
|
|
#include "../CPlayerInterface.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-07-16 20:16:12 +02:00
|
|
|
CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack(const Point & position)
|
2023-07-06 21:14:12 +02:00
|
|
|
{
|
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
|
|
|
pos += position;
|
2023-08-21 16:56:50 +02:00
|
|
|
setRedrawParent(true);
|
2023-07-06 21:14:12 +02:00
|
|
|
|
|
|
|
const auto backpackCap = VLC->settings()->getInteger(EGameSettings::HEROES_BACKPACK_CAP);
|
2023-08-21 16:56:50 +02:00
|
|
|
auto visibleCapacityMax = HERO_BACKPACK_WINDOW_SLOT_ROWS * HERO_BACKPACK_WINDOW_SLOT_COLUMNS;
|
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-08-20 18:57:53 +02:00
|
|
|
backpack.resize(visibleCapacityMax);
|
2023-07-06 21:14:12 +02:00
|
|
|
size_t artPlaceIdx = 0;
|
|
|
|
for(auto & artPlace : backpack)
|
|
|
|
{
|
2023-08-31 21:27:32 +02:00
|
|
|
const auto pos = Point(slotSizeWithMargin * (artPlaceIdx % HERO_BACKPACK_WINDOW_SLOT_COLUMNS),
|
|
|
|
slotSizeWithMargin * (artPlaceIdx / HERO_BACKPACK_WINDOW_SLOT_COLUMNS));
|
2023-09-08 17:49:06 +02:00
|
|
|
backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos));
|
2023-08-31 21:27:32 +02:00
|
|
|
artPlace = std::make_shared<CHeroArtPlace>(pos);
|
2023-07-06 21:14:12 +02:00
|
|
|
artPlace->setArtifact(nullptr);
|
|
|
|
artPlace->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
|
2023-09-17 17:40:14 +02:00
|
|
|
artPlace->showPopupCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
|
2023-07-06 21:14:12 +02:00
|
|
|
artPlaceIdx++;
|
|
|
|
}
|
2023-07-17 18:42:55 +02:00
|
|
|
|
2023-08-20 18:57:53 +02:00
|
|
|
if(backpackCap < 0 || visibleCapacityMax < backpackCap)
|
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
|
|
|
|
{
|
|
|
|
scrollBackpack(static_cast<int>(pos) * HERO_BACKPACK_WINDOW_SLOT_COLUMNS - backpackPos);
|
|
|
|
};
|
|
|
|
backpackListBox = std::make_shared<CListBoxWithCallback>(
|
2023-08-21 16:56:50 +02:00
|
|
|
posMoved, onCreate, Point(0, 0), Point(0, 0), HERO_BACKPACK_WINDOW_SLOT_ROWS, 0, 0, 1,
|
2023-08-31 21:27:32 +02:00
|
|
|
Rect(HERO_BACKPACK_WINDOW_SLOT_COLUMNS * slotSizeWithMargin + sliderPosOffsetX, 0, HERO_BACKPACK_WINDOW_SLOT_ROWS * slotSizeWithMargin - 2, 0));
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
2023-08-31 21:27:32 +02:00
|
|
|
|
|
|
|
pos.w = visibleCapacityMax > HERO_BACKPACK_WINDOW_SLOT_COLUMNS ? HERO_BACKPACK_WINDOW_SLOT_COLUMNS : visibleCapacityMax;
|
|
|
|
pos.w *= slotSizeWithMargin;
|
|
|
|
if(backpackListBox)
|
|
|
|
pos.w += sliderPosOffsetX + 16; // 16 is slider width. TODO: get it from CListBox directly;
|
|
|
|
|
|
|
|
pos.h = (visibleCapacityMax / HERO_BACKPACK_WINDOW_SLOT_COLUMNS);
|
|
|
|
if(visibleCapacityMax % HERO_BACKPACK_WINDOW_SLOT_COLUMNS != 0)
|
|
|
|
pos.h += 1;
|
|
|
|
pos.h *= slotSizeWithMargin;
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBackpack::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
|
|
|
|
{
|
|
|
|
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBackpack::pickUpArtifact(CHeroArtPlace & artPlace)
|
|
|
|
{
|
2023-10-14 21:00:39 +02:00
|
|
|
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
|
|
|
|
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 20:16:12 +02:00
|
|
|
void CArtifactsOfHeroBackpack::scrollBackpack(int offset)
|
2023-07-06 21:14:12 +02:00
|
|
|
{
|
2023-07-17 18:42:55 +02:00
|
|
|
if(backpackListBox)
|
2023-08-31 21:27:32 +02:00
|
|
|
backpackListBox->resize(getActiveSlotRowsNum());
|
2023-07-17 18:42:55 +02:00
|
|
|
backpackPos += offset;
|
2023-08-19 23:22:31 +02:00
|
|
|
auto slot = ArtifactPosition::BACKPACK_START + backpackPos;
|
2023-07-17 18:42:55 +02:00
|
|
|
for(auto artPlace : backpack)
|
2023-07-16 20:16:12 +02:00
|
|
|
{
|
2023-07-17 18:42:55 +02:00
|
|
|
setSlotData(artPlace, slot, *curHero);
|
|
|
|
slot = slot + 1;
|
2023-07-16 20:16:12 +02:00
|
|
|
}
|
2023-07-17 18:42:55 +02:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsOfHeroBackpack::updateBackpackSlots()
|
|
|
|
{
|
|
|
|
if(backpackListBox)
|
2023-08-31 21:27:32 +02:00
|
|
|
backpackListBox->resize(getActiveSlotRowsNum());
|
2023-07-17 18:42:55 +02:00
|
|
|
CArtifactsOfHeroBase::updateBackpackSlots();
|
|
|
|
}
|
|
|
|
|
2023-08-31 21:27:32 +02:00
|
|
|
size_t CArtifactsOfHeroBackpack::getActiveSlotRowsNum()
|
2023-07-17 18:42:55 +02:00
|
|
|
{
|
|
|
|
return (curHero->artifactsInBackpack.size() + HERO_BACKPACK_WINDOW_SLOT_COLUMNS - 1) / HERO_BACKPACK_WINDOW_SLOT_COLUMNS;
|
2023-07-06 21:14:12 +02:00
|
|
|
}
|