CArtifactsBuying.cpp 4.4 KB

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