CCampaignScreen.cpp 4.7 KB

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