CArtifactsSelling.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * CArtifactsSelling.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 "CArtifactsSelling.h"
  12. #include "../../gui/CGuiHandler.h"
  13. #include "../../gui/Shortcut.h"
  14. #include "../../widgets/Buttons.h"
  15. #include "../../widgets/TextControls.h"
  16. #include "../../CGameInfo.h"
  17. #include "../../CPlayerInterface.h"
  18. #include "../../../CCallback.h"
  19. #include "../../../lib/CArtifactInstance.h"
  20. #include "../../../lib/mapObjects/CGHeroInstance.h"
  21. #include "../../../lib/mapObjects/IMarket.h"
  22. #include "../../../lib/texts/CGeneralTextHandler.h"
  23. CArtifactsSelling::CArtifactsSelling(const IMarket * market, const CGHeroInstance * hero, const std::string & title)
  24. : CMarketBase(market, hero)
  25. , CResourcesBuying(
  26. [this](const std::shared_ptr<CTradeableItem> & resSlot){CArtifactsSelling::onSlotClickPressed(resSlot, offerTradePanel);},
  27. [this](){CArtifactsSelling::updateSubtitles();})
  28. {
  29. OBJECT_CONSTRUCTION;
  30. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title));
  31. labels.push_back(std::make_shared<CLabel>(155, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->getNameTranslated())));
  32. deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("TPMRKB.DEF"),
  33. CGI->generaltexth->zelp[595], [this](){CArtifactsSelling::makeDeal();}, EShortcut::MARKET_DEAL);
  34. bidSelectedSlot = std::make_shared<CTradeableItem>(Rect(Point(123, 470), Point(69, 66)), EType::ARTIFACT_TYPE, 0, 0);
  35. // Market resources panel
  36. assert(offerTradePanel);
  37. offerTradePanel->moveTo(pos.topLeft() + Point(326, 184));
  38. offerTradePanel->showcaseSlot->moveTo(pos.topLeft() + Point(409, 473));
  39. offerTradePanel->showcaseSlot->subtitle->moveBy(Point(0, 1));
  40. // Hero's artifacts
  41. heroArts = std::make_shared<CArtifactsOfHeroMarket>(Point(-361, 46), offerTradePanel->selectionWidth);
  42. heroArts->setHero(hero);
  43. heroArts->onSelectArtCallback = [this](const CArtPlace * artPlace)
  44. {
  45. assert(artPlace);
  46. selectedHeroSlot = artPlace->slot;
  47. CArtifactsSelling::highlightingChanged();
  48. CIntObject::redraw();
  49. };
  50. heroArts->onClickNotTradableCallback = []()
  51. {
  52. // This item can't be traded
  53. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
  54. };
  55. CArtifactsSelling::updateShowcases();
  56. CArtifactsSelling::deselect();
  57. }
  58. void CArtifactsSelling::deselect()
  59. {
  60. CMarketBase::deselect();
  61. CMarketTraderText::deselect();
  62. selectedHeroSlot = ArtifactPosition::PRE_FIRST;
  63. heroArts->unmarkSlots();
  64. bidSelectedSlot->clear();
  65. }
  66. void CArtifactsSelling::makeDeal()
  67. {
  68. const auto art = hero->getArt(selectedHeroSlot);
  69. assert(art);
  70. LOCPLINT->cb->trade(market->getObjInstanceID(), EMarketMode::ARTIFACT_RESOURCE, art->getId(),
  71. GameResID(offerTradePanel->getSelectedItemId()), offerQty, hero);
  72. CMarketTraderText::makeDeal();
  73. }
  74. void CArtifactsSelling::updateShowcases()
  75. {
  76. const auto art = hero->getArt(selectedHeroSlot);
  77. if(art && offerTradePanel->isHighlighted())
  78. {
  79. bidSelectedSlot->image->enable();
  80. bidSelectedSlot->setID(art->getTypeId().num);
  81. bidSelectedSlot->image->setFrame(CGI->artifacts()->getByIndex(art->getTypeId())->getIconIndex());
  82. bidSelectedSlot->subtitle->setText(std::to_string(bidQty));
  83. }
  84. else
  85. {
  86. bidSelectedSlot->clear();
  87. }
  88. CMarketBase::updateShowcases();
  89. }
  90. void CArtifactsSelling::update()
  91. {
  92. CMarketBase::update();
  93. if(selectedHeroSlot != ArtifactPosition::PRE_FIRST)
  94. {
  95. if(hero->getArt(selectedHeroSlot) == nullptr)
  96. {
  97. deselect();
  98. selectedHeroSlot = ArtifactPosition::PRE_FIRST;
  99. }
  100. else
  101. {
  102. heroArts->getArtPlace(selectedHeroSlot)->selectSlot(true);
  103. }
  104. highlightingChanged();
  105. }
  106. }
  107. std::shared_ptr<CArtifactsOfHeroMarket> CArtifactsSelling::getAOHset() const
  108. {
  109. return heroArts;
  110. }
  111. CMarketBase::MarketShowcasesParams CArtifactsSelling::getShowcasesParams() const
  112. {
  113. if(hero->getArt(selectedHeroSlot) && offerTradePanel->isHighlighted())
  114. return MarketShowcasesParams
  115. {
  116. std::nullopt,
  117. ShowcaseParams {std::to_string(offerQty), offerTradePanel->getSelectedItemId()}
  118. };
  119. else
  120. return MarketShowcasesParams {std::nullopt, std::nullopt};
  121. }
  122. void CArtifactsSelling::updateSubtitles()
  123. {
  124. const auto art = this->hero->getArt(selectedHeroSlot);
  125. const int bidId = art == nullptr ? -1 : art->getTypeId().num;
  126. CMarketBase::updateSubtitlesForBid(EMarketMode::ARTIFACT_RESOURCE, bidId);
  127. }
  128. void CArtifactsSelling::highlightingChanged()
  129. {
  130. const auto art = hero->getArt(selectedHeroSlot);
  131. if(art && offerTradePanel->isHighlighted())
  132. {
  133. market->getOffer(art->getTypeId(), offerTradePanel->getSelectedItemId(), bidQty, offerQty, EMarketMode::ARTIFACT_RESOURCE);
  134. deal->block(!LOCPLINT->makingTurn);
  135. }
  136. CMarketBase::highlightingChanged();
  137. CMarketTraderText::highlightingChanged();
  138. }
  139. std::string CArtifactsSelling::getTraderText()
  140. {
  141. const auto art = hero->getArt(selectedHeroSlot);
  142. if(art && offerTradePanel->isHighlighted())
  143. {
  144. MetaString message = MetaString::createFromTextID("core.genrltxt.268");
  145. message.replaceNumber(offerQty);
  146. message.replaceRawString(offerQty == 1 ? CGI->generaltexth->allTexts[161] : CGI->generaltexth->allTexts[160]);
  147. message.replaceName(GameResID(offerTradePanel->getSelectedItemId()));
  148. message.replaceName(art->getTypeId());
  149. return message.toString();
  150. }
  151. else
  152. {
  153. return madeTransaction ? CGI->generaltexth->allTexts[162] : CGI->generaltexth->allTexts[163];
  154. }
  155. }