CMarketBase.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * CMarketBase.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 "CMarketBase.h"
  12. #include "../MiscWidgets.h"
  13. #include "../Images.h"
  14. #include "../../gui/CGuiHandler.h"
  15. #include "../../widgets/Buttons.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/CHeroHandler.h"
  23. #include "../../../lib/mapObjects/CGMarket.h"
  24. CMarketBase::CMarketBase(const IMarket * market, const CGHeroInstance * hero)
  25. : market(market)
  26. , hero(hero)
  27. {
  28. }
  29. void CMarketBase::deselect()
  30. {
  31. if(bidTradePanel && bidTradePanel->highlightedSlot)
  32. {
  33. bidTradePanel->highlightedSlot->selectSlot(false);
  34. bidTradePanel->highlightedSlot.reset();
  35. }
  36. if(offerTradePanel && offerTradePanel->highlightedSlot)
  37. {
  38. offerTradePanel->highlightedSlot->selectSlot(false);
  39. offerTradePanel->highlightedSlot.reset();
  40. }
  41. deal->block(true);
  42. bidQty = 0;
  43. offerQty = 0;
  44. updateShowcases();
  45. }
  46. void CMarketBase::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<TradePanelBase> & curPanel)
  47. {
  48. assert(newSlot);
  49. assert(curPanel);
  50. if(newSlot == curPanel->highlightedSlot)
  51. return;
  52. curPanel->onSlotClickPressed(newSlot);
  53. highlightingChanged();
  54. redraw();
  55. }
  56. void CMarketBase::update()
  57. {
  58. if(bidTradePanel)
  59. bidTradePanel->update();
  60. if(offerTradePanel)
  61. offerTradePanel->update();
  62. }
  63. void CMarketBase::updateSubtitlesForBid(EMarketMode marketMode, int bidId)
  64. {
  65. if(bidId == -1)
  66. {
  67. if(offerTradePanel)
  68. offerTradePanel->clearSubtitles();
  69. }
  70. else
  71. {
  72. for(const auto & slot : offerTradePanel->slots)
  73. {
  74. int slotBidQty = 0;
  75. int slotOfferQty = 0;
  76. market->getOffer(bidId, slot->id, slotBidQty, slotOfferQty, marketMode);
  77. offerTradePanel->updateOffer(*slot, slotBidQty, slotOfferQty);
  78. }
  79. }
  80. };
  81. void CMarketBase::updateShowcases()
  82. {
  83. const auto updateSelectedBody = [](const std::shared_ptr<TradePanelBase> & tradePanel, const std::optional<const ShowcaseParams> & params)
  84. {
  85. if(params.has_value())
  86. {
  87. tradePanel->setShowcaseSubtitle(params.value().text);
  88. tradePanel->showcaseSlot->image->enable();
  89. tradePanel->showcaseSlot->image->setFrame(params.value().imageIndex);
  90. }
  91. else
  92. {
  93. tradePanel->showcaseSlot->clear();
  94. }
  95. };
  96. const auto params = getShowcasesParams();
  97. if(bidTradePanel)
  98. updateSelectedBody(bidTradePanel, params.bidParams);
  99. if(offerTradePanel)
  100. updateSelectedBody(offerTradePanel, params.offerParams);
  101. }
  102. void CMarketBase::highlightingChanged()
  103. {
  104. offerTradePanel->update();
  105. updateShowcases();
  106. }
  107. CExperienceAltar::CExperienceAltar()
  108. {
  109. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  110. // Experience needed to reach next level
  111. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  112. // Total experience on the Altar
  113. texts.emplace_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  114. expToLevel = std::make_shared<CLabel>(76, 477, FONT_SMALL, ETextAlignment::CENTER);
  115. expForHero = std::make_shared<CLabel>(76, 545, FONT_SMALL, ETextAlignment::CENTER);
  116. }
  117. void CExperienceAltar::deselect()
  118. {
  119. expForHero->setText(std::to_string(0));
  120. }
  121. void CExperienceAltar::update()
  122. {
  123. expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(hero->exp) + 1) - hero->exp));
  124. }
  125. CCreaturesSelling::CCreaturesSelling()
  126. {
  127. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  128. assert(hero);
  129. CreaturesPanel::slotsData slots;
  130. for(auto slotId = SlotID(0); slotId.num < GameConstants::ARMY_SIZE; slotId++)
  131. {
  132. if(const auto & creature = hero->getCreature(slotId))
  133. slots.emplace_back(std::make_tuple(creature->getId(), slotId, hero->getStackCount(slotId)));
  134. }
  135. bidTradePanel = std::make_shared<CreaturesPanel>(nullptr, slots);
  136. bidTradePanel->updateSlotsCallback = std::bind(&CCreaturesSelling::updateSubtitles, this);
  137. }
  138. bool CCreaturesSelling::slotDeletingCheck(const std::shared_ptr<CTradeableItem> & slot) const
  139. {
  140. return hero->getStackCount(SlotID(slot->serial)) == 0 ? true : false;
  141. }
  142. void CCreaturesSelling::updateSubtitles() const
  143. {
  144. for(const auto & heroSlot : bidTradePanel->slots)
  145. heroSlot->subtitle->setText(std::to_string(this->hero->getStackCount(SlotID(heroSlot->serial))));
  146. }
  147. CResourcesBuying::CResourcesBuying(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  148. const TradePanelBase::UpdateSlotsFunctor & updSlotsCallback)
  149. {
  150. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  151. offerTradePanel = std::make_shared<ResourcesPanel>(clickPressedCallback, updSlotsCallback);
  152. offerTradePanel->moveTo(pos.topLeft() + Point(327, 182));
  153. labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  154. }
  155. CResourcesSelling::CResourcesSelling(const CTradeableItem::ClickPressedFunctor & clickPressedCallback)
  156. {
  157. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  158. bidTradePanel = std::make_shared<ResourcesPanel>(clickPressedCallback, std::bind(&CResourcesSelling::updateSubtitles, this));
  159. labels.emplace_back(std::make_shared<CLabel>(156, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
  160. }
  161. void CResourcesSelling::updateSubtitles() const
  162. {
  163. for(const auto & slot : bidTradePanel->slots)
  164. slot->subtitle->setText(std::to_string(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(slot->serial))));
  165. }
  166. CMarketSlider::CMarketSlider(const CSlider::SliderMovingFunctor & movingCallback)
  167. {
  168. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  169. offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, movingCallback, 0, 0, 0, Orientation::HORIZONTAL);
  170. maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
  171. [this]()
  172. {
  173. offerSlider->scrollToMax();
  174. });
  175. }
  176. void CMarketSlider::deselect()
  177. {
  178. maxAmount->block(true);
  179. offerSlider->scrollTo(0);
  180. offerSlider->block(true);
  181. }
  182. void CMarketSlider::onOfferSliderMoved(int newVal)
  183. {
  184. if(bidTradePanel->highlightedSlot && offerTradePanel->highlightedSlot)
  185. {
  186. offerSlider->scrollTo(newVal);
  187. updateShowcases();
  188. redraw();
  189. }
  190. }
  191. CMarketTraderText::CMarketTraderText(const Point & pos, const EFonts & font, const ColorRGBA & color)
  192. : madeTransaction(false)
  193. {
  194. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  195. traderText = std::make_shared<CTextBox>("", Rect(pos, traderTextDimensions), 0, font, ETextAlignment::CENTER, color);
  196. }
  197. void CMarketTraderText::deselect()
  198. {
  199. highlightingChanged();
  200. }
  201. void CMarketTraderText::makeDeal()
  202. {
  203. madeTransaction = true;
  204. }
  205. void CMarketTraderText::highlightingChanged()
  206. {
  207. traderText->setText(getTraderText());
  208. }