CTransferResources.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, [this](){return CTransferResources::getSelectionParams();})
  21. , CResourcesSelling([this](const std::shared_ptr<CTradeableItem> & heroSlot){CTransferResources::onSlotClickPressed(heroSlot, hLeft);})
  22. , CMarketSlider([this](int newVal){CMarketSlider::onOfferSliderMoved(newVal);})
  23. {
  24. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  25. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, 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>(dealButtonPosWithSlider, AnimationPath::builtin("TPMRKB.DEF"),
  28. CGI->generaltexth->zelp[595], [this]() {CTransferResources::makeDeal();});
  29. // Player's resources
  30. assert(bidTradePanel);
  31. bidTradePanel->moveTo(pos.topLeft() + Point(40, 183));
  32. // Players panel
  33. offerTradePanel = std::make_shared<PlayersPanel>([this](const std::shared_ptr<CTradeableItem> & heroSlot)
  34. {
  35. CTransferResources::onSlotClickPressed(heroSlot, hRight);
  36. });
  37. offerTradePanel->moveTo(pos.topLeft() + Point(333, 84));
  38. CMarketBase::update();
  39. CTransferResources::deselect();
  40. }
  41. void CTransferResources::deselect()
  42. {
  43. CMarketBase::deselect();
  44. CMarketSlider::deselect();
  45. }
  46. void CTransferResources::makeDeal()
  47. {
  48. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  49. {
  50. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_PLAYER, GameResID(hLeft->id), PlayerColor(hRight->id), toTrade, hero);
  51. deselect();
  52. }
  53. }
  54. CMarketBase::SelectionParams CTransferResources::getSelectionParams() const
  55. {
  56. if(hLeft && hRight)
  57. return std::make_tuple(
  58. SelectionParamOneSide {std::to_string(offerSlider->getValue()), hLeft->id},
  59. SelectionParamOneSide {CGI->generaltexth->capColors[hRight->id], hRight->id});
  60. else
  61. return std::make_tuple(std::nullopt, std::nullopt);
  62. }
  63. void CTransferResources::highlightingChanged()
  64. {
  65. if(hLeft)
  66. {
  67. if(hRight)
  68. {
  69. offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id)));
  70. offerSlider->scrollTo(0);
  71. offerSlider->block(false);
  72. maxAmount->block(false);
  73. deal->block(false);
  74. }
  75. offerTradePanel->update();
  76. }
  77. updateSelected();
  78. }
  79. void CTransferResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  80. {
  81. assert(newSlot);
  82. if(newSlot == hCurSlot)
  83. return;
  84. CMarketBase::onSlotClickPressed(newSlot, hCurSlot);
  85. highlightingChanged();
  86. redraw();
  87. }