Browse Source

Keymapper: add enabled condition, fix being able

to save/load in multiplayer
pull/2095/head
Jmgr 5 years ago committed by Anders Jenbo
parent
commit
b4ea4c29a0
  1. 6
      Source/controls/keymapper.cpp
  2. 17
      Source/controls/keymapper.hpp
  3. 30
      Source/diablo.cpp

6
Source/controls/keymapper.cpp

@ -43,7 +43,7 @@ Keymapper::ActionIndex Keymapper::addAction(const Action &action)
return actions.size() - 1; return actions.size() - 1;
} }
void Keymapper::keyPressed(int key, bool isPlayerDead) const void Keymapper::keyPressed(int key) const
{ {
auto it = keyIDToAction.find(key); auto it = keyIDToAction.find(key);
if (it == keyIDToAction.end()) if (it == keyIDToAction.end())
@ -51,8 +51,8 @@ void Keymapper::keyPressed(int key, bool isPlayerDead) const
auto &action = it->second; auto &action = it->second;
// Some actions cannot be triggered while the player is dead. // Some actions cannot always be triggered.
if (isPlayerDead && action.get().ifDead == Action::IfDead::Ignore) if (!action.get().enable())
return; return;
action(); action();

17
Source/controls/keymapper.hpp

@ -21,25 +21,18 @@ public:
*/ */
class Action final { class Action final {
public: public:
/** Can this action be triggered while the player is dead? */
enum class IfDead {
Allow,
Ignore,
};
Action(const std::string &name, int defaultKey, std::function<void()> action) Action(const std::string &name, int defaultKey, std::function<void()> action)
: name(name) : name(name)
, defaultKey(defaultKey) , defaultKey(defaultKey)
, action(action) , action(action)
, ifDead(IfDead::Ignore) , enable([] { return true; })
{ {
} }
Action(const std::string &name, int defaultKey, std::function<void()> action, IfDead ifDead) Action(const std::string &name, int defaultKey, std::function<void()> action, std::function<bool()> enable)
: name(name) : name(name)
, defaultKey(defaultKey) , defaultKey(defaultKey)
, action(action) , action(action)
, ifDead(ifDead) , enable(enable)
{ {
} }
@ -52,7 +45,7 @@ public:
std::string name; std::string name;
int defaultKey; int defaultKey;
std::function<void()> action; std::function<void()> action;
IfDead ifDead; std::function<bool()> enable;
int key {}; int key {};
friend class Keymapper; friend class Keymapper;
@ -67,7 +60,7 @@ public:
Keymapper(SetConfigKeyFunction setKeyFunction, GetConfigKeyFunction getKeyFunction); Keymapper(SetConfigKeyFunction setKeyFunction, GetConfigKeyFunction getKeyFunction);
ActionIndex addAction(const Action &action); ActionIndex addAction(const Action &action);
void keyPressed(int key, bool isPlayerDead) const; void keyPressed(int key) const;
std::string keyNameForAction(ActionIndex actionIndex) const; std::string keyNameForAction(ActionIndex actionIndex) const;
void save() const; void save() const;
void load(); void load();

30
Source/diablo.cpp

@ -1158,7 +1158,7 @@ static void PressKey(int vkey)
if (sgnTimeoutCurs != CURSOR_NONE) { if (sgnTimeoutCurs != CURSOR_NONE) {
return; return;
} }
keymapper.keyPressed(vkey, deathflag); keymapper.keyPressed(vkey);
if (vkey == DVL_VK_RETURN) { if (vkey == DVL_VK_RETURN) {
if ((GetAsyncKeyState(DVL_VK_MENU) & 0x8000) != 0) if ((GetAsyncKeyState(DVL_VK_MENU) & 0x8000) != 0)
dx_reinit(); dx_reinit();
@ -1190,7 +1190,7 @@ static void PressKey(int vkey)
return; return;
} }
keymapper.keyPressed(vkey, deathflag); keymapper.keyPressed(vkey);
if (vkey == DVL_VK_RETURN) { if (vkey == DVL_VK_RETURN) {
if ((GetAsyncKeyState(DVL_VK_MENU) & 0x8000) != 0) { if ((GetAsyncKeyState(DVL_VK_MENU) & 0x8000) != 0) {
@ -2130,23 +2130,31 @@ void spellBookKeyPressed()
invflag = false; invflag = false;
} }
bool isPlayerDead()
{
return plr[myplr]._pmode == PM_DEATH || deathflag;
}
void initKeymapActions() void initKeymapActions()
{ {
keymapper.addAction({ keymapper.addAction({
"Help", "Help",
DVL_VK_F1, DVL_VK_F1,
helpKeyPressed, helpKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
#ifdef _DEBUG #ifdef _DEBUG
keymapper.addAction({ keymapper.addAction({
"ItemInfo", "ItemInfo",
DVL_VK_INVALID, DVL_VK_INVALID,
itemInfoKeyPressed, itemInfoKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"QuestDebug", "QuestDebug",
DVL_VK_INVALID, DVL_VK_INVALID,
PrintDebugQuest, PrintDebugQuest,
[&]() { return !isPlayerDead(); },
}); });
#endif #endif
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
@ -2160,6 +2168,7 @@ void initKeymapActions()
} }
ToggleSpell(i); ToggleSpell(i);
}, },
[&]() { return !isPlayerDead(); },
}); });
} }
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
@ -2167,33 +2176,37 @@ void initKeymapActions()
spszMsgNameTbl[i], spszMsgNameTbl[i],
DVL_VK_F9 + i, DVL_VK_F9 + i,
[i]() { diablo_hotkey_msg(i); }, [i]() { diablo_hotkey_msg(i); },
Keymapper::Action::IfDead::Allow,
}); });
} }
keymapper.addAction({ keymapper.addAction({
"DecreaseGamma", "DecreaseGamma",
'G', 'G',
DecreaseGamma, DecreaseGamma,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"IncreaseGamma", "IncreaseGamma",
'F', 'F',
IncreaseGamma, IncreaseGamma,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"Inventory", "Inventory",
'I', 'I',
inventoryKeyPressed, inventoryKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"Character", "Character",
'C', 'C',
characterSheetKeyPressed, characterSheetKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"QuestLog", "QuestLog",
'Q', 'Q',
questLogKeyPressed, questLogKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"Zoom", "Zoom",
@ -2202,16 +2215,19 @@ void initKeymapActions()
zoomflag = !zoomflag; zoomflag = !zoomflag;
CalcViewportGeometry(); CalcViewportGeometry();
}, },
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"DisplaySpells", "DisplaySpells",
'S', 'S',
displaySpellsKeyPressed, displaySpellsKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"SpellBook", "SpellBook",
'B', 'B',
spellBookKeyPressed, spellBookKeyPressed,
[&]() { return !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"GameInfo", "GameInfo",
@ -2226,6 +2242,7 @@ void initKeymapActions()
strcpy(pszStr, fmt::format(_(/* TRANSLATORS: {:s} means: Character Name, Game Version, Game Difficulty. */ "{:s}, version = {:s}, mode = {:s}"), gszProductName, PROJECT_VERSION, difficulties[sgGameInitInfo.nDifficulty]).c_str()); strcpy(pszStr, fmt::format(_(/* TRANSLATORS: {:s} means: Character Name, Game Version, Game Difficulty. */ "{:s}, version = {:s}, mode = {:s}"), gszProductName, PROJECT_VERSION, difficulties[sgGameInitInfo.nDifficulty]).c_str());
NetSendCmdString(1 << myplr, pszStr); NetSendCmdString(1 << myplr, pszStr);
}, },
[&]() { return !isPlayerDead(); },
}); });
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
keymapper.addAction({ keymapper.addAction({
@ -2237,24 +2254,25 @@ void initKeymapActions()
UseInvItem(myplr, INVITEM_BELT_FIRST + i); UseInvItem(myplr, INVITEM_BELT_FIRST + i);
} }
}, },
[&]() { return !isPlayerDead(); },
}); });
} }
keymapper.addAction({ keymapper.addAction({
"QuickSave", "QuickSave",
DVL_VK_F2, DVL_VK_F2,
[] { gamemenu_save_game(false); }, [] { gamemenu_save_game(false); },
[&]() { return !gbIsMultiplayer && !isPlayerDead(); },
}); });
keymapper.addAction({ keymapper.addAction({
"QuickLoad", "QuickLoad",
DVL_VK_F3, DVL_VK_F3,
[] { gamemenu_load_game(false); }, [] { gamemenu_load_game(false); },
Keymapper::Action::IfDead::Allow, [&]() { return !gbIsMultiplayer && gbValidSaveFile; },
}); });
keymapper.addAction({ keymapper.addAction({
"QuitGame", "QuitGame",
DVL_VK_INVALID, DVL_VK_INVALID,
[] { gamemenu_quit_game(false); }, [] { gamemenu_quit_game(false); },
Keymapper::Action::IfDead::Allow,
}); });
#ifdef _DEBUG #ifdef _DEBUG
keymapper.addAction({ keymapper.addAction({
@ -2266,12 +2284,14 @@ void initKeymapActions()
return; return;
} }
}, },
[&]() { return !isPlayerDead(); },
}); });
#endif #endif
keymapper.addAction({ keymapper.addAction({
"StopHero", "StopHero",
DVL_VK_INVALID, DVL_VK_INVALID,
[] { plr[myplr].Stop(); }, [] { plr[myplr].Stop(); },
[&]() { return !isPlayerDead(); },
}); });
} }

Loading…
Cancel
Save