CArtifactsBuying.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/CGMarket.h"
  20. #include "../../../lib/mapObjects/CGTownInstance.h"
  21. CArtifactsBuying::CArtifactsBuying(const IMarket * market, const CGHeroInstance * hero)
  22. : CMarketBase(market, hero, [this](){return CArtifactsBuying::getSelectionParams();})
  23. , CResourcesSelling([this](const std::shared_ptr<CTradeableItem> & heroSlot){CArtifactsBuying::onSlotClickPressed(heroSlot, bidTradePanel);})
  24. {
  25. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  26. std::string title;
  27. if(auto townMarket = dynamic_cast<const CGTownInstance*>(market))
  28. title = (*CGI->townh)[townMarket->getFaction()]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  29. else
  30. title = "Black market"; // find string allTexts!!
  31. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title));
  32. deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("TPMRKB.DEF"),
  33. CGI->generaltexth->zelp[595], [this](){CArtifactsBuying::makeDeal();});
  34. labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  35. // Player's resources
  36. assert(bidTradePanel);
  37. bidTradePanel->moveTo(pos.topLeft() + Point(39, 184));
  38. bidTradePanel->showcaseSlot->image->moveTo(pos.topLeft() + Point(141, 454));
  39. // Artifacts panel
  40. offerTradePanel = std::make_shared<ArtifactsPanel>([this](const std::shared_ptr<CTradeableItem> & newSlot)
  41. {
  42. CArtifactsBuying::onSlotClickPressed(newSlot, offerTradePanel);
  43. }, [this]()
  44. {
  45. CMarketBase::updateSubtitlesForBid(EMarketMode::RESOURCE_ARTIFACT, bidTradePanel->getSelectedItemId());
  46. }, market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT));
  47. offerTradePanel->deleteSlotsCheck = [this](const std::shared_ptr<CTradeableItem> & slot)
  48. {
  49. return vstd::contains(this->market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT), ArtifactID(slot->id)) ? false : true;
  50. };
  51. offerTradePanel->moveTo(pos.topLeft() + Point(328, 181));
  52. CMarketBase::update();
  53. CMarketBase::deselect();
  54. }
  55. void CArtifactsBuying::makeDeal()
  56. {
  57. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_ARTIFACT, GameResID(bidTradePanel->highlightedSlot->id),
  58. ArtifactID(offerTradePanel->highlightedSlot->id), offerQty, hero);
  59. deselect();
  60. }
  61. CMarketBase::SelectionParams CArtifactsBuying::getSelectionParams() const
  62. {
  63. if(bidTradePanel->highlightedSlot && offerTradePanel->highlightedSlot)
  64. return std::make_tuple(
  65. SelectionParamOneSide {std::to_string(deal->isBlocked() ? 0 : bidQty), bidTradePanel->highlightedSlot->id},
  66. SelectionParamOneSide {std::to_string(deal->isBlocked() ? 0 : offerQty), CGI->artifacts()->getByIndex(offerTradePanel->highlightedSlot->id)->getIconIndex()});
  67. else
  68. return std::make_tuple(std::nullopt, std::nullopt);
  69. }
  70. void CArtifactsBuying::highlightingChanged()
  71. {
  72. if(bidTradePanel->highlightedSlot && offerTradePanel->highlightedSlot)
  73. {
  74. market->getOffer(bidTradePanel->highlightedSlot->id, offerTradePanel->highlightedSlot->id, bidQty, offerQty, EMarketMode::RESOURCE_ARTIFACT);
  75. deal->block(LOCPLINT->cb->getResourceAmount(GameResID(bidTradePanel->highlightedSlot->id)) >= bidQty ? false : true);
  76. }
  77. CMarketBase::highlightingChanged();
  78. }