summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-09-25 13:05:52 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-09-25 13:05:52 (EDT)
commitf1210cea09c85c5c972d691d38b3b59f2cdbb15f (patch)
tree3530b56bf41861ef5e62eeb54ebac0c486f7de7f
parent3423586ef2741f797c392fcf70e3f0a61cf0b220 (diff)
downloadmazefight-f1210cea09c85c5c972d691d38b3b59f2cdbb15f.zip
mazefight-f1210cea09c85c5c972d691d38b3b59f2cdbb15f.tar.gz
mazefight-f1210cea09c85c5c972d691d38b3b59f2cdbb15f.tar.bz2
tk/text: Replace min_char, max_char with callback
-rw-r--r--src/game.c2
-rw-r--r--src/menu.c14
-rw-r--r--src/tk.h1
-rw-r--r--src/tk/text.c10
4 files changed, 20 insertions, 7 deletions
diff --git a/src/game.c b/src/game.c
index 552a606..93ea2db 100644
--- a/src/game.c
+++ b/src/game.c
@@ -54,7 +54,7 @@ _mf_game_form(SDL_Renderer *renderer, TTF_Font *text_font,
struct _mf_game *game)
{
game->timer = mftk_text_new('\0', '\0', 5, "00:00", text_font,
- text_color, SDL_FALSE, NULL, NULL, NULL);
+ text_color, SDL_FALSE, NULL, NULL, NULL, NULL);
return mftk_grid_new(2, 1, MF_ROW_M, MF_COL_M,
game->timer,
MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
diff --git a/src/menu.c b/src/menu.c
index c9509e5..c5087ee 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -50,6 +50,12 @@ struct _mf_menu {
};
static int
+_mf_menu_isdigit(void *user_data __attribute__((__unused__)), char c)
+{
+ return isdigit(c);
+}
+
+static int
_mf_menu_seed(void *user_data, const char *seed)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
@@ -169,11 +175,11 @@ _mf_menu_form(SDL_Renderer *renderer, TTF_Font *text_font,
}
menu->seed_text = mftk_text_new('0', '9', rand_max_len, menu->seed_buf,
- text_font, text_color, SDL_TRUE, &_mf_menu_seed,
- &_mf_menu_play, menu);
+ text_font, text_color, SDL_TRUE, &_mf_menu_isdigit,
+ &_mf_menu_seed, &_mf_menu_play, menu);
menu->enemies_text = mftk_text_new('0', '9', 2, menu->enemies_buf,
- text_font, text_color, SDL_TRUE, &_mf_menu_enemies,
- &_mf_menu_play, menu);
+ text_font, text_color, SDL_TRUE, &_mf_menu_isdigit,
+ &_mf_menu_enemies, &_mf_menu_play, menu);
grid = mftk_grid_new(6, 2, MF_ROW_M, MF_COL_M,
mftk_label_new(text_font, "Seed", text_color, renderer),
MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
diff --git a/src/tk.h b/src/tk.h
index c83ac9e..598b971 100644
--- a/src/tk.h
+++ b/src/tk.h
@@ -78,6 +78,7 @@ mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color,
struct mftk_widget *
mftk_text_new(char min_char, char max_char, int len, const char *val,
TTF_Font *font, SDL_Color *color, int editable,
+ int (*allowed)(void *, char),
int (*action)(void *, const char *), int (*submit)(void *),
void *user_data);
diff --git a/src/tk/text.c b/src/tk/text.c
index ccfaf7f..f509c14 100644
--- a/src/tk/text.c
+++ b/src/tk/text.c
@@ -42,6 +42,7 @@ struct mftk_text {
SDL_Color *color;
SDL_Texture *texture;
int editable;
+ int (*allowed)(void *, char);
int (*action)(void *, const char *);
int (*submit)(void *);
void *user_data;
@@ -144,8 +145,11 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e)
for (i = 0, j = 0;
i < newlen && e->text.text[j] != '\0';
++j) {
- if (e->text.text[j] < t->min_char) continue;
- if (e->text.text[j] > t->max_char) continue;
+ if (t->allowed != NULL &&
+ t->allowed(t->user_data,
+ e->text.text[j]) <= 0) {
+ continue;
+ }
t->val[t->cur + i] = e->text.text[j];
++i;
}
@@ -299,6 +303,7 @@ _mftk_text_destroy(struct mftk_widget *w)
struct mftk_widget *
mftk_text_new(char min_char, char max_char, int len, const char *val,
TTF_Font *font, SDL_Color *color, int editable,
+ int (*allowed)(void *, char),
int (*action)(void *, const char *), int (*submit)(void *),
void *user_data)
{
@@ -322,6 +327,7 @@ mftk_text_new(char min_char, char max_char, int len, const char *val,
t->color = color;
t->texture = NULL;
t->editable = editable;
+ t->allowed = allowed;
t->action = action;
t->submit = submit;
t->user_data = user_data;