| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /*
- * 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"
- #include "CMainMenu.h"
- #include "../CPlayerInterface.h"
- #include "../CServerHandler.h"
- #include "../GameEngine.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/CCreatureHandler.h"
- #include "../../lib/campaign/CampaignHandler.h"
- #include "../../lib/mapping/CMapService.h"
- #include "../../lib/mapObjects/CGHeroInstance.h"
- CCampaignScreen::CCampaignScreen(const JsonNode& config, std::string name)
- : CWindowObject(BORDERED), campaignSet(name)
- {
- OBJECT_CONSTRUCTION;
- 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())
- {
- images.push_back(CMainMenu::createPicture(node));
- 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)
- {
- auto button = std::make_shared<CCampaignButton>(node, config, campaignSet);
- button->enable();
- campButtons.push_back(button);
- }
- maxPages = (campaigns.size() + campaignsPerPage - 1) / campaignsPerPage;
- buttonNext = std::make_shared<CButton>(Point(340, 560), AnimationPath::builtin("campaigns/next"), "", [this, name]() { switchPage(1, name); });
- buttonNext->disable();
- buttonPrev = std::make_shared<CButton>(Point(275, 560), AnimationPath::builtin("campaigns/back"), "", [this, name]() { switchPage(-1, name); });
- buttonPrev->disable();
- if (!config[name]["exitbutton"].isNull())
- {
- buttonBack = createExitButton(config[name]["exitbutton"]);
- buttonBack->setHoverable(true);
- }
- updateCampaignButtons(config, campaignSet);
- }
- void CCampaignScreen::activate()
- {
- ENGINE->music().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 = LIBRARY->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, [this]() { close(); }, EShortcut::GLOBAL_CANCEL);
- }
- CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode& config, const JsonNode& parentConfig, std::string campaignSet)
- : campaignSet(campaignSet)
- {
- OBJECT_CONSTRUCTION;
- 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"]);
- status = CCampaignScreen::ENABLED;
- auto header = CampaignHandler::getHeader(campFile);
- hoverText = header->getNameTranslated();
- if (persistentStorage["completedCampaigns"][header->getFilename()].Bool())
- status = CCampaignScreen::COMPLETED;
- for (const JsonNode& node : parentConfig[campaignSet]["items"].Vector())
- {
- for (const JsonNode& requirement : config["requires"].Vector())
- {
- if (node["id"].Integer() == requirement.Integer())
- if (!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
- status = CCampaignScreen::DISABLED;
- }
- if (!CResourceHandler::get()->existsResource(ResourcePath(node["file"].String(), EResType::CAMPAIGN)))
- status = CCampaignScreen::DISABLED;
- }
- 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)
- {
- CMainMenu::openCampaignLobby(campFile, campaignSet);
- }
- void CCampaignScreen::CCampaignButton::hover(bool on)
- {
- OBJECT_CONSTRUCTION;
- if (on && !videoPath.empty())
- videoPlayer = std::make_shared<VideoWidget>(Point(), videoPath, false);
- else
- videoPlayer.reset();
- 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(" ");
- }
- }
- 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();
- }
|