1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/windows/CMarketWindow.cpp

290 lines
11 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"
2024-08-24 12:05:11 +02:00
#include "../../lib/entities/building/CBuilding.h"
#include "../../lib/texts/CGeneralTextHandler.h"
2024-02-03 13:26:24 +02:00
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/mapObjects/CGMarket.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
2024-08-12 16:38:30 +02:00
#include "../../CCallback.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);
OBJECT_CONSTRUCTION;
2024-02-29 22:33:12 +02:00
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-05-08 13:16:16 +02:00
update();
2024-02-03 13:26:24 +02:00
}
2024-02-03 22:59:05 +02:00
void CMarketWindow::updateGarrisons()
{
2024-05-08 13:16:16 +02:00
update();
2024-02-29 22:33:12 +02:00
}
void CMarketWindow::updateResources()
2024-02-29 22:33:12 +02:00
{
2024-05-08 13:16:16 +02:00
update();
2024-02-03 22:59:05 +02:00
}
void CMarketWindow::updateExperience()
2024-02-03 13:26:24 +02:00
{
2024-05-08 13:16:16 +02:00
update();
}
void CMarketWindow::update()
{
CWindowWithArtifacts::update();
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;
}
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;
if(currentMode == EMarketMode::ARTIFACT_EXP && modeButton != EMarketMode::CREATURE_EXP)
return false;
if(currentMode == EMarketMode::CREATURE_EXP && modeButton != EMarketMode::ARTIFACT_EXP)
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, EShortcut shortcut)
2024-02-29 22:33:12 +02:00
{
changeModeButtons.emplace_back(std::make_shared<CButton>(buttonPos, picPath, buttonHelpContainer, pressButtonFunctor, shortcut));
2024-02-29 22:33:12 +02:00
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))
addButton(AnimationPath::builtin("TPMRKBU1.DEF"), CGI->generaltexth->zelp[612], std::bind(&CMarketWindow::createTransferResources, this, market, hero), EShortcut::MARKET_RESOURCE_PLAYER);
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::ARTIFACT_RESOURCE))
addButton(AnimationPath::builtin("TPMRKBU3.DEF"), CGI->generaltexth->zelp[613], std::bind(&CMarketWindow::createArtifactsSelling, this, market, hero), EShortcut::MARKET_ARTIFACT_RESOURCE);
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::RESOURCE_ARTIFACT))
addButton(AnimationPath::builtin("TPMRKBU2.DEF"), CGI->generaltexth->zelp[598], std::bind(&CMarketWindow::createArtifactsBuying, this, market, hero), EShortcut::MARKET_RESOURCE_ARTIFACT);
2024-02-29 22:33:12 +02:00
buttonPos = Point(516, 520 - buttonHeightWithMargin);
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::CREATURE_RESOURCE))
addButton(AnimationPath::builtin("TPMRKBU4.DEF"), CGI->generaltexth->zelp[599], std::bind(&CMarketWindow::createFreelancersGuild, this, market, hero), EShortcut::MARKET_CREATURE_RESOURCE);
2024-04-08 11:46:46 +02:00
if(isButtonVisible(EMarketMode::RESOURCE_RESOURCE))
addButton(AnimationPath::builtin("TPMRKBU5.DEF"), CGI->generaltexth->zelp[605], std::bind(&CMarketWindow::createMarketResources, this, market, hero), EShortcut::MARKET_RESOURCE_RESOURCE);
2024-02-29 22:33:12 +02:00
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
{
addButton(AnimationPath::builtin("ALTSACC.DEF"), CGI->generaltexth->zelp[572], std::bind(&CMarketWindow::createAltarCreatures, this, market, hero), EShortcut::MARKET_CREATURE_EXPERIENCE);
2024-02-29 22:33:12 +02:00
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
{
addButton(AnimationPath::builtin("ALTART.DEF"), CGI->generaltexth->zelp[580], std::bind(&CMarketWindow::createAltarArtifacts, this, market, hero), EShortcut::MARKET_ARTIFACT_EXPERIENCE);
2024-02-29 22:33:12 +02:00
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-08-24 12:05:11 +02:00
std::string CMarketWindow::getMarketTitle(const ObjectInstanceID marketId, const EMarketMode mode) const
{
assert(LOCPLINT->cb->getMarket(marketId));
assert(vstd::contains(LOCPLINT->cb->getMarket(marketId)->availableModes(), mode));
if(const auto town = LOCPLINT->cb->getTown(marketId))
{
for(const auto & buildingId : town->getBuildings())
{
if(const auto building = town->getTown()->buildings.at(buildingId); vstd::contains(building->marketModes, mode))
2024-08-24 12:05:11 +02:00
return building->getNameTranslated();
}
}
return LOCPLINT->cb->getObj(marketId)->getObjectName();
}
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;
2024-02-25 22:58:53 +02:00
2024-02-29 22:33:12 +02:00
background = createBg(ImagePath::builtin("TPMRKABS.bmp"), PLAYER_COLORED);
2024-08-24 12:05:11 +02:00
marketWidget = std::make_shared<CArtifactsBuying>(market, hero, getMarketTitle(market->getObjInstanceID(), EMarketMode::RESOURCE_ARTIFACT));
2024-02-29 22:33:12 +02:00
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;
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-08-24 12:05:11 +02:00
auto artsSellingMarket = std::make_shared<CArtifactsSelling>(market, hero, getMarketTitle(market->getObjInstanceID(), EMarketMode::ARTIFACT_RESOURCE));
2024-02-29 22:33:12 +02:00
artSets.clear();
const auto heroArts = artsSellingMarket->getAOHset();
addSet(heroArts);
2024-02-29 22:33:12 +02:00
marketWidget = artsSellingMarket;
2024-08-12 16:38:30 +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;
2024-02-03 13:26:24 +02:00
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;
2024-02-03 13:26:24 +02:00
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;
2024-02-03 13:26:24 +02:00
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;
2024-02-03 13:26:24 +02:00
background = createBg(ImagePath::builtin("ALTRART2.bmp"), PLAYER_COLORED);
auto altarArtifactsStorage = std::make_shared<CAltarArtifacts>(market, hero);
2024-08-17 19:19:48 +02:00
marketWidget = altarArtifactsStorage;
2024-02-03 13:26:24 +02:00
artSets.clear();
2024-08-17 19:19:48 +02:00
const auto heroArts = altarArtifactsStorage->getAOHset();
2024-05-20 14:31:07 +02:00
heroArts->clickPressedCallback = [this, heroArts](const CArtPlace & artPlace, const Point & cursorPosition)
{
clickPressedOnArtPlace(heroArts->getHero(), artPlace.slot, true, true, false);
};
heroArts->showPopupCallback = [this, heroArts](CArtPlace & artPlace, const Point & cursorPosition)
{
showArtifactAssembling(*heroArts, artPlace, cursorPosition);
};
2024-05-20 14:31:07 +02:00
heroArts->gestureCallback = [this, heroArts](const CArtPlace & artPlace, const Point & cursorPosition)
{
showQuickBackpackWindow(heroArts->getHero(), artPlace.slot, cursorPosition);
};
addSet(heroArts);
2024-02-29 22:33:12 +02:00
initWidgetInternals(EMarketMode::ARTIFACT_EXP, CGI->generaltexth->zelp[568]);
updateExperience();
2024-08-17 19:19:48 +02:00
quitButton->addCallback([altarArtifactsStorage](){altarArtifactsStorage->putBackArtifacts();});
2024-02-03 13:26:24 +02:00
}
void CMarketWindow::createAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
{
OBJECT_CONSTRUCTION;
2024-02-03 13:26:24 +02:00
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]);
updateExperience();
2024-02-03 13:26:24 +02:00
}