From 96fa0fb6d440c562ad691cd8473c874e09e3cdd9 Mon Sep 17 00:00:00 2001 From: dydzio Date: Sun, 6 Nov 2016 13:02:00 +0100 Subject: [PATCH] Implement giving commander artifacts back to hero --- client/widgets/CArtifactHolder.cpp | 23 ++++++++++++++++++++--- client/widgets/CArtifactHolder.h | 6 +++++- client/windows/CCreatureWindow.cpp | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index 6f3f27b1c..602a9e3d9 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -960,7 +960,7 @@ void CArtPlace::clickRight(tribool down, bool previousState) LRClickableAreaWTextComp::clickRight(down, previousState); } -CCommanderArtPlace::CCommanderArtPlace(Point position, const CArtifactInstance * Art) : CArtPlace(position, Art) +CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num) { createImage(); setArtifact(Art); @@ -968,8 +968,9 @@ CCommanderArtPlace::CCommanderArtPlace(Point position, const CArtifactInstance * void CCommanderArtPlace::clickLeft(tribool down, bool previousState) { - if(ourArt && text.size()) - CArtPlace::clickLeft(down, previousState); + if (down && ourArt && text.size()) + LOCPLINT->showYesNoDialog("Do you want to give this artifact back to hero?", [this] { returnArtToHeroCallback(); }, [] {}); + //CArtPlace::clickLeft(down, previousState); } void CCommanderArtPlace::clickRight(tribool down, bool previousState) @@ -991,6 +992,22 @@ void CCommanderArtPlace::createImage() image->disable(); } +void CCommanderArtPlace::returnArtToHeroCallback() +{ + ArtifactPosition artifactPos = commanderSlotID;; + ArtifactPosition freeSlot = ourArt->firstBackpackSlot(commanderOwner); + + ArtifactLocation src(commanderOwner->commander.get(), artifactPos); + ArtifactLocation dst(commanderOwner, freeSlot); + + if (ourArt->canBePutAt(dst, true)) + { + LOCPLINT->cb->swapArtifacts(src, dst); + setArtifact(nullptr); + parent->redraw(); + } +} + void CCommanderArtPlace::setArtifact(const CArtifactInstance * art) { baseType = -1; //by default we don't store any component diff --git a/client/widgets/CArtifactHolder.h b/client/widgets/CArtifactHolder.h index f9756e960..46419ab45 100644 --- a/client/widgets/CArtifactHolder.h +++ b/client/widgets/CArtifactHolder.h @@ -59,9 +59,13 @@ public: class CCommanderArtPlace : public CArtPlace { protected: + const CGHeroInstance * commanderOwner; + ArtifactPosition commanderSlotID; + void createImage() override; + void returnArtToHeroCallback(); public: - CCommanderArtPlace(Point position, const CArtifactInstance * Art = nullptr); //c-tor + CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art = nullptr); //c-tor void clickLeft(tribool down, bool previousState) override; void clickRight(tribool down, bool previousState) override; diff --git a/client/windows/CCreatureWindow.cpp b/client/windows/CCreatureWindow.cpp index 4ff124298..c927fba92 100644 --- a/client/windows/CCreatureWindow.cpp +++ b/client/windows/CCreatureWindow.cpp @@ -461,7 +461,7 @@ void CStackWindow::CWindowSection::createCommander() { Point artPos = getArtifactPos(equippedArtifact.first); //auto icon = new CClickableObject(new CAnimImage("artifact", equippedArtifact.second.artifact.get()->artType.get()->iconIndex, 0, artPos.x, artPos.y), [=] {}); - auto icon = new CCommanderArtPlace(artPos, equippedArtifact.second.artifact); + auto icon = new CCommanderArtPlace(artPos, parent->info->owner, equippedArtifact.first, equippedArtifact.second.artifact); //TODO: Use CArtPlace or equivalent instead of CClickableObject and handle commander artifact actions to match WOG (return artifact to hero etc.) }