1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-01-25 01:36:38 +00:00

Turns out using IDs was a bad idea, reverting to KEY_* strings

This commit is contained in:
fgenesis 2017-02-18 22:09:43 +01:00
parent 3dda97d32a
commit eccadf5bd7
6 changed files with 75 additions and 81 deletions

View file

@ -23,7 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "ActionMapper.h" #include "ActionMapper.h"
#include "Core.h" #include "Core.h"
#include "SDL.h" #include "SDL.h"
#include "LegacyKeycodes.h" #include "GameKeyNames.h"
static std::string inputcode2string(int k) static std::string inputcode2string(int k)
@ -32,35 +32,34 @@ static std::string inputcode2string(int k)
return std::string(); return std::string();
if(k < KEY_MAXARRAY) if(k < KEY_MAXARRAY)
{ {
// See parseKey() below // Returns KEY_* or NULL
std::stringstream os; const std::string& str = getKeyNameFromInputCode(k);
os << "K:"; if(str.length())
#ifdef BBGE_BUILD_SDL2 return str;
int keycode = SDL_GetKeyFromScancode((SDL_Scancode)k);
os << keycode << "," << k; // fallback
#else std::ostringstream os;
os << k; os << "K:" << k;
#endif
return os.str(); return os.str();
} }
if(k >= JOY_BUTTON_0 && k < JOY_BUTTON_END) if(k >= JOY_BUTTON_0 && k < JOY_BUTTON_END)
{ {
std::stringstream os; std::ostringstream os;
os << "JB:" << (k - JOY_BUTTON_0); os << "JB:" << (k - JOY_BUTTON_0);
return os.str(); return os.str();
} }
if(k >= JOY_AXIS_0_POS && k < JOY_AXIS_END_POS) if(k >= JOY_AXIS_0_POS && k < JOY_AXIS_END_POS)
{ {
std::stringstream os; std::ostringstream os;
os << "AX:+" << (k - JOY_AXIS_0_POS); os << "AX:+" << (k - JOY_AXIS_0_POS);
return os.str(); return os.str();
} }
if(k >= JOY_AXIS_0_NEG && k < JOY_AXIS_END_NEG) if(k >= JOY_AXIS_0_NEG && k < JOY_AXIS_END_NEG)
{ {
std::stringstream os; std::ostringstream os;
os << "AX:-" << (k - JOY_AXIS_0_NEG); os << "AX:-" << (k - JOY_AXIS_0_NEG);
return os.str(); return os.str();
} }
@ -97,14 +96,10 @@ static const char *jbtnname(int joystickID, int btn)
return j ? j->getButtonName(btn) : NULL; return j ? j->getButtonName(btn) : NULL;
} }
std::string getInputCodeToString(int k) std::string getInputCodeToString(int k)
{ {
std::string s = inputcode2string(k); std::string s = inputcode2string(k);
if(s.empty()) return s.empty() ? "0" : s;
return "NONE";
return spacesToUnderscores(s);
} }
std::string getInputCodeToUserString(unsigned int k, size_t joystickID) std::string getInputCodeToUserString(unsigned int k, size_t joystickID)
@ -117,6 +112,7 @@ std::string getInputCodeToUserString(unsigned int k, size_t joystickID)
if(k < KEY_MAXARRAY) if(k < KEY_MAXARRAY)
{ {
#ifdef BBGE_BUILD_SDL2 #ifdef BBGE_BUILD_SDL2
pretty = SDL_GetScancodeName((SDL_Scancode)k);
const SDL_Keycode kcode = SDL_GetKeyFromScancode((SDL_Scancode)k); const SDL_Keycode kcode = SDL_GetKeyFromScancode((SDL_Scancode)k);
if(kcode != SDLK_UNKNOWN) if(kcode != SDLK_UNKNOWN)
pretty = SDL_GetKeyName(kcode); pretty = SDL_GetKeyName(kcode);
@ -145,38 +141,8 @@ std::string getInputCodeToUserString(unsigned int k, size_t joystickID)
return s; return s;
} }
return inputcode2string(k); std::string s = inputcode2string(k);
} return s.empty() ? "-" : s;
// two comma-separated ints
// first is the keycode, second the scancode
// (Keymap-independent) Scancodes are used when built with SDL2 support and specified
// (Keymap-dependent) Keycode is used otherwise
static int parseKey(const char *ks)
{
int k = 0;
#ifdef BBGE_BUILD_SDL2
if(const char *comma = strchr(ks, ','))
{
k = atoi(comma + 1);
if(k && k < KEY_MAXARRAY)
return k;
}
#endif
// Use the keycode
k = atoi(ks);
if(k < KEY_MAXARRAY)
{
#ifdef BBGE_BUILD_SDL2
// But when we're on SDL2, don't forget to turn they keycode back into a scancode, since we work with scancodes internally
k = SDL_GetScancodeFromKey(k);
#endif
return k;
}
return 0;
} }
int getStringToInputCode(const std::string& s) int getStringToInputCode(const std::string& s)
@ -188,7 +154,7 @@ int getStringToInputCode(const std::string& s)
if(s == "MMB") if(s == "MMB")
return MOUSE_BUTTON_MIDDLE; return MOUSE_BUTTON_MIDDLE;
if(!strncmp(s.c_str(), "K:", 2)) if(!strncmp(s.c_str(), "K:", 2))
return parseKey(s.c_str() + 2); return atoi(s.c_str() + 2);
if(!strncmp(s.c_str(), "JB:", 3)) if(!strncmp(s.c_str(), "JB:", 3))
return JOY_BUTTON_0 + atoi(s.c_str() + 3); return JOY_BUTTON_0 + atoi(s.c_str() + 3);
if(!strncmp(s.c_str(), "MB:", 3)) if(!strncmp(s.c_str(), "MB:", 3))
@ -203,12 +169,10 @@ int getStringToInputCode(const std::string& s)
default: return 0; default: return 0;
} }
} }
if(s == "NONE")
return 0;
// Maybe we're upgrading from an old config? // Maybe we're upgrading from an old config?
// Note that this returns 0 for "0", which was considered "no key" // This handles KEY_* and some old mouse/joystick names.
if(int k = getInputCodeFromLegacyName(s.c_str())) if(int k = getInputCodeFromKeyName(s.c_str()))
return k; return k;
return 0; return 0;
@ -218,9 +182,8 @@ int getStringToInputCode(const std::string& s)
ActionInput::ActionInput() ActionInput::ActionInput()
{ {
for (int i = 0; i < INP_MSESIZE; i++) data.single.mse[i] = 0; for (int i = 0; i < INP_COMBINED_SIZE; i++)
for (int i = 0; i < INP_KEYSIZE; i++) data.single.key[i] = 0; data.all[i] = 0;
for (int i = 0; i < INP_JOYSIZE; i++) data.single.joy[i] = 0;
} }
std::string ActionInput::toString() const std::string ActionInput::toString() const

