1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Quick backpack

This commit is contained in:
SoundSSGood 2023-11-07 14:17:34 +02:00
parent f40e711721
commit 29b61081f3
6 changed files with 184 additions and 63 deletions

View File

@ -17,60 +17,28 @@
#include "ObjectLists.h"
#include "../CPlayerInterface.h"
#include "../../lib/ArtifactUtils.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/networkPacks/ArtifactLocation.h"
#include "../../CCallback.h"
CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack(const Point & position)
CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack()
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
pos += position;
setRedrawParent(true);
}
CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack(const Point & position)
: CArtifactsOfHeroBackpack()
{
pos += position;
const auto backpackCap = VLC->settings()->getInteger(EGameSettings::HEROES_BACKPACK_CAP);
auto visibleCapacityMax = HERO_BACKPACK_WINDOW_SLOT_ROWS * HERO_BACKPACK_WINDOW_SLOT_COLUMNS;
auto visibleCapacityMax = slots_rows_max * slots_columns_max;
if(backpackCap >= 0)
visibleCapacityMax = visibleCapacityMax > backpackCap ? backpackCap : visibleCapacityMax;
backpack.resize(visibleCapacityMax);
size_t artPlaceIdx = 0;
for(auto & artPlace : backpack)
{
const auto pos = Point(slotSizeWithMargin * (artPlaceIdx % HERO_BACKPACK_WINDOW_SLOT_COLUMNS),
slotSizeWithMargin * (artPlaceIdx / HERO_BACKPACK_WINDOW_SLOT_COLUMNS));
backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos));
artPlace = std::make_shared<CHeroArtPlace>(pos);
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace->showPopupCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
artPlaceIdx++;
}
if(backpackCap < 0 || visibleCapacityMax < backpackCap)
{
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>(
posMoved, onCreate, Point(0, 0), Point(0, 0), HERO_BACKPACK_WINDOW_SLOT_ROWS, 0, 0, 1,
Rect(HERO_BACKPACK_WINDOW_SLOT_COLUMNS * slotSizeWithMargin + sliderPosOffsetX, 0, HERO_BACKPACK_WINDOW_SLOT_ROWS * slotSizeWithMargin - 2, 0));
}
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;
initAOHbackpack(visibleCapacityMax, backpackCap < 0 || visibleCapacityMax < backpackCap);
}
void CArtifactsOfHeroBackpack::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
@ -107,5 +75,91 @@ void CArtifactsOfHeroBackpack::updateBackpackSlots()
size_t CArtifactsOfHeroBackpack::getActiveSlotRowsNum()
{
return (curHero->artifactsInBackpack.size() + HERO_BACKPACK_WINDOW_SLOT_COLUMNS - 1) / HERO_BACKPACK_WINDOW_SLOT_COLUMNS;
return (curHero->artifactsInBackpack.size() + slots_columns_max - 1) / slots_columns_max;
}
size_t CArtifactsOfHeroBackpack::getSlotsNum()
{
return backpack.size();
}
void CArtifactsOfHeroBackpack::initAOHbackpack(size_t slots, bool slider)
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
backpack.resize(slots);
size_t artPlaceIdx = 0;
for(auto & artPlace : backpack)
{
const auto pos = Point(slotSizeWithMargin * (artPlaceIdx % slots_columns_max),
slotSizeWithMargin * (artPlaceIdx / slots_columns_max));
backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos));
artPlace = std::make_shared<CHeroArtPlace>(pos);
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace->showPopupCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
artPlaceIdx++;
}
if(slider)
{
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) * slots_columns_max - backpackPos);
};
backpackListBox = std::make_shared<CListBoxWithCallback>(
posMoved, onCreate, Point(0, 0), Point(0, 0), slots_rows_max, 0, 0, 1,
Rect(slots_columns_max * slotSizeWithMargin + sliderPosOffsetX, 0, slots_rows_max * slotSizeWithMargin - 2, 0));
}
pos.w = slots > slots_columns_max ? slots_columns_max : slots;
pos.w *= slotSizeWithMargin;
if(slider)
pos.w += sliderPosOffsetX + 16; // 16 is slider width. TODO: get it from CListBox directly;
pos.h = slots / slots_columns_max;
if(slots % slots_columns_max != 0)
pos.h += 1;
pos.h *= slotSizeWithMargin;
}
CArtifactsOfHeroQuickBackpack::CArtifactsOfHeroQuickBackpack(const Point & position, const ArtifactPosition filterBySlot)
{
pos += position;
if(!ArtifactUtils::isSlotEquipment(filterBySlot))
return;
this->filterBySlot = filterBySlot;
}
void CArtifactsOfHeroQuickBackpack::setHero(const CGHeroInstance * hero)
{
if(curHero == hero)
return;
curHero = hero;
if(curHero)
{
std::map<const CArtifact*, const CArtifactInstance*> filteredArts;
for(auto & slotInfo : curHero->artifactsInBackpack)
{
if(slotInfo.artifact->artType->canBePutAt(curHero, filterBySlot, true))
filteredArts.insert(std::pair(slotInfo.artifact->artType, slotInfo.artifact));
}
backpack.clear();
initAOHbackpack(filteredArts.size(), false);
auto artPlace = backpack.begin();
for(auto & art : filteredArts)
setSlotData(*artPlace++, curHero->getSlotByInstance(art.second), *curHero);
}
}
ArtifactPosition CArtifactsOfHeroQuickBackpack::getFilterSlot()
{
return filterBySlot;
}

