CMarketBase.cpp 6.9 KB

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