CFreelancerGuild.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/TextControls.h"
  15. #include "../../CGameInfo.h"
  16. #include "../../CPlayerInterface.h"
  17. #include "../../../CCallback.h"
  18. #include "../../../lib/CGeneralTextHandler.h"
  19. #include "../../../lib/mapObjects/CGHeroInstance.h"
  20. #include "../../../lib/mapObjects/CGMarket.h"
  21. #include "../../../lib/mapObjects/CGTownInstance.h"
  22. CFreelancerGuild::CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero)
  23. : CMarketBase(market, hero, [this](){return CFreelancerGuild::getSelectionParams();})
  24. , CResourcesBuying(
  25. [this](const std::shared_ptr<CTradeableItem> & heroSlot){CFreelancerGuild::onSlotClickPressed(heroSlot, hLeft);},
  26. [this](){CMarketBase::updateSubtitles(EMarketMode::CREATURE_RESOURCE);})
  27. , CMarketSlider([this](int newVal){CMarketSlider::onOfferSliderMoved(newVal);})
  28. {
  29. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  30. labels.emplace_back(std::make_shared<CLabel>(titlePos.x, titlePos.y, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW,
  31. (*CGI->townh)[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->getNameTranslated()));
  32. labels.emplace_back(std::make_shared<CLabel>(155, 103, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  33. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  34. deal = std::make_shared<CButton>(dealButtonPosWithSlider, AnimationPath::builtin("TPMRKB.DEF"),
  35. CGI->generaltexth->zelp[595], [this]() {CFreelancerGuild::makeDeal();});
  36. offerSlider->moveTo(pos.topLeft() + Point(232, 489));
  37. // Hero creatures panel
  38. assert(bidTradePanel);
  39. bidTradePanel->moveTo(pos.topLeft() + Point(45, 123));
  40. bidTradePanel->selectedSlot->subtitle->moveBy(Point(0, -1));
  41. bidTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
  42. std::for_each(bidTradePanel->slots.cbegin(), bidTradePanel->slots.cend(), [this](auto & slot)
  43. {
  44. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
  45. {
  46. CFreelancerGuild::onSlotClickPressed(heroSlot, hLeft);
  47. };
  48. });
  49. // Guild resources panel
  50. assert(offerTradePanel);
  51. std::for_each(offerTradePanel->slots.cbegin(), offerTradePanel->slots.cend(), [this](auto & slot)
  52. {
  53. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
  54. {
  55. CFreelancerGuild::onSlotClickPressed(heroSlot, hRight);
  56. };
  57. });
  58. CFreelancerGuild::deselect();
  59. }
  60. void CFreelancerGuild::deselect()
  61. {
  62. CMarketBase::deselect();
  63. CMarketSlider::deselect();
  64. }
  65. void CFreelancerGuild::makeDeal()
  66. {
  67. if(auto toTrade = offerSlider->getValue(); toTrade != 0)
  68. {
  69. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_RESOURCE, SlotID(hLeft->serial), GameResID(hRight->id), bidQty * toTrade, hero);
  70. deselect();
  71. }
  72. }
  73. CMarketBase::SelectionParams CFreelancerGuild::getSelectionParams() const
  74. {
  75. if(hLeft && hRight)
  76. return std::make_tuple(
  77. SelectionParamOneSide {std::to_string(bidQty * offerSlider->getValue()), CGI->creatures()->getByIndex(hLeft->id)->getIconIndex()},
  78. SelectionParamOneSide {std::to_string(offerQty * offerSlider->getValue()), hRight->id});
  79. else
  80. return std::make_tuple(std::nullopt, std::nullopt);
  81. }
  82. void CFreelancerGuild::highlightingChanged()
  83. {
  84. if(hLeft)
  85. {
  86. if(hRight)
  87. {
  88. market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::CREATURE_RESOURCE);
  89. offerSlider->setAmount((hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack() ? 1 : 0)) / bidQty);
  90. offerSlider->scrollTo(0);
  91. offerSlider->block(false);
  92. maxAmount->block(false);
  93. deal->block(false);
  94. }
  95. offerTradePanel->update();
  96. }
  97. updateSelected();
  98. }
  99. void CFreelancerGuild::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  100. {
  101. assert(newSlot);
  102. if(newSlot == hCurSlot)
  103. return;
  104. CMarketBase::onSlotClickPressed(newSlot, hCurSlot);
  105. highlightingChanged();
  106. redraw();
  107. }