CHeroBackpackWindow.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * CHeroBackpackWindow.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 "CHeroBackpackWindow.h"
  12. #include "../GameEngine.h"
  13. #include "../GameInstance.h"
  14. #include "../gui/Shortcut.h"
  15. #include "../widgets/Buttons.h"
  16. #include "../widgets/Images.h"
  17. #include "../widgets/TextControls.h"
  18. #include "CMessage.h"
  19. #include "render/Canvas.h"
  20. #include "CPlayerInterface.h"
  21. #include "../../CCallback.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/networkPacks/ArtifactLocation.h"
  24. #include "../../lib/texts/CGeneralTextHandler.h"
  25. CHeroBackpackWindow::CHeroBackpackWindow(const CGHeroInstance * hero, const std::vector<CArtifactsOfHeroPtr> & artsSets)
  26. : CWindowWithArtifacts(&artsSets)
  27. {
  28. OBJECT_CONSTRUCTION;
  29. stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0));
  30. arts = std::make_shared<CArtifactsOfHeroBackpack>();
  31. arts->moveBy(Point(windowMargin, windowMargin));
  32. arts->clickPressedCallback = [this](const CArtPlace & artPlace, const Point & cursorPosition)
  33. {
  34. clickPressedOnArtPlace(arts->getHero(), artPlace.slot, true, false, true, cursorPosition);
  35. };
  36. arts->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition)
  37. {
  38. showArtifactAssembling(*arts, artPlace, cursorPosition);
  39. };
  40. addSet(arts);
  41. arts->setHero(hero);
  42. std::string sortByValue = VLC->generaltexth->translate("vcmi.heroWindow.sortBackpackByCost.hover");
  43. std::string sortBySlot = VLC->generaltexth->translate("vcmi.heroWindow.sortBackpackBySlot.hover");
  44. std::string sortByClass = VLC->generaltexth->translate("vcmi.heroWindow.sortBackpackByClass.hover");
  45. buttons.emplace_back(std::make_shared<CButton>(Point(), AnimationPath::builtin("heroBackpackSort"),
  46. CButton::tooltipLocalized("vcmi.heroWindow.sortBackpackByCost"),
  47. [hero]() { GAME->interface()->cb->sortBackpackArtifactsByCost(hero->id); }));
  48. buttons.back()->setTextOverlay(sortByValue, EFonts::FONT_SMALL, Colors::YELLOW);
  49. buttons.emplace_back(std::make_shared<CButton>(Point(), AnimationPath::builtin("heroBackpackSort"),
  50. CButton::tooltipLocalized("vcmi.heroWindow.sortBackpackBySlot"),
  51. [hero]() { GAME->interface()->cb->sortBackpackArtifactsBySlot(hero->id); }));
  52. buttons.back()->setTextOverlay(sortBySlot, EFonts::FONT_SMALL, Colors::YELLOW);
  53. buttons.emplace_back(std::make_shared<CButton>(Point(), AnimationPath::builtin("heroBackpackSort"),
  54. CButton::tooltipLocalized("vcmi.heroWindow.sortBackpackByClass"),
  55. [hero]() { GAME->interface()->cb->sortBackpackArtifactsByClass(hero->id); }));
  56. buttons.back()->setTextOverlay(sortByClass, EFonts::FONT_SMALL, Colors::YELLOW);
  57. pos.w = stretchedBackground->pos.w = arts->pos.w + 2 * windowMargin;
  58. pos.h = stretchedBackground->pos.h = arts->pos.h + buttons.back()->pos.h + 3 * windowMargin;
  59. auto buttonPos = Point(pos.x + windowMargin, pos.y + arts->pos.h + 2 * windowMargin);
  60. for(const auto & button : buttons)
  61. {
  62. button->moveTo(buttonPos);
  63. buttonPos += Point(button->pos.w + 10, 0);
  64. }
  65. statusbar = CGStatusBar::create(0, pos.h, ImagePath::builtin("ADROLLVR.bmp"), pos.w);
  66. pos.h += statusbar->pos.h;
  67. addUsedEvents(LCLICK);
  68. center();
  69. }
  70. void CHeroBackpackWindow::notFocusedClick()
  71. {
  72. close();
  73. }
  74. void CHeroBackpackWindow::showAll(Canvas & to)
  75. {
  76. CIntObject::showAll(to);
  77. CMessage::drawBorder(PlayerColor(GAME->interface()->playerID), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  78. }
  79. CHeroQuickBackpackWindow::CHeroQuickBackpackWindow(const CGHeroInstance * hero, ArtifactPosition targetSlot)
  80. {
  81. OBJECT_CONSTRUCTION;
  82. stretchedBackground = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, 0, 0));
  83. arts = std::make_shared<CArtifactsOfHeroQuickBackpack>(targetSlot);
  84. arts->moveBy(Point(windowMargin, windowMargin));
  85. arts->clickPressedCallback = [this](const CArtPlace & artPlace, const Point & cursorPosition)
  86. {
  87. if(const auto curHero = arts->getHero())
  88. swapArtifactAndClose(*arts, artPlace.slot, ArtifactLocation(curHero->id, arts->getFilterSlot()));
  89. };
  90. addSet(arts);
  91. arts->setHero(hero);
  92. addUsedEvents(GESTURE);
  93. addUsedEvents(LCLICK);
  94. pos.w = stretchedBackground->pos.w = arts->pos.w + 2 * windowMargin;
  95. pos.h = stretchedBackground->pos.h = arts->pos.h + windowMargin;
  96. }
  97. void CHeroQuickBackpackWindow::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  98. {
  99. if(on)
  100. return;
  101. arts->swapSelected();
  102. close();
  103. }
  104. void CHeroQuickBackpackWindow::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  105. {
  106. arts->selectSlotAt(currentPosition);
  107. redraw();
  108. }
  109. void CHeroQuickBackpackWindow::notFocusedClick()
  110. {
  111. close();
  112. }
  113. void CHeroQuickBackpackWindow::showAll(Canvas & to)
  114. {
  115. if(arts->getSlotsNum() == 0)
  116. {
  117. // Dirty solution for closing that window
  118. close();
  119. return;
  120. }
  121. CMessage::drawBorder(PlayerColor(GAME->interface()->playerID), to, pos.w + 28, pos.h + 29, pos.x - 14, pos.y - 15);
  122. CIntObject::showAll(to);
  123. }