1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Refactoring. Callbacks have been encapsulated. Added cursorPosition

This commit is contained in:
SoundSSGood 2023-11-13 17:43:02 +02:00
parent 29b61081f3
commit 21b02414bf
19 changed files with 73 additions and 62 deletions

View File

@ -139,7 +139,7 @@ void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
}
void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
void CCommanderArtPlace::showPopupWindow(const Point& cursorPosition)
{
if(ourArt && text.size())
CArtPlace::showPopupWindow(cursorPosition);
@ -180,16 +180,16 @@ bool CArtPlace::isSelected() const
return selection->visible;
}
void CHeroArtPlace::clickPressed(const Point & cursorPosition)
void CArtPlace::clickPressed(const Point & cursorPosition)
{
if(leftClickCallback)
leftClickCallback(*this);
if(clickPressedCallback)
clickPressedCallback(*this, cursorPosition);
}
void CHeroArtPlace::showPopupWindow(const Point & cursorPosition)
void CArtPlace::showPopupWindow(const Point & cursorPosition)
{
if(showPopupCallback)
showPopupCallback(*this);
showPopupCallback(*this, cursorPosition);
}
void CArtPlace::showAll(Canvas & to)
@ -216,6 +216,16 @@ void CArtPlace::setArtifact(const CArtifactInstance * art)
}
}
void CArtPlace::setClickPressedCallback(ClickFunctor callback)
{
clickPressedCallback = callback;
}
void CArtPlace::setShowPopupCallback(ClickFunctor callback)
{
showPopupCallback = callback;
}
void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
{
for(const auto & combinedArt : arts)

View File

@ -32,14 +32,22 @@ public:
class CArtPlace : public LRClickableAreaWTextComp
{
public:
using ClickFunctor = std::function<void(CArtPlace&, const Point&)>;
ArtifactPosition slot;
CArtPlace(Point position, const CArtifactInstance * art = nullptr);
const CArtifactInstance* getArt();
const CArtifactInstance * getArt();
void lockSlot(bool on);
bool isLocked() const;
void selectSlot(bool on);
bool isSelected() const;
void showAll(Canvas & to) override;
void setArtifact(const CArtifactInstance * art);
void setClickPressedCallback(ClickFunctor callback);
void setShowPopupCallback(ClickFunctor callback);
void clickPressed(const Point & cursorPosition) override;
void showPopupWindow(const Point & cursorPosition) override;
protected:
std::shared_ptr<CAnimImage> image;
@ -47,6 +55,8 @@ protected:
int imageIndex;
std::shared_ptr<CAnimImage> selection;
bool locked;
ClickFunctor clickPressedCallback;
ClickFunctor showPopupCallback;
void setInternals(const CArtifactInstance * artInst);
};
@ -68,15 +78,7 @@ public:
class CHeroArtPlace: public CArtPlace
{
public:
using ClickFunctor = std::function<void(CHeroArtPlace&)>;
ArtifactPosition slot;
ClickFunctor leftClickCallback;
ClickFunctor showPopupCallback;
CHeroArtPlace(Point position, const CArtifactInstance * art = nullptr);
void clickPressed(const Point & cursorPosition) override;
void showPopupWindow(const Point & cursorPosition) override;
void addCombinedArtInfo(std::map<const CArtifact*, int> & arts);
};

View File

@ -23,8 +23,8 @@ CArtifactsOfHeroAltar::CArtifactsOfHeroAltar(const Point & position)
: visibleArtSet(ArtBearer::ArtBearer::HERO)
{
init(
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1, _2),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1, _2),
position,
std::bind(&CArtifactsOfHeroAltar::scrollBackpack, this, _1));
pickedArtFromSlot = ArtifactPosition::PRE_FIRST;
@ -69,7 +69,7 @@ void CArtifactsOfHeroAltar::scrollBackpack(int offset)
redraw();
}
void CArtifactsOfHeroAltar::pickUpArtifact(CHeroArtPlace & artPlace)
void CArtifactsOfHeroAltar::pickUpArtifact(CArtPlace & artPlace)
{
if(const auto art = artPlace.getArt())
{

View File

@ -26,7 +26,7 @@ public:
void updateWornSlots() override;
void updateBackpackSlots() override;
void scrollBackpack(int offset) override;
void pickUpArtifact(CHeroArtPlace & artPlace);
void pickUpArtifact(CArtPlace & artPlace);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickedArtMoveToAltar(const ArtifactPosition & slot);
void deleteFromVisible(const CArtifactInstance * artInst);

View File

@ -46,7 +46,7 @@ void CArtifactsOfHeroBackpack::swapArtifacts(const ArtifactLocation & srcLoc, co
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroBackpack::pickUpArtifact(CHeroArtPlace & artPlace)
void CArtifactsOfHeroBackpack::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
@ -96,8 +96,8 @@ void CArtifactsOfHeroBackpack::initAOHbackpack(size_t slots, bool slider)
backpackSlotsBackgrounds.emplace_back(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/artifactSlotEmpty"), pos));
artPlace = std::make_shared<CHeroArtPlace>(pos);
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace->showPopupCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1, _2));
artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1, _2));
artPlaceIdx++;
}

