CAltarWindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * CAltarWindow.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 "CAltarWindow.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/Canvas.h"
  14. #include "../gui/Shortcut.h"
  15. #include "../widgets/Buttons.h"
  16. #include "../widgets/TextControls.h"
  17. #include "../CGameInfo.h"
  18. #include "../../lib/CGeneralTextHandler.h"
  19. #include "../../lib/CHeroHandler.h"
  20. #include "../../lib/mapObjects/CGHeroInstance.h"
  21. CAltarWindow::CAltarWindow(const IMarket * market, const CGHeroInstance * hero, const std::function<void()> & onWindowClosed, EMarketMode mode)
  22. : CWindowObject(PLAYER_COLORED, ImagePath::builtin(mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"))
  23. , hero(hero)
  24. , windowClosedCallback(onWindowClosed)
  25. {
  26. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  27. assert(mode == EMarketMode::ARTIFACT_EXP || mode == EMarketMode::CREATURE_EXP);
  28. if(mode == EMarketMode::ARTIFACT_EXP)
  29. createAltarArtifacts(market, hero);
  30. else if(mode == EMarketMode::CREATURE_EXP)
  31. createAltarCreatures(market, hero);
  32. updateExpToLevel();
  33. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  34. }
  35. void CAltarWindow::updateExpToLevel()
  36. {
  37. altar->expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(altar->hero->exp) + 1) - altar->hero->exp));
  38. }
  39. void CAltarWindow::updateGarrison()
  40. {
  41. if(auto altarCreatures = std::static_pointer_cast<CAltarCreatures>(altar))
  42. altarCreatures->updateGarrison();
  43. }
  44. const CGHeroInstance * CAltarWindow::getHero() const
  45. {
  46. return hero;
  47. }
  48. void CAltarWindow::close()
  49. {
  50. if(windowClosedCallback)
  51. windowClosedCallback();
  52. CWindowObject::close();
  53. }
  54. void CAltarWindow::createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
  55. {
  56. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  57. background = createBg(ImagePath::builtin("ALTRART2.bmp"), PLAYER_COLORED);
  58. auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero);
  59. altar = altarArtifacts;
  60. artSets.clear();
  61. addSetAndCallbacks(altarArtifacts->getAOHset());
  62. changeModeButton = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTSACC.DEF"),
  63. CGI->generaltexth->zelp[572], std::bind(&CAltarWindow::createAltarCreatures, this, market, hero));
  64. if(altar->hero->getAlignment() == EAlignment::GOOD)
  65. changeModeButton->block(true);
  66. quitButton = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"),
  67. CGI->generaltexth->zelp[568], std::bind(&CAltarWindow::close, this), EShortcut::GLOBAL_RETURN);
  68. altar->setRedrawParent(true);
  69. redraw();
  70. }
  71. void CAltarWindow::createAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
  72. {
  73. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  74. background = createBg(ImagePath::builtin("ALTARMON.bmp"), PLAYER_COLORED);
  75. altar = std::make_shared<CAltarCreatures>(market, hero);
  76. changeModeButton = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTART.DEF"),
  77. CGI->generaltexth->zelp[580], std::bind(&CAltarWindow::createAltarArtifacts, this, market, hero));
  78. if(altar->hero->getAlignment() == EAlignment::EVIL)
  79. changeModeButton->block(true);
  80. quitButton = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"),
  81. CGI->generaltexth->zelp[568], std::bind(&CAltarWindow::close, this), EShortcut::GLOBAL_RETURN);
  82. altar->setRedrawParent(true);
  83. redraw();
  84. }
  85. void CAltarWindow::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
  86. {
  87. if(!getState().has_value())
  88. return;
  89. if(auto altarArtifacts = std::static_pointer_cast<CAltarArtifacts>(altar))
  90. {
  91. if(const auto pickedArt = getPickedArtifact())
  92. altarArtifacts->setSelectedArtifact(pickedArt);
  93. else
  94. altarArtifacts->setSelectedArtifact(nullptr);
  95. }
  96. CWindowWithArtifacts::artifactMoved(srcLoc, destLoc, withRedraw);
  97. }
  98. void CAltarWindow::showAll(Canvas & to)
  99. {
  100. // This func is temporary workaround for compliance with CTradeWindow
  101. CWindowObject::showAll(to);
  102. if(altar->hRight)
  103. {
  104. to.drawBorder(Rect::createAround(altar->hRight->pos, 1), Colors::BRIGHT_YELLOW, 2);
  105. altar->hRight->showAllAt(altar->pos.topLeft() + Point(396, 423), "", to);
  106. }
  107. if(altar->hLeft)
  108. {
  109. to.drawBorder(Rect::createAround(altar->hLeft->pos, 1), Colors::BRIGHT_YELLOW, 2);
  110. altar->hLeft->showAllAt(altar->pos.topLeft() + Point(150, 423), "", to);
  111. }
  112. }