View File

@ -22,18 +22,33 @@ class CListBoxWithCallback;
class CArtifactsOfHeroBackpack : public CArtifactsOfHeroBase
{
public:
CArtifactsOfHeroBackpack();
CArtifactsOfHeroBackpack(const Point & position);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CHeroArtPlace & artPlace);
void scrollBackpack(int offset) override;
void updateBackpackSlots() override;
size_t getActiveSlotRowsNum();
size_t getSlotsNum();
private:
protected:
std::shared_ptr<CListBoxWithCallback> backpackListBox;
std::vector<std::shared_ptr<CPicture>> backpackSlotsBackgrounds;
const size_t HERO_BACKPACK_WINDOW_SLOT_COLUMNS = 8;
const size_t HERO_BACKPACK_WINDOW_SLOT_ROWS = 8;
const size_t slots_columns_max = 8;
const size_t slots_rows_max = 8;
const int slotSizeWithMargin = 46;
const int sliderPosOffsetX = 10;
const int sliderPosOffsetX = 5;
void initAOHbackpack(size_t slots, bool slider);
};
class CArtifactsOfHeroQuickBackpack : public CArtifactsOfHeroBackpack
{
public:
CArtifactsOfHeroQuickBackpack(const Point & position, const ArtifactPosition filterBySlot);
void setHero(const CGHeroInstance * hero);
ArtifactPosition getFilterSlot();
private:
ArtifactPosition filterBySlot;
};

View File

