CMarketResources.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/CGHeroInstance.h"
  21. #include "../../../lib/mapObjects/CGMarket.h"
  22. CMarketResources::CMarketResources(const IMarket * market, const CGHeroInstance * hero)
  23. : CTradeBase(market, hero)
  24. , CResourcesBuying([this](){CMarketResources::updateSubtitles();})
  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(leftTradePanel);
  38. std::for_each(leftTradePanel->slots.cbegin(), leftTradePanel->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. leftTradePanel->moveTo(pos.topLeft() + Point(39, 181));
  46. // Market resources panel
  47. assert(rightTradePanel);
  48. rightTradePanel->moveTo(pos.topLeft() + Point(327, 181));
  49. std::for_each(rightTradePanel->slots.cbegin(), rightTradePanel->slots.cend(), [this](auto & slot)
  50. {
  51. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & resSlot)
  52. {
  53. CMarketResources::onSlotClickPressed(resSlot, hRight);
  54. };
  55. });
  56. CResourcesSelling::updateSlots();
  57. CMarketResources::deselect();
  58. }
  59. void CMarketResources::makeDeal()
  60. {
  61. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  62. {
  63. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_RESOURCE, GameResID(hLeft->id), GameResID(hRight->id), bidQty * toTrade, hero);
  64. deselect();
  65. }
  66. }
  67. void CMarketResources::deselect()
  68. {
  69. CResourcesBuying::deselect();
  70. updateSelected();
  71. }
  72. void CMarketResources::updateSelected()
  73. {
  74. std::optional<size_t> lImageIndex = std::nullopt;
  75. std::optional<size_t> rImageIndex = std::nullopt;
  76. if(hLeft && hRight && hLeft->id != hRight->id)
  77. {
  78. leftTradePanel->selectedSubtitle->setText(std::to_string(bidQty * offerSlider->getValue()));
  79. rightTradePanel->selectedSubtitle->setText(std::to_string(offerQty * offerSlider->getValue()));
  80. lImageIndex = hLeft->id;
  81. rImageIndex = hRight->id;
  82. }
  83. else
  84. {
  85. leftTradePanel->selectedSubtitle->setText("");
  86. rightTradePanel->selectedSubtitle->setText("");
  87. }
  88. leftTradePanel->setSelectedFrameIndex(lImageIndex);
  89. rightTradePanel->setSelectedFrameIndex(rImageIndex);
  90. }
  91. void CMarketResources::onOfferSliderMoved(int newVal)
  92. {
  93. if(hLeft && hRight)
  94. {
  95. offerSlider->scrollTo(newVal);
  96. updateSelected();
  97. redraw();
  98. }
  99. }
  100. void CMarketResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  101. {
  102. if(newSlot == hCurSlot)
  103. return;
  104. CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
  105. if(hLeft)
  106. {
  107. if(hRight)
  108. {
  109. market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::RESOURCE_RESOURCE);
  110. offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id)) / bidQty);
  111. offerSlider->scrollTo(0);
  112. const bool isControlsBlocked = hLeft->id != hRight->id ? false : true;
  113. offerSlider->block(isControlsBlocked);
  114. maxAmount->block(isControlsBlocked);
  115. deal->block(isControlsBlocked);
  116. }
  117. updateSelected();
  118. rightTradePanel->updateSlots();
  119. }
  120. redraw();
  121. }
  122. void CMarketResources::updateSubtitles()
  123. {
  124. CResourcesBuying::updateSubtitles(EMarketMode::RESOURCE_RESOURCE);
  125. if(hLeft)
  126. rightTradePanel->slots[hLeft->serial]->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  127. }