CMarketBase.cpp 7.1 KB

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