| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * CFreelancerGuild.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "CFreelancerGuild.h"
- #include "../../gui/CGuiHandler.h"
- #include "../../widgets/Buttons.h"
- #include "../../widgets/Slider.h"
- #include "../../widgets/TextControls.h"
- #include "../../CGameInfo.h"
- #include "../../CPlayerInterface.h"
- #include "../../../CCallback.h"
- #include "../../../lib/CGeneralTextHandler.h"
- #include "../../../lib/mapObjects/CGHeroInstance.h"
- #include "../../../lib/mapObjects/CGMarket.h"
- #include "../../../lib/mapObjects/CGTownInstance.h"
- CFreelancerGuild::CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero)
- : CTradeBase(market, hero)
- , CResourcesPurchasing([this](){CResourcesPurchasing::updateSubtitles(EMarketMode::CREATURE_RESOURCE);})
- {
- OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
- labels.emplace_back(std::make_shared<CLabel>(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW,
- (*CGI->townh)[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->getNameTranslated()));
- labels.emplace_back(std::make_shared<CLabel>(155, 103, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
- boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
- deal = std::make_shared<CButton>(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"),
- CGI->generaltexth->zelp[595], [this]() {CFreelancerGuild::makeDeal();});
- maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
- [this]() {offerSlider->scrollToMax();});
- offerSlider = std::make_shared<CSlider>(Point(232, 489), 137, [this](int newVal)
- {
- CFreelancerGuild::onOfferSliderMoved(newVal);
- }, 0, 0, 0, Orientation::HORIZONTAL);
- // Hero creatures panel
- assert(leftTradePanel);
- leftTradePanel->moveTo(pos.topLeft() + Point(45, 123));
- leftTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
- std::for_each(leftTradePanel->slots.cbegin(), leftTradePanel->slots.cend(), [this](auto & slot)
- {
- slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
- {
- CFreelancerGuild::onSlotClickPressed(heroSlot, hLeft);
- };
- });
- // Guild resources panel
- assert(rightTradePanel);
- rightTradePanel->moveBy(Point(327, 181));
- std::for_each(rightTradePanel->slots.cbegin(), rightTradePanel->slots.cend(), [this](auto & slot)
- {
- slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
- {
- CFreelancerGuild::onSlotClickPressed(heroSlot, hRight);
- };
- });
- CFreelancerGuild::deselect();
- }
- void CFreelancerGuild::updateSelected()
- {
- std::optional<size_t> lImageIndex = std::nullopt;
- std::optional<size_t> rImageIndex = std::nullopt;
-
- if(hLeft && hRight)
- {
- leftTradePanel->selectedSubtitle->setText(std::to_string(bidQty * offerSlider->getValue()));
- rightTradePanel->selectedSubtitle->setText(std::to_string(offerQty * offerSlider->getValue()));
- lImageIndex = CGI->creatures()->getByIndex(hLeft->id)->getIconIndex();
- rImageIndex = hRight->id;
- }
- else
- {
- leftTradePanel->selectedSubtitle->setText("");
- rightTradePanel->selectedSubtitle->setText("");
- }
- leftTradePanel->setSelectedFrameIndex(lImageIndex);
- rightTradePanel->setSelectedFrameIndex(rImageIndex);
- }
- void CFreelancerGuild::makeDeal()
- {
- if(auto toTrade = offerSlider->getValue(); toTrade != 0)
- {
- LOCPLINT->cb->trade(market, EMarketMode::CREATURE_RESOURCE, SlotID(hLeft->serial), GameResID(hRight->id), bidQty * toTrade, hero);
- deselect();
- }
- }
- void CFreelancerGuild::deselect()
- {
- CResourcesPurchasing::deselect();
- updateSelected();
- }
- void CFreelancerGuild::onOfferSliderMoved(int newVal)
- {
- if(hLeft && hRight)
- {
- offerSlider->scrollTo(newVal);
- updateSelected();
- redraw();
- }
- }
- void CFreelancerGuild::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
- {
- if(newSlot == hCurSlot)
- return;
- CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
- if(hLeft)
- {
- if(hRight)
- {
- market->getOffer(hLeft->id, hRight->id, bidQty, offerQty, EMarketMode::CREATURE_RESOURCE);
- offerSlider->setAmount((hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack() ? 1 : 0)) / bidQty);
- offerSlider->scrollTo(0);
- offerSlider->block(false);
- maxAmount->block(false);
- deal->block(false);
- }
- updateSelected();
- rightTradePanel->updateSlots();
- }
- redraw();
- }
|