CArtifactsSelling.cpp 5.7 KB

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