CArtifactsSelling.cpp 5.9 KB

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