CArtifactsSelling.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "../../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/CArtifactInstance.h"
  19. #include "../../../lib/CGeneralTextHandler.h"
  20. #include "../../../lib/mapObjects/CGHeroInstance.h"
  21. #include "../../../lib/mapObjects/CGMarket.h"
  22. #include "../../../lib/mapObjects/CGTownInstance.h"
  23. CArtifactsSelling::CArtifactsSelling(const IMarket * market, const CGHeroInstance * hero)
  24. : CMarketBase(market, hero, [this](){return CArtifactsSelling::getSelectionParams();})
  25. , CResourcesBuying(
  26. [this](const std::shared_ptr<CTradeableItem> & resSlot){CArtifactsSelling::onSlotClickPressed(resSlot, offerTradePanel);},
  27. [this](){CMarketBase::updateSubtitlesForBid(EMarketMode::ARTIFACT_RESOURCE, this->hero->getArt(selectedHeroSlot)->getTypeId().num);})
  28. {
  29. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  30. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW,
  31. (*CGI->townh)[dynamic_cast<const CGTownInstance*>(market)->getFaction()]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated()));
  32. labels.push_back(std::make_shared<CLabel>(155, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->getNameTranslated())));
  33. deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("TPMRKB.DEF"),
  34. CGI->generaltexth->zelp[595], [this](){CArtifactsSelling::makeDeal();});
  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->selectArtCallback = [this](const CArtPlace * artPlace)
  45. {
  46. assert(artPlace);
  47. selectedHeroSlot = artPlace->slot;
  48. CArtifactsSelling::highlightingChanged();
  49. CIntObject::redraw();
  50. };
  51. CArtifactsSelling::updateShowcases();
  52. CArtifactsSelling::deselect();
  53. }
  54. void CArtifactsSelling::deselect()
  55. {
  56. CMarketBase::deselect();
  57. selectedHeroSlot = ArtifactPosition::PRE_FIRST;
  58. heroArts->unmarkSlots();
  59. bidSelectedSlot->clear();
  60. }
  61. void CArtifactsSelling::makeDeal()
  62. {
  63. const auto art = hero->getArt(selectedHeroSlot);
  64. assert(art);
  65. LOCPLINT->cb->trade(market, EMarketMode::ARTIFACT_RESOURCE, art->getId(), GameResID(offerTradePanel->getSelectedItemId()), offerQty, hero);
  66. }
  67. void CArtifactsSelling::updateShowcases()
  68. {
  69. const auto art = hero->getArt(selectedHeroSlot);
  70. if(art && offerTradePanel->highlightedSlot)
  71. {
  72. bidSelectedSlot->image->enable();
  73. bidSelectedSlot->setID(art->getTypeId().num);
  74. bidSelectedSlot->image->setFrame(CGI->artifacts()->getByIndex(art->getTypeId())->getIconIndex());
  75. bidSelectedSlot->subtitle->setText(std::to_string(bidQty));
  76. }
  77. else
  78. {
  79. bidSelectedSlot->clear();
  80. }
  81. CMarketBase::updateShowcases();
  82. }
  83. void CArtifactsSelling::update()
  84. {
  85. CMarketBase::update();
  86. if(selectedHeroSlot != ArtifactPosition::PRE_FIRST)
  87. {
  88. if(selectedHeroSlot.num >= ArtifactPosition::BACKPACK_START + hero->artifactsInBackpack.size())
  89. {
  90. selectedHeroSlot = ArtifactPosition::BACKPACK_START;
  91. deselect();
  92. }
  93. if(hero->getArt(selectedHeroSlot) == nullptr)
  94. deselect();
  95. }
  96. }
  97. std::shared_ptr<CArtifactsOfHeroMarket> CArtifactsSelling::getAOHset() const
  98. {
  99. return heroArts;
  100. }
  101. CMarketBase::SelectionParams CArtifactsSelling::getSelectionParams() const
  102. {
  103. if(hero->getArt(selectedHeroSlot) && offerTradePanel->highlightedSlot)
  104. return std::make_tuple(
  105. std::nullopt,
  106. SelectionParamOneSide {std::to_string(offerQty), offerTradePanel->getSelectedItemId()}
  107. );
  108. else
  109. return std::make_tuple(std::nullopt, std::nullopt);
  110. }
  111. void CArtifactsSelling::highlightingChanged()
  112. {
  113. const auto art = hero->getArt(selectedHeroSlot);
  114. if(art && offerTradePanel->highlightedSlot)
  115. {
  116. market->getOffer(art->getTypeId(), offerTradePanel->getSelectedItemId(), bidQty, offerQty, EMarketMode::ARTIFACT_RESOURCE);
  117. deal->block(false);
  118. }
  119. CMarketBase::highlightingChanged();
  120. }