CTradeBase.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "../Images.h"
  14. #include "../../gui/CGuiHandler.h"
  15. #include "../../widgets/Buttons.h"
  16. #include "../../widgets/Slider.h"
  17. #include "../../widgets/TextControls.h"
  18. #include "../../CGameInfo.h"
  19. #include "../../CPlayerInterface.h"
  20. #include "../../../CCallback.h"
  21. #include "../../../lib/CGeneralTextHandler.h"
  22. #include "../../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../../lib/CHeroHandler.h"
  24. #include "../../../lib/mapObjects/CGMarket.h"
  25. CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero, const SelectionParamsFunctor & getParamsCallback)
  26. : market(market)
  27. , hero(hero)
  28. , selectionParamsCallback(getParamsCallback)
  29. {
  30. }
  31. void CTradeBase::deselect()
  32. {
  33. if(hLeft)
  34. hLeft->selectSlot(false);
  35. if(hRight)
  36. hRight->selectSlot(false);
  37. hLeft = hRight = nullptr;
  38. deal->block(true);
  39. if(maxAmount)
  40. maxAmount->block(true);
  41. if(offerSlider)
  42. {
  43. offerSlider->scrollTo(0);
  44. offerSlider->block(true);
  45. }
  46. bidQty = 0;
  47. offerQty = 0;
  48. updateSelected();
  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. void CTradeBase::updateSlots()
  60. {
  61. if(bidTradePanel)
  62. bidTradePanel->updateSlots();
  63. if(offerTradePanel)
  64. offerTradePanel->updateSlots();
  65. }
  66. void CTradeBase::updateSubtitles(EMarketMode marketMode)
  67. {
  68. if(hLeft)
  69. for(const auto & slot : offerTradePanel->slots)
  70. {
  71. int bidQty = 0;
  72. int offerQty = 0;
  73. market->getOffer(hLeft->id, slot->id, bidQty, offerQty, marketMode);
  74. offerTradePanel->updateOffer(*slot, bidQty, offerQty);
  75. }
  76. else
  77. offerTradePanel->clearSubtitles();
  78. };
  79. void CTradeBase::updateSelected()
  80. {
  81. const auto updateSelectedBody = [](std::shared_ptr<TradePanelBase> & tradePanel, const std::optional<const SelectionParamOneSide> & params)
  82. {
  83. std::optional<size_t> lImageIndex = std::nullopt;
  84. if(params.has_value())
  85. {
  86. tradePanel->setSelectedSubtitleText(params.value().text);
  87. lImageIndex = params.value().imageIndex;
  88. }
  89. else
  90. {
  91. tradePanel->clearSelectedSubtitleText();
  92. }
  93. tradePanel->setSelectedFrameIndex(lImageIndex);
  94. };
  95. assert(selectionParamsCallback);
  96. const auto params = selectionParamsCallback();
  97. if(bidTradePanel)
  98. updateSelectedBody(bidTradePanel, std::get<0>(params));
  99. if(offerTradePanel)
  100. updateSelectedBody(offerTradePanel, std::get<1>(params));
  101. }
  102. CExperienceAltar::CExperienceAltar()
  103. {
  104. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  105. // Experience needed to reach next level
  106. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  107. // Total experience on the Altar
  108. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  109. expToLevel = std::make_shared<CLabel>(76, 477, FONT_SMALL, ETextAlignment::CENTER);
  110. expForHero = std::make_shared<CLabel>(76, 545, FONT_SMALL, ETextAlignment::CENTER);
  111. }
  112. void CExperienceAltar::updateSlots()
  113. {
  114. CTradeBase::updateSlots();
  115. expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(hero->exp) + 1) - hero->exp));
  116. }
  117. CCreaturesSelling::CCreaturesSelling()
  118. {
  119. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  120. assert(hero);
  121. CreaturesPanel::slotsData slots;
  122. for(auto slotId = SlotID(0); slotId.num < GameConstants::ARMY_SIZE; slotId++)
  123. {
  124. if(const auto & creature = hero->getCreature(slotId))
  125. slots.emplace_back(std::make_tuple(creature->getId(), slotId, hero->getStackCount(slotId)));
  126. }
  127. bidTradePanel = std::make_shared<CreaturesPanel>(nullptr, slots);
  128. bidTradePanel->updateSlotsCallback = std::bind(&CCreaturesSelling::updateSubtitles, this);
  129. }
  130. bool CCreaturesSelling::slotDeletingCheck(const std::shared_ptr<CTradeableItem> & slot)
  131. {
  132. return hero->getStackCount(SlotID(slot->serial)) == 0 ? true : false;
  133. }
  134. void CCreaturesSelling::updateSubtitles()
  135. {
  136. for(auto & heroSlot : bidTradePanel->slots)
  137. heroSlot->subtitle->setText(std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial))));
  138. }
  139. CResourcesBuying::CResourcesBuying(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  140. const TradePanelBase::UpdateSlotsFunctor & updSlotsCallback)
  141. {
  142. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  143. offerTradePanel = std::make_shared<ResourcesPanel>(clickPressedCallback, updSlotsCallback);
  144. offerTradePanel->moveTo(pos.topLeft() + Point(327, 182));
  145. labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  146. }
  147. CResourcesSelling::CResourcesSelling(const CTradeableItem::ClickPressedFunctor & clickPressedCallback)
  148. {
  149. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  150. bidTradePanel = std::make_shared<ResourcesPanel>(clickPressedCallback, std::bind(&CResourcesSelling::updateSubtitles, this));
  151. labels.emplace_back(std::make_shared<CLabel>(156, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
  152. }
  153. void CResourcesSelling::updateSubtitles()
  154. {
  155. for(const auto & slot : bidTradePanel->slots)
  156. slot->subtitle->setText(std::to_string(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(slot->serial))));
  157. }