CTradeBase.cpp 5.0 KB

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