1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

add scrolling for resources

This commit is contained in:
Laserlicht
2025-09-21 16:28:19 +02:00
parent 55cdbb931c
commit 8f00a1221e
7 changed files with 117 additions and 36 deletions

View File

@@ -46,7 +46,8 @@ void CMarketBase::deselect()
offerTradePanel->highlightedSlot->selectSlot(false); offerTradePanel->highlightedSlot->selectSlot(false);
offerTradePanel->highlightedSlot.reset(); offerTradePanel->highlightedSlot.reset();
} }
deal->block(true); if(deal)
deal->block(true);
bidQty = 0; bidQty = 0;
offerQty = 0; offerQty = 0;
updateShowcases(); updateShowcases();
@@ -54,7 +55,12 @@ void CMarketBase::deselect()
void CMarketBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<TradePanelBase> & curPanel) void CMarketBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<TradePanelBase> & curPanel)
{ {
assert(newSlot); if(!newSlot)
{
deselect();
return;
}
assert(curPanel); assert(curPanel);
if(newSlot == curPanel->highlightedSlot) if(newSlot == curPanel->highlightedSlot)
return; return;
@@ -81,13 +87,14 @@ void CMarketBase::updateSubtitlesForBid(EMarketMode marketMode, int bidId)
} }
else else
{ {
for(const auto & slot : offerTradePanel->slots) if(offerTradePanel)
{ for(const auto & slot : offerTradePanel->slots)
int slotBidQty = 0; {
int slotOfferQty = 0; int slotBidQty = 0;
market->getOffer(bidId, slot->id, slotBidQty, slotOfferQty, marketMode); int slotOfferQty = 0;
offerTradePanel->updateOffer(*slot, slotBidQty, slotOfferQty); market->getOffer(bidId, slot->id, slotBidQty, slotOfferQty, marketMode);
} offerTradePanel->updateOffer(*slot, slotBidQty, slotOfferQty);
}
} }
}; };
@@ -120,6 +127,11 @@ void CMarketBase::highlightingChanged()
updateShowcases(); updateShowcases();
} }
CMarketBase::MarketShowcasesParams CMarketBase::getShowcasesParams() const
{
return {};
}
CExperienceAltar::CExperienceAltar() CExperienceAltar::CExperienceAltar()
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
@@ -164,8 +176,9 @@ bool CCreaturesSelling::slotDeletingCheck(const std::shared_ptr<CTradeableItem>
void CCreaturesSelling::updateSubtitles() const void CCreaturesSelling::updateSubtitles() const
{ {
for(const auto & heroSlot : bidTradePanel->slots) if(bidTradePanel)
heroSlot->subtitle->setText(std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial)))); for(const auto & heroSlot : bidTradePanel->slots)
heroSlot->subtitle->setText(std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial))));
} }
CResourcesBuying::CResourcesBuying(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, CResourcesBuying::CResourcesBuying(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
@@ -188,8 +201,9 @@ CResourcesSelling::CResourcesSelling(const CTradeableItem::ClickPressedFunctor &
void CResourcesSelling::updateSubtitles() const void CResourcesSelling::updateSubtitles() const
{ {
for(const auto & slot : bidTradePanel->slots) if(bidTradePanel)
slot->subtitle->setText(std::to_string(GAME->interface()->cb->getResourceAmount(static_cast<EGameResID>(slot->serial)))); for(const auto & slot : bidTradePanel->slots)
slot->subtitle->setText(std::to_string(GAME->interface()->cb->getResourceAmount(static_cast<EGameResID>(slot->serial))));
} }
CMarketSlider::CMarketSlider(const CSlider::SliderMovingFunctor & movingCallback) CMarketSlider::CMarketSlider(const CSlider::SliderMovingFunctor & movingCallback)
@@ -197,6 +211,7 @@ CMarketSlider::CMarketSlider(const CSlider::SliderMovingFunctor & movingCallback
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, movingCallback, 0, 0, 0, Orientation::HORIZONTAL); offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, movingCallback, 0, 0, 0, Orientation::HORIZONTAL);
offerSlider->setScrollBounds(Rect());
maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), LIBRARY->generaltexth->zelp[596], maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), LIBRARY->generaltexth->zelp[596],
[this]() [this]()
{ {

View File

@@ -58,7 +58,7 @@ protected:
virtual void onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<TradePanelBase> & curPanel); virtual void onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<TradePanelBase> & curPanel);
virtual void updateSubtitlesForBid(EMarketMode marketMode, int bidId); virtual void updateSubtitlesForBid(EMarketMode marketMode, int bidId);
virtual void updateShowcases(); virtual void updateShowcases();
virtual MarketShowcasesParams getShowcasesParams() const = 0; virtual MarketShowcasesParams getShowcasesParams() const;
virtual void highlightingChanged(); virtual void highlightingChanged();
}; };

