1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

class CArtifactsOfHeroMain : public CKeyShortcut

This commit is contained in:
SoundSSGood 2024-04-27 02:08:47 +03:00
parent ce9d2d8ab8
commit ce68b3f45d
13 changed files with 67 additions and 39 deletions

View File

@ -314,20 +314,12 @@ CKeyShortcut::CKeyShortcut(EShortcut key)
{
}
CKeyShortcut::CKeyShortcut(const EShortcut & key, const KeyPressedFunctor & keyPressedCallback)
: CKeyShortcut(key)
{
this->keyPressedCallback = keyPressedCallback;
}
void CKeyShortcut::keyPressed(EShortcut key)
{
if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
{
shortcutPressed = true;
clickPressed(GH.getCursorPosition());
if(keyPressedCallback)
keyPressedCallback();
}
}

View File

@ -134,18 +134,12 @@ public:
class CKeyShortcut : public virtual CIntObject
{
public:
using KeyPressedFunctor = std::function<void()>;
bool shortcutPressed;
EShortcut assignedKey;
CKeyShortcut();
CKeyShortcut(EShortcut key);
CKeyShortcut(const EShortcut & key, const KeyPressedFunctor & keyPressedCallback);
void keyPressed(EShortcut key) override;
void keyReleased(EShortcut key) override;
private:
bool shortcutPressed;
KeyPressedFunctor keyPressedCallback;
};
class WindowBase : public CIntObject

View File

@ -13,7 +13,7 @@
class CButton;
class CArtifactsOfHeroBase : public CIntObject
class CArtifactsOfHeroBase : virtual public CIntObject
{
protected:
using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>;

View File

@ -35,15 +35,56 @@ CArtifactsOfHeroMain::~CArtifactsOfHeroMain()
void CArtifactsOfHeroMain::enableArtifactsCostumeSwitcher()
{
size_t costumeIdx = 0;
for(const auto & hotkey : costumesSwitcherHotkeys)
addUsedEvents(AEventsReceiver::KEYBOARD);
}
void CArtifactsOfHeroMain::keyPressed(EShortcut key)
{
auto keyProc = costumesSwitcherProcessors.emplace_back(std::make_shared<CKeyShortcut>(hotkey,
[this, costumeIdx]()
if(!shortcutPressed)
{
uint32_t costumeIdx;
switch(key)
{
case EShortcut::HERO_COSTUME_0:
costumeIdx = 0;
break;
case EShortcut::HERO_COSTUME_1:
costumeIdx = 1;
break;
case EShortcut::HERO_COSTUME_2:
costumeIdx = 2;
break;
case EShortcut::HERO_COSTUME_3:
costumeIdx = 3;
break;
case EShortcut::HERO_COSTUME_4:
costumeIdx = 4;
break;
case EShortcut::HERO_COSTUME_5:
costumeIdx = 5;
break;
case EShortcut::HERO_COSTUME_6:
costumeIdx = 6;
break;
case EShortcut::HERO_COSTUME_7:
costumeIdx = 7;
break;
case EShortcut::HERO_COSTUME_8:
costumeIdx = 8;
break;
case EShortcut::HERO_COSTUME_9:
costumeIdx = 9;
break;
default:
return;
}
shortcutPressed = true;
LOCPLINT->cb->manageHeroCostume(getHero()->id, costumeIdx, GH.isKeyboardCtrlDown());
}));
keyProc->addUsedEvents(AEventsReceiver::KEYBOARD);
costumeIdx++;
}
}
void CArtifactsOfHeroMain::keyReleased(EShortcut key)
{
if(vstd::contains(costumesSwitcherHotkeys, key))
shortcutPressed = false;
}

View File

@ -13,12 +13,14 @@
#include "../gui/Shortcut.h"
class CArtifactsOfHeroMain : public CArtifactsOfHeroBase
class CArtifactsOfHeroMain : public CArtifactsOfHeroBase, public CKeyShortcut
{
public:
CArtifactsOfHeroMain(const Point & position);
~CArtifactsOfHeroMain() override;
void enableArtifactsCostumeSwitcher();
void keyPressed(EShortcut key) override;
void keyReleased(EShortcut key) override;
private:
const std::vector<EShortcut> costumesSwitcherHotkeys =
@ -34,5 +36,4 @@ private:
EShortcut::HERO_COSTUME_8,
EShortcut::HERO_COSTUME_9
};
std::vector<std::shared_ptr<CKeyShortcut>> costumesSwitcherProcessors;
};

View File

