diff options
author | P. J. McDermott <pj@pehjota.net> | 2022-01-27 04:59:39 (EST) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2022-01-27 05:08:15 (EST) |
commit | a6908296b6ac83ab3eb5474ad6b4a509c8f5099c (patch) | |
tree | f162186443c1b6f82b747185845394bbd8badae8 /src/tk | |
parent | 4a0566ce98cb5dd9a719c486a87b18a23875db12 (diff) | |
download | mazefight-a6908296b6ac83ab3eb5474ad6b4a509c8f5099c.zip mazefight-a6908296b6ac83ab3eb5474ad6b4a509c8f5099c.tar.gz mazefight-a6908296b6ac83ab3eb5474ad6b4a509c8f5099c.tar.bz2 |
tk: Blink text cursor
Diffstat (limited to 'src/tk')
-rw-r--r-- | src/tk/style.h | 2 | ||||
-rw-r--r-- | src/tk/text.c | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/tk/style.h b/src/tk/style.h index be4c2bb..95bbf00 100644 --- a/src/tk/style.h +++ b/src/tk/style.h @@ -45,4 +45,6 @@ #define MFTK_COLOR_CHKM_B 0x00 #define MFTK_COLOR_CHKM_A 0xFF +#define MFTK_CURSOR_BLINK_MS 500 /* Milliseconds between cursor blinks */ + #endif /* MFTK_STYLE_H_ */ diff --git a/src/tk/text.c b/src/tk/text.c index 9ba9faa..76bb27a 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -41,6 +41,8 @@ struct mftk_text { int ascent; SDL_Texture *texture; int editable; + int blink_state; + Uint32 blink_timer; int (*action)(void *, const char *); int (*submit)(void *); void *user_data; @@ -60,7 +62,12 @@ _mftk_text_layout(struct mftk_widget *w __attribute__((__unused__))) static void _mftk_text_focus(struct mftk_widget *w __attribute__((__unused__))) { + struct mftk_text *t = (struct mftk_text *) w; + SDL_StartTextInput(); + + t->blink_state = SDL_TRUE; + t->blink_timer = SDL_GetTicks(); } static void @@ -78,6 +85,9 @@ _mftk_text_key_event(struct mftk_widget *w, SDL_Event *e) int newlen; int j; + t->blink_state = SDL_TRUE; + t->blink_timer = SDL_GetTicks(); + switch (e->type) { case SDL_KEYDOWN: switch (e->key.keysym.sym) { @@ -249,6 +259,7 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) struct mftk_text *t = (struct mftk_text *) w; SDL_Rect rect; int cur_x; + Uint32 timer; if (t->val[0] != '\0' && t->texture == NULL && _mftk_text_render_val(t, renderer) < 0) { @@ -282,7 +293,17 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) SDL_GetError()); return -1; } - if (w->focused == SDL_TRUE && SDL_RenderDrawLine(renderer, + timer = SDL_GetTicks(); + if (timer - t->blink_timer >= MFTK_CURSOR_BLINK_MS) { + if (t->blink_state == SDL_TRUE) { + t->blink_state = SDL_FALSE; + } else if (t->blink_state == SDL_FALSE) { + t->blink_state = SDL_TRUE; + } + t->blink_timer = timer; + } + if (w->focused == SDL_TRUE && t->blink_state == SDL_TRUE && + SDL_RenderDrawLine(renderer, x + cur_x, y + t->y, x + cur_x, y + t->h) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", @@ -327,6 +348,8 @@ mftk_text_new(const char *allowed_chars, int len, const char *val, t->ascent = TTF_FontAscent (font); t->texture = NULL; t->editable = editable; + t->blink_state = SDL_TRUE; + t->blink_timer = 0; t->action = action; t->submit = submit; t->user_data = user_data; |