CArtifactsBuying.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * CArtifactsBuying.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 "CArtifactsBuying.h"
  12. #include "../../gui/Shortcut.h"
  13. #include "../../widgets/Buttons.h"
  14. #include "../../widgets/TextControls.h"
  15. #include "../../CPlayerInterface.h"
  16. #include "../../GameInstance.h"
  17. #include "../../../CCallback.h"
  18. #include "../../../lib/mapObjects/CGHeroInstance.h"
  19. #include "../../../lib/mapObjects/IMarket.h"
  20. #include "../../../lib/texts/CGeneralTextHandler.h"
  21. CArtifactsBuying::CArtifactsBuying(const IMarket * market, const CGHeroInstance * hero, const std::string & title)
  22. : CMarketBase(market, hero)
  23. , CResourcesSelling([this](const std::shared_ptr<CTradeableItem> & heroSlot){CArtifactsBuying::onSlotClickPressed(heroSlot, bidTradePanel);})
  24. {
  25. OBJECT_CONSTRUCTION;
  26. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title));
  27. deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("TPMRKB.DEF"),
  28. VLC->generaltexth->zelp[595], [this](){CArtifactsBuying::makeDeal();}, EShortcut::MARKET_DEAL);
  29. labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, VLC->generaltexth->allTexts[168]));
  30. // Player's resources
  31. assert(bidTradePanel);
  32. bidTradePanel->moveTo(pos.topLeft() + Point(39, 184));
  33. bidTradePanel->showcaseSlot->image->moveTo(pos.topLeft() + Point(141, 454));
  34. // Artifacts panel
  35. offerTradePanel = std::make_shared<ArtifactsPanel>([this](const std::shared_ptr<CTradeableItem> & newSlot)
  36. {
  37. CArtifactsBuying::onSlotClickPressed(newSlot, offerTradePanel);
  38. }, [this]()
  39. {
  40. CMarketBase::updateSubtitlesForBid(EMarketMode::RESOURCE_ARTIFACT, bidTradePanel->getHighlightedItemId());
  41. }, market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT));
  42. offerTradePanel->deleteSlotsCheck = [this](const std::shared_ptr<CTradeableItem> & slot)
  43. {
  44. return vstd::contains(this->market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT), ArtifactID(slot->id)) ? false : true;
  45. };
  46. offerTradePanel->moveTo(pos.topLeft() + Point(328, 181));
  47. CMarketBase::update();
  48. CArtifactsBuying::deselect();
  49. }
  50. void CArtifactsBuying::deselect()
  51. {
  52. CMarketBase::deselect();
  53. CMarketTraderText::deselect();
  54. }
  55. void CArtifactsBuying::makeDeal()
  56. {
  57. if(ArtifactID(offerTradePanel->getHighlightedItemId()).toArtifact()->canBePutAt(hero))
  58. {
  59. GAME->interface()->cb->trade(market->getObjInstanceID(), EMarketMode::RESOURCE_ARTIFACT, GameResID(bidTradePanel->getHighlightedItemId()),
  60. ArtifactID(offerTradePanel->getHighlightedItemId()), offerQty, hero);
  61. CMarketTraderText::makeDeal();
  62. deselect();
  63. }
  64. else
  65. {
  66. GAME->interface()->showInfoDialog(VLC->generaltexth->translate("core.genrltxt.326"));
  67. }
  68. }
  69. CMarketBase::MarketShowcasesParams CArtifactsBuying::getShowcasesParams() const
  70. {
  71. if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
  72. return MarketShowcasesParams
  73. {
  74. ShowcaseParams {std::to_string(deal->isBlocked() ? 0 : bidQty), bidTradePanel->getHighlightedItemId()},
  75. ShowcaseParams {std::to_string(deal->isBlocked() ? 0 : offerQty), VLC->artifacts()->getByIndex(offerTradePanel->getHighlightedItemId())->getIconIndex()}
  76. };
  77. else
  78. return MarketShowcasesParams {std::nullopt, std::nullopt};
  79. }
  80. void CArtifactsBuying::highlightingChanged()
  81. {
  82. if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
  83. {
  84. market->getOffer(bidTradePanel->getHighlightedItemId(), offerTradePanel->getHighlightedItemId(), bidQty, offerQty, EMarketMode::RESOURCE_ARTIFACT);
  85. deal->block(GAME->interface()->cb->getResourceAmount(GameResID(bidTradePanel->getHighlightedItemId())) < bidQty || !GAME->interface()->makingTurn);
  86. }
  87. CMarketBase::highlightingChanged();
  88. CMarketTraderText::highlightingChanged();
  89. }
  90. std::string CArtifactsBuying::getTraderText()
  91. {
  92. if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
  93. {
  94. MetaString message = MetaString::createFromTextID("core.genrltxt.267");
  95. message.replaceName(ArtifactID(offerTradePanel->getHighlightedItemId()));
  96. message.replaceNumber(bidQty);
  97. message.replaceTextID(bidQty == 1 ? "core.genrltxt.161" : "core.genrltxt.160");
  98. message.replaceName(GameResID(bidTradePanel->getHighlightedItemId()));
  99. return message.toString();
  100. }
  101. else
  102. {
  103. return madeTransaction ? VLC->generaltexth->allTexts[162] : VLC->generaltexth->allTexts[163];
  104. }
  105. }