CFreelancerGuild.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CFreelancerGuild.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 "CFreelancerGuild.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. #include "../../../lib/mapObjects/CGTownInstance.h"
  23. CFreelancerGuild::CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero)
  24. : CTradeBase(market, hero)
  25. , CResourcesPurchasing([this](){CResourcesPurchasing::updateSubtitles(EMarketMode::CREATURE_RESOURCE);})
  26. {
  27. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  28. labels.emplace_back(std::make_shared<CLabel>(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW,
  29. (*CGI->townh)[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->getNameTranslated()));
  30. labels.emplace_back(std::make_shared<CLabel>(155, 103, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  31. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  32. deal = std::make_shared<CButton>(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"),
  33. CGI->generaltexth->zelp[595], [this]() {CFreelancerGuild::makeDeal();});
  34. maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
  35. [this]() {offerSlider->scrollToMax();});
  36. offerSlider = std::make_shared<CSlider>(Point(232, 489), 137, [this](int newVal)
  37. {
  38. CFreelancerGuild::onOfferSliderMoved(newVal);
  39. }, 0, 0, 0, Orientation::HORIZONTAL);
  40. // Hero creatures panel
  41. assert(leftTradePanel);
  42. leftTradePanel->moveTo(pos.topLeft() + Point(45, 123));
  43. leftTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
  44. std::for_each(leftTradePanel->slots.cbegin(), leftTradePanel->slots.cend(), [this](auto & slot)
  45. {
  46. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
  47. {
  48. CFreelancerGuild::onSlotClickPressed(heroSlot, hLeft);
  49. };
  50. });
  51. // Guild resources panel
  52. assert(rightTradePanel);
  53. rightTradePanel->moveBy(Point(327, 181));
  54. std::for_each(rightTradePanel->slots.cbegin(), rightTradePanel->slots.cend(), [this](auto & slot)
  55. {
  56. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
  57. {
  58. CFreelancerGuild::onSlotClickPressed(heroSlot, hRight);
  59. };
  60. });
  61. CFreelancerGuild::deselect();
  62. }
  63. void CFreelancerGuild::updateSelected()
  64. {
  65. std::optional<size_t> lImageIndex = std::nullopt;
  66. std::optional<size_t> rImageIndex = std::nullopt;
  67. if(hLeft && hRight)
  68. {
  69. leftTradePanel->selectedSubtitle->setText(std::to_string(bidQty * offerSlider->getValue()));
  70. rightTradePanel->selectedSubtitle->setText(std::to_string(offerQty * offerSlider->getValue()));
  71. lImageIndex = CGI->creatures()->getByIndex(hLeft->id)->getIconIndex();
  72. rImageIndex = hRight->id;
  73. }
  74. else
  75. {
  76. leftTradePanel->selectedSubtitle->setText("");
  77. rightTradePanel->selectedSubtitle->setText("");
  78. }
  79. leftTradePanel->setSelectedFrameIndex(lImageIndex);
  80. rightTradePanel->setSelectedFrameIndex(rImageIndex);
  81. }
  82. void CFreelancerGuild::makeDeal()
  83. {
  84. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  85. {
  86. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_RESOURCE, SlotID(hLeft->serial), GameResID(hRight->id), bidQty * toTrade, hero);
  87. deselect();
  88. }
  89. }
  90. void CFreelancerGuild::deselect()
  91. {
  92. CResourcesPurchasing::deselect();
  93. updateSelected();
  94. }
  95. void CFreelancerGuild::onOfferSliderMoved(int newVal)
  96. {
  97. if(hLeft && hRight)
  98. {
  99. offerSlider->scrollTo(newVal);
  100. updateSelected();
  101. redraw();
  102. }
  103. }
  104. void CFreelancerGuild::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  105. {
  106. if(newSlot == hCurSlot)
  107. return;
  108. CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
  109. if(hLeft)
  110. {
  111. if(hRight)
  112. {
  113. market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::CREATURE_RESOURCE);
  114. offerSlider->setAmount((hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack() ? 1 : 0)) / bidQty);
  115. offerSlider->scrollTo(0);
  116. offerSlider->block(false);
  117. maxAmount->block(false);
  118. deal->block(false);
  119. }
  120. updateSelected();
  121. rightTradePanel->updateSlots();
  122. }
  123. redraw();
  124. }