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

174 lines
5.5 KiB
C++
Raw Normal View History

2024-02-25 22:58:53 +02:00
/*
* CArtifactsSelling.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 "CArtifactsSelling.h"
#include "../../gui/CGuiHandler.h"
#include "../../gui/Shortcut.h"
2024-02-25 22:58:53 +02:00
#include "../../widgets/Buttons.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
2024-02-29 22:33:12 +02:00
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
2024-02-25 22:58:53 +02:00
#include "../../../lib/CArtifactInstance.h"
2024-02-29 22:33:12 +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-25 22:58:53 +02:00
2024-08-12 16:38:30 +02:00
CArtifactsSelling::CArtifactsSelling(const IMarket * market, const CGHeroInstance * hero, const std::string & title)
2024-03-20 13:28:19 +02:00
: CMarketBase(market, hero)
2024-02-27 13:39:50 +02:00
, CResourcesBuying(
[this](const std::shared_ptr<CTradeableItem> & resSlot){CArtifactsSelling::onSlotClickPressed(resSlot, offerTradePanel);},
2024-03-20 13:28:19 +02:00
[this](){CArtifactsSelling::updateSubtitles();})
2024-02-25 22:58:53 +02:00
{
OBJECT_CONSTRUCTION;
2024-02-25 22:58:53 +02:00
2024-04-10 23:06:49 +02:00
labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title));
2024-03-02 20:30:29 +02:00
labels.push_back(std::make_shared<CLabel>(155, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->getNameTranslated())));
deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("TPMRKB.DEF"),
CGI->generaltexth->zelp[595], [this](){CArtifactsSelling::makeDeal();}, EShortcut::MARKET_DEAL);
2024-02-27 13:39:50 +02:00
bidSelectedSlot = std::make_shared<CTradeableItem>(Rect(Point(123, 470), Point(69, 66)), EType::ARTIFACT_TYPE, 0, 0);
2024-02-25 22:58:53 +02:00
2024-03-02 20:30:29 +02:00
// Market resources panel
assert(offerTradePanel);
offerTradePanel->moveTo(pos.topLeft() + Point(326, 184));
offerTradePanel->showcaseSlot->moveTo(pos.topLeft() + Point(409, 473));
offerTradePanel->showcaseSlot->subtitle->moveBy(Point(0, 1));
2024-03-02 20:30:29 +02:00
2024-02-25 22:58:53 +02:00
// Hero's artifacts
2024-03-02 20:30:29 +02:00
heroArts = std::make_shared<CArtifactsOfHeroMarket>(Point(-361, 46), offerTradePanel->selectionWidth);
2024-02-25 22:58:53 +02:00
heroArts->setHero(hero);
2024-05-02 00:49:17 +02:00
heroArts->onSelectArtCallback = [this](const CArtPlace * artPlace)
2024-02-25 22:58:53 +02:00
{
assert(artPlace);
2024-02-29 22:33:12 +02:00
selectedHeroSlot = artPlace->slot;
2024-03-02 20:30:29 +02:00
CArtifactsSelling::highlightingChanged();
CIntObject::redraw();
2024-02-25 22:58:53 +02:00
};
2024-05-02 00:49:17 +02:00
heroArts->onClickNotTradableCallback = []()
{
// This item can't be traded
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
};
CArtifactsSelling::updateShowcases();
2024-02-29 22:33:12 +02:00
CArtifactsSelling::deselect();
}
void CArtifactsSelling::deselect()
{
2024-03-02 20:30:29 +02:00
CMarketBase::deselect();
2024-03-20 13:28:19 +02:00
CMarketTraderText::deselect();
2024-02-29 22:33:12 +02:00
selectedHeroSlot = ArtifactPosition::PRE_FIRST;
heroArts->unmarkSlots();
bidSelectedSlot->clear();
2024-02-25 22:58:53 +02:00
}
void CArtifactsSelling::makeDeal()
{
2024-02-29 22:33:12 +02:00
const auto art = hero->getArt(selectedHeroSlot);
assert(art);
2024-08-20 16:15:50 +02:00
LOCPLINT->cb->trade(market->getObjInstanceID(), EMarketMode::ARTIFACT_RESOURCE, art->getId(),
2024-10-19 21:48:06 +02:00
GameResID(offerTradePanel->getHighlightedItemId()), offerQty, hero);
2024-03-20 13:28:19 +02:00
CMarketTraderText::makeDeal();
2024-02-25 22:58:53 +02:00
}
void CArtifactsSelling::updateShowcases()
2024-02-25 22:58:53 +02:00
{
const auto art = hero->getArt(selectedHeroSlot);
2024-03-20 13:28:19 +02:00
if(art && offerTradePanel->isHighlighted())
2024-02-25 22:58:53 +02:00
{
2024-02-27 13:39:50 +02:00
bidSelectedSlot->image->enable();
bidSelectedSlot->setID(art->getTypeId().num);
bidSelectedSlot->image->setFrame(CGI->artifacts()->getByIndex(art->getTypeId())->getIconIndex());
2024-02-27 13:39:50 +02:00
bidSelectedSlot->subtitle->setText(std::to_string(bidQty));
2024-02-25 22:58:53 +02:00
}
else
{
bidSelectedSlot->clear();
2024-02-25 22:58:53 +02:00
}
CMarketBase::updateShowcases();
2024-02-25 22:58:53 +02:00
}
2024-03-02 20:30:29 +02:00
void CArtifactsSelling::update()
2024-02-29 22:33:12 +02:00
{
2024-03-02 20:30:29 +02:00
CMarketBase::update();
2024-02-29 22:33:12 +02:00
if(selectedHeroSlot != ArtifactPosition::PRE_FIRST)
{
2024-03-20 13:28:19 +02:00
if(hero->getArt(selectedHeroSlot) == nullptr)
2024-03-02 20:30:29 +02:00
{
deselect();
2024-03-20 13:28:19 +02:00
selectedHeroSlot = ArtifactPosition::PRE_FIRST;
2024-03-02 20:30:29 +02:00
}
2024-03-20 13:28:19 +02:00
else
{
heroArts->getArtPlace(selectedHeroSlot)->selectSlot(true);
}
highlightingChanged();
2024-02-29 22:33:12 +02:00
}
}
2024-02-25 22:58:53 +02:00
std::shared_ptr<CArtifactsOfHeroMarket> CArtifactsSelling::getAOHset() const
{
return heroArts;
}
2024-03-20 13:28:19 +02:00
CMarketBase::MarketShowcasesParams CArtifactsSelling::getShowcasesParams() const
2024-02-25 22:58:53 +02:00
{
2024-03-20 13:28:19 +02:00
if(hero->getArt(selectedHeroSlot) && offerTradePanel->isHighlighted())
2024-04-08 11:46:46 +02:00
return MarketShowcasesParams
{
2024-02-25 22:58:53 +02:00
std::nullopt,
2024-10-19 21:48:06 +02:00
ShowcaseParams {std::to_string(offerQty), offerTradePanel->getHighlightedItemId()}
2024-04-08 11:46:46 +02:00
};
2024-02-25 22:58:53 +02:00
else
2024-04-08 11:46:46 +02:00
return MarketShowcasesParams {std::nullopt, std::nullopt};
2024-02-25 22:58:53 +02:00
}
2024-03-20 13:28:19 +02:00
void CArtifactsSelling::updateSubtitles()
{
const auto art = this->hero->getArt(selectedHeroSlot);
const int bidId = art == nullptr ? -1 : art->getTypeId().num;
CMarketBase::updateSubtitlesForBid(EMarketMode::ARTIFACT_RESOURCE, bidId);
}
2024-03-02 20:30:29 +02:00
void CArtifactsSelling::highlightingChanged()
{
const auto art = hero->getArt(selectedHeroSlot);
2024-03-20 13:28:19 +02:00
if(art && offerTradePanel->isHighlighted())
2024-03-02 20:30:29 +02:00
{
2024-10-19 21:48:06 +02:00
market->getOffer(art->getTypeId(), offerTradePanel->getHighlightedItemId(), bidQty, offerQty, EMarketMode::ARTIFACT_RESOURCE);
deal->block(!LOCPLINT->makingTurn);
2024-03-02 20:30:29 +02:00
}
CMarketBase::highlightingChanged();
2024-03-20 13:28:19 +02:00
CMarketTraderText::highlightingChanged();
}
std::string CArtifactsSelling::getTraderText()
{
const auto art = hero->getArt(selectedHeroSlot);
if(art && offerTradePanel->isHighlighted())
{
2024-04-08 11:46:46 +02:00
MetaString message = MetaString::createFromTextID("core.genrltxt.268");
message.replaceNumber(offerQty);
message.replaceRawString(offerQty == 1 ? CGI->generaltexth->allTexts[161] : CGI->generaltexth->allTexts[160]);
2024-10-19 21:48:06 +02:00
message.replaceName(GameResID(offerTradePanel->getHighlightedItemId()));
2024-04-08 11:46:46 +02:00
message.replaceName(art->getTypeId());
return message.toString();
2024-03-20 13:28:19 +02:00
}
else
{
return madeTransaction ? CGI->generaltexth->allTexts[162] : CGI->generaltexth->allTexts[163];
}
2024-02-25 22:58:53 +02:00
}