CCampaignScreen.cpp 4.4 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 "../CGameInfo.h"
  14. #include "../CMessage.h"
  15. #include "../CBitmapHandler.h"
  16. #include "../CMusicHandler.h"
  17. #include "../CVideoHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CServerHandler.h"
  20. #include "../gui/CAnimation.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../widgets/CComponent.h"
  23. #include "../widgets/Buttons.h"
  24. #include "../widgets/MiscWidgets.h"
  25. #include "../widgets/ObjectLists.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../windows/GUIClasses.h"
  28. #include "../windows/InfoWindows.h"
  29. #include "../windows/CWindowObject.h"
  30. #include "../../lib/filesystem/Filesystem.h"
  31. #include "../../lib/CGeneralTextHandler.h"
  32. #include "../../lib/CArtHandler.h"
  33. #include "../../lib/CBuildingHandler.h"
  34. #include "../../lib/spells/CSpellHandler.h"
  35. #include "../../lib/CSkillHandler.h"
  36. #include "../../lib/CTownHandler.h"
  37. #include "../../lib/CHeroHandler.h"
  38. #include "../../lib/CCreatureHandler.h"
  39. #include "../../lib/mapping/CCampaignHandler.h"
  40. #include "../../lib/mapping/CMapService.h"
  41. #include "../../lib/mapObjects/CGHeroInstance.h"
  42. CCampaignScreen::CCampaignScreen(const JsonNode & config)
  43. : CWindowObject(BORDERED)
  44. {
  45. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  46. for(const JsonNode & node : config["images"].Vector())
  47. images.push_back(CMainMenu::createPicture(node));
  48. if(!images.empty())
  49. {
  50. images[0]->center(); // move background to center
  51. moveTo(images[0]->pos.topLeft()); // move everything else to center
  52. images[0]->moveTo(pos.topLeft()); // restore moved twice background
  53. pos = images[0]->pos; // fix height\width of this window
  54. }
  55. if(!config["exitbutton"].isNull())
  56. {
  57. buttonBack = createExitButton(config["exitbutton"]);
  58. buttonBack->hoverable = true;
  59. }
  60. for(const JsonNode & node : config["items"].Vector())
  61. campButtons.push_back(std::make_shared<CCampaignButton>(node));
  62. }
  63. std::shared_ptr<CButton> CCampaignScreen::createExitButton(const JsonNode & button)
  64. {
  65. std::pair<std::string, std::string> help;
  66. if(!button["help"].isNull() && button["help"].Float() > 0)
  67. help = CGI->generaltexth->zelp[(size_t)button["help"].Float()];
  68. return std::make_shared<CButton>(Point((int)button["x"].Float(), (int)button["y"].Float()), button["name"].String(), help, [=](){ close();}, (int)button["hotkey"].Float());
  69. }
  70. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config)
  71. {
  72. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  73. pos.x += static_cast<int>(config["x"].Float());
  74. pos.y += static_cast<int>(config["y"].Float());
  75. pos.w = 200;
  76. pos.h = 116;
  77. campFile = config["file"].String();
  78. video = config["video"].String();
  79. status = config["open"].Bool() ? CCampaignScreen::ENABLED : CCampaignScreen::DISABLED;
  80. CCampaignHeader header = CCampaignHandler::getHeader(campFile);
  81. hoverText = header.name;
  82. if(status != CCampaignScreen::DISABLED)
  83. {
  84. addUsedEvents(LCLICK | HOVER);
  85. graphicsImage = std::make_shared<CPicture>(config["image"].String());
  86. hoverLabel = std::make_shared<CLabel>(pos.w / 2, pos.h + 20, FONT_MEDIUM, CENTER, Colors::YELLOW, "");
  87. parent->addChild(hoverLabel.get());
  88. }
  89. if(status == CCampaignScreen::COMPLETED)
  90. graphicsCompleted = std::make_shared<CPicture>("CAMPCHK");
  91. }
  92. void CCampaignScreen::CCampaignButton::show(SDL_Surface * to)
  93. {
  94. if(status == CCampaignScreen::DISABLED)
  95. return;
  96. CIntObject::show(to);
  97. // Play the campaign button video when the mouse cursor is placed over the button
  98. if(hovered)
  99. {
  100. if(CCS->videoh->fname != video)
  101. CCS->videoh->open(video);
  102. CCS->videoh->update(pos.x, pos.y, to, true, false); // plays sequentially frame by frame, starts at the beginning when the video is over
  103. }
  104. else if(CCS->videoh->fname == video) // When you got out of the bounds of the button then close the video
  105. {
  106. CCS->videoh->close();
  107. redraw();
  108. }
  109. }
  110. void CCampaignScreen::CCampaignButton::clickLeft(tribool down, bool previousState)
  111. {
  112. if(down)
  113. {
  114. CCS->videoh->close();
  115. CMainMenu::openCampaignLobby(campFile);
  116. }
  117. }
  118. void CCampaignScreen::CCampaignButton::hover(bool on)
  119. {
  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. }