From 791ee78cc4acf7190760e9e72e449c62891d5950 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Tue, 28 Nov 2023 17:01:53 +0200 Subject: [PATCH] players trade panel --- client/widgets/CTradeBase.cpp | 42 ++++++-- client/widgets/CTradeBase.h | 14 ++- client/windows/CTradeWindow.cpp | 178 +++++++++++++------------------- 3 files changed, 122 insertions(+), 112 deletions(-) diff --git a/client/widgets/CTradeBase.cpp b/client/widgets/CTradeBase.cpp index 99b225073..46283ac44 100644 --- a/client/widgets/CTradeBase.cpp +++ b/client/widgets/CTradeBase.cpp @@ -17,6 +17,9 @@ #include "../windows/InfoWindows.h" #include "../CGameInfo.h" +#include "../CPlayerInterface.h" + +#include "../../CCallback.h" #include "../../lib/CGeneralTextHandler.h" #include "../../lib/mapObjects/CGHeroInstance.h" @@ -126,23 +129,24 @@ void CTradeableItem::showAll(Canvas & to) { case RESOURCE: posToBitmap = Point(19, 9); - posToSubCenter = Point(36, 59); + posToSubCenter = Point(35, 57); break; case CREATURE_PLACEHOLDER: case CREATURE: posToSubCenter = Point(29, 77); break; case PLAYER: - posToSubCenter = Point(31, 76); + posToSubCenter = Point(31, 77); break; case ARTIFACT_PLACEHOLDER: case ARTIFACT_INSTANCE: - posToSubCenter = Point(19, 54); + posToSubCenter = Point(22, 51); if (downSelection) posToSubCenter.y += 8; break; case ARTIFACT_TYPE: - posToSubCenter = Point(19, 58); + posToSubCenter = Point(35, 57); + posToBitmap = Point(13, 0); break; } @@ -293,7 +297,8 @@ SResourcesPanel::SResourcesPanel(CTradeableItem::ClickPressedFunctor clickPresse slots.emplace_back(std::make_shared(slotsPos[res.num], EType::RESOURCE, res.num, true, res.num)); slots.back()->clickPressedCallback = clickPressedCallback; slots.back()->pos.w = 69; slots.back()->pos.h = 66; - slots.back()->selection = std::make_unique(Rect(slotsPos[res.num], slots.back()->pos.dimensions())); + slots.back()->selection = std::make_unique(Rect(slotsPos[res.num], slots.back()->pos.dimensions()), + Point(1, 1), selectionWidth); } updateSlotsCallback = updateSubtitles; } @@ -311,12 +316,35 @@ SArtifactsPanel::SArtifactsPanel(CTradeableItem::ClickPressedFunctor clickPresse { slot = std::make_shared(slotsPos[slotNum], EType::ARTIFACT_TYPE, arts[slotNum].getNum(), false, slotNum); slot->clickPressedCallback = clickPressedCallback; - slot->pos.w = slot->pos.h = 44; - slot->selection = std::make_unique(Rect(slotsPos[slotNum++], slot->pos.dimensions())); + slot->pos.w = 69; slot->pos.h = 66; + slot->selection = std::make_unique(Rect(slotsPos[slotNum++], slot->pos.dimensions()), Point(1, 1), selectionWidth); } updateSlotsCallback = updateSubtitles; } +SPlayersPanel::SPlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback) +{ + assert(PlayerColor::PLAYER_LIMIT_I <= slotsPos.size()); + OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE); + + std::vector players; + for(auto player = PlayerColor(0); player < PlayerColor::PLAYER_LIMIT_I; player++) + { + if(player != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(player) == EPlayerStatus::INGAME) + players.emplace_back(player); + } + + slots.resize(players.size()); + int slotNum = 0; + for(auto & slot : slots) + { + slot = std::make_shared(slotsPos[slotNum], EType::PLAYER, players[slotNum].num, false, slotNum); + slot->clickPressedCallback = clickPressedCallback; + slot->selection = std::make_unique(Rect(slotsPos[slotNum], slot->pos.dimensions()), Point(1, 1), selectionWidth); + slot->subtitle = CGI->generaltexth->capColors[players[slotNum++].num]; + } +} + CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero) : market(market) , hero(hero) diff --git a/client/widgets/CTradeBase.h b/client/widgets/CTradeBase.h index 4a7374126..79f690307 100644 --- a/client/widgets/CTradeBase.h +++ b/client/widgets/CTradeBase.h @@ -72,6 +72,7 @@ struct STradePanel : public CIntObject std::vector> slots; std::function updateSlotsCallback; std::shared_ptr selected; + const int selectionWidth = 2; virtual void updateSlots(); virtual void deselect(); @@ -111,6 +112,17 @@ struct SArtifactsPanel : public STradePanel std::vector & arts); }; +struct SPlayersPanel : public STradePanel +{ + const std::vector slotsPos = + { + Point(0, 0), Point(83, 0), Point(166, 0), + Point(0, 118), Point(83, 118), Point(166, 118), + Point(83, 236) + }; + SPlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback); +}; + class CTradeBase { public: @@ -119,7 +131,7 @@ public: //all indexes: 1 = left, 0 = right std::array>, 2> items; - std::vector> tradePanels; + std::shared_ptr leftTradePanel, rightTradePanel; //highlighted items (nullptr if no highlight) std::shared_ptr hLeft; diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index 4b927b495..bb5e4034b 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -84,91 +84,75 @@ void CTradeWindow::initItems(bool Left) } else { - if(Left && itemsType[1] == RESOURCE) + auto updRightSub = [this](EMarketMode mode) -> void { - tradePanels.emplace_back(std::make_shared( - [this](std::shared_ptr marketSlot) -> void + if(hLeft) + for(auto & slot : rightTradePanel->slots) { - if(hLeft != marketSlot) - { - if(hLeft) - hLeft->selection->selectSlot(false); - hLeft = marketSlot; - hLeft->selection->selectSlot(true); - selectionChanged(true); - } - }, - [this]() -> void - { - for(auto & slot : tradePanels[1]->slots) - slot->subtitle = std::to_string(LOCPLINT->cb->getResourceAmount(static_cast(slot->serial))); - })); - tradePanels.back()->moveBy(Point(39, 182)); - tradePanels.back()->updateSlots(); - return; - } - if(!Left && itemsType[0] == RESOURCE) - { - tradePanels.emplace_back(std::make_shared( - [this](std::shared_ptr marketSlot) -> void - { - if(hRight != marketSlot) - { - if(hRight) - hRight->selection->selectSlot(false); - hRight = marketSlot; - hRight->selection->selectSlot(true); - selectionChanged(false); - initSubs(false); - } - }, - [this]() -> void - { - if(hLeft) - for(auto & slot : tradePanels[0]->slots) - { - int h1, h2; //hlp variables for getting offer - market->getOffer(hLeft->id, slot->id, h1, h2, EMarketMode::RESOURCE_RESOURCE); + int h1, h2; //hlp variables for getting offer + market->getOffer(hLeft->id, slot->id, h1, h2, mode); - if(slot->id != hLeft->id) - tradePanels[0]->updateOffer(slot->serial, h1, h2); - else - slot->subtitle = CGI->generaltexth->allTexts[164]; // n/a - } - else - tradePanels[0]->clearSubtitles(); - })); - tradePanels.back()->moveBy(Point(327, 182)); - return; - } - if(!Left && itemsType[0] == ARTIFACT_TYPE) + rightTradePanel->updateOffer(slot->serial, h1, h2); + } + else + rightTradePanel->clearSubtitles(); + }; + + auto clickPressedTradePanel = [this](std::shared_ptr newSlot, bool left) { - tradePanels.emplace_back(std::make_shared( - [this](std::shared_ptr marketSlot) -> void - { - if(hRight != marketSlot) - { - if(hRight) - hRight->selection->selectSlot(false); - hRight = marketSlot; - hRight->selection->selectSlot(true); - selectionChanged(false); - initSubs(false); - } - }, + auto * selectedSlot = &hRight; + if(left) + selectedSlot = &hLeft; + + if(*selectedSlot) + (*selectedSlot)->selection->selectSlot(false); + *selectedSlot = newSlot; + newSlot->selection->selectSlot(true); + selectionChanged(left); + }; + + if(Left && (mode == EMarketMode::RESOURCE_RESOURCE || mode == EMarketMode::RESOURCE_ARTIFACT || mode == EMarketMode::RESOURCE_PLAYER)) + { + leftTradePanel = std::make_shared(std::bind(clickPressedTradePanel, _1, true), [this]() -> void { + for(auto & slot : leftTradePanel->slots) + slot->subtitle = std::to_string(LOCPLINT->cb->getResourceAmount(static_cast(slot->serial))); + }); + leftTradePanel->moveBy(Point(39, 182)); + leftTradePanel->updateSlots(); + return; + } + if(!Left && mode == EMarketMode::RESOURCE_RESOURCE) + { + rightTradePanel = std::make_shared(std::bind(clickPressedTradePanel, _1, false), + [this, updRightSub]() -> void + { + updRightSub(EMarketMode::RESOURCE_RESOURCE); if(hLeft) - for(auto & slot : tradePanels[0]->slots) - { - int h1, h2; //hlp variables for getting offer - market->getOffer(hLeft->id, slot->id, h1, h2, EMarketMode::RESOURCE_ARTIFACT); - tradePanels[0]->updateOffer(slot->serial, h1, h2); - } - else - tradePanels[0]->clearSubtitles(); - }, market->availableItemsIds(mode))); - tradePanels.back()->moveBy(Point(340, 182)); + rightTradePanel->slots[hLeft->serial]->subtitle = CGI->generaltexth->allTexts[164]; // n/a + }); + rightTradePanel->moveBy(Point(327, 181)); + return; + } + if(!Left && (mode == EMarketMode::ARTIFACT_RESOURCE || mode == EMarketMode::CREATURE_RESOURCE)) + { + rightTradePanel = std::make_shared(std::bind(clickPressedTradePanel, _1, false), + std::bind(updRightSub, EMarketMode::ARTIFACT_RESOURCE)); + rightTradePanel->moveBy(Point(327, 181)); + return; + } + if(!Left && mode == EMarketMode::RESOURCE_ARTIFACT) + { + rightTradePanel = std::make_shared(std::bind(clickPressedTradePanel, _1, false), + std::bind(updRightSub, EMarketMode::RESOURCE_ARTIFACT), market->availableItemsIds(mode)); + rightTradePanel->moveBy(Point(327, 181)); + return; + } + if(!Left && mode == EMarketMode::RESOURCE_PLAYER) + { + rightTradePanel = std::make_shared(std::bind(clickPressedTradePanel, _1, false)); + rightTradePanel->moveBy(Point(333, 83)); return; } @@ -267,15 +251,6 @@ void CTradeWindow::getPositionsFor(std::vector &poss, bool Left, EType typ switch(type) { - case PLAYER: - dx = 83; - dy = 118; - h = 64; - w = 58; - x = 44; - y = 83; - assert(!Left); - break; case CREATURE://45,123 x = 45; y = 123; @@ -313,9 +288,9 @@ void CTradeWindow::initSubs(bool Left) if(itemsType[Left] == RESOURCE || itemsType[Left] == ARTIFACT_TYPE) { if(Left) - tradePanels[1]->updateSlots(); + leftTradePanel->updateSlots(); else - tradePanels[0]->updateSlots(); + rightTradePanel->updateSlots(); return; } @@ -330,13 +305,6 @@ void CTradeWindow::initSubs(bool Left) break; } } - else //right side - { - if(itemsType[0] == PLAYER) - { - item->subtitle = CGI->generaltexth->capColors[item->id]; - } - } } } @@ -598,8 +566,10 @@ void CMarketplaceWindow::makeDeal() madeTransaction = true; hLeft = nullptr; hRight = nullptr; - for(auto & panel : tradePanels) - panel->deselect(); + if(leftTradePanel) + leftTradePanel->deselect(); + assert(rightTradePanel); + rightTradePanel->deselect(); selectionChanged(true); } @@ -760,11 +730,11 @@ Point CMarketplaceWindow::selectionOffset(bool Left) const switch(itemsType[1]) { case RESOURCE: - return Point(122, 446); + return Point(122, 448); case CREATURE: return Point(128, 450); case ARTIFACT_INSTANCE: - return Point(134, 466); + return Point(134, 469); } } else @@ -772,12 +742,12 @@ Point CMarketplaceWindow::selectionOffset(bool Left) const switch(itemsType[0]) { case RESOURCE: - if(mode == EMarketMode::ARTIFACT_RESOURCE) - return Point(410, 469); + if(mode == EMarketMode::ARTIFACT_RESOURCE) + return Point(410, 471); else - return Point(410, 446); + return Point(410, 448); case ARTIFACT_TYPE: - return Point(425, 447); + return Point(411, 449); case PLAYER: return Point(417, 451); }