View File

@ -25,7 +25,7 @@ public:
CArtifactsOfHeroBackpack();
CArtifactsOfHeroBackpack(const Point & position);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CHeroArtPlace & artPlace);
void pickUpArtifact(CArtPlace & artPlace);
void scrollBackpack(int offset) override;
void updateBackpackSlots() override;
size_t getActiveSlotRowsNum();

View File

@ -56,8 +56,8 @@ void CArtifactsOfHeroBase::setPutBackPickedArtifactCallback(PutBackPickedArtCall
}
void CArtifactsOfHeroBase::init(
CHeroArtPlace::ClickFunctor lClickCallback,
CHeroArtPlace::ClickFunctor showPopupCallback,
CArtPlace::ClickFunctor lClickCallback,
CArtPlace::ClickFunctor showPopupCallback,
const Point & position,
BpackScrollFunctor scrollCallback)
{
@ -78,14 +78,14 @@ void CArtifactsOfHeroBase::init(
{
artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr);
artPlace.second->leftClickCallback = lClickCallback;
artPlace.second->showPopupCallback = showPopupCallback;
artPlace.second->setClickPressedCallback(lClickCallback);
artPlace.second->setShowPopupCallback(showPopupCallback);
}
for(auto artPlace : backpack)
{
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = lClickCallback;
artPlace->showPopupCallback = showPopupCallback;
artPlace->setClickPressedCallback(lClickCallback);
artPlace->setShowPopupCallback(showPopupCallback);
}
leftBackpackRoll = std::make_shared<CButton>(Point(379, 364), AnimationPath::builtin("hsbtns3.def"), CButton::tooltip(), [scrollCallback]() {scrollCallback(-1);}, EShortcut::MOVE_LEFT);
rightBackpackRoll = std::make_shared<CButton>(Point(632, 364), AnimationPath::builtin("hsbtns5.def"), CButton::tooltip(), [scrollCallback]() {scrollCallback(+1);}, EShortcut::MOVE_RIGHT);
@ -95,16 +95,16 @@ void CArtifactsOfHeroBase::init(
setRedrawParent(true);
}
void CArtifactsOfHeroBase::leftClickArtPlace(CHeroArtPlace & artPlace)
void CArtifactsOfHeroBase::leftClickArtPlace(CArtPlace & artPlace, const Point & cursorPosition)
{
if(leftClickCallback)
leftClickCallback(*this, artPlace);
leftClickCallback(*this, artPlace, cursorPosition);
}
void CArtifactsOfHeroBase::rightClickArtPlace(CHeroArtPlace & artPlace)
void CArtifactsOfHeroBase::rightClickArtPlace(CArtPlace & artPlace, const Point & cursorPosition)
{
if(showPopupCallback)
showPopupCallback(*this, artPlace);
showPopupCallback(*this, artPlace, cursorPosition);
}
void CArtifactsOfHeroBase::setHero(const CGHeroInstance * hero)

View File

@ -21,7 +21,7 @@ protected:
public:
using ArtPlaceMap = std::map<ArtifactPosition, ArtPlacePtr>;
using ClickFunctor = std::function<void(CArtifactsOfHeroBase&, CHeroArtPlace&)>;
using ClickFunctor = std::function<void(CArtifactsOfHeroBase&, CArtPlace&, const Point&)>;
using PutBackPickedArtCallback = std::function<void()>;
ClickFunctor leftClickCallback;
@ -30,8 +30,8 @@ public:
CArtifactsOfHeroBase();
virtual void putBackPickedArtifact();
virtual void setPutBackPickedArtifactCallback(PutBackPickedArtCallback callback);
virtual void leftClickArtPlace(CHeroArtPlace & artPlace);
virtual void rightClickArtPlace(CHeroArtPlace & artPlace);
virtual void leftClickArtPlace(CArtPlace & artPlace, const Point & cursorPosition);
virtual void rightClickArtPlace(CArtPlace & artPlace, const Point & cursorPosition);
virtual void setHero(const CGHeroInstance * hero);
virtual const CGHeroInstance * getHero() const;
virtual void scrollBackpack(int offset);

View File

@ -30,14 +30,14 @@ CArtifactsOfHeroKingdom::CArtifactsOfHeroKingdom(ArtPlaceMap ArtWorn, std::vecto
{
artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr);
artPlace.second->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace.second->showPopupCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
artPlace.second->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1, _2));
artPlace.second->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1, _2));
}
for(auto artPlace : backpack)
{
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace->showPopupCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
artPlace->setClickPressedCallback(std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1, _2));
artPlace->setShowPopupCallback(std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1, _2));
}
leftBackpackRoll->addCallback(std::bind(&CArtifactsOfHeroBase::scrollBackpack, this, -1));
rightBackpackRoll->addCallback(std::bind(&CArtifactsOfHeroBase::scrollBackpack, this, +1));
@ -55,7 +55,7 @@ void CArtifactsOfHeroKingdom::swapArtifacts(const ArtifactLocation & srcLoc, con
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroKingdom::pickUpArtifact(CHeroArtPlace & artPlace)
void CArtifactsOfHeroKingdom::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));

