CCampaignScreen.cpp 7.6 KB

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