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

158 lines
4.7 KiB
C++
Raw Normal View History

/*
* CCampaignScreen.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 "CCampaignScreen.h"
2022-09-18 14:47:49 +02:00
#include "CMainMenu.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../CServerHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../media/IMusicPlayer.h"
#include "../render/Canvas.h"
#include "../widgets/CComponent.h"
#include "../widgets/Buttons.h"
#include "../widgets/MiscWidgets.h"
#include "../widgets/ObjectLists.h"
#include "../widgets/TextControls.h"
#include "../widgets/VideoWidget.h"
#include "../windows/GUIClasses.h"
#include "../windows/InfoWindows.h"
#include "../windows/CWindowObject.h"
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/CArtHandler.h"
#include "../../lib/spells/CSpellHandler.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CSkillHandler.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/CCreatureHandler.h"
2023-06-25 21:28:24 +02:00
#include "../../lib/campaign/CampaignHandler.h"
#include "../../lib/mapping/CMapService.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
2023-09-20 03:13:54 +02:00
CCampaignScreen::CCampaignScreen(const JsonNode & config, std::string name)
: CWindowObject(BORDERED), campaignSet(name)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-20 03:13:54 +02:00
for(const JsonNode & node : config[name]["images"].Vector())
images.push_back(CMainMenu::createPicture(node));
if(!images.empty())
{
images[0]->center(); // move background to center
moveTo(images[0]->pos.topLeft()); // move everything else to center
images[0]->moveTo(pos.topLeft()); // restore moved twice background
pos = images[0]->pos; // fix height\width of this window
}
2023-09-20 03:13:54 +02:00
if(!config[name]["exitbutton"].isNull())
{
2023-09-20 03:13:54 +02:00
buttonBack = createExitButton(config[name]["exitbutton"]);
buttonBack->setHoverable(true);
}
2023-09-20 03:13:54 +02:00
for(const JsonNode & node : config[name]["items"].Vector())
2023-09-20 23:06:32 +02:00
campButtons.push_back(std::make_shared<CCampaignButton>(node, config, campaignSet));
}
void CCampaignScreen::activate()
{
2023-09-04 12:03:15 +02:00
CCS->musich->playMusic(AudioPath::builtin("Music/MainMenu"), true, false);
CWindowObject::activate();
}
std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode & button)
{
std::pair<std::string, std::string> help;
if(!button["help"].isNull() && button["help"].Float() > 0)
help = CGI->generaltexth->zelp[(size_t)button["help"].Float()];
return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), AnimationPath::fromJson(button["name"]), help, [=](){ close();}, EShortcut::GLOBAL_CANCEL);
}
2023-09-20 23:06:32 +02:00
CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, const JsonNode & parentConfig, std::string campaignSet)
2023-09-20 21:18:13 +02:00
: campaignSet(campaignSet)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos.x += static_cast<int>(config["x"].Float());
pos.y += static_cast<int>(config["y"].Float());
pos.w = 200;
pos.h = 116;
campFile = config["file"].String();
videoPath = VideoPath::fromJson(config["video"]);
2023-09-20 23:06:32 +02:00
status = CCampaignScreen::ENABLED;
2023-06-25 21:28:24 +02:00
auto header = CampaignHandler::getHeader(campFile);
2023-09-27 22:53:13 +02:00
hoverText = header->getNameTranslated();
2023-09-21 21:27:06 +02:00
if(persistentStorage["completedCampaigns"][header->getFilename()].Bool())
status = CCampaignScreen::COMPLETED;
2023-09-20 23:06:32 +02:00
for(const JsonNode & node : parentConfig[campaignSet]["items"].Vector())
{
for(const JsonNode & requirement : config["requires"].Vector())
{
if(node["id"].Integer() == requirement.Integer())
2023-09-21 21:27:06 +02:00
if(!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
2023-09-20 23:06:32 +02:00
status = CCampaignScreen::DISABLED;
}
}
2023-09-21 21:27:06 +02:00
if(persistentStorage["unlockAllCampaigns"].Bool())
status = CCampaignScreen::ENABLED;
if(status != CCampaignScreen::DISABLED)
{
addUsedEvents(LCLICK | HOVER);
graphicsImage = std::make_shared<CPicture>(ImagePath::fromJson(config["image"]));
hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
parent->addChild(hoverLabel.get());
}
if(status == CCampaignScreen::COMPLETED)
graphicsCompleted = std::make_shared<CPicture>(ImagePath::builtin("CAMPCHK"));
}
void CCampaignScreen::CCampaignButton::clickReleased(const Point & cursorPosition)
{
2023-09-20 21:18:13 +02:00
CMainMenu::openCampaignLobby(campFile, campaignSet);
}
void CCampaignScreen::CCampaignButton::hover(bool on)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
if (on && !videoPath.empty())
2024-05-15 18:34:23 +02:00
videoPlayer = std::make_shared<VideoWidget>(Point(), videoPath, false);
2023-09-04 13:29:02 +02:00
else
videoPlayer.reset();
2023-09-04 13:29:02 +02:00
if(hoverLabel)
{
if(on)
hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
else
hoverLabel->setText(" ");
}
}