CHeroBackpackWindow.cpp 5.2 KB

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