CAltarWindow.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::updateGarrisons()
  40. {
  41. if(auto altarCreatures = std::static_pointer_cast<CAltarCreatures>(altar))
  42. altarCreatures->updateSlots();
  43. }
  44. bool CAltarWindow::holdsGarrison(const CArmedInstance * army)
  45. {
  46. return hero == army;
  47. }
  48. const CGHeroInstance * CAltarWindow::getHero() const
  49. {
  50. return hero;
  51. }
  52. void CAltarWindow::close()
  53. {
  54. if(windowClosedCallback)
  55. windowClosedCallback();
  56. CWindowObject::close();
  57. }
  58. void CAltarWindow::createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
  59. {
  60. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  61. background = createBg(ImagePath::builtin("ALTRART2.bmp"), PLAYER_COLORED);
  62. auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero);
  63. altar = altarArtifacts;
  64. artSets.clear();
  65. addSetAndCallbacks(altarArtifacts->getAOHset());
  66. changeModeButton = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTSACC.DEF"),
  67. CGI->generaltexth->zelp[572], std::bind(&CAltarWindow::createAltarCreatures, this, market, hero));
  68. if(altar->hero->getAlignment() == EAlignment::GOOD)
  69. changeModeButton->block(true);
  70. quitButton = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"),
  71. CGI->generaltexth->zelp[568], std::bind(&CAltarWindow::close, this), EShortcut::GLOBAL_RETURN);
  72. altar->setRedrawParent(true);
  73. redraw();
  74. }
  75. void CAltarWindow::createAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
  76. {
  77. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  78. background = createBg(ImagePath::builtin("ALTARMON.bmp"), PLAYER_COLORED);
  79. altar = std::make_shared<CAltarCreatures>(market, hero);
  80. changeModeButton = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTART.DEF"),
  81. CGI->generaltexth->zelp[580], std::bind(&CAltarWindow::createAltarArtifacts, this, market, hero));
  82. if(altar->hero->getAlignment() == EAlignment::EVIL)
  83. changeModeButton->block(true);
  84. quitButton = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"),
  85. CGI->generaltexth->zelp[568], std::bind(&CAltarWindow::close, this), EShortcut::GLOBAL_RETURN);
  86. altar->setRedrawParent(true);
  87. redraw();
  88. }
  89. void CAltarWindow::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
  90. {
  91. if(!getState().has_value())
  92. return;
  93. if(auto altarArtifacts = std::static_pointer_cast<CAltarArtifacts>(altar))
  94. {
  95. if(const auto pickedArt = getPickedArtifact())
  96. altarArtifacts->setSelectedArtifact(pickedArt);
  97. else
  98. altarArtifacts->setSelectedArtifact(nullptr);
  99. }
  100. CWindowWithArtifacts::artifactMoved(srcLoc, destLoc, withRedraw);
  101. }
  102. void CAltarWindow::showAll(Canvas & to)
  103. {
  104. // This func is temporary workaround for compliance with CTradeWindow
  105. CWindowObject::showAll(to);
  106. if(altar->hRight)
  107. {
  108. altar->hRight->showAllAt(altar->pos.topLeft() + Point(396, 423), "", to);
  109. }
  110. if(altar->hLeft)
  111. {
  112. altar->hLeft->showAllAt(altar->pos.topLeft() + Point(150, 423), "", to);
  113. }
  114. }