View file

@ -1,11 +1,12 @@
#include "LegacyKeycodes.h" #include "GameKeyNames.h"
#include "ActionStatus.h" #include "ActionStatus.h"
#include <stdio.h>
#include <map> #include <map>
#include <sstream>
typedef std::map<std::string, int> InputCodeMap; typedef std::map<std::string, int> InputCodeMap;
InputCodeMap inputCodeMap; InputCodeMap inputCodeMap;
static std::string keyNames[KEY_MAXARRAY];
static void initInputCodeMap() static void initInputCodeMap()
{ {
@ -84,6 +85,22 @@ static void initInputCodeMap()
K(KEY_RETURN) K(KEY_RETURN)
K(KEY_TAB) K(KEY_TAB)
K(KEY_ESCAPE) K(KEY_ESCAPE)
K(KEY_SPACE)
K(KEY_BACKSPACE)
K(KEY_NUMPADMINUS)
K(KEY_NUMPADPERIOD)
K(KEY_NUMPADPLUS)
K(KEY_NUMPADSLASH)
K(KEY_NUMPADSTAR)
K(KEY_PGDN)
K(KEY_PGUP)
K(KEY_APOSTROPHE)
K(KEY_EQUALS)
K(KEY_SEMICOLON)
K(KEY_LBRACKET)
K(KEY_RBRACKET)
K(KEY_TILDE)
K(MOUSE_BUTTON_LEFT) K(MOUSE_BUTTON_LEFT)
K(MOUSE_BUTTON_RIGHT) K(MOUSE_BUTTON_RIGHT)
@ -92,20 +109,30 @@ static void initInputCodeMap()
for (int jb = JOY_BUTTON_0; jb < JOY_BUTTON_END; jb++) for (int jb = JOY_BUTTON_0; jb < JOY_BUTTON_END; jb++)
{ {
std::ostringstream os; char buf[32];
os << "JOY_BUTTON_" << jb - JOY_BUTTON_0; sprintf(buf, "JOY_BUTTON_%d", jb - JOY_BUTTON_0);
inputCodeMap[os.str()] = jb; inputCodeMap[buf] = jb;
} }
// ----------------------
// Can just use pointers to the strings in the map; they'll stay where they are in memory
for(InputCodeMap::iterator it = inputCodeMap.begin(); it != inputCodeMap.end(); ++it)
keyNames[it->second] = it->first;
} }
int getInputCodeFromLegacyName(const char *name) int getInputCodeFromKeyName(const char *name)
{ {
return inputCodeMap[name]; return inputCodeMap[name];
} }
const std::string& getKeyNameFromInputCode(int k)
{
return keyNames[k];
}
struct LegacyKeymapInitializer struct KeyNameInitializer
{ {
LegacyKeymapInitializer() { initInputCodeMap(); } KeyNameInitializer() { initInputCodeMap(); }
}; };
static LegacyKeymapInitializer s_kinit; static KeyNameInitializer s_kinit;