View File

@@ -98,8 +98,10 @@ void CMarketResources::highlightingChanged()
void CMarketResources::updateSubtitles() void CMarketResources::updateSubtitles()
{ {
CMarketBase::updateSubtitlesForBid(EMarketMode::RESOURCE_RESOURCE, bidTradePanel->getHighlightedItemId()); CMarketBase::updateSubtitlesForBid(EMarketMode::RESOURCE_RESOURCE, bidTradePanel->getHighlightedItemId());
if(bidTradePanel->highlightedSlot) if(bidTradePanel && bidTradePanel->highlightedSlot)
offerTradePanel->slots[bidTradePanel->highlightedSlot->serial]->subtitle->setText(LIBRARY->generaltexth->allTexts[164]); // n/a for(auto & slot : offerTradePanel->slots)
if(slot->id == bidTradePanel->highlightedSlot->id)
slot->subtitle->setText(LIBRARY->generaltexth->allTexts[164]); // n/a
} }
std::string CMarketResources::getTraderText() std::string CMarketResources::getTraderText()

View File

@@ -13,6 +13,7 @@
#include "../../GameEngine.h" #include "../../GameEngine.h"
#include "../../GameInstance.h" #include "../../GameInstance.h"
#include "../../render/Canvas.h" #include "../../render/Canvas.h"
#include "../../widgets/Slider.h"
#include "../../widgets/TextControls.h" #include "../../widgets/TextControls.h"
#include "../../windows/InfoWindows.h" #include "../../windows/InfoWindows.h"
@@ -261,20 +262,59 @@ bool TradePanelBase::isHighlighted() const
ResourcesPanel::ResourcesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, ResourcesPanel::ResourcesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
const UpdateSlotsFunctor & updateSubtitles) const UpdateSlotsFunctor & updateSubtitles)
: clickPressedCallback(std::move(clickPressedCallback))
{ {
assert(resourcesForTrade.size() == slotsPos.size());
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
for(const auto & res : resourcesForTrade) // TODO: replace with LIBRARY->resourceTypeHandler->getAllObjects() -> also check order after doing this
resourcesForTrade =
{ {
auto slot = slots.emplace_back(std::make_shared<CTradeableItem>(Rect(slotsPos[res.num], slotDimension), EType::RESOURCE, res.num, res.num)); GameResID::WOOD, GameResID::MERCURY, GameResID::ORE,
slot->clickPressedCallback = clickPressedCallback; GameResID::SULFUR, GameResID::CRYSTAL, GameResID::GEMS,
slot->setSelectionWidth(selectionWidth); GameResID::GOLD,
GameResID::GOLD, // TODO: remove
GameResID::GOLD, // TODO: remove
GameResID::GOLD // TODO: remove
};
int lines = vstd::divideAndCeil(resourcesForTrade.size(), 3);
if(lines > 3)
{
slider = std::make_shared<CSlider>(Point(240, 0), 224, [this](int to){ updateSlots(to); setRedrawParent(true); redraw(); }, 3, lines, 0, Orientation::VERTICAL, CSlider::BROWN);
slider->setPanningStep(72);
slider->setScrollBounds(Rect(-240, 0, 240, 224));
} }
updateSlotsCallback = updateSubtitles;
updateSlotsCallback = std::move(updateSubtitles);
updateSlots(0);
showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::RESOURCE, 0, 0); showcaseSlot = std::make_shared<CTradeableItem>(Rect(selectedPos, slotDimension), EType::RESOURCE, 0, 0);
} }
void ResourcesPanel::updateSlots(int line)
{
OBJECT_CONSTRUCTION;
clickPressedCallback(nullptr);
int offset = line * 3;
slots.clear();
for (int i = 0; i < std::min(static_cast<int>(resourcesForTrade.size() - offset), 9); i++)
{
const auto& res = resourcesForTrade[i + offset];
auto slotPos = slotsPos[i];
if(resourcesForTrade.size() == 7 && i == 6) // for 7 ressources place gold in the middle
slotPos = slotsPos[i + 1];
auto slotPtr = std::make_shared<CTradeableItem>(Rect(slotPos, slotDimension), EType::RESOURCE, res.num, res.num);
slotPtr->clickPressedCallback = clickPressedCallback;
slotPtr->setSelectionWidth(selectionWidth);
slots.push_back(slotPtr);
}
updateSlotsCallback();
}
ArtifactsPanel::ArtifactsPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, ArtifactsPanel::ArtifactsPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
const UpdateSlotsFunctor & updateSubtitles, const std::vector<TradeItemBuy> & arts) const UpdateSlotsFunctor & updateSubtitles, const std::vector<TradeItemBuy> & arts)
{ {

View File

@@ -14,6 +14,8 @@
#include "../../../lib/networkPacks/TradeItem.h" #include "../../../lib/networkPacks/TradeItem.h"
class CSlider;
enum class EType enum class EType
{ {
RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, ARTIFACT RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, ARTIFACT
@@ -68,21 +70,21 @@ public:
class ResourcesPanel : public TradePanelBase class ResourcesPanel : public TradePanelBase
{ {
const std::vector<GameResID> resourcesForTrade =
{
GameResID::WOOD, GameResID::MERCURY, GameResID::ORE,
GameResID::SULFUR, GameResID::CRYSTAL, GameResID::GEMS,
GameResID::GOLD
};
const std::vector<Point> slotsPos = const std::vector<Point> slotsPos =
{ {
Point(0, 0), Point(83, 0), Point(166, 0), Point(0, 0), Point(83, 0), Point(166, 0),
Point(0, 79), Point(83, 79), Point(166, 79), Point(0, 79), Point(83, 79), Point(166, 79),
Point(83, 158) Point(0, 158), Point(83, 158), Point(166, 158)
}; };
const Point slotDimension = Point(69, 66); const Point slotDimension = Point(69, 66);
const Point selectedPos = Point(83, 267); const Point selectedPos = Point(83, 267);
CTradeableItem::ClickPressedFunctor clickPressedCallback;
std::vector<GameResID> resourcesForTrade;
std::shared_ptr<CSlider> slider;
void updateSlots(int line);
public: public:
ResourcesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, const UpdateSlotsFunctor & updateSubtitles); ResourcesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, const UpdateSlotsFunctor & updateSubtitles);
}; };

View File

@@ -200,11 +200,23 @@ std::string CMarketWindow::getMarketTitle(const ObjectInstanceID marketId, const
return GAME->interface()->cb->getObj(marketId)->getObjectName(); return GAME->interface()->cb->getObj(marketId)->getObjectName();
} }
ImagePath CMarketWindow::getImagePathBasedOnResources(std::string name)
{
int res = 9; //TODO: replace with LIBRARY->resourceTypeHandler->getAllObjects();
if(res == 8)
name += "-R8";
else if(res > 8)
name += "-R9";
return ImagePath::builtin(name);
}
void CMarketWindow::createArtifactsBuying(const IMarket * market, const CGHeroInstance * hero) void CMarketWindow::createArtifactsBuying(const IMarket * market, const CGHeroInstance * hero)
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
background = createBg(ImagePath::builtin("TPMRKABS.bmp"), PLAYER_COLORED); auto image = getImagePathBasedOnResources("TPMRKABS");
background = createBg(image, PLAYER_COLORED);
marketWidget = std::make_shared<CArtifactsBuying>(market, hero, getMarketTitle(market->getObjInstanceID(), EMarketMode::RESOURCE_ARTIFACT)); marketWidget = std::make_shared<CArtifactsBuying>(market, hero, getMarketTitle(market->getObjInstanceID(), EMarketMode::RESOURCE_ARTIFACT));
initWidgetInternals(EMarketMode::RESOURCE_ARTIFACT, LIBRARY->generaltexth->zelp[600]); initWidgetInternals(EMarketMode::RESOURCE_ARTIFACT, LIBRARY->generaltexth->zelp[600]);
} }
@@ -213,7 +225,9 @@ void CMarketWindow::createArtifactsSelling(const IMarket * market, const CGHeroI
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
background = createBg(ImagePath::builtin("TPMRKASS.bmp"), PLAYER_COLORED); auto image = getImagePathBasedOnResources("TPMRKASS");
background = createBg(image, PLAYER_COLORED);
// Create image that copies part of background containing slot MISC_1 into position of slot MISC_5 // Create image that copies part of background containing slot MISC_1 into position of slot MISC_5
artSlotBack = std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 0, 0); artSlotBack = std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 0, 0);
artSlotBack->moveTo(pos.topLeft() + Point(18, 339)); artSlotBack->moveTo(pos.topLeft() + Point(18, 339));
@@ -229,7 +243,9 @@ void CMarketWindow::createMarketResources(const IMarket * market, const CGHeroIn
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
background = createBg(ImagePath::builtin("TPMRKRES.bmp"), PLAYER_COLORED); auto image = getImagePathBasedOnResources("TPMRKRES");
background = createBg(image, PLAYER_COLORED);
marketWidget = std::make_shared<CMarketResources>(market, hero); marketWidget = std::make_shared<CMarketResources>(market, hero);
initWidgetInternals(EMarketMode::RESOURCE_RESOURCE, LIBRARY->generaltexth->zelp[600]); initWidgetInternals(EMarketMode::RESOURCE_RESOURCE, LIBRARY->generaltexth->zelp[600]);
} }
@@ -238,7 +254,9 @@ void CMarketWindow::createFreelancersGuild(const IMarket * market, const CGHeroI
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
background = createBg(ImagePath::builtin("TPMRKCRS.bmp"), PLAYER_COLORED); auto image = getImagePathBasedOnResources("TPMRKCRS");
background = createBg(image, PLAYER_COLORED);
marketWidget = std::make_shared<CFreelancerGuild>(market, hero); marketWidget = std::make_shared<CFreelancerGuild>(market, hero);
initWidgetInternals(EMarketMode::CREATURE_RESOURCE, LIBRARY->generaltexth->zelp[600]); initWidgetInternals(EMarketMode::CREATURE_RESOURCE, LIBRARY->generaltexth->zelp[600]);
} }
@@ -247,7 +265,9 @@ void CMarketWindow::createTransferResources(const IMarket * market, const CGHero
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
background = createBg(ImagePath::builtin("TPMRKPTS.bmp"), PLAYER_COLORED); auto image = getImagePathBasedOnResources("TPMRKPTS");
background = createBg(image, PLAYER_COLORED);
marketWidget = std::make_shared<CTransferResources>(market, hero); marketWidget = std::make_shared<CTransferResources>(market, hero);
initWidgetInternals(EMarketMode::RESOURCE_PLAYER, LIBRARY->generaltexth->zelp[600]); initWidgetInternals(EMarketMode::RESOURCE_PLAYER, LIBRARY->generaltexth->zelp[600]);
} }

View File

@@ -38,6 +38,8 @@ private:
void createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero); void createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero);
void createAltarCreatures(const IMarket * market, const CGHeroInstance * hero); void createAltarCreatures(const IMarket * market, const CGHeroInstance * hero);
ImagePath getImagePathBasedOnResources(std::string name);
const int buttonHeightWithMargin = 32 + 3; const int buttonHeightWithMargin = 32 + 3;
std::vector<std::shared_ptr<CButton>> changeModeButtons; std::vector<std::shared_ptr<CButton>> changeModeButtons;
std::shared_ptr<CButton> quitButton; std::shared_ptr<CButton> quitButton;