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