CMarketBase.cpp 6.8 KB

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