CCampaignScreen.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * CCampaignScreen.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CCampaignScreen.h"
  12. #include "CMainMenu.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../CServerHandler.h"
  15. #include "../GameEngine.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../media/IMusicPlayer.h"
  18. #include "../render/Canvas.h"
  19. #include "../widgets/CComponent.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../widgets/TextControls.h"
  24. #include "../widgets/VideoWidget.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../windows/CWindowObject.h"
  28. #include "../../lib/filesystem/Filesystem.h"
  29. #include "../../lib/texts/CGeneralTextHandler.h"
  30. #include "../../lib/CArtHandler.h"
  31. #include "../../lib/spells/CSpellHandler.h"
  32. #include "../../lib/CConfigHandler.h"
  33. #include "../../lib/CSkillHandler.h"
  34. #include "../../lib/CCreatureHandler.h"
  35. #include "../../lib/campaign/CampaignHandler.h"
  36. #include "../../lib/mapping/CMapService.h"
  37. #include "../../lib/mapObjects/CGHeroInstance.h"
  38. CCampaignScreen::CCampaignScreen(const JsonNode& config, std::string name)
  39. : CWindowObject(BORDERED), campaignSet(name)
  40. {
  41. OBJECT_CONSTRUCTION;
  42. const auto& campaigns = config[name]["items"].Vector();
  43. // Define mapping of background name -> campaigns per page
  44. const std::unordered_map<std::string, int> campaignsPerPageMap = {
  45. {"CampaignBackground4", 4},
  46. {"CampaignBackground5", 5},
  47. {"CampaignBackground6", 6},
  48. {"CampaignBackground7", 7},
  49. {"CAMPBACK", 7},
  50. {"CAMPBKX2", 7},
  51. {"CampaignBackground8", 8}
  52. };
  53. // Process images and check if name is in the map
  54. for (const JsonNode& node : config[name]["images"].Vector())
  55. {
  56. images.push_back(CMainMenu::createPicture(node));
  57. std::string imageName = node["name"].String();
  58. auto it = campaignsPerPageMap.find(imageName);
  59. if (it != campaignsPerPageMap.end())
  60. {
  61. campaignsPerPage = it->second;
  62. }
  63. }
  64. if (!images.empty())
  65. {
  66. images[0]->center();
  67. moveTo(images[0]->pos.topLeft());
  68. images[0]->moveTo(pos.topLeft());
  69. pos = images[0]->pos;
  70. }
  71. for (const auto& node : campaigns)
  72. {
  73. auto button = std::make_shared<CCampaignButton>(node, config, campaignSet);
  74. button->enable();
  75. campButtons.push_back(button);
  76. }
  77. maxPages = (campaigns.size() + campaignsPerPage - 1) / campaignsPerPage;
  78. buttonNext = std::make_shared<CButton>(Point(340, 560), AnimationPath::builtin("campaigns/next"), "", [this, name]() { switchPage(1, name); });
  79. buttonNext->disable();
  80. buttonPrev = std::make_shared<CButton>(Point(275, 560), AnimationPath::builtin("campaigns/back"), "", [this, name]() { switchPage(-1, name); });
  81. buttonPrev->disable();
  82. if (!config[name]["exitbutton"].isNull())
  83. {
  84. buttonBack = createExitButton(config[name]["exitbutton"]);
  85. buttonBack->setHoverable(true);
  86. }
  87. updateCampaignButtons(config, campaignSet);
  88. }
  89. void CCampaignScreen::activate()
  90. {
  91. ENGINE->music().playMusic(AudioPath::builtin("Music/MainMenu"), true, false);
  92. CWindowObject::activate();
  93. }
  94. std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode& button)
  95. {
  96. std::pair<std::string, std::string> help;
  97. if (!button["help"].isNull() && button["help"].Float() > 0)
  98. help = LIBRARY->generaltexth->zelp[(size_t)button["help"].Float()];
  99. return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), AnimationPath::fromJson(button["name"]), help, [this]() { close(); }, EShortcut::GLOBAL_CANCEL);
  100. }
  101. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode& config, const JsonNode& parentConfig, std::string campaignSet)
  102. : campaignSet(campaignSet)
  103. {
  104. OBJECT_CONSTRUCTION;
  105. pos.x += static_cast<int>(config["x"].Float());
  106. pos.y += static_cast<int>(config["y"].Float());
  107. pos.w = 200;
  108. pos.h = 116;
  109. campFile = config["file"].String();
  110. videoPath = VideoPath::fromJson(config["video"]);
  111. status = CCampaignScreen::ENABLED;
  112. auto header = CampaignHandler::getHeader(campFile);
  113. hoverText = header->getNameTranslated();
  114. if (persistentStorage["completedCampaigns"][header->getFilename()].Bool())
  115. status = CCampaignScreen::COMPLETED;
  116. for (const JsonNode& node : parentConfig[campaignSet]["items"].Vector())
  117. {
  118. for (const JsonNode& requirement : config["requires"].Vector())
  119. {
  120. if (node["id"].Integer() == requirement.Integer())
  121. if (!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
  122. status = CCampaignScreen::DISABLED;
  123. }
  124. if (!CResourceHandler::get()->existsResource(ResourcePath(node["file"].String(), EResType::CAMPAIGN)))
  125. status = CCampaignScreen::DISABLED;
  126. }
  127. if (persistentStorage["unlockAllCampaigns"].Bool())
  128. status = CCampaignScreen::ENABLED;
  129. if (status != CCampaignScreen::DISABLED)
  130. {
  131. addUsedEvents(LCLICK | HOVER);
  132. graphicsImage = std::make_shared<CPicture>(ImagePath::fromJson(config["image"]));
  133. hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
  134. parent->addChild(hoverLabel.get());
  135. }
  136. if (status == CCampaignScreen::COMPLETED)
  137. graphicsCompleted = std::make_shared<CPicture>(ImagePath::builtin("CAMPCHK"));
  138. }
  139. void CCampaignScreen::CCampaignButton::clickReleased(const Point& cursorPosition)
  140. {
  141. CMainMenu::openCampaignLobby(campFile, campaignSet);
  142. }
  143. void CCampaignScreen::CCampaignButton::hover(bool on)
  144. {
  145. OBJECT_CONSTRUCTION;
  146. if (on && !videoPath.empty())
  147. videoPlayer = std::make_shared<VideoWidget>(Point(), videoPath, false);
  148. else
  149. videoPlayer.reset();
  150. if (hoverLabel)
  151. {
  152. if (on)
  153. hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
  154. else
  155. hoverLabel->setText(" ");
  156. }
  157. }
  158. void CCampaignScreen::switchPage(int delta, const std::string& campaignSet)
  159. {
  160. currentPage += delta;
  161. currentPage = std::clamp(currentPage, 0, maxPages - 1);
  162. const auto& campaignConfig = CMainMenuConfig::get().getCampaigns();
  163. updateCampaignButtons(campaignConfig, campaignSet);
  164. }
  165. void CCampaignScreen::updateCampaignButtons(const JsonNode& parentConfig, const std::string& campaignSet)
  166. {
  167. const auto& campaigns = parentConfig[campaignSet]["items"].Vector();
  168. int minId = (currentPage * campaignsPerPage) + 1;
  169. int maxId = minId + campaignsPerPage - 1;
  170. for (size_t i = 0; i < campButtons.size(); ++i)
  171. {
  172. int campaignId = campaigns[i]["id"].Integer();
  173. if (campaignId >= minId && campaignId <= maxId)
  174. campButtons[i]->enable();
  175. else
  176. campButtons[i]->disable();
  177. }
  178. if (maxId < campaigns.size())
  179. buttonNext->enable();
  180. else
  181. buttonNext->disable();
  182. if (currentPage > 0)
  183. buttonPrev->enable();
  184. else
  185. buttonPrev->disable();
  186. redraw();
  187. }