[Add] Created lstd for portable SDLKey isalpha functions and such.
This commit is contained in:
parent
4f93b615a4
commit
4e572b704c
45
src/lstd.c
Normal file
45
src/lstd.c
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @file lstd.c
|
||||
*
|
||||
* @brief Portable replacements for some functions.
|
||||
*/
|
||||
#include "lstd.h"
|
||||
|
||||
/**
|
||||
* @brief Check to see if a key is alpha.
|
||||
* @param k Key to check.
|
||||
* @return 1 if is alpha.
|
||||
*/
|
||||
int lstd_isalpha(SDLKey k) {
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
|
||||
/* Alpha. */
|
||||
if((k >= SDLK_a) && (k <= SDLK_z))
|
||||
ret = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Like isalnum but for keysyms.
|
||||
* @param k Key to check.
|
||||
* @return 1 if is alnum.
|
||||
*/
|
||||
int lstd_isalnum(SDLKey k) {
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
|
||||
/* Alpha. */
|
||||
if((k >= SDLK_a) && (k <= SDLK_z))
|
||||
ret = 1;
|
||||
|
||||
/* Number. */
|
||||
if((k >= SDLK_0) && (k <= SDLK_9))
|
||||
ret = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
6
src/lstd.h
Normal file
6
src/lstd.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
int lstd_isalpha(SDLKey k);
|
||||
int lstd_isalnum(SDLKey k);
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "toolkit.h"
|
||||
#include "sound.h"
|
||||
#include "music.h"
|
||||
#include "lstd.h"
|
||||
#include "options.h"
|
||||
|
||||
#define KEYBINDS_WIDTH 440 /**< Options menu width. */
|
||||
@ -27,30 +28,12 @@
|
||||
/* Extern. */
|
||||
extern const char* keybindNames[]; /* From input.c */
|
||||
|
||||
static int opt_isalpha(SDLKey k);
|
||||
static const char* modToText(SDLMod mod);
|
||||
static void menuKeybinds_update(unsigned int wid, char* name);
|
||||
|
||||
static void opt_setSFXLevel(unsigned int wid, char* str);
|
||||
static void opt_setMusicLevel(unsigned int wid, char* str);
|
||||
|
||||
/**
|
||||
* @brief Check to see if a key is alpha.
|
||||
* @param k Key to check.
|
||||
* @return 1 if is alpha.
|
||||
*/
|
||||
static int opt_isalpha(SDLKey k) {
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
|
||||
/* Alpha. */
|
||||
if((k >= SDLK_a) && (k <= SDLK_z))
|
||||
ret = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Opens the keybindings menu.
|
||||
*/
|
||||
@ -84,7 +67,7 @@ void opt_menuKeybinds(void) {
|
||||
switch(type) {
|
||||
case KEYBIND_KEYBOARD:
|
||||
/* SDL_GetKeyName returns lowercase which is ugly. */
|
||||
if(opt_isalpha(key))
|
||||
if(lstd_isalpha(key))
|
||||
snprintf(str[j], 64, "%s <%c>", keybindNames[j], toupper(key));
|
||||
else
|
||||
snprintf(str[j], 64, "%s <%s>", keybindNames[j], SDL_GetKeyName(key));
|
||||
@ -165,7 +148,7 @@ static void menuKeybinds_update(unsigned int wid, char* name) {
|
||||
break;
|
||||
case KEYBIND_KEYBOARD:
|
||||
/* SDL_GetKeyName returns lowercase which is ugly. */
|
||||
if(opt_isalpha(key))
|
||||
if(lstd_isalpha(key))
|
||||
snprintf(bind, 32, "keyboard: %s%s%c",
|
||||
(mod != KMOD_NONE) ? modToText(mod) : "",
|
||||
(mod != KMOD_NONE) ? " + " : "",
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "pause.h"
|
||||
#include "opengl.h"
|
||||
#include "input.h"
|
||||
#include "lstd.h"
|
||||
#include "toolkit.h"
|
||||
|
||||
#define INPUT_DELAY 500
|
||||
@ -198,8 +199,6 @@ static void toolkit_clip(double x, double y, double w, double h);
|
||||
static void toolkit_unclip(void);
|
||||
static void toolkit_drawRect(double x, double y, double w, double h,
|
||||
glColour* c, glColour* lc);
|
||||
/* Misc. */
|
||||
static int toolkit_isalnum(SDLKey k);
|
||||
|
||||
/**
|
||||
* @brief Set the internal widget position.
|
||||
@ -1971,7 +1970,7 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
|
||||
#endif
|
||||
|
||||
/* Only catch some keys. */
|
||||
if(!toolkit_isalnum(key) && (key != SDLK_BACKSPACE) &&
|
||||
if(!lstd_isalnum(key) && (key != SDLK_BACKSPACE) &&
|
||||
(key != SDLK_SPACE) && (key != SDLK_RETURN))
|
||||
return 0;
|
||||
|
||||
@ -2211,27 +2210,6 @@ static void toolkit_clearKey(void) {
|
||||
input_keyCounter = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Like isalnum but for keysyms.
|
||||
* @param k Key to check.
|
||||
* @return 1 if is alnum.
|
||||
*/
|
||||
static int toolkit_isalnum(SDLKey k) {
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
|
||||
/* Alpha. */
|
||||
if((k >= SDLK_a) && (k <= SDLK_z))
|
||||
ret = 1;
|
||||
|
||||
/* Number. */
|
||||
if((k >= SDLK_0) && (k <= SDLK_9))
|
||||
ret = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handles keyboard events.
|
||||
* @param event Keyboard event to handle.
|
||||
@ -2251,7 +2229,7 @@ static int toolkit_keyEvent(SDL_Event* event) {
|
||||
key = event->key.keysym.sym;
|
||||
|
||||
/* Hack to simulate key repetition. */
|
||||
if((key == SDLK_BACKSPACE) || toolkit_isalnum(key)) {
|
||||
if((key == SDLK_BACKSPACE) || lstd_isalnum(key)) {
|
||||
if(event->type == SDL_KEYDOWN) toolkit_regKey(key);
|
||||
else if(event->type == SDL_KEYUP) toolkit_unregKey(key);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user