View File

@ -24,5 +24,5 @@ public:
std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll);
~CArtifactsOfHeroKingdom();
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CHeroArtPlace & artPlace);
void pickUpArtifact(CArtPlace & artPlace);
};

View File

@ -19,8 +19,8 @@
CArtifactsOfHeroMain::CArtifactsOfHeroMain(const Point & position)
{
init(
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1, _2),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1, _2),
position,
std::bind(&CArtifactsOfHeroBase::scrollBackpack, this, _1));
}
@ -35,7 +35,7 @@ void CArtifactsOfHeroMain::swapArtifacts(const ArtifactLocation & srcLoc, const
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroMain::pickUpArtifact(CHeroArtPlace & artPlace)
void CArtifactsOfHeroMain::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));

View File

@ -23,5 +23,5 @@ public:
CArtifactsOfHeroMain(const Point & position);
~CArtifactsOfHeroMain();
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CHeroArtPlace & artPlace);
void pickUpArtifact(CArtPlace & artPlace);
};

View File

@ -15,8 +15,8 @@
CArtifactsOfHeroMarket::CArtifactsOfHeroMarket(const Point & position)
{
init(
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1, _2),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1, _2),
position,
std::bind(&CArtifactsOfHeroMarket::scrollBackpack, this, _1));
};

View File

