CHeroBackpackWindow.cpp 4.3 KB

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