@ -206,6 +206,14 @@ void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst
}
}
}
else if constexpr(std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroQuickBackpack>>)
{
const auto hero = artSetPtr->getHero();
artSetPtr->swapArtifacts(ArtifactLocation(hero->id, artPlace.slot),
ArtifactLocation(hero->id, artSetPtr->getFilterSlot()));
if(closeCallback)
closeCallback();
}
}, artSetWeak.value());
}
@ -245,7 +253,8 @@ void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsIns
// Altar window, Market window right click handler
else if constexpr(
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>> ||
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>>)
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>> ||
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroQuickBackpack>>)
{
if(artPlace.getArt() && artPlace.text.size())
artPlace.LRClickableAreaWTextComp::showPopupWindow(GH.getCursorPosition());

View File

@ -24,7 +24,8 @@ public:
std::weak_ptr<CArtifactsOfHeroAltar>,
std::weak_ptr<CArtifactsOfHeroKingdom>,
std::weak_ptr<CArtifactsOfHeroMain>,
std::weak_ptr<CArtifactsOfHeroBackpack>>;
std::weak_ptr<CArtifactsOfHeroBackpack>,
std::weak_ptr<CArtifactsOfHeroQuickBackpack>>;
using CloseCallback = std::function<void()>;
void addSet(CArtifactsOfHeroPtr artSet);

View File

@ -19,28 +19,34 @@
#include "render/Canvas.h"
#include "CPlayerInterface.h"
CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero)
CHeroBackpackWindow::CHeroBackpackWindow()
: CWindowObject((EOptions)0)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 410, 425));
stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0));
}
CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero)
: CHeroBackpackWindow()
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
arts = std::make_shared<CArtifactsOfHeroBackpack>(Point(windowMargin, windowMargin));
arts->setHero(hero);
addSetAndCallbacks(arts);
arts->setHero(hero);
addCloseCallback(std::bind(&CHeroBackpackWindow::close, this));
quitButton = std::make_shared<CButton>(Point(), AnimationPath::builtin("IOKAY32.def"), CButton::tooltip(""), [this]() { close(); }, EShortcut::GLOBAL_RETURN);
stretchedBackground->pos.w = arts->pos.w + 2 * windowMargin;
stretchedBackground->pos.h = arts->pos.h + quitButton->pos.h + 3 * windowMargin;
pos.w = stretchedBackground->pos.w;
pos.h = stretchedBackground->pos.h;
init();
center();
}
quitButton->moveBy(Point(GH.screenDimensions().x / 2 - quitButton->pos.w / 2 - quitButton->pos.x, arts->pos.h + 2 * windowMargin));
void CHeroBackpackWindow::init()
{
quitButton = std::make_shared<CButton>(Point(), AnimationPath::builtin("IOKAY32.def"), CButton::tooltip(""), [this]() { close(); }, EShortcut::GLOBAL_RETURN);
pos.w = stretchedBackground->pos.w = arts->pos.w + 2 * windowMargin;
pos.h = stretchedBackground->pos.h = arts->pos.h + quitButton->pos.h + 3 * windowMargin;
quitButton->moveTo(Point(pos.x + pos.w / 2 - quitButton->pos.w / 2, pos.y + arts->pos.h + 2 * windowMargin));
}
void CHeroBackpackWindow::showAll(Canvas & to)
@ -48,3 +54,28 @@ void CHeroBackpackWindow::showAll(Canvas & to)
CIntObject::showAll(to);
CMessage::drawBorder(PlayerColor(LOCPLINT->playerID), to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
}
CHeroQuickBackpackWindow::CHeroQuickBackpackWindow(const CGHeroInstance * hero, ArtifactPosition targetSlot)
: CHeroBackpackWindow()
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
auto artsQuickBp = std::make_shared<CArtifactsOfHeroQuickBackpack>(Point(windowMargin, windowMargin), targetSlot);
addSetAndCallbacks(static_cast<std::weak_ptr<CArtifactsOfHeroQuickBackpack>>(artsQuickBp));
arts = artsQuickBp;
arts->setHero(hero);
addCloseCallback(std::bind(&CHeroQuickBackpackWindow::close, this));
init();
}
void CHeroQuickBackpackWindow::showAll(Canvas & to)
{
if(arts->getSlotsNum() == 0)
{
close();
return;
}
CHeroBackpackWindow::showAll(to);
}

View File

@ -17,13 +17,24 @@ class CFilledTexture;
class CHeroBackpackWindow : public CWindowObject, public CWindowWithArtifacts
{
public:
CHeroBackpackWindow();
CHeroBackpackWindow(const CGHeroInstance * hero);
private:
protected:
std::shared_ptr<CArtifactsOfHeroBackpack> arts;
std::shared_ptr<CButton> quitButton;
std::shared_ptr<CFilledTexture> stretchedBackground;
const int windowMargin = 10;
const int windowMargin = 5;
void init();
void showAll(Canvas & to) override;
};
class CHeroQuickBackpackWindow : public CHeroBackpackWindow
{
public:
CHeroQuickBackpackWindow(const CGHeroInstance * hero, ArtifactPosition targetSlot);
private:
void showAll(Canvas & to) override;
};