9
BBGE/GameKeyNames.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef BBGE_LEGACYKEYCODES_H
#define BBGE_LEGACYKEYCODES_H
#include <string>
int getInputCodeFromKeyName(const char *name);
const std::string& getKeyNameFromInputCode(int k);
#endif

View file

@ -1,6 +0,0 @@
#ifndef BBGE_LEGACYKEYCODES_H
#define BBGE_LEGACYKEYCODES_H
int getInputCodeFromLegacyName(const char *name);
#endif

View file

@ -432,6 +432,7 @@ SET(AQUARIA_SRCS
${SRCDIR}/Entity.cpp ${SRCDIR}/Entity.cpp
${SRCDIR}/FlockEntity.cpp ${SRCDIR}/FlockEntity.cpp
${SRCDIR}/Game.cpp ${SRCDIR}/Game.cpp
${SRCDIR}/GameKeyNames.cpp
${SRCDIR}/GameStructs.cpp ${SRCDIR}/GameStructs.cpp
${SRCDIR}/GameplayVariables.cpp ${SRCDIR}/GameplayVariables.cpp
${SRCDIR}/GasCloud.cpp ${SRCDIR}/GasCloud.cpp

View file

@ -362,6 +362,14 @@
RelativePath="..\..\BBGE\FrameBuffer.h" RelativePath="..\..\BBGE\FrameBuffer.h"
> >
</File> </File>
<File
RelativePath="..\..\BBGE\GameKeyNames.cpp"
>
</File>
<File
RelativePath="..\..\BBGE\GameKeyNames.h"
>
</File>
<File <File
RelativePath="..\..\BBGE\GameKeys.h" RelativePath="..\..\BBGE\GameKeys.h"
> >
@ -390,14 +398,6 @@
RelativePath="..\..\BBGE\Joystick.h" RelativePath="..\..\BBGE\Joystick.h"
> >
</File> </File>
<File
RelativePath="..\..\BBGE\LegacyKeycodes.cpp"
>
</File>
<File
RelativePath="..\..\BBGE\LegacyKeycodes.h"
>
</File>
<File <File
RelativePath="..\..\BBGE\LensFlare.cpp" RelativePath="..\..\BBGE\LensFlare.cpp"
> >