From 7d54dc4b520c2193a2f9517df469a0df4257e4e5 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 29 Jan 2022 03:01:08 -0500 Subject: tk: Add border with focus indication to text More visible (and consistent with the rest of the UI changes) than just the cursor. --- (limited to 'src/tk') diff --git a/src/tk/text.c b/src/tk/text.c index 7401777..99ec76a 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -40,6 +40,7 @@ struct mftk_text { int line_skip; int ascent; SDL_Texture *texture; + int border; int editable; int blink_state; Uint32 blink_timer; @@ -258,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; + SDL_Color color; int cur_x; Uint32 timer; @@ -271,6 +273,29 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) rect.y = y; rect.w = w->w; rect.h = w->h; + if (w->focused == SDL_TRUE) { + color.r = MFTK_COLOR_BDRF_R; + color.g = MFTK_COLOR_BDRF_G; + color.b = MFTK_COLOR_BDRF_B; + color.a = MFTK_COLOR_BDRF_A; + } else { + color.r = MFTK_COLOR_BDRN_R; + color.g = MFTK_COLOR_BDRN_G; + color.b = MFTK_COLOR_BDRN_B; + color.a = MFTK_COLOR_BDRN_A; + } + if (SDL_SetRenderDrawColor(renderer, color.r, color.g, + color.b, color.a) < 0 || + SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + return -1; + } + rect.x += t->border; + rect.y += t->border; + rect.w -= t->border * 2; + rect.h -= t->border * 2; if (SDL_SetRenderDrawColor(renderer, MFTK_COLOR_BACK_R, MFTK_COLOR_BACK_G, MFTK_COLOR_BACK_B, MFTK_COLOR_BACK_A) < @@ -298,17 +323,6 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) SDL_GetError()); return -1; } - if (t->editable == SDL_TRUE && ( - SDL_SetRenderDrawColor(renderer, - _mftk_text_color.r, _mftk_text_color.g, - _mftk_text_color.b, _mftk_text_color.a) - < 0 || SDL_RenderDrawLine(renderer, - x, y + w->h, x + w->w, y + w->h) < 0)) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Couldn't render widget: %s", - SDL_GetError()); - return -1; - } timer = SDL_GetTicks(); if (timer - t->blink_timer >= MFTK_CURSOR_BLINK_MS) { if (t->blink_state == SDL_TRUE) { @@ -318,13 +332,19 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) } 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", - SDL_GetError()); - return -1; + if (w->focused == SDL_TRUE && t->blink_state == SDL_TRUE) { + if (SDL_SetRenderDrawColor(renderer, + _mftk_text_color.r, _mftk_text_color.g, + _mftk_text_color.b, _mftk_text_color.a) + < 0 || + 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", + SDL_GetError()); + return -1; + } } return 0; @@ -342,7 +362,7 @@ _mftk_text_destroy(struct mftk_widget *w) struct mftk_widget * mftk_text_new(const char *allowed_chars, int len, const char *val, - TTF_Font *font, int editable, + TTF_Font *font, int border, int editable, int (*action)(void *, const char *), int (*submit)(void *), void *user_data) { @@ -363,6 +383,7 @@ mftk_text_new(const char *allowed_chars, int len, const char *val, t->line_skip = TTF_FontLineSkip(font); t->ascent = TTF_FontAscent (font); t->texture = NULL; + t->border = border; t->editable = editable; t->blink_state = SDL_TRUE; t->blink_timer = 0; @@ -408,7 +429,11 @@ mftk_text_new(const char *allowed_chars, int len, const char *val, w->w = advance; } } - w->h = t->line_skip + (editable == SDL_TRUE ? 1 : 0); + w->h = t->line_skip; + if (editable == SDL_TRUE) { + w->w += border * 2; + w->h += border * 2; + } return w; } -- cgit v0.9.1