From 4e0ea7355b56ce5cfd0beafb73ad7d0eb62d2a11 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Wed, 21 Feb 2024 20:48:14 +0200 Subject: [PATCH] Transfer resources widget --- client/CMakeLists.txt | 4 + client/widgets/markets/CFreelancerGuild.cpp | 4 +- client/widgets/markets/CFreelancerGuild.h | 2 +- client/widgets/markets/CMarketResources.cpp | 6 +- client/widgets/markets/CMarketResources.h | 2 +- client/widgets/markets/CTradeBase.cpp | 6 +- client/widgets/markets/CTradeBase.h | 4 +- client/widgets/markets/CTransferResources.cpp | 130 ++++++++++++++++++ client/widgets/markets/CTransferResources.h | 25 ++++ client/widgets/markets/TradePanels.cpp | 9 ++ client/widgets/markets/TradePanels.h | 2 + client/windows/CMarketWindow.cpp | 33 ++++- client/windows/CMarketWindow.h | 4 + client/windows/CTradeWindow.cpp | 5 - 14 files changed, 215 insertions(+), 21 deletions(-) create mode 100644 client/widgets/markets/CTransferResources.cpp create mode 100644 client/widgets/markets/CTransferResources.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index e90d1fc03..c9293bbe7 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -125,8 +125,10 @@ set(client_SRCS widgets/RadialMenu.cpp widgets/markets/CAltarArtifacts.cpp widgets/markets/CAltarCreatures.cpp + widgets/markets/CArtifactsBuying.cpp widgets/markets/CFreelancerGuild.cpp widgets/markets/CMarketResources.cpp + widgets/markets/CTransferResources.cpp widgets/markets/CTradeBase.cpp widgets/markets/TradePanels.cpp @@ -312,8 +314,10 @@ set(client_HEADERS widgets/RadialMenu.h widgets/markets/CAltarArtifacts.h widgets/markets/CAltarCreatures.h + widgets/markets/CArtifactsBuying.h widgets/markets/CFreelancerGuild.h widgets/markets/CMarketResources.h + widgets/markets/CTransferResources.h widgets/markets/CTradeBase.h widgets/markets/TradePanels.h diff --git a/client/widgets/markets/CFreelancerGuild.cpp b/client/widgets/markets/CFreelancerGuild.cpp index 3602c3db6..f2d0f6b3c 100644 --- a/client/widgets/markets/CFreelancerGuild.cpp +++ b/client/widgets/markets/CFreelancerGuild.cpp @@ -28,7 +28,7 @@ CFreelancerGuild::CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero) : CTradeBase(market, hero) - , CResourcesPurchasing([this](){CResourcesPurchasing::updateSubtitles(EMarketMode::CREATURE_RESOURCE);}) + , CResourcesBuying([this](){CResourcesBuying::updateSubtitles(EMarketMode::CREATURE_RESOURCE);}) { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); @@ -103,7 +103,7 @@ void CFreelancerGuild::makeDeal() void CFreelancerGuild::deselect() { - CResourcesPurchasing::deselect(); + CResourcesBuying::deselect(); updateSelected(); } diff --git a/client/widgets/markets/CFreelancerGuild.h b/client/widgets/markets/CFreelancerGuild.h index b85168e48..60a5cb50d 100644 --- a/client/widgets/markets/CFreelancerGuild.h +++ b/client/widgets/markets/CFreelancerGuild.h @@ -11,7 +11,7 @@ #include "CTradeBase.h" -class CFreelancerGuild : public CCreaturesSelling , public CResourcesPurchasing +class CFreelancerGuild : public CCreaturesSelling , public CResourcesBuying { public: CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero); diff --git a/client/widgets/markets/CMarketResources.cpp b/client/widgets/markets/CMarketResources.cpp index a2d565fbc..0a028cf47 100644 --- a/client/widgets/markets/CMarketResources.cpp +++ b/client/widgets/markets/CMarketResources.cpp @@ -27,7 +27,7 @@ CMarketResources::CMarketResources(const IMarket * market, const CGHeroInstance * hero) : CTradeBase(market, hero) - , CResourcesPurchasing([this](){CMarketResources::updateSubtitles();}) + , CResourcesBuying([this](){CMarketResources::updateSubtitles();}) { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); @@ -78,7 +78,7 @@ void CMarketResources::makeDeal() void CMarketResources::deselect() { - CResourcesPurchasing::deselect(); + CResourcesBuying::deselect(); updateSelected(); } @@ -139,7 +139,7 @@ void CMarketResources::onSlotClickPressed(const std::shared_ptr void CMarketResources::updateSubtitles() { - CResourcesPurchasing::updateSubtitles(EMarketMode::RESOURCE_RESOURCE); + CResourcesBuying::updateSubtitles(EMarketMode::RESOURCE_RESOURCE); if(hLeft) rightTradePanel->slots[hLeft->serial]->subtitle = CGI->generaltexth->allTexts[164]; // n/a } diff --git a/client/widgets/markets/CMarketResources.h b/client/widgets/markets/CMarketResources.h index 38074b39d..c040195bd 100644 --- a/client/widgets/markets/CMarketResources.h +++ b/client/widgets/markets/CMarketResources.h @@ -11,7 +11,7 @@ #include "CTradeBase.h" -class CMarketResources : public CResourcesSelling, public CResourcesPurchasing +class CMarketResources : public CResourcesSelling, public CResourcesBuying { public: CMarketResources(const IMarket * market, const CGHeroInstance * hero); diff --git a/client/widgets/markets/CTradeBase.cpp b/client/widgets/markets/CTradeBase.cpp index 34c8dcd0a..1e1ed1c4f 100644 --- a/client/widgets/markets/CTradeBase.cpp +++ b/client/widgets/markets/CTradeBase.cpp @@ -125,7 +125,7 @@ void CCreaturesSelling::updateSlots() leftTradePanel->updateSlots(); } -CResourcesPurchasing::CResourcesPurchasing(TradePanelBase::UpdateSlotsFunctor callback) +CResourcesBuying::CResourcesBuying(TradePanelBase::UpdateSlotsFunctor callback) { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); @@ -133,7 +133,7 @@ CResourcesPurchasing::CResourcesPurchasing(TradePanelBase::UpdateSlotsFunctor ca labels.emplace_back(std::make_shared(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168])); } -void CResourcesPurchasing::updateSubtitles(EMarketMode marketMode) +void CResourcesBuying::updateSubtitles(EMarketMode marketMode) { assert(marketMode == EMarketMode::RESOURCE_RESOURCE || marketMode == EMarketMode::CREATURE_RESOURCE || marketMode == EMarketMode::ARTIFACT_RESOURCE); @@ -149,7 +149,7 @@ void CResourcesPurchasing::updateSubtitles(EMarketMode marketMode) rightTradePanel->clearSubtitles(); }; -void CResourcesPurchasing::deselect() +void CResourcesBuying::deselect() { CTradeBase::deselect(); bidQty = 0; diff --git a/client/widgets/markets/CTradeBase.h b/client/widgets/markets/CTradeBase.h index b067266b7..a0d59fe6f 100644 --- a/client/widgets/markets/CTradeBase.h +++ b/client/widgets/markets/CTradeBase.h @@ -75,10 +75,10 @@ public: void updateSlots() override; }; -class CResourcesPurchasing : virtual public CTradeBase, virtual public CIntObject +class CResourcesBuying : virtual public CTradeBase, virtual public CIntObject { public: - CResourcesPurchasing(TradePanelBase::UpdateSlotsFunctor callback); + CResourcesBuying(TradePanelBase::UpdateSlotsFunctor callback); void updateSubtitles(EMarketMode marketMode); void deselect() override; diff --git a/client/widgets/markets/CTransferResources.cpp b/client/widgets/markets/CTransferResources.cpp new file mode 100644 index 000000000..60789af45 --- /dev/null +++ b/client/widgets/markets/CTransferResources.cpp @@ -0,0 +1,130 @@ +/* + * CTransferResources.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 "CTransferResources.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" + +CTransferResources::CTransferResources(const IMarket * market, const CGHeroInstance * hero) + : CTradeBase(market, hero) +{ + OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); + + labels.emplace_back(std::make_shared(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[158])); + labels.emplace_back(std::make_shared(445, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169])); + deal = std::make_shared(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"), + CGI->generaltexth->zelp[595], [this]() {CTransferResources::makeDeal();}); + maxAmount = std::make_shared(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596], + [this]() {offerSlider->scrollToMax();}); + offerSlider = std::make_shared(Point(230, 489), 137, [this](int newVal) + { + CTransferResources::onOfferSliderMoved(newVal); + }, 0, 0, 0, Orientation::HORIZONTAL); + + // Player's resources + assert(leftTradePanel); + std::for_each(leftTradePanel->slots.cbegin(), leftTradePanel->slots.cend(), [this](auto & slot) + { + slot->clickPressedCallback = [this](const std::shared_ptr & heroSlot) + { + CTransferResources::onSlotClickPressed(heroSlot, hLeft); + }; + }); + leftTradePanel->moveTo(pos.topLeft() + Point(40, 182)); + + // Players panel + rightTradePanel = std::make_shared([this](const std::shared_ptr & heroSlot) + { + CTransferResources::onSlotClickPressed(heroSlot, hRight); + }); + rightTradePanel->moveTo(pos.topLeft() + Point(333, 83)); + + CResourcesSelling::updateSlots(); + CTransferResources::deselect(); +} + +void CTransferResources::makeDeal() +{ + if(auto toTrade = offerSlider->getValue(); toTrade != 0) + { + LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_PLAYER, GameResID(hLeft->id), PlayerColor(hRight->id), toTrade, hero); + deselect(); + } +} + +void CTransferResources::deselect() +{ + CTradeBase::deselect(); + updateSelected(); +} + +void CTransferResources::updateSelected() +{ + std::optional lImageIndex = std::nullopt; + std::optional rImageIndex = std::nullopt; + + if(hLeft && hRight) + { + leftTradePanel->selectedSubtitle->setText(std::to_string(offerSlider->getValue())); + rightTradePanel->selectedSubtitle->setText(CGI->generaltexth->capColors[hRight->id]); + lImageIndex = hLeft->id; + rImageIndex = hRight->id; + } + else + { + leftTradePanel->selectedSubtitle->setText(""); + rightTradePanel->selectedSubtitle->setText(""); + } + leftTradePanel->setSelectedFrameIndex(lImageIndex); + rightTradePanel->setSelectedFrameIndex(rImageIndex); +} + +void CTransferResources::onOfferSliderMoved(int newVal) +{ + if(hLeft && hRight) + { + offerSlider->scrollTo(newVal); + updateSelected(); + redraw(); + } +} + +void CTransferResources::onSlotClickPressed(const std::shared_ptr & newSlot, std::shared_ptr & hCurSlot) +{ + if(newSlot == hCurSlot) + return; + + CTradeBase::onSlotClickPressed(newSlot, hCurSlot); + if(hLeft) + { + if(hRight) + { + offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id))); + offerSlider->scrollTo(0); + offerSlider->block(false); + maxAmount->block(false); + deal->block(false); + } + updateSelected(); + rightTradePanel->updateSlots(); + } + redraw(); +} diff --git a/client/widgets/markets/CTransferResources.h b/client/widgets/markets/CTransferResources.h new file mode 100644 index 000000000..d21d007f2 --- /dev/null +++ b/client/widgets/markets/CTransferResources.h @@ -0,0 +1,25 @@ +/* + * CTransferResources.h, 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 + * + */ +#pragma once + +#include "CTradeBase.h" + +class CTransferResources : public CResourcesSelling +{ +public: + CTransferResources(const IMarket * market, const CGHeroInstance * hero); + void makeDeal() override; + void deselect() override; + +private: + void updateSelected(); + void onOfferSliderMoved(int newVal); + void onSlotClickPressed(const std::shared_ptr & newSlot, std::shared_ptr & hCurSlot); +}; diff --git a/client/widgets/markets/TradePanels.cpp b/client/widgets/markets/TradePanels.cpp index 2a5032d62..1013f75c8 100644 --- a/client/widgets/markets/TradePanels.cpp +++ b/client/widgets/markets/TradePanels.cpp @@ -196,12 +196,19 @@ void CTradeableItem::hover(bool on) case EType::CREATURE_PLACEHOLDER: GH.statusbar()->write(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->getNamePluralTranslated())); break; + case EType::ARTIFACT_TYPE: case EType::ARTIFACT_PLACEHOLDER: if(id < 0) GH.statusbar()->write(CGI->generaltexth->zelp[582].first); else GH.statusbar()->write(CGI->artifacts()->getByIndex(id)->getNameTranslated()); break; + case EType::RESOURCE: + GH.statusbar()->write(CGI->generaltexth->restypes[id]); + break; + case EType::PLAYER: + GH.statusbar()->write(CGI->generaltexth->capColors[id]); + break; } } @@ -372,6 +379,8 @@ PlayersPanel::PlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallb slot->subtitle = CGI->generaltexth->capColors[players[slotNum].num]; slotNum++; } + selectedImage = std::make_shared(AnimationPath::builtin("CREST58"), 0, 0, selectedImagePos.x, selectedImagePos.y); + selectedSubtitle = std::make_shared(selectedSubtitlePos.x, selectedSubtitlePos.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE); } CreaturesPanel::CreaturesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, const slotsData & initialSlots) diff --git a/client/widgets/markets/TradePanels.h b/client/widgets/markets/TradePanels.h index 79aceb320..282682c78 100644 --- a/client/widgets/markets/TradePanels.h +++ b/client/widgets/markets/TradePanels.h @@ -122,6 +122,8 @@ class PlayersPanel : public TradePanelBase Point(83, 236) }; const Point slotDimension = Point(58, 64); + const Point selectedImagePos = Point(83, 368); + const Point selectedSubtitlePos = Point(112, 443); public: explicit PlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback); diff --git a/client/windows/CMarketWindow.cpp b/client/windows/CMarketWindow.cpp index 0ca5f6aba..6dc02c211 100644 --- a/client/windows/CMarketWindow.cpp +++ b/client/windows/CMarketWindow.cpp @@ -16,8 +16,10 @@ #include "../widgets/Buttons.h" #include "../widgets/TextControls.h" +#include "../widgets/markets/CArtifactsBuying.h" #include "../widgets/markets/CFreelancerGuild.h" #include "../widgets/markets/CMarketResources.h" +#include "../widgets/markets/CTransferResources.h" #include "../CGameInfo.h" #include "../CPlayerInterface.h" @@ -72,6 +74,8 @@ void CMarketWindow::resourceChanged() { if(resRes) resRes->updateSlots(); + if(trRes) + trRes->updateSlots(); } void CMarketWindow::close() @@ -165,8 +169,20 @@ void CMarketWindow::createArtifactsBuying(const IMarket * market, const CGHeroIn OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE); background = createBg(ImagePath::builtin("TPMRKABS.bmp"), PLAYER_COLORED); - this->market = std::make_shared(market, hero, []() {}, EMarketMode::RESOURCE_ARTIFACT); - createInternals(EMarketMode::RESOURCE_ARTIFACT, market, hero); + //this->market = std::make_shared(market, hero, []() {}, EMarketMode::RESOURCE_ARTIFACT); + //createInternals(EMarketMode::RESOURCE_ARTIFACT, market, hero); + + artsBuy = std::make_shared(market, hero); + + background->center(); + pos = background->pos; + artsBuy->setRedrawParent(true); + artsBuy->moveTo(pos.topLeft()); + + createChangeModeButtons(EMarketMode::RESOURCE_ARTIFACT, market, hero); + quitButton = std::make_shared(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"), + CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN); + redraw(); } void CMarketWindow::createArtifactsSelling(const IMarket * market, const CGHeroInstance * hero) @@ -219,8 +235,17 @@ void CMarketWindow::createTransferResources(const IMarket * market, const CGHero OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE); background = createBg(ImagePath::builtin("TPMRKPTS.bmp"), PLAYER_COLORED); - this->market = std::make_shared(market, hero, []() {}, EMarketMode::RESOURCE_PLAYER); - createInternals(EMarketMode::RESOURCE_PLAYER, market, hero); + trRes = std::make_shared(market, hero); + + background->center(); + pos = background->pos; + trRes->setRedrawParent(true); + trRes->moveTo(pos.topLeft()); + + createChangeModeButtons(EMarketMode::RESOURCE_PLAYER, market, hero); + quitButton = std::make_shared(quitButtonPos, AnimationPath::builtin("IOK6432.DEF"), + CGI->generaltexth->zelp[600], [this]() {close(); }, EShortcut::GLOBAL_RETURN); + redraw(); } void CMarketWindow::createAltarArtifacts(const IMarket * market, const CGHeroInstance * hero) diff --git a/client/windows/CMarketWindow.h b/client/windows/CMarketWindow.h index efd5cf08d..9e7be28ee 100644 --- a/client/windows/CMarketWindow.h +++ b/client/windows/CMarketWindow.h @@ -12,8 +12,10 @@ #include "CTradeWindow.h" #include "CAltarWindow.h" +class CArtifactsBuying; class CFreelancerGuild; class CMarketResources; +class CTransferResources; class CMarketWindow : public CStatusbarWindow, public CAltarWindow // TODO remove CAltarWindow { @@ -47,4 +49,6 @@ private: std::shared_ptr market; std::shared_ptr guild; std::shared_ptr resRes; + std::shared_ptr trRes; + std::shared_ptr artsBuy; }; diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index 54dd09114..e0b86bc54 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -134,11 +134,6 @@ void CTradeWindow::initItems(bool Left) return vstd::contains(market->availableItemsIds(EMarketMode::RESOURCE_ARTIFACT), ArtifactID(slot->id)) ? false : true; }; } - else if(!Left && mode == EMarketMode::RESOURCE_PLAYER) - { - rightTradePanel = std::make_shared(std::bind(clickPressedTradePanel, _1, false)); - rightTradePanel->moveBy(Point(333, 83)); - } } }