CAltarWindow.cpp 4.7 KB

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