CCampaignScreen.cpp 5.1 KB

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