CCampaignScreen.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/CConfigHandler.h"
  29. #include "../../lib/CCreatureHandler.h"
  30. #include "../../lib/CSkillHandler.h"
  31. #include "../../lib/GameLibrary.h"
  32. #include "../../lib/campaign/CampaignHandler.h"
  33. #include "../../lib/filesystem/Filesystem.h"
  34. #include "../../lib/mapObjects/CGHeroInstance.h"
  35. #include "../../lib/mapping/CMapService.h"
  36. #include "../../lib/spells/CSpellHandler.h"
  37. #include "../../lib/texts/CGeneralTextHandler.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(); // move background to center
  67. moveTo(images[0]->pos.topLeft()); // move everything else to center
  68. images[0]->moveTo(pos.topLeft()); // restore moved twice background
  69. pos = images[0]->pos; // fix height\width of this window
  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. if (!config[name]["nextbutton"].isNull())
  79. {
  80. buttonNext = std::make_shared<CButton>(
  81. Point(config[name]["nextbutton"]["x"].Integer(), config[name]["nextbutton"]["y"].Integer()),
  82. AnimationPath::fromJson(config[name]["nextbutton"]["name"]),
  83. std::make_pair("", ""),
  84. [this, name]() { switchPage(1); }
  85. );
  86. buttonNext->setHoverable(true);
  87. buttonNext->disable();
  88. }
  89. if (!config[name]["backbutton"].isNull())
  90. {
  91. buttonPrev = std::make_shared<CButton>(
  92. Point(config[name]["backbutton"]["x"].Integer(), config[name]["backbutton"]["y"].Integer()),
  93. AnimationPath::fromJson(config[name]["backbutton"]["name"]),
  94. std::make_pair("", ""),
  95. [this, name]() { switchPage(-1); }
  96. );
  97. buttonPrev->setHoverable(true);
  98. buttonPrev->disable();
  99. }
  100. page = std::make_shared<CLabel>(10, 570, FONT_MEDIUM, ETextAlignment::BOTTOMLEFT, Colors::YELLOW, "");
  101. if (!config[name]["exitbutton"].isNull())
  102. {
  103. buttonBack = createExitButton(config[name]["exitbutton"]);
  104. buttonBack->setHoverable(true);
  105. }
  106. updateCampaignButtons(config);
  107. }
  108. void CCampaignScreen::activate()
  109. {
  110. ENGINE->music().playMusic(AudioPath::builtin("Music/MainMenu"), true, false);
  111. CWindowObject::activate();
  112. }
  113. std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode & button)
  114. {
  115. std::pair<std::string, std::string> help;
  116. if(!button["help"].isNull() && button["help"].Float() > 0)
  117. help = LIBRARY->generaltexth->zelp[(size_t)button["help"].Float()];
  118. return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), AnimationPath::fromJson(button["name"]), help, [this](){ close();}, EShortcut::GLOBAL_CANCEL);
  119. }
  120. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, const JsonNode & parentConfig, std::string campaignSet)
  121. : campaignSet(campaignSet)
  122. {
  123. OBJECT_CONSTRUCTION;
  124. pos.x += static_cast<int>(config["x"].Float());
  125. pos.y += static_cast<int>(config["y"].Float());
  126. pos.w = 200;
  127. pos.h = 116;
  128. campFile = config["file"].String();
  129. videoPath = VideoPath::fromJson(config["video"]);
  130. status = CCampaignScreen::ENABLED;
  131. if(CResourceHandler::get()->existsResource(ResourcePath(campFile, EResType::CAMPAIGN)))
  132. {
  133. auto header = CampaignHandler::getHeader(campFile);
  134. hoverText = header->getNameTranslated();
  135. if (persistentStorage["completedCampaigns"][header->getFilename()].Bool())
  136. status = CCampaignScreen::COMPLETED;
  137. }
  138. else
  139. {
  140. status = CCampaignScreen::DISABLED;
  141. }
  142. for(const JsonNode & node : parentConfig[campaignSet]["items"].Vector())
  143. {
  144. for(const JsonNode & requirement : config["requires"].Vector())
  145. {
  146. if(node["id"].Integer() == requirement.Integer())
  147. if(!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
  148. status = CCampaignScreen::DISABLED;
  149. }
  150. }
  151. if(persistentStorage["unlockAllCampaigns"].Bool())
  152. status = CCampaignScreen::ENABLED;
  153. if(status != CCampaignScreen::DISABLED)
  154. {
  155. addUsedEvents(LCLICK | HOVER);
  156. graphicsImage = std::make_shared<CPicture>(ImagePath::fromJson(config["image"]));
  157. hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
  158. parent->addChild(hoverLabel.get());
  159. }
  160. if(status == CCampaignScreen::COMPLETED)
  161. graphicsCompleted = std::make_shared<CPicture>(ImagePath::builtin("CAMPCHK"));
  162. }
  163. void CCampaignScreen::CCampaignButton::clickReleased(const Point & cursorPosition)
  164. {
  165. CMainMenu::openCampaignLobby(campFile, campaignSet);
  166. }
  167. void CCampaignScreen::CCampaignButton::hover(bool on)
  168. {
  169. OBJECT_CONSTRUCTION;
  170. if (on && !videoPath.empty())
  171. videoPlayer = std::make_shared<VideoWidget>(Point(), videoPath, false);
  172. else
  173. videoPlayer.reset();
  174. if(hoverLabel)
  175. {
  176. if(on)
  177. hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
  178. else
  179. hoverLabel->setText(" ");
  180. }
  181. }
  182. void CCampaignScreen::switchPage(int delta)
  183. {
  184. currentPage += delta;
  185. currentPage = std::clamp(currentPage, 0, maxPages - 1);
  186. const auto& campaignConfig = CMainMenuConfig::get().getCampaigns();
  187. updateCampaignButtons(campaignConfig);
  188. }
  189. void CCampaignScreen::updateCampaignButtons(const JsonNode & parentConfig)
  190. {
  191. const auto& campaigns = parentConfig[campaignSet]["items"].Vector();
  192. int minId = (currentPage * campaignsPerPage) + 1;
  193. int maxId = minId + campaignsPerPage - 1;
  194. for(size_t i = 0; i < campButtons.size(); ++i)
  195. {
  196. int campaignId = campaigns[i]["id"].Integer();
  197. if(campaignId >= minId && campaignId <= maxId)
  198. campButtons[i]->enable();
  199. else
  200. campButtons[i]->disable();
  201. if(!CResourceHandler::get()->existsResource(ResourcePath(campaigns[i]["file"].String(), EResType::CAMPAIGN)))
  202. {
  203. campButtons[i]->disable();
  204. logGlobal->warn("Campaign %s doesn't exist", campaigns[i]["file"].String());
  205. }
  206. }
  207. if(buttonNext && buttonPrev)
  208. {
  209. page->setText(std::to_string(currentPage + 1) + "/" + std::to_string(maxPages));
  210. if (maxId < campaigns.size())
  211. buttonNext->enable();
  212. else
  213. buttonNext->disable();
  214. if (currentPage > 0)
  215. buttonPrev->enable();
  216. else
  217. buttonPrev->disable();
  218. }
  219. redraw();
  220. }