CArtifactsBuying.cpp 4.6 KB

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