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