CMarketResources.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * CMarketResources.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 "CMarketResources.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. #include "../../../lib/mapObjects/CGMarket.h"
  21. CMarketResources::CMarketResources(const IMarket * market, const CGHeroInstance * hero)
  22. : CTradeBase(market, hero)
  23. , CResourcesBuying([this](){CMarketResources::updateSubtitles();})
  24. , CMarketMisc([this](){return CMarketResources::getSelectionParams();})
  25. {
  26. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  27. labels.emplace_back(std::make_shared<CLabel>(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[158]));
  28. deal = std::make_shared<CButton>(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"),
  29. CGI->generaltexth->zelp[595], [this]() {CMarketResources::makeDeal(); });
  30. maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
  31. [this]() {offerSlider->scrollToMax(); });
  32. offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, [this](int newVal)
  33. {
  34. CMarketResources::onOfferSliderMoved(newVal);
  35. }, 0, 0, 0, Orientation::HORIZONTAL);
  36. // Player's resources
  37. assert(bidTradePanel);
  38. std::for_each(bidTradePanel->slots.cbegin(), bidTradePanel->slots.cend(), [this](auto & slot)
  39. {
  40. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
  41. {
  42. CMarketResources::onSlotClickPressed(heroSlot, hLeft);
  43. };
  44. });
  45. bidTradePanel->moveTo(pos.topLeft() + Point(39, 181));
  46. // Market resources panel
  47. assert(offerTradePanel);
  48. std::for_each(offerTradePanel->slots.cbegin(), offerTradePanel->slots.cend(), [this](auto & slot)
  49. {
  50. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & resSlot)
  51. {
  52. CMarketResources::onSlotClickPressed(resSlot, hRight);
  53. };
  54. });
  55. CResourcesSelling::updateSlots();
  56. CMarketMisc::deselect();
  57. }
  58. void CMarketResources::makeDeal()
  59. {
  60. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  61. {
  62. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_RESOURCE, GameResID(hLeft->id), GameResID(hRight->id), bidQty * toTrade, hero);
  63. deselect();
  64. }
  65. }
  66. CMarketMisc::SelectionParams CMarketResources::getSelectionParams()
  67. {
  68. if(hLeft && hRight && hLeft->id != hRight->id)
  69. return std::make_tuple(
  70. SelectionParamOneSide {std::to_string(bidQty * offerSlider->getValue()), hLeft->id},
  71. SelectionParamOneSide {std::to_string(offerQty * offerSlider->getValue()), hRight->id});
  72. else
  73. return std::make_tuple(std::nullopt, std::nullopt);
  74. }
  75. void CMarketResources::onOfferSliderMoved(int newVal)
  76. {
  77. if(hLeft && hRight)
  78. {
  79. offerSlider->scrollTo(newVal);
  80. updateSelected();
  81. redraw();
  82. }
  83. }
  84. void CMarketResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  85. {
  86. if(newSlot == hCurSlot)
  87. return;
  88. CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
  89. if(hLeft)
  90. {
  91. if(hRight)
  92. {
  93. market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::RESOURCE_RESOURCE);
  94. offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id)) / bidQty);
  95. offerSlider->scrollTo(0);
  96. const bool isControlsBlocked = hLeft->id != hRight->id ? false : true;
  97. offerSlider->block(isControlsBlocked);
  98. maxAmount->block(isControlsBlocked);
  99. deal->block(isControlsBlocked);
  100. }
  101. updateSelected();
  102. offerTradePanel->updateSlots();
  103. }
  104. redraw();
  105. }
  106. void CMarketResources::updateSubtitles()
  107. {
  108. CResourcesBuying::updateSubtitles(EMarketMode::RESOURCE_RESOURCE);
  109. if(hLeft)
  110. offerTradePanel->slots[hLeft->serial]->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  111. }