1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
vcmi/client/widgets/markets/CArtifactsBuying.cpp

121 lines
4.4 KiB
C++
Raw Normal View History

2024-02-22 22:49:30 +02:00
/*
* CArtifactsBuying.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 "CArtifactsBuying.h"
#include "../../gui/Shortcut.h"
2024-02-22 22:49:30 +02:00
#include "../../widgets/Buttons.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
2024-03-29 12:29:16 +02:00
#include "../../../lib/mapObjects/CGHeroInstance.h"
2024-08-12 16:38:30 +02:00
#include "../../../lib/mapObjects/IMarket.h"
2024-07-21 12:49:40 +02:00
#include "../../../lib/texts/CGeneralTextHandler.h"
2024-02-22 22:49:30 +02:00
2024-08-12 16:38:30 +02:00
CArtifactsBuying::CArtifactsBuying(const IMarket * market, const CGHeroInstance * hero, const std::string & title)
2024-03-20 13:28:19 +02:00
: CMarketBase(market, hero)
, CResourcesSelling([this](const std::shared_ptr<CTradeableItem> & heroSlot){CArtifactsBuying::onSlotClickPressed(heroSlot, bidTradePanel);})
2024-02-22 22:49:30 +02:00
{
OBJECT_CONSTRUCTION;
2024-02-22 22:49:30 +02:00
2024-03-02 20:30:29 +02:00
labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title));
deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("TPMRKB.DEF"),
CGI->generaltexth->zelp[595], [this](){CArtifactsBuying::makeDeal();}, EShortcut::MARKET_DEAL);
2024-02-22 22:49:30 +02:00
labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
// Player's resources
2024-02-25 22:58:53 +02:00
assert(bidTradePanel);
2024-02-27 13:39:50 +02:00
bidTradePanel->moveTo(pos.topLeft() + Point(39, 184));
bidTradePanel->showcaseSlot->image->moveTo(pos.topLeft() + Point(141, 454));
2024-02-22 22:49:30 +02:00
// Artifacts panel
2024-02-25 22:58:53 +02:00
offerTradePanel = std::make_shared<ArtifactsPanel>([this](const std::shared_ptr<CTradeableItem> & newSlot)
2024-02-22 22:49:30 +02:00
{
CArtifactsBuying::onSlotClickPressed(newSlot, offerTradePanel);
2024-02-22 22:49:30 +02:00
}, [this]()
{
CMarketBase::updateSubtitlesForBid(EMarketMode::RESOURCE_ARTIFACT, bidTradePanel->getSelectedItemId());
2024-02-22 22:49:30 +02:00
}, market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT));
2024-02-25 22:58:53 +02:00
offerTradePanel->deleteSlotsCheck = [this](const std::shared_ptr<CTradeableItem> & slot)
2024-02-22 22:49:30 +02:00
{
return vstd::contains(this->market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT), ArtifactID(slot->id)) ? false : true;
};
2024-02-29 22:33:12 +02:00
offerTradePanel->moveTo(pos.topLeft() + Point(328, 181));
2024-02-22 22:49:30 +02:00
2024-03-02 20:30:29 +02:00
CMarketBase::update();
2024-03-20 13:28:19 +02:00
CArtifactsBuying::deselect();
}
void CArtifactsBuying::deselect()
{
2024-03-02 20:30:29 +02:00
CMarketBase::deselect();
2024-03-20 13:28:19 +02:00
CMarketTraderText::deselect();
2024-02-22 22:49:30 +02:00
}
void CArtifactsBuying::makeDeal()
{
2024-03-29 12:29:16 +02:00
if(ArtifactID(offerTradePanel->getSelectedItemId()).toArtifact()->canBePutAt(hero))
{
2024-08-20 16:15:50 +02:00
LOCPLINT->cb->trade(market->getObjInstanceID(), EMarketMode::RESOURCE_ARTIFACT, GameResID(bidTradePanel->getSelectedItemId()),
2024-03-29 12:29:16 +02:00
ArtifactID(offerTradePanel->getSelectedItemId()), offerQty, hero);
CMarketTraderText::makeDeal();
deselect();
}
else
{
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.326"));
}
2024-02-22 22:49:30 +02:00
}
2024-03-20 13:28:19 +02:00
CMarketBase::MarketShowcasesParams CArtifactsBuying::getShowcasesParams() const
2024-02-22 22:49:30 +02:00
{
2024-03-20 13:28:19 +02:00
if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
2024-04-08 11:46:46 +02:00
return MarketShowcasesParams
{
2024-03-20 13:28:19 +02:00
ShowcaseParams {std::to_string(deal->isBlocked() ? 0 : bidQty), bidTradePanel->getSelectedItemId()},
2024-04-08 11:46:46 +02:00
ShowcaseParams {std::to_string(deal->isBlocked() ? 0 : offerQty), CGI->artifacts()->getByIndex(offerTradePanel->getSelectedItemId())->getIconIndex()}
};
2024-02-22 22:49:30 +02:00
else
2024-04-08 11:46:46 +02:00
return MarketShowcasesParams {std::nullopt, std::nullopt};
2024-02-22 22:49:30 +02:00
}
2024-03-02 20:30:29 +02:00
void CArtifactsBuying::highlightingChanged()
2024-02-22 22:49:30 +02:00
{
2024-03-20 13:28:19 +02:00
if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
2024-02-22 22:49:30 +02:00
{
2024-03-20 13:28:19 +02:00
market->getOffer(bidTradePanel->getSelectedItemId(), offerTradePanel->getSelectedItemId(), bidQty, offerQty, EMarketMode::RESOURCE_ARTIFACT);
deal->block(LOCPLINT->cb->getResourceAmount(GameResID(bidTradePanel->getSelectedItemId())) < bidQty || !LOCPLINT->makingTurn);
2024-02-22 22:49:30 +02:00
}
CMarketBase::highlightingChanged();
2024-03-20 13:28:19 +02:00
CMarketTraderText::highlightingChanged();
}
std::string CArtifactsBuying::getTraderText()
{
if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
{
2024-04-08 11:46:46 +02:00
MetaString message = MetaString::createFromTextID("core.genrltxt.267");
message.replaceName(ArtifactID(offerTradePanel->getSelectedItemId()));
message.replaceNumber(bidQty);
message.replaceTextID(bidQty == 1 ? "core.genrltxt.161" : "core.genrltxt.160");
message.replaceName(GameResID(bidTradePanel->getSelectedItemId()));
return message.toString();
2024-03-20 13:28:19 +02:00
}
else
{
return madeTransaction ? CGI->generaltexth->allTexts[162] : CGI->generaltexth->allTexts[163];
}
2024-02-22 22:49:30 +02:00
}