CCampaignScreen.cpp 4.7 KB

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