CArtifactsSelling.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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->selectArtCallback = [this](const CArtPlace * artPlace)
  49. {
  50. assert(artPlace);
  51. selectedHeroSlot = artPlace->slot;
  52. CArtifactsSelling::highlightingChanged();
  53. CIntObject::redraw();
  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, EMarketMode::ARTIFACT_RESOURCE, art->getId(), GameResID(offerTradePanel->getSelectedItemId()), offerQty, hero);
  71. CMarketTraderText::makeDeal();
  72. }
  73. void CArtifactsSelling::updateShowcases()
  74. {
  75. const auto art = hero->getArt(selectedHeroSlot);
  76. if(art && offerTradePanel->isHighlighted())
  77. {
  78. bidSelectedSlot->image->enable();
  79. bidSelectedSlot->setID(art->getTypeId().num);
  80. bidSelectedSlot->image->setFrame(CGI->artifacts()->getByIndex(art->getTypeId())->getIconIndex());
  81. bidSelectedSlot->subtitle->setText(std::to_string(bidQty));
  82. }
  83. else
  84. {
  85. bidSelectedSlot->clear();
  86. }
  87. CMarketBase::updateShowcases();
  88. }
  89. void CArtifactsSelling::update()
  90. {
  91. CMarketBase::update();
  92. if(selectedHeroSlot != ArtifactPosition::PRE_FIRST)
  93. {
  94. if(hero->getArt(selectedHeroSlot) == nullptr)
  95. {
  96. deselect();
  97. selectedHeroSlot = ArtifactPosition::PRE_FIRST;
  98. }
  99. else
  100. {
  101. heroArts->getArtPlace(selectedHeroSlot)->selectSlot(true);
  102. }
  103. highlightingChanged();
  104. }
  105. }
  106. std::shared_ptr<CArtifactsOfHeroMarket> CArtifactsSelling::getAOHset() const
  107. {
  108. return heroArts;
  109. }
  110. CMarketBase::MarketShowcasesParams CArtifactsSelling::getShowcasesParams() const
  111. {
  112. if(hero->getArt(selectedHeroSlot) && offerTradePanel->isHighlighted())
  113. return MarketShowcasesParams
  114. {
  115. std::nullopt,
  116. ShowcaseParams {std::to_string(offerQty), offerTradePanel->getSelectedItemId()}
  117. };
  118. else
  119. return MarketShowcasesParams {std::nullopt, std::nullopt};
  120. }
  121. void CArtifactsSelling::updateSubtitles()
  122. {
  123. const auto art = this->hero->getArt(selectedHeroSlot);
  124. const int bidId = art == nullptr ? -1 : art->getTypeId().num;
  125. CMarketBase::updateSubtitlesForBid(EMarketMode::ARTIFACT_RESOURCE, bidId);
  126. }
  127. void CArtifactsSelling::highlightingChanged()
  128. {
  129. const auto art = hero->getArt(selectedHeroSlot);
  130. if(art && offerTradePanel->isHighlighted())
  131. {
  132. market->getOffer(art->getTypeId(), offerTradePanel->getSelectedItemId(), bidQty, offerQty, EMarketMode::ARTIFACT_RESOURCE);
  133. deal->block(false);
  134. }
  135. CMarketBase::highlightingChanged();
  136. CMarketTraderText::highlightingChanged();
  137. }
  138. std::string CArtifactsSelling::getTraderText()
  139. {
  140. const auto art = hero->getArt(selectedHeroSlot);
  141. if(art && offerTradePanel->isHighlighted())
  142. {
  143. MetaString message = MetaString::createFromTextID("core.genrltxt.268");
  144. message.replaceNumber(offerQty);
  145. message.replaceRawString(offerQty == 1 ? CGI->generaltexth->allTexts[161] : CGI->generaltexth->allTexts[160]);
  146. message.replaceName(GameResID(offerTradePanel->getSelectedItemId()));
  147. message.replaceName(art->getTypeId());
  148. return message.toString();
  149. }
  150. else
  151. {
  152. return madeTransaction ? CGI->generaltexth->allTexts[162] : CGI->generaltexth->allTexts[163];
  153. }
  154. }