CCampaignScreen.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "../CGameInfo.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../CServerHandler.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/Shortcut.h"
  18. #include "../media/IMusicPlayer.h"
  19. #include "../render/Canvas.h"
  20. #include "../widgets/CComponent.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/MiscWidgets.h"
  23. #include "../widgets/ObjectLists.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../widgets/VideoWidget.h"
  26. #include "../windows/GUIClasses.h"
  27. #include "../windows/InfoWindows.h"
  28. #include "../windows/CWindowObject.h"
  29. #include "../../lib/filesystem/Filesystem.h"
  30. #include "../../lib/CGeneralTextHandler.h"
  31. #include "../../lib/CArtHandler.h"
  32. #include "../../lib/CBuildingHandler.h"
  33. #include "../../lib/spells/CSpellHandler.h"
  34. #include "../../lib/CConfigHandler.h"
  35. #include "../../lib/CSkillHandler.h"
  36. #include "../../lib/CTownHandler.h"
  37. #include "../../lib/CHeroHandler.h"
  38. #include "../../lib/CCreatureHandler.h"
  39. #include "../../lib/campaign/CampaignHandler.h"
  40. #include "../../lib/mapping/CMapService.h"
  41. #include "../../lib/mapObjects/CGHeroInstance.h"
  42. CCampaignScreen::CCampaignScreen(const JsonNode & config, std::string name)
  43. : CWindowObject(BORDERED), campaignSet(name)
  44. {
  45. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  46. for(const JsonNode & node : config[name]["images"].Vector())
  47. images.push_back(CMainMenu::createPicture(node));
  48. if(!images.empty())
  49. {
  50. images[0]->center(); // move background to center
  51. moveTo(images[0]->pos.topLeft()); // move everything else to center
  52. images[0]->moveTo(pos.topLeft()); // restore moved twice background
  53. pos = images[0]->pos; // fix height\width of this window
  54. }
  55. if(!config[name]["exitbutton"].isNull())
  56. {
  57. buttonBack = createExitButton(config[name]["exitbutton"]);
  58. buttonBack->setHoverable(true);
  59. }
  60. for(const JsonNode & node : config[name]["items"].Vector())
  61. campButtons.push_back(std::make_shared<CCampaignButton>(node, config, campaignSet));
  62. }
  63. void CCampaignScreen::activate()
  64. {
  65. CCS->musich->playMusic(AudioPath::builtin("Music/MainMenu"), true, false);
  66. CWindowObject::activate();
  67. }
  68. std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode & button)
  69. {
  70. std::pair<std::string, std::string> help;
  71. if(!button["help"].isNull() && button["help"].Float() > 0)
  72. help = CGI->generaltexth->zelp[(size_t)button["help"].Float()];
  73. return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), AnimationPath::fromJson(button["name"]), help, [=](){ close();}, EShortcut::GLOBAL_CANCEL);
  74. }
  75. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, const JsonNode & parentConfig, std::string campaignSet)
  76. : campaignSet(campaignSet)
  77. {
  78. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  79. pos.x += static_cast<int>(config["x"].Float());
  80. pos.y += static_cast<int>(config["y"].Float());
  81. pos.w = 200;
  82. pos.h = 116;
  83. campFile = config["file"].String();
  84. videoPath = VideoPath::fromJson(config["video"]);
  85. status = CCampaignScreen::ENABLED;
  86. auto header = CampaignHandler::getHeader(campFile);
  87. hoverText = header->getNameTranslated();
  88. if(persistentStorage["completedCampaigns"][header->getFilename()].Bool())
  89. status = CCampaignScreen::COMPLETED;
  90. for(const JsonNode & node : parentConfig[campaignSet]["items"].Vector())
  91. {
  92. for(const JsonNode & requirement : config["requires"].Vector())
  93. {
  94. if(node["id"].Integer() == requirement.Integer())
  95. if(!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
  96. status = CCampaignScreen::DISABLED;
  97. }
  98. }
  99. if(persistentStorage["unlockAllCampaigns"].Bool())
  100. status = CCampaignScreen::ENABLED;
  101. if(status != CCampaignScreen::DISABLED)
  102. {
  103. addUsedEvents(LCLICK | HOVER);
  104. graphicsImage = std::make_shared<CPicture>(ImagePath::fromJson(config["image"]));
  105. hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
  106. parent->addChild(hoverLabel.get());
  107. }
  108. if(status == CCampaignScreen::COMPLETED)
  109. graphicsCompleted = std::make_shared<CPicture>(ImagePath::builtin("CAMPCHK"));
  110. }
  111. void CCampaignScreen::CCampaignButton::clickReleased(const Point & cursorPosition)
  112. {
  113. CMainMenu::openCampaignLobby(campFile, campaignSet);
  114. }
  115. void CCampaignScreen::CCampaignButton::hover(bool on)
  116. {
  117. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  118. if (on && !videoPath.empty())
  119. videoPlayer = std::make_shared<VideoWidget>(Point(), videoPath, false);
  120. else
  121. videoPlayer.reset();
  122. if(hoverLabel)
  123. {
  124. if(on)
  125. hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
  126. else
  127. hoverLabel->setText(" ");
  128. }
  129. }