CArtifactsBuying.cpp 4.6 KB

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