1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/client/windows/CMarketWindow.cpp

262 lines
9.6 KiB
C++
Raw Normal View History

2024-02-03 13:26:24 +02:00
/*
* CMarketWindow.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 "CMarketWindow.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../widgets/Buttons.h"
#include "../widgets/TextControls.h"
2024-02-29 22:33:12 +02:00
#include "../widgets/markets/CAltarArtifacts.h"
#include "../widgets/markets/CAltarCreatures.h"
2024-02-21 20:48:14 +02:00
#include "../widgets/markets/CArtifactsBuying.h"
2024-02-25 22:58:53 +02:00
#include "../widgets/markets/CArtifactsSelling.h"
2024-02-03 22:59:05 +02:00
#include "../widgets/markets/CFreelancerGuild.h"
2024-02-19 23:40:43 +02:00
#include "../widgets/markets/CMarketResources.h"
2024-02-21 20:48:14 +02:00
#include "../widgets/markets/CTransferResources.h"
2024-02-03 13:26:24 +02:00
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/mapObjects/CGMarket.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
CMarketWindow::CMarketWindow(const IMarket * market, const CGHeroInstance * hero, const std::function<void()> & onWindowClosed, EMarketMode mode)
2024-04-23 15:21:45 +02:00
: CWindowObject(PLAYER_COLORED)
2024-02-03 13:26:24 +02:00
, windowClosedCallback(onWindowClosed)
{
assert(mode == EMarketMode::RESOURCE_RESOURCE || mode == EMarketMode::RESOURCE_PLAYER || mode == EMarketMode::CREATURE_RESOURCE ||
mode == EMarketMode::RESOURCE_ARTIFACT || mode == EMarketMode::ARTIFACT_RESOURCE || mode == EMarketMode::ARTIFACT_EXP ||
mode == EMarketMode::CREATURE_EXP);
2024-02-29 22:33:12 +02:00
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
2024-02-03 13:26:24 +02:00
if(mode == EMarketMode::RESOURCE_RESOURCE)
createMarketResources(market, hero);
else if(mode == EMarketMode::RESOURCE_PLAYER)
createTransferResources(market, hero);
else if(mode == EMarketMode::CREATURE_RESOURCE)
createFreelancersGuild(market, hero);
else if(mode == EMarketMode::RESOURCE_ARTIFACT)
createArtifactsBuying(market, hero);
else if(mode == EMarketMode::ARTIFACT_RESOURCE)
createArtifactsSelling(market, hero);
else if(mode == EMarketMode::ARTIFACT_EXP)
createAltarArtifacts(market, hero);
else if(mode == EMarketMode::CREATURE_EXP)
createAltarCreatures(market, hero);
statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
}
2024-02-29 22:33:12 +02:00
void CMarketWindow::updateArtifacts()
2024-02-03 13:26:24 +02:00
{
2024-02-29 22:33:12 +02:00
assert(marketWidget);
2024-03-02 20:30:29 +02:00
marketWidget->update();
2024-02-03 13:26:24 +02:00
}
2024-02-03 22:59:05 +02:00
void CMarketWindow::updateGarrisons()
{
2024-02-29 22:33:12 +02:00
assert(marketWidget);
2024-03-02 20:30:29 +02:00
marketWidget->update();
2024-02-29 22:33:12 +02:00
}
void CMarketWindow::updateResource()
{
assert(marketWidget);
2024-03-02 20:30:29 +02:00
marketWidget->update();
2024-02-03 22:59:05 +02:00
}
2024-02-29 22:33:12 +02:00
void CMarketWindow::updateHero()
2024-02-03 13:26:24 +02:00
{
2024-02-29 22:33:12 +02:00
assert(marketWidget);
2024-03-02 20:30:29 +02:00
marketWidget->update();
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::close()
{
if(windowClosedCallback)
windowClosedCallback();
CWindowObject::close();
}
2024-02-29 22:33:12 +02:00
bool CMarketWindow::holdsGarrison(const CArmedInstance * army)
2024-02-03 13:26:24 +02:00
{
2024-02-29 22:33:12 +02:00
assert(marketWidget);
return marketWidget->hero == army;
}
void CMarketWindow::artifactRemoved(const ArtifactLocation & artLoc)
{
2024-03-02 20:30:29 +02:00
marketWidget->update();
2024-02-29 22:33:12 +02:00
CWindowWithArtifacts::artifactRemoved(artLoc);
}
void CMarketWindow::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
{
2024-03-20 13:28:19 +02:00
CWindowWithArtifacts::artifactMoved(srcLoc, destLoc, withRedraw);
2024-02-29 22:33:12 +02:00
assert(marketWidget);
2024-03-02 20:30:29 +02:00
marketWidget->update();
2024-02-03 13:26:24 +02:00
}
2024-02-03 22:59:05 +02:00
void CMarketWindow::createChangeModeButtons(EMarketMode currentMode, const IMarket * market, const CGHeroInstance * hero)
2024-02-03 13:26:24 +02:00
{
2024-04-08 11:46:46 +02:00
auto isButtonVisible = [currentMode, market, hero](EMarketMode modeButton) -> bool
2024-02-03 13:26:24 +02:00
{
2024-02-03 22:59:05 +02:00
if(currentMode == modeButton)
2024-02-03 13:26:24 +02:00
return false;
2024-02-03 22:59:05 +02:00
if(!market->allowsTrade(modeButton))
2024-02-03 13:26:24 +02:00
return false;
2024-02-03 22:59:05 +02:00
if(modeButton == EMarketMode::RESOURCE_RESOURCE || modeButton == EMarketMode::RESOURCE_PLAYER)
2024-02-03 13:26:24 +02:00
{
2024-02-03 22:59:05 +02:00
if(const auto town = dynamic_cast<const CGTownInstance*>(market))
2024-02-03 13:26:24 +02:00
return town->getOwner() == LOCPLINT->playerID;
else
return true;
}
else
{
2024-02-03 22:59:05 +02:00
return hero != nullptr;
2024-02-03 13:26:24 +02:00
}
};
changeModeButtons.clear();
2024-02-29 22:33:12 +02:00
auto buttonPos = Point(18, 520);
2024-02-03 13:26:24 +02:00
2024-02-29 22:33:12 +02:00
auto addButton = [this, &buttonPos](const AnimationPath & picPath, const std::pair<std::string, std::string> & buttonHelpContainer,
const std::function<void()> & pressButtonFunctor)
{
changeModeButtons.emplace_back(std::make_shared<CButton>(buttonPos, picPath, buttonHelpContainer, pressButtonFunctor));
buttonPos -= Point(0, buttonHeightWithMargin);
};
2024-02-03 13:26:24 +02:00
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::RESOURCE_PLAYER))
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("TPMRKBU1.DEF"), CGI->generaltexth->zelp[612], std::bind(&CMarketWindow::createTransferResources, this, market, hero));
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::ARTIFACT_RESOURCE))
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("TPMRKBU3.DEF"), CGI->generaltexth->zelp[613], std::bind(&CMarketWindow::createArtifactsSelling, this, market, hero));
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::RESOURCE_ARTIFACT))
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("TPMRKBU2.DEF"), CGI->generaltexth->zelp[598], std::bind(&CMarketWindow::createArtifactsBuying, this, market, hero));
buttonPos = Point(516, 520 - buttonHeightWithMargin);
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::CREATURE_RESOURCE))
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("TPMRKBU4.DEF"), CGI->generaltexth->zelp[599], std::bind(&CMarketWindow::createFreelancersGuild, this, market, hero));
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::RESOURCE_RESOURCE))
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("TPMRKBU5.DEF"), CGI->generaltexth->zelp[605], std::bind(&CMarketWindow::createMarketResources, this, market, hero));
buttonPos = Point(516, 421);
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::CREATURE_EXP))
2024-02-03 13:26:24 +02:00
{
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("ALTSACC.DEF"), CGI->generaltexth->zelp[572], std::bind(&CMarketWindow::createAltarCreatures, this, market, hero));
if(marketWidget->hero->getAlignment() == EAlignment::GOOD)
2024-02-03 13:26:24 +02:00
changeModeButtons.back()->block(true);
}
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::ARTIFACT_EXP))
2024-02-03 13:26:24 +02:00
{
2024-02-29 22:33:12 +02:00
addButton(AnimationPath::builtin("ALTART.DEF"), CGI->generaltexth->zelp[580], std::bind(&CMarketWindow::createAltarArtifacts, this, market, hero));
if(marketWidget->hero->getAlignment() == EAlignment::EVIL)
2024-02-03 13:26:24 +02:00
changeModeButtons.back()->block(true);
}
}
2024-02-29 22:33:12 +02:00
void CMarketWindow::initWidgetInternals(const EMarketMode mode, const std::pair<std::string, std::string> & quitButtonHelpContainer)
2024-02-03 13:26:24 +02:00
{
2024-02-21 20:48:14 +02:00
background->center();
pos = background->pos;
2024-02-29 22:33:12 +02:00
marketWidget->setRedrawParent(true);
marketWidget->moveTo(pos.topLeft());
2024-02-21 20:48:14 +02:00
2024-02-29 22:33:12 +02:00
createChangeModeButtons(mode, marketWidget->market, marketWidget->hero);
2024-02-21 20:48:14 +02:00
quitButton = std::make_shared<CButton>(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"),
2024-02-29 22:33:12 +02:00
quitButtonHelpContainer, [this](){close();}, EShortcut::GLOBAL_RETURN);
2024-02-21 20:48:14 +02:00
redraw();
2024-02-03 13:26:24 +02:00
}
2024-02-29 22:33:12 +02:00
void CMarketWindow::createArtifactsBuying(const IMarket * market, const CGHeroInstance * hero)
2024-02-03 13:26:24 +02:00
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
2024-02-25 22:58:53 +02:00
2024-02-29 22:33:12 +02:00
background = createBg(ImagePath::builtin("TPMRKABS.bmp"), PLAYER_COLORED);
marketWidget = std::make_shared<CArtifactsBuying>(market, hero);
initWidgetInternals(EMarketMode::RESOURCE_ARTIFACT, CGI->generaltexth->zelp[600]);
}
2024-02-25 22:58:53 +02:00
2024-02-29 22:33:12 +02:00
void CMarketWindow::createArtifactsSelling(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
2024-02-25 22:58:53 +02:00
2024-02-29 22:33:12 +02:00
background = createBg(ImagePath::builtin("TPMRKASS.bmp"), PLAYER_COLORED);
2024-03-02 20:30:29 +02:00
// Create image that copies part of background containing slot MISC_1 into position of slot MISC_5
2024-04-10 23:06:49 +02:00
artSlotBack = std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 0, 0);
2024-05-03 11:36:13 +02:00
artSlotBack->moveTo(pos.topLeft() + Point(18, 339));
2024-02-29 22:33:12 +02:00
auto artsSellingMarket = std::make_shared<CArtifactsSelling>(market, hero);
artSets.clear();
addSetAndCallbacks(artsSellingMarket->getAOHset());
marketWidget = artsSellingMarket;
2024-04-10 23:06:49 +02:00
initWidgetInternals(EMarketMode::ARTIFACT_RESOURCE, CGI->generaltexth->zelp[600]);
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::createMarketResources(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
background = createBg(ImagePath::builtin("TPMRKRES.bmp"), PLAYER_COLORED);
2024-02-29 22:33:12 +02:00
marketWidget = std::make_shared<CMarketResources>(market, hero);
initWidgetInternals(EMarketMode::RESOURCE_RESOURCE, CGI->generaltexth->zelp[600]);
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::createFreelancersGuild(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
background = createBg(ImagePath::builtin("TPMRKCRS.bmp"), PLAYER_COLORED);
2024-02-29 22:33:12 +02:00
marketWidget = std::make_shared<CFreelancerGuild>(market, hero);
initWidgetInternals(EMarketMode::CREATURE_RESOURCE, CGI->generaltexth->zelp[600]);
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::createTransferResources(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
background = createBg(ImagePath::builtin("TPMRKPTS.bmp"), PLAYER_COLORED);
2024-02-29 22:33:12 +02:00
marketWidget = std::make_shared<CTransferResources>(market, hero);
initWidgetInternals(EMarketMode::RESOURCE_PLAYER, CGI->generaltexth->zelp[600]);
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
background = createBg(ImagePath::builtin("ALTRART2.bmp"), PLAYER_COLORED);
auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero);
2024-02-29 22:33:12 +02:00
marketWidget = altarArtifacts;
2024-02-03 13:26:24 +02:00
artSets.clear();
2024-02-25 22:58:53 +02:00
addSetAndCallbacks(altarArtifacts->getAOHset());
2024-02-29 22:33:12 +02:00
initWidgetInternals(EMarketMode::ARTIFACT_EXP, CGI->generaltexth->zelp[568]);
updateHero();
quitButton->addCallback([altarArtifacts](){altarArtifacts->putBackArtifacts();});
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::createAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
background = createBg(ImagePath::builtin("ALTARMON.bmp"), PLAYER_COLORED);
2024-02-29 22:33:12 +02:00
marketWidget = std::make_shared<CAltarCreatures>(market, hero);
initWidgetInternals(EMarketMode::CREATURE_EXP, CGI->generaltexth->zelp[568]);
updateHero();
2024-02-03 13:26:24 +02:00
}