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

128 lines
4.0 KiB
C++
Raw Normal View History

2024-02-19 23:40:43 +02:00
/*
* CMarketResources.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 "CMarketResources.h"
#include "../../gui/CGuiHandler.h"
#include "../../widgets/Buttons.h"
#include "../../widgets/Slider.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
#include "../../../lib/CGeneralTextHandler.h"
#include "../../../lib/mapObjects/CGMarket.h"
CMarketResources::CMarketResources(const IMarket * market, const CGHeroInstance * hero)
: CTradeBase(market, hero)
2024-02-21 20:48:14 +02:00
, CResourcesBuying([this](){CMarketResources::updateSubtitles();})
2024-02-22 22:49:30 +02:00
, CMarketMisc([this](){return CMarketResources::getSelectionParams();})
2024-02-19 23:40:43 +02:00
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
labels.emplace_back(std::make_shared<CLabel>(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[158]));
deal = std::make_shared<CButton>(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"),
CGI->generaltexth->zelp[595], [this]() {CMarketResources::makeDeal(); });
maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
[this]() {offerSlider->scrollToMax(); });
offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, [this](int newVal)
{
CMarketResources::onOfferSliderMoved(newVal);
}, 0, 0, 0, Orientation::HORIZONTAL);
// Player's resources
2024-02-25 22:58:53 +02:00
assert(bidTradePanel);
std::for_each(bidTradePanel->slots.cbegin(), bidTradePanel->slots.cend(), [this](auto & slot)
2024-02-19 23:40:43 +02:00
{
slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
{
CMarketResources::onSlotClickPressed(heroSlot, hLeft);
};
});
2024-02-25 22:58:53 +02:00
bidTradePanel->moveTo(pos.topLeft() + Point(39, 181));
2024-02-19 23:40:43 +02:00
// Market resources panel
2024-02-25 22:58:53 +02:00
assert(offerTradePanel);
std::for_each(offerTradePanel->slots.cbegin(), offerTradePanel->slots.cend(), [this](auto & slot)
2024-02-19 23:40:43 +02:00
{
slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & resSlot)
{
CMarketResources::onSlotClickPressed(resSlot, hRight);
};
});
CResourcesSelling::updateSlots();
2024-02-22 22:49:30 +02:00
CMarketMisc::deselect();
2024-02-19 23:40:43 +02:00
}
void CMarketResources::makeDeal()
{
if(auto toTrade = offerSlider->getValue(); toTrade != 0)
{
LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_RESOURCE, GameResID(hLeft->id), GameResID(hRight->id), bidQty * toTrade, hero);
deselect();
}
}
2024-02-22 22:49:30 +02:00
CMarketMisc::SelectionParams CMarketResources::getSelectionParams()
2024-02-19 23:40:43 +02:00
{
if(hLeft && hRight && hLeft->id != hRight->id)
2024-02-25 22:58:53 +02:00
return std::make_tuple(
SelectionParamOneSide {std::to_string(bidQty * offerSlider->getValue()), hLeft->id},
SelectionParamOneSide {std::to_string(offerQty * offerSlider->getValue()), hRight->id});
2024-02-19 23:40:43 +02:00
else
2024-02-25 22:58:53 +02:00
return std::make_tuple(std::nullopt, std::nullopt);
2024-02-19 23:40:43 +02:00
}
void CMarketResources::onOfferSliderMoved(int newVal)
{
if(hLeft && hRight)
{
offerSlider->scrollTo(newVal);
updateSelected();
redraw();
}
}
void CMarketResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
{
if(newSlot == hCurSlot)
return;
CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
if(hLeft)
{
if(hRight)
{
market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::RESOURCE_RESOURCE);
offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id)) / bidQty);
offerSlider->scrollTo(0);
const bool isControlsBlocked = hLeft->id != hRight->id ? false : true;
offerSlider->block(isControlsBlocked);
maxAmount->block(isControlsBlocked);
deal->block(isControlsBlocked);
}
updateSelected();
2024-02-25 22:58:53 +02:00
offerTradePanel->updateSlots();
2024-02-19 23:40:43 +02:00
}
redraw();
}
void CMarketResources::updateSubtitles()
{
2024-02-21 20:48:14 +02:00
CResourcesBuying::updateSubtitles(EMarketMode::RESOURCE_RESOURCE);
2024-02-19 23:40:43 +02:00
if(hLeft)
2024-02-25 22:58:53 +02:00
offerTradePanel->slots[hLeft->serial]->subtitle = CGI->generaltexth->allTexts[164]; // n/a
2024-02-19 23:40:43 +02:00
}