CTransferResources.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Slider.h"
  15. #include "../../widgets/TextControls.h"
  16. #include "../../CGameInfo.h"
  17. #include "../../CPlayerInterface.h"
  18. #include "../../../CCallback.h"
  19. #include "../../../lib/CGeneralTextHandler.h"
  20. CTransferResources::CTransferResources(const IMarket * market, const CGHeroInstance * hero)
  21. : CTradeBase(market, hero)
  22. , CMarketMisc([this](){return CTransferResources::getSelectionParams();})
  23. {
  24. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  25. labels.emplace_back(std::make_shared<CLabel>(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[158]));
  26. labels.emplace_back(std::make_shared<CLabel>(445, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
  27. deal = std::make_shared<CButton>(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"),
  28. CGI->generaltexth->zelp[595], [this]() {CTransferResources::makeDeal();});
  29. maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
  30. [this]() {offerSlider->scrollToMax();});
  31. offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, [this](int newVal)
  32. {
  33. CTransferResources::onOfferSliderMoved(newVal);
  34. }, 0, 0, 0, Orientation::HORIZONTAL);
  35. // Player's resources
  36. assert(bidTradePanel);
  37. std::for_each(bidTradePanel->slots.cbegin(), bidTradePanel->slots.cend(), [this](auto & slot)
  38. {
  39. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
  40. {
  41. CTransferResources::onSlotClickPressed(heroSlot, hLeft);
  42. };
  43. });
  44. bidTradePanel->moveTo(pos.topLeft() + Point(40, 182));
  45. // Players panel
  46. offerTradePanel = std::make_shared<PlayersPanel>([this](const std::shared_ptr<CTradeableItem> & heroSlot)
  47. {
  48. CTransferResources::onSlotClickPressed(heroSlot, hRight);
  49. });
  50. offerTradePanel->moveTo(pos.topLeft() + Point(333, 83));
  51. CResourcesSelling::updateSlots();
  52. CTransferResources::deselect();
  53. }
  54. void CTransferResources::makeDeal()
  55. {
  56. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  57. {
  58. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_PLAYER, GameResID(hLeft->id), PlayerColor(hRight->id), toTrade, hero);
  59. deselect();
  60. }
  61. }
  62. CMarketMisc::SelectionParams CTransferResources::getSelectionParams()
  63. {
  64. if(hLeft && hRight)
  65. return std::make_tuple(
  66. SelectionParamOneSide {std::to_string(offerSlider->getValue()), hLeft->id},
  67. SelectionParamOneSide {CGI->generaltexth->capColors[hRight->id], hRight->id});
  68. else
  69. return std::make_tuple(std::nullopt, std::nullopt);
  70. }
  71. void CTransferResources::onOfferSliderMoved(int newVal)
  72. {
  73. if(hLeft && hRight)
  74. {
  75. offerSlider->scrollTo(newVal);
  76. updateSelected();
  77. redraw();
  78. }
  79. }
  80. void CTransferResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  81. {
  82. if(newSlot == hCurSlot)
  83. return;
  84. CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
  85. if(hLeft)
  86. {
  87. if(hRight)
  88. {
  89. offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id)));
  90. offerSlider->scrollTo(0);
  91. offerSlider->block(false);
  92. maxAmount->block(false);
  93. deal->block(false);
  94. }
  95. updateSelected();
  96. offerTradePanel->updateSlots();
  97. }
  98. redraw();
  99. }