1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-12 10:03:53 +02:00
vcmi/client/widgets/markets/CTransferResources.cpp

112 lines
3.7 KiB
C++
Raw Normal View History

2024-02-21 20:48:14 +02:00
/*
* CTransferResources.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 "CTransferResources.h"
#include "../../gui/CGuiHandler.h"
#include "../../widgets/Buttons.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
#include "../../../lib/CGeneralTextHandler.h"
2024-04-08 11:46:46 +02:00
#include "../../../lib/MetaString.h"
2024-02-21 20:48:14 +02:00
CTransferResources::CTransferResources(const IMarket * market, const CGHeroInstance * hero)
2024-03-20 13:28:19 +02:00
: CMarketBase(market, hero)
, CResourcesSelling([this](const std::shared_ptr<CTradeableItem> & heroSlot){CTransferResources::onSlotClickPressed(heroSlot, bidTradePanel);})
2024-03-02 20:30:29 +02:00
, CMarketSlider([this](int newVal){CMarketSlider::onOfferSliderMoved(newVal);})
2024-03-20 13:28:19 +02:00
, CMarketTraderText(Point(28, 48))
2024-02-21 20:48:14 +02:00
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
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, CGI->generaltexth->allTexts[158]));
2024-02-21 20:48:14 +02:00
labels.emplace_back(std::make_shared<CLabel>(445, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
2024-03-02 20:30:29 +02:00
deal = std::make_shared<CButton>(dealButtonPosWithSlider, AnimationPath::builtin("TPMRKB.DEF"),
CGI->generaltexth->zelp[595], [this](){CTransferResources::makeDeal();});
2024-02-21 20:48:14 +02:00
// 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(40, 183));
2024-02-21 20:48:14 +02:00
// Players panel
2024-02-25 22:58:53 +02:00
offerTradePanel = std::make_shared<PlayersPanel>([this](const std::shared_ptr<CTradeableItem> & heroSlot)
2024-02-21 20:48:14 +02:00
{
CTransferResources::onSlotClickPressed(heroSlot, offerTradePanel);
2024-02-21 20:48:14 +02:00
});
2024-02-27 13:39:50 +02:00
offerTradePanel->moveTo(pos.topLeft() + Point(333, 84));
2024-02-21 20:48:14 +02:00
2024-03-02 20:30:29 +02:00
CMarketBase::update();
2024-02-21 20:48:14 +02:00
CTransferResources::deselect();
}
2024-03-02 20:30:29 +02:00
void CTransferResources::deselect()
{
CMarketBase::deselect();
CMarketSlider::deselect();
2024-03-20 13:28:19 +02:00
CMarketTraderText::deselect();
2024-03-02 20:30:29 +02:00
}
2024-02-21 20:48:14 +02:00
void CTransferResources::makeDeal()
{
if(auto toTrade = offerSlider->getValue(); toTrade != 0)
{
LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_PLAYER, GameResID(bidTradePanel->getSelectedItemId()),
2024-03-20 13:28:19 +02:00
PlayerColor(offerTradePanel->getSelectedItemId()), toTrade, hero);
CMarketTraderText::makeDeal();
2024-02-21 20:48:14 +02:00
deselect();
}
}
2024-03-20 13:28:19 +02:00
CMarketBase::MarketShowcasesParams CTransferResources::getShowcasesParams() const
2024-02-21 20:48:14 +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(offerSlider->getValue()), bidTradePanel->getSelectedItemId()},
2024-04-08 11:46:46 +02:00
ShowcaseParams {CGI->generaltexth->capColors[offerTradePanel->getSelectedItemId()], offerTradePanel->getSelectedItemId()}
};
2024-02-21 20:48:14 +02:00
else
2024-04-08 11:46:46 +02:00
return MarketShowcasesParams {std::nullopt, std::nullopt};
2024-02-21 20:48:14 +02:00
}
2024-03-02 20:30:29 +02:00
void CTransferResources::highlightingChanged()
2024-02-21 20:48:14 +02:00
{
2024-03-20 13:28:19 +02:00
if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
2024-02-21 20:48:14 +02:00
{
offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(bidTradePanel->getSelectedItemId())));
offerSlider->scrollTo(0);
offerSlider->block(false);
maxAmount->block(false);
deal->block(!LOCPLINT->makingTurn);
2024-02-21 20:48:14 +02:00
}
CMarketBase::highlightingChanged();
2024-03-20 13:28:19 +02:00
CMarketTraderText::highlightingChanged();
}
std::string CTransferResources::getTraderText()
{
if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
{
2024-04-08 11:46:46 +02:00
MetaString message = MetaString::createFromTextID("core.genrltxt.165");
message.replaceName(GameResID(bidTradePanel->getSelectedItemId()));
message.replaceName(PlayerColor(offerTradePanel->getSelectedItemId()));
return message.toString();
2024-03-20 13:28:19 +02:00
}
else
{
return madeTransaction ? CGI->generaltexth->allTexts[166] : CGI->generaltexth->allTexts[167];
}
2024-02-21 20:48:14 +02:00
}