1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/widgets/markets/CMarketBase.cpp

246 lines
7.0 KiB
C++
Raw Normal View History

2023-12-04 23:21:49 +02:00
/*
2024-03-02 20:30:29 +02:00
* CMarketBase.cpp, part of VCMI engine
2023-12-04 23:21:49 +02:00
*
* 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"
2024-03-02 20:30:29 +02:00
#include "CMarketBase.h"
2023-12-04 23:21:49 +02:00
#include "../MiscWidgets.h"
2024-02-27 13:39:50 +02:00
#include "../Images.h"
2023-12-04 23:21:49 +02:00
#include "../../gui/CGuiHandler.h"
#include "../../gui/Shortcut.h"
2023-12-04 23:21:49 +02:00
#include "../../widgets/Buttons.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
2024-02-19 23:40:43 +02:00
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
2023-12-04 23:21:49 +02:00
#include "../../../lib/texts/CGeneralTextHandler.h"
2023-12-04 23:21:49 +02:00
#include "../../../lib/mapObjects/CGHeroInstance.h"
2024-02-29 22:33:12 +02:00
#include "../../../lib/CHeroHandler.h"
2024-02-03 22:59:05 +02:00
#include "../../../lib/mapObjects/CGMarket.h"
2023-12-04 23:21:49 +02:00
2024-03-20 13:28:19 +02:00
CMarketBase::CMarketBase(const IMarket * market, const CGHeroInstance * hero)
2023-12-04 23:21:49 +02:00
: market(market)
, hero(hero)
{
}
2024-03-02 20:30:29 +02:00
void CMarketBase::deselect()
2023-12-04 23:21:49 +02:00
{
if(bidTradePanel && bidTradePanel->highlightedSlot)
{
bidTradePanel->highlightedSlot->selectSlot(false);
bidTradePanel->highlightedSlot.reset();
}
if(offerTradePanel && offerTradePanel->highlightedSlot)
{
offerTradePanel->highlightedSlot->selectSlot(false);
offerTradePanel->highlightedSlot.reset();
}
2023-12-04 23:21:49 +02:00
deal->block(true);
2024-02-29 12:02:39 +02:00
bidQty = 0;
offerQty = 0;
updateShowcases();
2023-12-04 23:21:49 +02:00
}
void CMarketBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<TradePanelBase> & curPanel)
2023-12-04 23:21:49 +02:00
{
assert(newSlot);
assert(curPanel);
if(newSlot == curPanel->highlightedSlot)
2023-12-04 23:21:49 +02:00
return;
curPanel->onSlotClickPressed(newSlot);
highlightingChanged();
redraw();
2023-12-04 23:21:49 +02:00
}
2024-03-02 20:30:29 +02:00
void CMarketBase::update()
2024-02-27 13:39:50 +02:00
{
if(bidTradePanel)
2024-03-02 20:30:29 +02:00
bidTradePanel->update();
2024-02-27 13:39:50 +02:00
if(offerTradePanel)
2024-03-02 20:30:29 +02:00
offerTradePanel->update();
2024-02-27 13:39:50 +02:00
}
void CMarketBase::updateSubtitlesForBid(EMarketMode marketMode, int bidId)
2024-02-27 13:39:50 +02:00
{
if(bidId == -1)
{
if(offerTradePanel)
offerTradePanel->clearSubtitles();
}
else
{
2024-02-27 13:39:50 +02:00
for(const auto & slot : offerTradePanel->slots)
{
int slotBidQty = 0;
int slotOfferQty = 0;
market->getOffer(bidId, slot->id, slotBidQty, slotOfferQty, marketMode);
offerTradePanel->updateOffer(*slot, slotBidQty, slotOfferQty);
2024-02-27 13:39:50 +02:00
}
}
2024-02-27 13:39:50 +02:00
};
void CMarketBase::updateShowcases()
2024-02-29 12:02:39 +02:00
{
2024-03-20 13:28:19 +02:00
const auto updateSelectedBody = [](const std::shared_ptr<TradePanelBase> & tradePanel, const std::optional<const ShowcaseParams> & params)
2024-02-29 12:02:39 +02:00
{
if(params.has_value())
{
tradePanel->setShowcaseSubtitle(params.value().text);
tradePanel->showcaseSlot->image->enable();
tradePanel->showcaseSlot->image->setFrame(params.value().imageIndex);
2024-02-29 12:02:39 +02:00
}
else
{
tradePanel->showcaseSlot->clear();
2024-02-29 12:02:39 +02:00
}
};
2024-03-20 13:28:19 +02:00
const auto params = getShowcasesParams();
2024-02-29 12:02:39 +02:00
if(bidTradePanel)
2024-04-08 11:46:46 +02:00
updateSelectedBody(bidTradePanel, params.bidParams);
2024-02-29 12:02:39 +02:00
if(offerTradePanel)
2024-04-08 11:46:46 +02:00
updateSelectedBody(offerTradePanel, params.offerParams);
2024-02-29 12:02:39 +02:00
}
void CMarketBase::highlightingChanged()
{
offerTradePanel->update();
updateShowcases();
}
2023-12-04 23:21:49 +02:00
CExperienceAltar::CExperienceAltar()
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
// Experience needed to reach next level
texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
// Total experience on the Altar
texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
2024-02-29 22:33:12 +02:00
expToLevel = std::make_shared<CLabel>(76, 477, FONT_SMALL, ETextAlignment::CENTER);
expForHero = std::make_shared<CLabel>(76, 545, FONT_SMALL, ETextAlignment::CENTER);
}
2024-03-02 20:30:29 +02:00
void CExperienceAltar::deselect()
{
expForHero->setText(std::to_string(0));
}
void CExperienceAltar::update()
2024-02-29 22:33:12 +02:00
{
expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(hero->exp) + 1) - hero->exp));
2023-12-04 23:21:49 +02:00
}
CCreaturesSelling::CCreaturesSelling()
{
2024-02-03 22:59:05 +02:00
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
2023-12-04 23:21:49 +02:00
assert(hero);
CreaturesPanel::slotsData slots;
for(auto slotId = SlotID(0); slotId.num < GameConstants::ARMY_SIZE; slotId++)
{
if(const auto & creature = hero->getCreature(slotId))
slots.emplace_back(std::make_tuple(creature->getId(), slotId, hero->getStackCount(slotId)));
}
2024-02-25 22:58:53 +02:00
bidTradePanel = std::make_shared<CreaturesPanel>(nullptr, slots);
2024-02-29 12:02:39 +02:00
bidTradePanel->updateSlotsCallback = std::bind(&CCreaturesSelling::updateSubtitles, this);
2023-12-04 23:21:49 +02:00
}
bool CCreaturesSelling::slotDeletingCheck(const std::shared_ptr<CTradeableItem> & slot) const
2023-12-04 23:21:49 +02:00
{
return hero->getStackCount(SlotID(slot->serial)) == 0 ? true : false;
}
void CCreaturesSelling::updateSubtitles() const
2023-12-04 23:21:49 +02:00
{
for(const auto & heroSlot : bidTradePanel->slots)
2024-02-27 13:39:50 +02:00
heroSlot->subtitle->setText(std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial))));
2023-12-04 23:21:49 +02:00
}
2024-02-03 22:59:05 +02:00
2024-02-27 13:39:50 +02:00
CResourcesBuying::CResourcesBuying(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
const TradePanelBase::UpdateSlotsFunctor & updSlotsCallback)
2024-02-03 22:59:05 +02:00
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
2024-02-27 13:39:50 +02:00
offerTradePanel = std::make_shared<ResourcesPanel>(clickPressedCallback, updSlotsCallback);
offerTradePanel->moveTo(pos.topLeft() + Point(327, 182));
2024-02-19 23:40:43 +02:00
labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
2024-02-03 22:59:05 +02:00
}
2024-02-27 13:39:50 +02:00
CResourcesSelling::CResourcesSelling(const CTradeableItem::ClickPressedFunctor & clickPressedCallback)
2024-02-19 23:40:43 +02:00
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
2024-02-29 12:02:39 +02:00
bidTradePanel = std::make_shared<ResourcesPanel>(clickPressedCallback, std::bind(&CResourcesSelling::updateSubtitles, this));
2024-02-19 23:40:43 +02:00
labels.emplace_back(std::make_shared<CLabel>(156, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
}
void CResourcesSelling::updateSubtitles() const
2024-02-19 23:40:43 +02:00
{
2024-02-27 13:39:50 +02:00
for(const auto & slot : bidTradePanel->slots)
slot->subtitle->setText(std::to_string(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(slot->serial))));
2024-02-19 23:40:43 +02:00
}
2024-03-02 20:30:29 +02:00
CMarketSlider::CMarketSlider(const CSlider::SliderMovingFunctor & movingCallback)
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, movingCallback, 0, 0, 0, Orientation::HORIZONTAL);
maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
[this]()
{
offerSlider->scrollToMax();
}, EShortcut::MARKET_MAX_AMOUNT);
2024-03-02 20:30:29 +02:00
}
void CMarketSlider::deselect()
{
maxAmount->block(true);
offerSlider->scrollTo(0);
offerSlider->block(true);
}
void CMarketSlider::onOfferSliderMoved(int newVal)
{
if(bidTradePanel->highlightedSlot && offerTradePanel->highlightedSlot)
2024-03-02 20:30:29 +02:00
{
offerSlider->scrollTo(newVal);
updateShowcases();
2024-03-02 20:30:29 +02:00
redraw();
}
}
2024-03-20 13:28:19 +02:00
CMarketTraderText::CMarketTraderText(const Point & pos, const EFonts & font, const ColorRGBA & color)
: madeTransaction(false)
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
traderText = std::make_shared<CTextBox>("", Rect(pos, traderTextDimensions), 0, font, ETextAlignment::CENTER, color);
}
void CMarketTraderText::deselect()
{
highlightingChanged();
}
void CMarketTraderText::makeDeal()
{
madeTransaction = true;
}
void CMarketTraderText::highlightingChanged()
{
traderText->setText(getTraderText());
}