CCampaignScreen.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "../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 "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../windows/CWindowObject.h"
  28. #include "../../lib/filesystem/Filesystem.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/CArtHandler.h"
  31. #include "../../lib/CBuildingHandler.h"
  32. #include "../../lib/spells/CSpellHandler.h"
  33. #include "../../lib/CSkillHandler.h"
  34. #include "../../lib/CTownHandler.h"
  35. #include "../../lib/CHeroHandler.h"
  36. #include "../../lib/CCreatureHandler.h"
  37. #include "../../lib/mapping/CCampaignHandler.h"
  38. #include "../../lib/mapping/CMapService.h"
  39. #include "../../lib/mapObjects/CGHeroInstance.h"
  40. CCampaignScreen::CCampaignScreen(const JsonNode & config)
  41. : CWindowObject(BORDERED)
  42. {
  43. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  44. for(const JsonNode & node : config["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["exitbutton"].isNull())
  54. {
  55. buttonBack = createExitButton(config["exitbutton"]);
  56. buttonBack->hoverable = true;
  57. }
  58. for(const JsonNode & node : config["items"].Vector())
  59. campButtons.push_back(std::make_shared<CCampaignButton>(node));
  60. }
  61. std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode & button)
  62. {
  63. std::pair<std::string, std::string> help;
  64. if(!button["help"].isNull() && button["help"].Float() > 0)
  65. help = CGI->generaltexth->zelp[(size_t)button["help"].Float()];
  66. return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), button["name"].String(), help, [=](){ close();}, EShortcut::GLOBAL_CANCEL);
  67. }
  68. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config)
  69. {
  70. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  71. pos.x += static_cast<int>(config["x"].Float());
  72. pos.y += static_cast<int>(config["y"].Float());
  73. pos.w = 200;
  74. pos.h = 116;
  75. campFile = config["file"].String();
  76. video = config["video"].String();
  77. status = config["open"].Bool() ? CCampaignScreen::ENABLED : CCampaignScreen::DISABLED;
  78. CCampaignHeader header = CCampaignHandler::getHeader(campFile);
  79. hoverText = header.name;
  80. if(status != CCampaignScreen::DISABLED)
  81. {
  82. addUsedEvents(LCLICK | HOVER);
  83. graphicsImage = std::make_shared<CPicture>(config["image"].String());
  84. hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, "");
  85. parent->addChild(hoverLabel.get());
  86. }
  87. if(status == CCampaignScreen::COMPLETED)
  88. graphicsCompleted = std::make_shared<CPicture>("CAMPCHK");
  89. }
  90. void CCampaignScreen::CCampaignButton::show(SDL_Surface * to)
  91. {
  92. if(status == CCampaignScreen::DISABLED)
  93. return;
  94. CIntObject::show(to);
  95. // Play the campaign button video when the mouse cursor is placed over the button
  96. if(isHovered())
  97. {
  98. if(CCS->videoh->fname != video)
  99. CCS->videoh->open(video);
  100. CCS->videoh->update(pos.x, pos.y, to, true, false); // plays sequentially frame by frame, starts at the beginning when the video is over
  101. }
  102. else if(CCS->videoh->fname == video) // When you got out of the bounds of the button then close the video
  103. {
  104. CCS->videoh->close();
  105. redraw();
  106. }
  107. }
  108. void CCampaignScreen::CCampaignButton::clickLeft(tribool down, bool previousState)
  109. {
  110. if(down)
  111. {
  112. CCS->videoh->close();
  113. CMainMenu::openCampaignLobby(campFile);
  114. }
  115. }
  116. void CCampaignScreen::CCampaignButton::hover(bool on)
  117. {
  118. if(hoverLabel)
  119. {
  120. if(on)
  121. hoverLabel->setText(hoverText); // Shows the name of the campaign when you get into the bounds of the button
  122. else
  123. hoverLabel->setText(" ");
  124. }
  125. }