CHeroBackpackWindow.cpp 5.0 KB

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