CCampaignScreen.cpp 4.5 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 "../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)
  42. : CWindowObject(BORDERED)
  43. {
  44. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  45. for(const JsonNode & node : config["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["exitbutton"].isNull())
  55. {
  56. buttonBack = createExitButton(config["exitbutton"]);
  57. buttonBack->hoverable = true;
  58. }
  59. for(const JsonNode & node : config["items"].Vector())
  60. campButtons.push_back(std::make_shared<CCampaignButton>(node));
  61. }
  62. void CCampaignScreen::activate()
  63. {
  64. CCS->musich->playMusic("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()), button["name"].String(), help, [=](){ close();}, EShortcut::GLOBAL_CANCEL);
  73. }
  74. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config)
  75. {
  76. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  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. video = config["video"].String();
  83. status = config["open"].Bool() ? CCampaignScreen::ENABLED : CCampaignScreen::DISABLED;
  84. auto header = CampaignHandler::getHeader(campFile);
  85. hoverText = header->getName();
  86. if(status != CCampaignScreen::DISABLED)
  87. {
  88. addUsedEvents(LCLICK | HOVER);
  89. graphicsImage = std::make_shared<CPicture>(config["image"].String());
  90. hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
  91. parent->addChild(hoverLabel.get());
  92. }
  93. if(status == CCampaignScreen::COMPLETED)
  94. graphicsCompleted = std::make_shared<CPicture>("CAMPCHK");
  95. }
  96. void CCampaignScreen::CCampaignButton::show(Canvas & to)
  97. {
  98. if(status == CCampaignScreen::DISABLED)
  99. return;
  100. CIntObject::show(to);
  101. // Play the campaign button video when the mouse cursor is placed over the button
  102. if(isHovered())
  103. {
  104. if(CCS->videoh->fname != video)
  105. CCS->videoh->open(video);
  106. 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
  107. }
  108. else if(CCS->videoh->fname == video) // When you got out of the bounds of the button then close the video
  109. {
  110. CCS->videoh->close();
  111. redraw();
  112. }
  113. }
  114. void CCampaignScreen::CCampaignButton::clickReleased(const Point & cursorPosition)
  115. {
  116. CCS->videoh->close();
  117. CMainMenu::openCampaignLobby(campFile);
  118. }
  119. void CCampaignScreen::CCampaignButton::hover(bool on)
  120. {
  121. if(hoverLabel)
  122. {
  123. if(on)
  124. hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
  125. else
  126. hoverLabel->setText(" ");
  127. }
  128. }