CTradeBase.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "../../../lib/mapObjects/CGMarket.h"
  20. CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero)
  21. : market(market)
  22. , hero(hero)
  23. {
  24. }
  25. void CTradeBase::removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  26. {
  27. for(auto item : toRemove)
  28. removeItem(item);
  29. }
  30. void CTradeBase::removeItem(std::shared_ptr<CTradeableItem> item)
  31. {
  32. rightTradePanel->slots.erase(std::remove(rightTradePanel->slots.begin(), rightTradePanel->slots.end(), item));
  33. if(hRight == item)
  34. hRight.reset();
  35. }
  36. void CTradeBase::getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  37. {
  38. for(auto item : leftTradePanel->slots)
  39. if(!hero->getStackCount(SlotID(item->serial)))
  40. toRemove.insert(item);
  41. }
  42. void CTradeBase::deselect()
  43. {
  44. if(hLeft)
  45. hLeft->selectSlot(false);
  46. if(hRight)
  47. hRight->selectSlot(false);
  48. hLeft = hRight = nullptr;
  49. deal->block(true);
  50. }
  51. void CTradeBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  52. {
  53. if(newSlot == hCurSlot)
  54. return;
  55. if(hCurSlot)
  56. hCurSlot->selectSlot(false);
  57. hCurSlot = newSlot;
  58. newSlot->selectSlot(true);
  59. }
  60. CExperienceAltar::CExperienceAltar()
  61. {
  62. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  63. // Experience needed to reach next level
  64. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  65. // Total experience on the Altar
  66. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  67. expToLevel = std::make_shared<CLabel>(75, 477, FONT_SMALL, ETextAlignment::CENTER);
  68. expForHero = std::make_shared<CLabel>(75, 545, FONT_SMALL, ETextAlignment::CENTER);
  69. }
  70. CCreaturesSelling::CCreaturesSelling()
  71. {
  72. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  73. assert(hero);
  74. CreaturesPanel::slotsData slots;
  75. for(auto slotId = SlotID(0); slotId.num < GameConstants::ARMY_SIZE; slotId++)
  76. {
  77. if(const auto & creature = hero->getCreature(slotId))
  78. slots.emplace_back(std::make_tuple(creature->getId(), slotId, hero->getStackCount(slotId)));
  79. }
  80. leftTradePanel = std::make_shared<CreaturesPanel>(nullptr, slots);
  81. leftTradePanel->updateSlotsCallback = std::bind(&CCreaturesSelling::updateSubtitle, this);
  82. }
  83. bool CCreaturesSelling::slotDeletingCheck(const std::shared_ptr<CTradeableItem> & slot)
  84. {
  85. return hero->getStackCount(SlotID(slot->serial)) == 0 ? true : false;
  86. }
  87. void CCreaturesSelling::updateSubtitle()
  88. {
  89. for(auto & heroSlot : leftTradePanel->slots)
  90. heroSlot->subtitle = std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial)));
  91. }
  92. void CCreaturesSelling::updateSlots()
  93. {
  94. leftTradePanel->deleteSlots();
  95. leftTradePanel->updateSlots();
  96. }
  97. CResourcesMarket::CResourcesMarket(EMarketMode marketMode)
  98. {
  99. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  100. rightTradePanel = std::make_shared<ResourcesPanel>([](const std::shared_ptr<CTradeableItem>&) {}, [this, marketMode]()
  101. {
  102. updateSubtitles(marketMode);
  103. });
  104. labels.emplace_back(std::make_shared<CLabel>(400, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  105. }
  106. void CResourcesMarket::updateSubtitles(EMarketMode marketMode)
  107. {
  108. if(hLeft)
  109. for(const auto & slot : rightTradePanel->slots)
  110. {
  111. int h1, h2; //hlp variables for getting offer
  112. market->getOffer(hLeft->id, slot->id, h1, h2, marketMode);
  113. rightTradePanel->updateOffer(*slot, h1, h2);
  114. }
  115. else
  116. rightTradePanel->clearSubtitles();
  117. };