2018-01-05 20:21:07 +03:00
|
|
|
/*
|
|
|
|
* 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 15:47:49 +03:00
|
|
|
#include "CMainMenu.h"
|
|
|
|
|
2018-01-05 20:21:07 +03:00
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../CServerHandler.h"
|
2025-02-10 21:49:23 +00:00
|
|
|
#include "../GameEngine.h"
|
2023-04-27 20:21:06 +03:00
|
|
|
#include "../gui/Shortcut.h"
|
2024-05-02 15:55:20 +03:00
|
|
|
#include "../media/IMusicPlayer.h"
|
2023-06-02 16:42:18 +03:00
|
|
|
#include "../render/Canvas.h"
|
2018-01-05 20:21:07 +03:00
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
2024-05-02 22:14:50 +03:00
|
|
|
#include "../widgets/VideoWidget.h"
|
2018-01-05 20:21:07 +03:00
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
#include "../windows/CWindowObject.h"
|
|
|
|
|
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2024-07-20 12:55:17 +00:00
|
|
|
#include "../../lib/texts/CGeneralTextHandler.h"
|
2018-01-05 20:21:07 +03:00
|
|
|
|
|
|
|
#include "../../lib/CArtHandler.h"
|
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
2024-05-02 15:55:20 +03:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2018-01-05 20:21:07 +03:00
|
|
|
#include "../../lib/CSkillHandler.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
|
2023-06-25 22:28:24 +03:00
|
|
|
#include "../../lib/campaign/CampaignHandler.h"
|
2018-01-05 20:21:07 +03:00
|
|
|
#include "../../lib/mapping/CMapService.h"
|
|
|
|
|
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
CCampaignScreen::CCampaignScreen(const JsonNode& config, std::string name)
|
2023-09-20 03:13:54 +02:00
|
|
|
: CWindowObject(BORDERED), campaignSet(name)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
2024-08-09 15:30:04 +00:00
|
|
|
OBJECT_CONSTRUCTION;
|
2018-01-05 20:21:07 +03:00
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
const auto& campaigns = config[name]["items"].Vector();
|
|
|
|
|
|
|
|
// Define mapping of background name -> campaigns per page
|
|
|
|
const std::unordered_map<std::string, int> campaignsPerPageMap = {
|
|
|
|
{"CampaignBackground4", 4},
|
|
|
|
{"CampaignBackground5", 5},
|
|
|
|
{"CampaignBackground6", 6},
|
|
|
|
{"CampaignBackground7", 7},
|
|
|
|
{"CAMPBACK", 7},
|
|
|
|
{"CAMPBKX2", 7},
|
|
|
|
{"CampaignBackground8", 8}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Process images and check if name is in the map
|
|
|
|
for (const JsonNode& node : config[name]["images"].Vector())
|
|
|
|
{
|
2018-01-05 20:21:07 +03:00
|
|
|
images.push_back(CMainMenu::createPicture(node));
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
std::string imageName = node["name"].String();
|
|
|
|
auto it = campaignsPerPageMap.find(imageName);
|
|
|
|
if (it != campaignsPerPageMap.end())
|
|
|
|
{
|
|
|
|
campaignsPerPage = it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!images.empty())
|
|
|
|
{
|
|
|
|
images[0]->center();
|
|
|
|
moveTo(images[0]->pos.topLeft());
|
|
|
|
images[0]->moveTo(pos.topLeft());
|
|
|
|
pos = images[0]->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& node : campaigns)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
2025-03-17 20:17:09 +01:00
|
|
|
auto button = std::make_shared<CCampaignButton>(node, config, campaignSet);
|
|
|
|
button->enable();
|
|
|
|
campButtons.push_back(button);
|
2018-01-05 20:21:07 +03:00
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
maxPages = (campaigns.size() + campaignsPerPage - 1) / campaignsPerPage;
|
|
|
|
|
2025-03-17 20:21:22 +01:00
|
|
|
buttonNext = std::make_shared<CButton>(Point(340, 560), AnimationPath::builtin("campaigns/next"), std::make_pair("", ""), [this, name]() { switchPage(1, name); });
|
2025-03-17 20:17:09 +01:00
|
|
|
buttonNext->disable();
|
|
|
|
|
2025-03-17 20:21:22 +01:00
|
|
|
buttonPrev = std::make_shared<CButton>(Point(275, 560), AnimationPath::builtin("campaigns/back"), std::make_pair("", ""), [this, name]() { switchPage(-1, name); });
|
2025-03-17 20:17:09 +01:00
|
|
|
buttonPrev->disable();
|
|
|
|
|
|
|
|
|
|
|
|
if (!config[name]["exitbutton"].isNull())
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
2023-09-20 03:13:54 +02:00
|
|
|
buttonBack = createExitButton(config[name]["exitbutton"]);
|
2024-02-27 22:19:09 +02:00
|
|
|
buttonBack->setHoverable(true);
|
2018-01-05 20:21:07 +03:00
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
updateCampaignButtons(config, campaignSet);
|
2018-01-05 20:21:07 +03:00
|
|
|
}
|
|
|
|
|
2023-06-27 20:09:11 +03:00
|
|
|
void CCampaignScreen::activate()
|
|
|
|
{
|
2025-02-10 22:08:50 +00:00
|
|
|
ENGINE->music().playMusic(AudioPath::builtin("Music/MainMenu"), true, false);
|
2023-06-27 20:09:11 +03:00
|
|
|
|
|
|
|
CWindowObject::activate();
|
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode& button)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
|
|
|
std::pair<std::string, std::string> help;
|
2025-03-17 20:17:09 +01:00
|
|
|
if (!button["help"].isNull() && button["help"].Float() > 0)
|
2025-02-14 16:23:37 +00:00
|
|
|
help = LIBRARY->generaltexth->zelp[(size_t)button["help"].Float()];
|
2018-01-05 20:21:07 +03:00
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), AnimationPath::fromJson(button["name"]), help, [this]() { close(); }, EShortcut::GLOBAL_CANCEL);
|
2018-01-05 20:21:07 +03:00
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode& config, const JsonNode& parentConfig, std::string campaignSet)
|
2023-09-20 21:18:13 +02:00
|
|
|
: campaignSet(campaignSet)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
2025-03-17 20:17:09 +01:00
|
|
|
|
2024-08-09 15:30:04 +00:00
|
|
|
OBJECT_CONSTRUCTION;
|
2018-01-05 20:21:07 +03:00
|
|
|
|
2020-10-01 01:38:06 -07:00
|
|
|
pos.x += static_cast<int>(config["x"].Float());
|
|
|
|
pos.y += static_cast<int>(config["y"].Float());
|
2018-01-05 20:21:07 +03:00
|
|
|
pos.w = 200;
|
|
|
|
pos.h = 116;
|
|
|
|
|
|
|
|
campFile = config["file"].String();
|
2024-05-02 22:14:50 +03:00
|
|
|
videoPath = VideoPath::fromJson(config["video"]);
|
2018-01-05 20:21:07 +03:00
|
|
|
|
2023-09-20 23:06:32 +02:00
|
|
|
status = CCampaignScreen::ENABLED;
|
2018-01-05 20:21:07 +03:00
|
|
|
|
2023-06-25 22:28:24 +03:00
|
|
|
auto header = CampaignHandler::getHeader(campFile);
|
2023-09-27 22:53:13 +02:00
|
|
|
hoverText = header->getNameTranslated();
|
2018-01-05 20:21:07 +03:00
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
if (persistentStorage["completedCampaigns"][header->getFilename()].Bool())
|
2023-09-20 22:18:53 +02:00
|
|
|
status = CCampaignScreen::COMPLETED;
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
for (const JsonNode& node : parentConfig[campaignSet]["items"].Vector())
|
2023-09-20 23:06:32 +02:00
|
|
|
{
|
2025-03-17 20:17:09 +01:00
|
|
|
for (const JsonNode& requirement : config["requires"].Vector())
|
2023-09-20 23:06:32 +02:00
|
|
|
{
|
2025-03-17 20:17:09 +01:00
|
|
|
if (node["id"].Integer() == requirement.Integer())
|
|
|
|
if (!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
|
2023-09-20 23:06:32 +02:00
|
|
|
status = CCampaignScreen::DISABLED;
|
|
|
|
}
|
2025-03-17 20:17:09 +01:00
|
|
|
if (!CResourceHandler::get()->existsResource(ResourcePath(node["file"].String(), EResType::CAMPAIGN)))
|
|
|
|
status = CCampaignScreen::DISABLED;
|
2023-09-20 23:06:32 +02:00
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
if (persistentStorage["unlockAllCampaigns"].Bool())
|
2023-09-21 21:27:06 +02:00
|
|
|
status = CCampaignScreen::ENABLED;
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
if (status != CCampaignScreen::DISABLED)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
|
|
|
addUsedEvents(LCLICK | HOVER);
|
2023-08-23 15:07:50 +03:00
|
|
|
graphicsImage = std::make_shared<CPicture>(ImagePath::fromJson(config["image"]));
|
2022-11-26 23:12:20 +02:00
|
|
|
hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
|
2018-01-05 20:21:07 +03:00
|
|
|
parent->addChild(hoverLabel.get());
|
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
if (status == CCampaignScreen::COMPLETED)
|
2023-08-23 15:07:50 +03:00
|
|
|
graphicsCompleted = std::make_shared<CPicture>(ImagePath::builtin("CAMPCHK"));
|
2025-03-17 20:17:09 +01:00
|
|
|
|
2018-01-05 20:21:07 +03:00
|
|
|
}
|
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
void CCampaignScreen::CCampaignButton::clickReleased(const Point& cursorPosition)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
2023-09-20 21:18:13 +02:00
|
|
|
CMainMenu::openCampaignLobby(campFile, campaignSet);
|
2018-01-05 20:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCampaignScreen::CCampaignButton::hover(bool on)
|
|
|
|
{
|
2024-08-09 15:30:04 +00:00
|
|
|
OBJECT_CONSTRUCTION;
|
2024-05-02 22:14:50 +03:00
|
|
|
|
|
|
|
if (on && !videoPath.empty())
|
2024-05-15 16:34:23 +00:00
|
|
|
videoPlayer = std::make_shared<VideoWidget>(Point(), videoPath, false);
|
2023-09-04 14:29:02 +03:00
|
|
|
else
|
2024-05-02 22:14:50 +03:00
|
|
|
videoPlayer.reset();
|
2023-09-04 14:29:02 +03:00
|
|
|
|
2025-03-17 20:17:09 +01:00
|
|
|
if (hoverLabel)
|
2018-01-05 20:21:07 +03:00
|
|
|
{
|
2025-03-17 20:17:09 +01:00
|
|
|
if (on)
|
2018-01-05 20:21:07 +03:00
|
|
|
hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
|
|
|
|
else
|
|
|
|
hoverLabel->setText(" ");
|
|
|
|
}
|
|
|
|
}
|
2025-03-17 20:17:09 +01:00
|
|
|
|
|
|
|
void CCampaignScreen::switchPage(int delta, const std::string& campaignSet)
|
|
|
|
{
|
|
|
|
currentPage += delta;
|
|
|
|
currentPage = std::clamp(currentPage, 0, maxPages - 1);
|
|
|
|
|
|
|
|
const auto& campaignConfig = CMainMenuConfig::get().getCampaigns();
|
|
|
|
|
|
|
|
updateCampaignButtons(campaignConfig, campaignSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCampaignScreen::updateCampaignButtons(const JsonNode& parentConfig, const std::string& campaignSet)
|
|
|
|
{
|
|
|
|
const auto& campaigns = parentConfig[campaignSet]["items"].Vector();
|
|
|
|
|
|
|
|
int minId = (currentPage * campaignsPerPage) + 1;
|
|
|
|
int maxId = minId + campaignsPerPage - 1;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < campButtons.size(); ++i)
|
|
|
|
{
|
|
|
|
int campaignId = campaigns[i]["id"].Integer();
|
|
|
|
|
|
|
|
if (campaignId >= minId && campaignId <= maxId)
|
|
|
|
campButtons[i]->enable();
|
|
|
|
else
|
|
|
|
campButtons[i]->disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxId < campaigns.size())
|
|
|
|
buttonNext->enable();
|
|
|
|
else
|
|
|
|
buttonNext->disable();
|
|
|
|
|
|
|
|
if (currentPage > 0)
|
|
|
|
buttonPrev->enable();
|
|
|
|
else
|
|
|
|
buttonPrev->disable();
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|