CTradeBase.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * CTradeBase.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 "CTradeBase.h"
  12. #include "../MiscWidgets.h"
  13. #include "../../gui/CGuiHandler.h"
  14. #include "../../widgets/Buttons.h"
  15. #include "../../widgets/TextControls.h"
  16. #include "../../CGameInfo.h"
  17. #include "../../../lib/CGeneralTextHandler.h"
  18. #include "../../../lib/mapObjects/CGHeroInstance.h"
  19. CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero)
  20. : market(market)
  21. , hero(hero)
  22. {
  23. }
  24. void CTradeBase::removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  25. {
  26. for(auto item : toRemove)
  27. removeItem(item);
  28. }
  29. void CTradeBase::removeItem(std::shared_ptr<CTradeableItem> item)
  30. {
  31. rightTradePanel->slots.erase(std::remove(rightTradePanel->slots.begin(), rightTradePanel->slots.end(), item));
  32. if(hRight == item)
  33. hRight.reset();
  34. }
  35. void CTradeBase::getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  36. {
  37. for(auto item : leftTradePanel->slots)
  38. if(!hero->getStackCount(SlotID(item->serial)))
  39. toRemove.insert(item);
  40. }
  41. void CTradeBase::deselect()
  42. {
  43. if(hLeft)
  44. hLeft->selectSlot(false);
  45. if(hRight)
  46. hRight->selectSlot(false);
  47. hLeft = hRight = nullptr;
  48. deal->block(true);
  49. }
  50. void CTradeBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  51. {
  52. if(newSlot == hCurSlot)
  53. return;
  54. if(hCurSlot)
  55. hCurSlot->selectSlot(false);
  56. hCurSlot = newSlot;
  57. newSlot->selectSlot(true);
  58. }
  59. CExperienceAltar::CExperienceAltar()
  60. {
  61. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  62. // Experience needed to reach next level
  63. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  64. // Total experience on the Altar
  65. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  66. expToLevel = std::make_shared<CLabel>(75, 477, FONT_SMALL, ETextAlignment::CENTER);
  67. expForHero = std::make_shared<CLabel>(75, 545, FONT_SMALL, ETextAlignment::CENTER);
  68. }
  69. CCreaturesSelling::CCreaturesSelling()
  70. {
  71. assert(hero);
  72. CreaturesPanel::slotsData slots;
  73. for(auto slotId = SlotID(0); slotId.num < GameConstants::ARMY_SIZE; slotId++)
  74. {
  75. if(const auto & creature = hero->getCreature(slotId))
  76. slots.emplace_back(std::make_tuple(creature->getId(), slotId, hero->getStackCount(slotId)));
  77. }
  78. leftTradePanel = std::make_shared<CreaturesPanel>(nullptr, slots);
  79. }
  80. bool CCreaturesSelling::slotDeletingCheck(const std::shared_ptr<CTradeableItem> & slot)
  81. {
  82. return hero->getStackCount(SlotID(slot->serial)) == 0 ? true : false;
  83. }
  84. void CCreaturesSelling::updateSubtitle()
  85. {
  86. for(auto & heroSlot : leftTradePanel->slots)
  87. heroSlot->subtitle = std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial)));
  88. }