@ -343,7 +343,7 @@ void CWindowWithArtifacts::deactivate()
CWindowObject::deactivate();
}
void CWindowWithArtifacts::enableArtifactsCostumeSwitcher()
void CWindowWithArtifacts::enableArtifactsCostumeSwitcher() const
{
for(auto artSet : artSets)
std::visit(

View File

@ -42,7 +42,7 @@ public:
void gestureArtPlaceHero(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition);
void activate() override;
void deactivate() override;
void enableArtifactsCostumeSwitcher();
void enableArtifactsCostumeSwitcher() const;
virtual void artifactRemoved(const ArtifactLocation & artLoc);
virtual void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw);

View File

@ -249,7 +249,7 @@ class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
{
public:
CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
CArtifactFittingSet(const CArtifactSet & artSet);
explicit CArtifactFittingSet(const CArtifactSet & artSet);
ArtBearer::ArtBearer bearerType() const override;
protected:

View File

@ -1078,7 +1078,7 @@ void ChangeArtifactsCostume::applyGs(CGameState * gs) const
if(const auto & costume = allCostumes.find(costumeIdx); costume != allCostumes.end())
costume->second = costumeSet;
else
allCostumes.emplace(costumeIdx, costumeSet);
allCostumes.try_emplace(costumeIdx, costumeSet);
}
void PlayerEndsGame::applyGs(CGameState * gs) const

View File

@ -1291,14 +1291,14 @@ struct DLL_LINKAGE ChangeObjectVisitors : public CPackForClient
struct DLL_LINKAGE ChangeArtifactsCostume : public CPackForClient
{
std::map<ArtifactPosition, ArtifactID> costumeSet;
size_t costumeIdx = 0;
uint32_t costumeIdx = 0;
const PlayerColor player = PlayerColor::NEUTRAL;
void applyGs(CGameState * gs) const;
void visitTyped(ICPackVisitor & visitor) override;
ChangeArtifactsCostume() = default;
ChangeArtifactsCostume(const PlayerColor & player, const size_t costumeIdx)
ChangeArtifactsCostume(const PlayerColor & player, const uint32_t costumeIdx)
: costumeIdx(costumeIdx)
, player(player)
{

View File

@ -432,7 +432,7 @@ struct DLL_LINKAGE ManageBackpackArtifacts : public CPackForServer
struct DLL_LINKAGE ManageEquippedArtifacts : public CPackForServer
{
ManageEquippedArtifacts() = default;
ManageEquippedArtifacts(const ObjectInstanceID & artHolder, const size_t costumeIdx, bool saveCostume = false)
ManageEquippedArtifacts(const ObjectInstanceID & artHolder, const uint32_t costumeIdx, bool saveCostume = false)
: artHolder(artHolder)
, costumeIdx(costumeIdx)
, saveCostume(saveCostume)
@ -440,7 +440,7 @@ struct DLL_LINKAGE ManageEquippedArtifacts : public CPackForServer
}
ObjectInstanceID artHolder;
size_t costumeIdx;
uint32_t costumeIdx;
bool saveCostume;
void visitTyped(ICPackVisitor & visitor) override;

View File

@ -2892,7 +2892,7 @@ bool CGameHandler::scrollBackpackArtifacts(const PlayerColor & player, const Obj
return true;
}
bool CGameHandler::saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx)
bool CGameHandler::saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx)
{
auto artSet = getArtSet(heroID);
COMPLAIN_RET_FALSE_IF(artSet == nullptr, "saveArtifactsCostume: wrong hero's ID");
@ -2908,7 +2908,7 @@ bool CGameHandler::saveArtifactsCostume(const PlayerColor & player, const Object
return true;
}
bool CGameHandler::switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx)
bool CGameHandler::switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx)
{
const auto artSet = getArtSet(heroID);
COMPLAIN_RET_FALSE_IF(artSet == nullptr, "switchArtifactsCostume: wrong hero's ID");

View File

@ -130,8 +130,8 @@ public:
bool moveArtifact(const PlayerColor & player, const ArtifactLocation & src, const ArtifactLocation & dst) override;
bool bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceID srcId, ObjectInstanceID dstId, bool swap, bool equipped, bool backpack);
bool scrollBackpackArtifacts(const PlayerColor & player, const ObjectInstanceID heroID, bool left);
bool saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx);
bool switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, size_t costumeIdx);
bool saveArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx);
bool switchArtifactsCostume(const PlayerColor & player, const ObjectInstanceID heroID, uint32_t costumeIdx);
bool eraseArtifactByClient(const ArtifactLocation & al);
void synchronizeArtifactHandlerLists();