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 "../../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"
|
|
|
|
#include "../../../lib/CGeneralTextHandler.h"
|
2024-02-29 22:33:12 +02:00
|
|
|
#include "../../../lib/mapObjects/CGHeroInstance.h"
|
2024-02-25 22:58:53 +02:00
|
|
|
#include "../../../lib/mapObjects/CGMarket.h"
|
2024-03-02 20:30:29 +02:00
|
|
|
#include "../../../lib/mapObjects/CGTownInstance.h"
|
2024-02-25 22:58:53 +02:00
|
|
|
|
|
|
|
CArtifactsSelling::CArtifactsSelling(const IMarket * market, const CGHeroInstance * hero)
|
2024-03-02 20:30:29 +02:00
|
|
|
: CMarketBase(market, hero, [this](){return CArtifactsSelling::getSelectionParams();})
|
2024-02-27 13:39:50 +02:00
|
|
|
, CResourcesBuying(
|
|
|
|
[this](const std::shared_ptr<CTradeableItem> & resSlot){CArtifactsSelling::onSlotClickPressed(resSlot, hRight);},
|
2024-03-02 20:30:29 +02:00
|
|
|
[this](){CMarketBase::updateSubtitles(EMarketMode::ARTIFACT_RESOURCE);})
|
2024-02-25 22:58:53 +02:00
|
|
|
{
|
|
|
|
OBJECT_CONSTRUCTION_CUSTOM_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->townh)[dynamic_cast<const CGTownInstance*>(market)->getFaction()]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated()));
|
|
|
|
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"),
|
2024-02-25 22:58:53 +02:00
|
|
|
CGI->generaltexth->zelp[595], [this](){CArtifactsSelling::makeDeal();});
|
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->selectedSlot->moveTo(pos.topLeft() + Point(409, 473));
|
|
|
|
offerTradePanel->selectedSlot->subtitle->moveBy(Point(0, 1));
|
|
|
|
|
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);
|
|
|
|
heroArts->selectArtCallback = [this](CArtPlace * artPlace)
|
|
|
|
{
|
|
|
|
assert(artPlace);
|
2024-02-27 13:39:50 +02:00
|
|
|
const auto artForTrade = artPlace->getArt();
|
|
|
|
assert(artForTrade);
|
|
|
|
bidSelectedSlot->setID(artForTrade->getTypeId().num);
|
|
|
|
hLeft = bidSelectedSlot;
|
2024-02-29 22:33:12 +02:00
|
|
|
selectedHeroSlot = artPlace->slot;
|
2024-03-02 20:30:29 +02:00
|
|
|
CArtifactsSelling::highlightingChanged();
|
|
|
|
offerTradePanel->update();
|
2024-02-25 22:58:53 +02:00
|
|
|
redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
CArtifactsSelling::updateSelected();
|
2024-02-29 22:33:12 +02:00
|
|
|
CArtifactsSelling::deselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsSelling::deselect()
|
|
|
|
{
|
2024-03-02 20:30:29 +02:00
|
|
|
CMarketBase::deselect();
|
2024-02-29 22:33:12 +02:00
|
|
|
selectedHeroSlot = ArtifactPosition::PRE_FIRST;
|
|
|
|
heroArts->unmarkSlots();
|
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);
|
|
|
|
LOCPLINT->cb->trade(market, EMarketMode::ARTIFACT_RESOURCE, art->getId(), GameResID(hRight->id), offerQty, hero);
|
2024-02-25 22:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactsSelling::updateSelected()
|
|
|
|
{
|
2024-03-02 20:30:29 +02:00
|
|
|
CMarketBase::updateSelected();
|
2024-02-25 22:58:53 +02:00
|
|
|
|
2024-02-27 13:39:50 +02:00
|
|
|
if(hLeft && hRight)
|
2024-02-25 22:58:53 +02:00
|
|
|
{
|
2024-02-27 13:39:50 +02:00
|
|
|
bidSelectedSlot->image->enable();
|
|
|
|
bidSelectedSlot->image->setFrame(CGI->artifacts()->getByIndex(hLeft->id)->getIconIndex());
|
|
|
|
bidSelectedSlot->subtitle->setText(std::to_string(bidQty));
|
2024-02-25 22:58:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-27 13:39:50 +02:00
|
|
|
bidSelectedSlot->image->disable();
|
|
|
|
bidSelectedSlot->image->setFrame(0);
|
|
|
|
bidSelectedSlot->subtitle->clear();
|
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-02 20:30:29 +02:00
|
|
|
if(selectedHeroSlot.num >= ArtifactPosition::BACKPACK_START + hero->artifactsInBackpack.size())
|
|
|
|
{
|
|
|
|
selectedHeroSlot = ArtifactPosition::BACKPACK_START;
|
|
|
|
deselect();
|
|
|
|
}
|
2024-02-29 22:33:12 +02:00
|
|
|
if(hero->getArt(selectedHeroSlot) == nullptr)
|
|
|
|
deselect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:58:53 +02:00
|
|
|
std::shared_ptr<CArtifactsOfHeroMarket> CArtifactsSelling::getAOHset() const
|
|
|
|
{
|
|
|
|
return heroArts;
|
|
|
|
}
|
|
|
|
|
2024-03-02 20:30:29 +02:00
|
|
|
CMarketBase::SelectionParams CArtifactsSelling::getSelectionParams() const
|
2024-02-25 22:58:53 +02:00
|
|
|
{
|
2024-02-27 13:39:50 +02:00
|
|
|
if(hLeft && hRight)
|
2024-02-25 22:58:53 +02:00
|
|
|
return std::make_tuple(
|
|
|
|
std::nullopt,
|
2024-02-29 22:33:12 +02:00
|
|
|
SelectionParamOneSide {std::to_string(offerQty), hRight->id}
|
2024-02-25 22:58:53 +02:00
|
|
|
);
|
|
|
|
else
|
|
|
|
return std::make_tuple(std::nullopt, std::nullopt);
|
|
|
|
}
|
|
|
|
|
2024-03-02 20:30:29 +02:00
|
|
|
void CArtifactsSelling::highlightingChanged()
|
|
|
|
{
|
|
|
|
if(hLeft && hRight)
|
|
|
|
{
|
|
|
|
market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::ARTIFACT_RESOURCE);
|
|
|
|
deal->block(false);
|
|
|
|
}
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:58:53 +02:00
|
|
|
void CArtifactsSelling::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
|
|
|
|
{
|
|
|
|
assert(newSlot);
|
|
|
|
if(newSlot == hCurSlot)
|
|
|
|
return;
|
|
|
|
|
2024-03-02 20:30:29 +02:00
|
|
|
CMarketBase::onSlotClickPressed(newSlot, hCurSlot);
|
|
|
|
highlightingChanged();
|
2024-02-25 22:58:53 +02:00
|
|
|
redraw();
|
|
|
|
}
|