diff --git a/client/widgets/CArtifactHolder.h b/client/widgets/CArtifactHolder.h index 03baf60c4..c39cbdca7 100644 --- a/client/widgets/CArtifactHolder.h +++ b/client/widgets/CArtifactHolder.h @@ -29,7 +29,7 @@ public: virtual void artifactAssembled(const ArtifactLocation & artLoc)=0; }; -class CArtPlace : public LRClickableAreaWTextComp, public SelectableSlot +class CArtPlace : public SelectableSlot { public: using ClickFunctor = std::function; diff --git a/client/widgets/CArtifactsOfHeroMarket.cpp b/client/widgets/CArtifactsOfHeroMarket.cpp index fadc632cd..52e9828ed 100644 --- a/client/widgets/CArtifactsOfHeroMarket.cpp +++ b/client/widgets/CArtifactsOfHeroMarket.cpp @@ -21,9 +21,9 @@ CArtifactsOfHeroMarket::CArtifactsOfHeroMarket(const Point & position) std::bind(&CArtifactsOfHeroMarket::scrollBackpack, this, _1)); for(const auto & [slot, artPlace] : artWorn) - artPlace->selection->setBorderWidth(2); + artPlace->setSelectionWidth(2); for(auto artPlace : backpack) - artPlace->selection->setBorderWidth(2); + artPlace->setSelectionWidth(2); }; void CArtifactsOfHeroMarket::scrollBackpack(int offset) diff --git a/client/widgets/MiscWidgets.cpp b/client/widgets/MiscWidgets.cpp index 391304bd1..d70f24c31 100644 --- a/client/widgets/MiscWidgets.cpp +++ b/client/widgets/MiscWidgets.cpp @@ -705,9 +705,8 @@ void SimpleLine::showAll(Canvas & to) } SelectableSlot::SelectableSlot(Rect area, Point oversize, const int width) + : LRClickableAreaWTextComp(area) { - pos += area.topLeft(); - pos.w = area.w; pos.h = area.h; selection = std::make_unique( Rect(area.topLeft() - oversize, area.dimensions() + oversize * 2), Colors::TRANSPARENCY, Colors::YELLOW, width); selectSlot(false); @@ -732,3 +731,8 @@ bool SelectableSlot::isSelected() const { return selection->getDrawBorder(); } + +void SelectableSlot::setSelectionWidth(int width) +{ + selection->setBorderWidth(width); +} diff --git a/client/widgets/MiscWidgets.h b/client/widgets/MiscWidgets.h index 11172a4c5..534fd0d29 100644 --- a/client/widgets/MiscWidgets.h +++ b/client/widgets/MiscWidgets.h @@ -273,14 +273,15 @@ public: void showAll(Canvas & to) override; }; -class SelectableSlot : virtual public CIntObject +class SelectableSlot : public LRClickableAreaWTextComp { -public: std::unique_ptr selection; +public: SelectableSlot(Rect area, Point oversize, const int width); SelectableSlot(Rect area, Point oversize); SelectableSlot(Rect area, const int width = 1); void selectSlot(bool on); bool isSelected() const; + void setSelectionWidth(int width); }; diff --git a/client/widgets/markets/CAltarArtifacts.cpp b/client/widgets/markets/CAltarArtifacts.cpp index b59d08e2f..70ba7aa36 100644 --- a/client/widgets/markets/CAltarArtifacts.cpp +++ b/client/widgets/markets/CAltarArtifacts.cpp @@ -31,6 +31,8 @@ CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); + deal = std::make_shared(dealButtonPos, AnimationPath::builtin("ALTSACR.DEF"), + CGI->generaltexth->zelp[585], [this]() {CAltarArtifacts::makeDeal(); }); labels.emplace_back(std::make_shared(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477])); labels.emplace_back(std::make_shared(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478])); selectedCost = std::make_shared(302, 500, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE); @@ -50,7 +52,7 @@ CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * int slotNum = 0; for(auto & altarSlotPos : posSlotsAltar) { - auto altarSlot = std::make_shared(altarSlotPos, EType::ARTIFACT_PLACEHOLDER, -1, false, slotNum); + auto altarSlot = std::make_shared(Rect(altarSlotPos, Point(44, 44)), EType::ARTIFACT_PLACEHOLDER, -1, false, slotNum); altarSlot->clickPressedCallback = std::bind(&CAltarArtifacts::onSlotClickPressed, this, _1, hRight); altarSlot->subtitle.clear(); items.front().emplace_back(altarSlot); diff --git a/client/widgets/markets/CAltarCreatures.cpp b/client/widgets/markets/CAltarCreatures.cpp index 84b5f05f8..908ce602e 100644 --- a/client/widgets/markets/CAltarCreatures.cpp +++ b/client/widgets/markets/CAltarCreatures.cpp @@ -30,6 +30,8 @@ CAltarCreatures::CAltarCreatures(const IMarket * market, const CGHeroInstance * { OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE); + deal = std::make_shared(dealButtonPos, AnimationPath::builtin("ALTSACR.DEF"), + CGI->generaltexth->zelp[584], [this]() {CAltarCreatures::makeDeal();}); labels.emplace_back(std::make_shared(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated()))); labels.emplace_back(std::make_shared(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479])); diff --git a/client/widgets/markets/CTradeBase.cpp b/client/widgets/markets/CTradeBase.cpp index 226799f3c..2dacc01e2 100644 --- a/client/widgets/markets/CTradeBase.cpp +++ b/client/widgets/markets/CTradeBase.cpp @@ -25,8 +25,6 @@ CTradeBase::CTradeBase(const IMarket * market, const CGHeroInstance * hero) : market(market) , hero(hero) { - deal = std::make_shared(Point(), AnimationPath::builtin("ALTSACR.DEF"), - CGI->generaltexth->zelp[585], std::bind(&CTradeBase::makeDeal, this)); } void CTradeBase::removeItems(const std::set> & toRemove) @@ -53,9 +51,9 @@ void CTradeBase::getEmptySlots(std::set> & toRem void CTradeBase::deselect() { if(hLeft) - hLeft->selection->selectSlot(false); + hLeft->selectSlot(false); if(hRight) - hRight->selection->selectSlot(false); + hRight->selectSlot(false); hLeft = hRight = nullptr; deal->block(true); } @@ -66,9 +64,9 @@ void CTradeBase::onSlotClickPressed(const std::shared_ptr & newS return; if(hCurSlot) - hCurSlot->selection->selectSlot(false); + hCurSlot->selectSlot(false); hCurSlot = newSlot; - newSlot->selection->selectSlot(true); + newSlot->selectSlot(true); } CExperienceAltar::CExperienceAltar() @@ -79,7 +77,6 @@ CExperienceAltar::CExperienceAltar() texts.emplace_back(std::make_shared(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW)); // Total experience on the Altar texts.emplace_back(std::make_shared(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW)); - deal->moveBy(dealButtonPos); expToLevel = std::make_shared(75, 477, FONT_SMALL, ETextAlignment::CENTER); expForHero = std::make_shared(75, 545, FONT_SMALL, ETextAlignment::CENTER); } diff --git a/client/widgets/markets/TradePanels.cpp b/client/widgets/markets/TradePanels.cpp index b2ac2b9f9..16a48eb2f 100644 --- a/client/widgets/markets/TradePanels.cpp +++ b/client/widgets/markets/TradePanels.cpp @@ -10,8 +10,6 @@ #include "StdInc.h" #include "TradePanels.h" -#include "../MiscWidgets.h" - #include "../../gui/CGuiHandler.h" #include "../../render/Canvas.h" #include "../../widgets/TextControls.h" @@ -25,9 +23,9 @@ #include "../../../lib/CGeneralTextHandler.h" #include "../../../lib/mapObjects/CGHeroInstance.h" -CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial) - : CIntObject(LCLICK | HOVER | SHOW_POPUP, pos) - , hlp(nullptr) +CTradeableItem::CTradeableItem(const Rect & area, EType Type, int ID, bool Left, int Serial) + : SelectableSlot(area, Point(1, 1)) + , artInstance(nullptr) , type(EType(-1)) // set to invalid, will be corrected in setType , id(ID) , serial(Serial) @@ -35,13 +33,15 @@ CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Ser , downSelection(false) { OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE); + + addUsedEvents(LCLICK); + addUsedEvents(HOVER); + addUsedEvents(SHOW_POPUP); setType(Type); - if(image) - { - this->pos.w = image->pos.w; - this->pos.h = image->pos.h; - } + + this->pos.w = area.w; + this->pos.h = area.h; } void CTradeableItem::setType(EType newType) @@ -248,7 +248,7 @@ const CArtifactInstance * CTradeableItem::getArtInstance() const { case EType::ARTIFACT_PLACEHOLDER: case EType::ARTIFACT_INSTANCE: - return hlp; + return artInstance; default: return nullptr; } @@ -257,7 +257,7 @@ const CArtifactInstance * CTradeableItem::getArtInstance() const void CTradeableItem::setArtInstance(const CArtifactInstance * art) { assert(type == EType::ARTIFACT_PLACEHOLDER || type == EType::ARTIFACT_INSTANCE); - hlp = art; + artInstance = art; if(art) setID(art->getTypeId()); else @@ -273,7 +273,7 @@ void TradePanelBase::updateSlots() void TradePanelBase::deselect() { for(const auto & slot : slots) - slot->selection->selectSlot(false); + slot->selectSlot(false); } void TradePanelBase::clearSubtitles() @@ -305,10 +305,10 @@ ResourcesPanel::ResourcesPanel(CTradeableItem::ClickPressedFunctor clickPressedC for(const auto & res : resourcesForTrade) { - auto slot = slots.emplace_back(std::make_shared(slotsPos[res.num], EType::RESOURCE, res.num, true, res.num)); + auto slot = slots.emplace_back(std::make_shared(Rect(slotsPos[res.num], slotDimension), + EType::RESOURCE, res.num, true, res.num)); slot->clickPressedCallback = clickPressedCallback; - slot->pos.w = 69; slots.back()->pos.h = 66; - slot->selection = std::make_unique(Rect(slotsPos[res.num], slots.back()->pos.dimensions()), Point(1, 1), selectionWidth); + slot->setSelectionWidth(selectionWidth); } updateSlotsCallback = updateSubtitles; } @@ -325,10 +325,10 @@ ArtifactsPanel::ArtifactsPanel(CTradeableItem::ClickPressedFunctor clickPressedC auto artType = arts[slotIdx].getNum(); if(artType != ArtifactID::NONE) { - auto slot = slots.emplace_back(std::make_shared(slotsPos[slotIdx], EType::ARTIFACT_TYPE, artType, false, slotIdx)); + auto slot = slots.emplace_back(std::make_shared(Rect(slotsPos[slotIdx], slotDimension), + EType::ARTIFACT_TYPE, artType, false, slotIdx)); slot->clickPressedCallback = clickPressedCallback; - slot->pos.w = 69; slot->pos.h = 66; - slot->selection = std::make_unique(Rect(slotsPos[slotIdx], slot->pos.dimensions()), Point(1, 1), selectionWidth); + slot->setSelectionWidth(selectionWidth); } } updateSlotsCallback = updateSubtitles; @@ -350,9 +350,9 @@ PlayersPanel::PlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallb int slotNum = 0; for(auto & slot : slots) { - slot = std::make_shared(slotsPos[slotNum], EType::PLAYER, players[slotNum].num, false, slotNum); + slot = std::make_shared(Rect(slotsPos[slotNum], slotDimension), 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->setSelectionWidth(selectionWidth); slot->subtitle = CGI->generaltexth->capColors[players[slotNum].num]; slotNum++; } @@ -366,13 +366,12 @@ CreaturesPanel::CreaturesPanel(CTradeableItem::ClickPressedFunctor clickPressedC for(const auto & [creatureId, slotId, creaturesNum] : initialSlots) { - auto slot = slots.emplace_back(std::make_shared(slotsPos[slotId.num], + auto slot = slots.emplace_back(std::make_shared(Rect(slotsPos[slotId.num], slotDimension), creaturesNum == 0 ? EType::CREATURE_PLACEHOLDER : EType::CREATURE, creatureId.num, true, slotId)); slot->clickPressedCallback = clickPressedCallback; if(creaturesNum != 0) slot->subtitle = std::to_string(creaturesNum); - slot->pos.w = 58; slot->pos.h = 64; - slot->selection = std::make_unique(Rect(slotsPos[slotId.num], slot->pos.dimensions()), Point(1, 1), selectionWidth); + slot->setSelectionWidth(selectionWidth); } } @@ -384,11 +383,10 @@ CreaturesPanel::CreaturesPanel(CTradeableItem::ClickPressedFunctor clickPressedC for(const auto & srcSlot : srcSlots) { - auto slot = slots.emplace_back(std::make_shared(slotsPos[srcSlot->serial], + auto slot = slots.emplace_back(std::make_shared(Rect(slotsPos[srcSlot->serial], srcSlot->pos.dimensions()), emptySlots ? EType::CREATURE_PLACEHOLDER : EType::CREATURE, srcSlot->id, true, srcSlot->serial)); slot->clickPressedCallback = clickPressedCallback; slot->subtitle = emptySlots ? "" : srcSlot->subtitle; - slot->pos.w = srcSlot->pos.w; slot->pos.h = srcSlot->pos.h; - slot->selection = std::make_unique(Rect(slotsPos[slot->serial], slot->pos.dimensions()), Point(1, 1), selectionWidth); + slot->setSelectionWidth(selectionWidth); } } diff --git a/client/widgets/markets/TradePanels.h b/client/widgets/markets/TradePanels.h index 20a028ed4..23bc1b336 100644 --- a/client/widgets/markets/TradePanels.h +++ b/client/widgets/markets/TradePanels.h @@ -9,19 +9,17 @@ */ #pragma once +#include "../MiscWidgets.h" #include "../Images.h" #include "../../../lib/networkPacks/TradeItem.h" -class CTextBox; -class SelectableSlot; - enum class EType { RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, CREATURE_PLACEHOLDER, ARTIFACT_PLACEHOLDER, ARTIFACT_INSTANCE }; -class CTradeableItem : public CIntObject, public std::enable_shared_from_this +class CTradeableItem : public SelectableSlot, public std::enable_shared_from_this { public: std::shared_ptr image; @@ -29,7 +27,7 @@ public: int getIndex(); using ClickPressedFunctor = std::function&)>; - const CArtifactInstance * hlp; //holds ptr to artifact instance id type artifact + const CArtifactInstance * artInstance; //holds ptr to artifact instance id type artifact EType type; int id; const int serial; @@ -43,7 +41,6 @@ public: const CArtifactInstance * getArtInstance() const; void setArtInstance(const CArtifactInstance * art); - std::unique_ptr selection; bool downSelection; void showAllAt(const Point & dstPos, const std::string & customSub, Canvas & to); @@ -53,7 +50,7 @@ public: void showAll(Canvas & to) override; void clickPressed(const Point & cursorPosition) override; std::string getName(int number = -1) const; - CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial); + CTradeableItem(const Rect & area, EType Type, int ID, bool Left, int Serial); }; class TradePanelBase : public CIntObject @@ -89,6 +86,7 @@ class ResourcesPanel : public TradePanelBase Point(0, 79), Point(83, 79), Point(166, 79), Point(83, 158) }; + const Point slotDimension = Point(69, 66); public: ResourcesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, UpdateSlotsFunctor updateSubtitles); @@ -103,6 +101,7 @@ class ArtifactsPanel : public TradePanelBase Point(83, 158) }; const size_t slotsForTrade = 7; + const Point slotDimension = Point(69, 66); public: ArtifactsPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, UpdateSlotsFunctor updateSubtitles, @@ -117,6 +116,7 @@ class PlayersPanel : public TradePanelBase Point(0, 118), Point(83, 118), Point(166, 118), Point(83, 236) }; + const Point slotDimension = Point(58, 64); public: explicit PlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback); @@ -130,6 +130,7 @@ class CreaturesPanel : public TradePanelBase Point(0, 98), Point(83, 98), Point(166, 98), Point(83, 196) }; + const Point slotDimension = Point(58, 64); public: using slotsData = std::vector>; diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index e5ff7ab63..6243ed634 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -77,7 +77,7 @@ void CTradeWindow::initItems(bool Left) { if(mode == EMarketMode::ARTIFACT_RESOURCE) { - auto item = std::make_shared(Point(137, 469), itemsType[Left], -1, 1, 0); + auto item = std::make_shared(Rect(Point(137, 469), Point()), itemsType[Left], -1, 1, 0); item->recActions &= ~(UPDATE | SHOWALL); items[Left].push_back(item); }