@ -14,7 +14,7 @@
class CArtifactsOfHeroMarket : public CArtifactsOfHeroBase
{
public:
std::function<void(CHeroArtPlace*)> selectArtCallback;
std::function<void(CArtPlace*)> selectArtCallback;
CArtifactsOfHeroMarket(const Point & position);
void scrollBackpack(int offset) override;

View File

@ -48,8 +48,8 @@ void CWindowWithArtifacts::addSetAndCallbacks(CArtifactsOfHeroPtr artSet)
std::visit([this, artPutBackHandler](auto artSetWeak)
{
auto artSet = artSetWeak.lock();
artSet->leftClickCallback = std::bind(&CWindowWithArtifacts::leftClickArtPlaceHero, this, _1, _2);
artSet->showPopupCallback = std::bind(&CWindowWithArtifacts::rightClickArtPlaceHero, this, _1, _2);
artSet->leftClickCallback = std::bind(&CWindowWithArtifacts::leftClickArtPlaceHero, this, _1, _2, _3);
artSet->showPopupCallback = std::bind(&CWindowWithArtifacts::rightClickArtPlaceHero, this, _1, _2, _3);
artSet->setPutBackPickedArtifactCallback(artPutBackHandler);
}, artSet);
}
@ -77,7 +77,7 @@ const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact()
return nullptr;
}
void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition)
{
const auto artSetWeak = findAOHbyRef(artsInst);
assert(artSetWeak.has_value());
@ -85,7 +85,7 @@ void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst
if(artPlace.isLocked())
return;
const auto checkSpecialArts = [](const CGHeroInstance * hero, CHeroArtPlace & artPlace) -> bool
const auto checkSpecialArts = [](const CGHeroInstance * hero, CArtPlace & artPlace) -> bool
{
if(artPlace.getArt()->getTypeId() == ArtifactID::SPELLBOOK)
{
@ -217,7 +217,7 @@ void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst
}, artSetWeak.value());
}
void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition)
{
const auto artSetWeak = findAOHbyRef(artsInst);
assert(artSetWeak.has_value());
@ -226,7 +226,7 @@ void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsIns
return;
std::visit(
[&artPlace](auto artSetWeak) -> void
[&artPlace, &cursorPosition](auto artSetWeak) -> void
{
const auto artSetPtr = artSetWeak.lock();
@ -247,7 +247,7 @@ void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsIns
return;
}
if(artPlace.text.size())
artPlace.LRClickableAreaWTextComp::showPopupWindow(GH.getCursorPosition());
artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
}
}
// Altar window, Market window right click handler
@ -257,7 +257,7 @@ void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsIns
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroQuickBackpack>>)
{
if(artPlace.getArt() && artPlace.text.size())
artPlace.LRClickableAreaWTextComp::showPopupWindow(GH.getCursorPosition());
artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
}
}, artSetWeak.value());
}

View File

@ -33,8 +33,8 @@ public:
void addCloseCallback(CloseCallback callback);
const CGHeroInstance * getHeroPickedArtifact();
const CArtifactInstance * getPickedArtifact();
void leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace);
void rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace);
void leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition);
void rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition);
void artifactRemoved(const ArtifactLocation & artLoc) override;
void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw) override;

View File

@ -111,6 +111,5 @@ public:
void createBackpackWindow();
//friends
friend void CHeroArtPlace::clickPressed(const Point & cursorPosition);
friend class CPlayerInterface;
};

View File

@ -331,7 +331,7 @@ void CTradeWindow::setMode(EMarketMode Mode)
}
}
void CTradeWindow::artifactSelected(CHeroArtPlace *slot)
void CTradeWindow::artifactSelected(CArtPlace * slot)
{
assert(mode == EMarketMode::ARTIFACT_RESOURCE);
items[1][0]->setArtInstance(slot->getArt());

View File

@ -40,7 +40,7 @@ public:
void getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const;
void setMode(EMarketMode Mode); //mode setter
void artifactSelected(CHeroArtPlace *slot); //used when selling artifacts -> called when user clicked on artifact slot
void artifactSelected(CArtPlace * slot); //used when selling artifacts -> called when user clicked on artifact slot
virtual void selectionChanged(bool side) = 0; //true == left
virtual Point selectionOffset(bool Left) const = 0;
virtual std::string updateSlotSubtitle(bool Left) const = 0;