CTransferResources.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * CTransferResources.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 "CTransferResources.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. CTransferResources::CTransferResources(const IMarket * market, const CGHeroInstance * hero)
  20. : CMarketBase(market, hero)
  21. , CResourcesSelling([this](const std::shared_ptr<CTradeableItem> & heroSlot){CTransferResources::onSlotClickPressed(heroSlot, bidTradePanel);})
  22. , CMarketSlider([this](int newVal){CMarketSlider::onOfferSliderMoved(newVal);})
  23. , CMarketTraderText(Point(28, 48))
  24. {
  25. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  26. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[158]));
  27. labels.emplace_back(std::make_shared<CLabel>(445, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
  28. deal = std::make_shared<CButton>(dealButtonPosWithSlider, AnimationPath::builtin("TPMRKB.DEF"),
  29. CGI->generaltexth->zelp[595], [this](){CTransferResources::makeDeal();});
  30. // Player's resources
  31. assert(bidTradePanel);
  32. bidTradePanel->moveTo(pos.topLeft() + Point(40, 183));
  33. // Players panel
  34. offerTradePanel = std::make_shared<PlayersPanel>([this](const std::shared_ptr<CTradeableItem> & heroSlot)
  35. {
  36. CTransferResources::onSlotClickPressed(heroSlot, offerTradePanel);
  37. });
  38. offerTradePanel->moveTo(pos.topLeft() + Point(333, 84));
  39. CMarketBase::update();
  40. CTransferResources::deselect();
  41. }
  42. void CTransferResources::deselect()
  43. {
  44. CMarketBase::deselect();
  45. CMarketSlider::deselect();
  46. CMarketTraderText::deselect();
  47. }
  48. void CTransferResources::makeDeal()
  49. {
  50. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  51. {
  52. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_PLAYER, GameResID(bidTradePanel->getSelectedItemId()),
  53. PlayerColor(offerTradePanel->getSelectedItemId()), toTrade, hero);
  54. CMarketTraderText::makeDeal();
  55. deselect();
  56. }
  57. }
  58. CMarketBase::MarketShowcasesParams CTransferResources::getShowcasesParams() const
  59. {
  60. if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
  61. return std::make_tuple(
  62. ShowcaseParams {std::to_string(offerSlider->getValue()), bidTradePanel->getSelectedItemId()},
  63. ShowcaseParams {CGI->generaltexth->capColors[offerTradePanel->getSelectedItemId()], offerTradePanel->getSelectedItemId()});
  64. else
  65. return std::make_tuple(std::nullopt, std::nullopt);
  66. }
  67. void CTransferResources::highlightingChanged()
  68. {
  69. if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
  70. {
  71. offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(bidTradePanel->getSelectedItemId())));
  72. offerSlider->scrollTo(0);
  73. offerSlider->block(false);
  74. maxAmount->block(false);
  75. deal->block(false);
  76. }
  77. CMarketBase::highlightingChanged();
  78. CMarketTraderText::highlightingChanged();
  79. }
  80. std::string CTransferResources::getTraderText()
  81. {
  82. if(bidTradePanel->isHighlighted() && offerTradePanel->isHighlighted())
  83. {
  84. return boost::str(boost::format(
  85. CGI->generaltexth->allTexts[165]) %
  86. CGI->generaltexth->restypes[bidTradePanel->getSelectedItemId()] %
  87. CGI->generaltexth->capColors[offerTradePanel->getSelectedItemId()]);
  88. }
  89. else
  90. {
  91. return madeTransaction ? CGI->generaltexth->allTexts[166] : CGI->generaltexth->allTexts[167];
  92. }
  93. }