[Add] You can now set keybindings by key name.

This commit is contained in:
Allanis 2014-03-18 15:12:04 +00:00
parent e57782523b
commit 1fcc6be891
4 changed files with 64 additions and 6 deletions

View File

@ -130,7 +130,8 @@ int conf_loadConfig(const char* file) {
int i; int i;
double d; double d;
char* str, *mod; char* str, *mod;
int type, key, reverse; SDLKey key;
int type, reverse;
int w, h, fsaa; int w, h, fsaa;
SDLMod m; SDLMod m;
@ -229,10 +230,14 @@ int conf_loadConfig(const char* file) {
lua_gettable(L, -2); lua_gettable(L, -2);
if(lua_isstring(L, -1)) if(lua_isstring(L, -1))
str = (char*)lua_tostring(L, -1); str = (char*)lua_tostring(L, -1);
else { else if(lua_isnil(L, -1)) {
WARN("Found keybind with no type field!"); WARN("Found keybind with no type field!");
str = "null"; str = "null";
} }
else {
WARN("Found keybind with invalid type field!");
str = "null";
}
lua_remove(L, -1); lua_remove(L, -1);
/* Get the key. */ /* Get the key. */
@ -240,9 +245,14 @@ int conf_loadConfig(const char* file) {
lua_gettable(L, -2); lua_gettable(L, -2);
if(lua_isnumber(L, -1)) if(lua_isnumber(L, -1))
key = (int)lua_tonumber(L, -1); key = (int)lua_tonumber(L, -1);
else { else if(lua_isstring(L, -1))
key = input_keyConv((char*)lua_tostring(L, -1));
else if(lua_isnil(L, -1)) {
WARN("Found keybind with no key field!"); WARN("Found keybind with no key field!");
key = -1; key = SDLK_UNKNOWN;
} else {
WARN("Found keybind with invalid key field!");
key = SDLK_UNKNOWN;
} }
lua_remove(L, -1); lua_remove(L, -1);
@ -266,8 +276,7 @@ int conf_loadConfig(const char* file) {
mod = NULL; mod = NULL;
lua_remove(L, -1); lua_remove(L, -1);
if((key != SDLK_UNKNOWN) && (str != NULL)) {
if((key != -1) && (str != NULL)) {
/* Then the keybind is valid. Get the type. */ /* Then the keybind is valid. Get the type. */
if(strcmp(str, "null")==0) type = KEYBIND_NULL; if(strcmp(str, "null")==0) type = KEYBIND_NULL;
else if(strcmp(str, "keyboard")==0) type = KEYBIND_KEYBOARD; else if(strcmp(str, "keyboard")==0) type = KEYBIND_KEYBOARD;

View File

@ -104,6 +104,11 @@ unsigned int input_afterburnSensibility = 200; /**< ms between taps to afterbur
/* From player.c */ /* From player.c */
extern double player_turn; extern double player_turn;
static char* keyconv[SDLK_LAST]; /**< Key conversion table. */
static void input_keyConvGen(void);
static void input_keyConvDestroy(void);
/** /**
* @fn void input_setDefault(void) * @fn void input_setDefault(void)
* *
@ -173,6 +178,8 @@ void input_init(void) {
tmp->reverse = 1.; tmp->reverse = 1.;
input_keybinds[i] = tmp; input_keybinds[i] = tmp;
} }
input_keyConvGen();
} }
/** /**
@ -185,6 +192,41 @@ void input_exit(void) {
for(i = 0; strcmp(keybindNames[i], "end"); i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
free(input_keybinds[i]); free(input_keybinds[i]);
free(input_keybinds); free(input_keybinds);
input_keyConvDestroy();
}
/**
* @brief Create the key conversion table.
*/
static void input_keyConvGen(void) {
SDLKey k;
for(k == SDLK_FIRST; k < SDLK_LAST; k++)
keyconv[k] = SDL_GetKeyName(k);
}
/**
* @brief Destroy the key conversion table.
*/
static void input_keyConvDestroy(void) {
}
/**
* @brief Get the key id from its name.
* @param name Name of the key to get id from.
* @return ID of the key.
*/
SDLKey input_keyConv(char* name) {
SDLKey k;
for(k = SDLK_FIRST; k < SDLK_LAST; k++)
if(strcmp(name, keyconv[k])==0)
return k;
WARN("Keyname '%s' doesn't match any key.", name);
return SDLK_UNKNOWN;
} }
/** /**

View File

@ -13,6 +13,7 @@ typedef enum {
/* Set input. */ /* Set input. */
void input_setDefault(void); void input_setDefault(void);
SDLKey input_keyConv(char* name);
void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod, int reverse); void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod, int reverse);
SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod, int* reverse); SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod, int* reverse);
const char* input_getKeybindDescription(char* keybind); const char* input_getKeybindDescription(char* keybind);

View File

@ -136,6 +136,12 @@ int main(int argc, char** argv) {
if(lfile_dirMakeExist("%s", lfile_basePath())) if(lfile_dirMakeExist("%s", lfile_basePath()))
WARN("Unable to create lephisto directory '%s'", lfile_basePath()); WARN("Unable to create lephisto directory '%s'", lfile_basePath());
/* Must be initialized befre input init is called. */
if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
WARN("Unable to initialize SDL video: %s", SDL_GetError());
return -1;
}
/* Input must be initialized for config to work. */ /* Input must be initialized for config to work. */
input_init(); input_init();