summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2022-01-29 03:01:08 (EST)
committer P. J. McDermott <pj@pehjota.net>2022-01-29 03:01:08 (EST)
commit7d54dc4b520c2193a2f9517df469a0df4257e4e5 (patch)
tree365224a5e1308ea533ac36cac5018e94b610d053
parent75fcdd36c96975a5a97b1cd41484cc2498b8c660 (diff)
downloadmazefight-7d54dc4b520c2193a2f9517df469a0df4257e4e5.zip
mazefight-7d54dc4b520c2193a2f9517df469a0df4257e4e5.tar.gz
mazefight-7d54dc4b520c2193a2f9517df469a0df4257e4e5.tar.bz2
tk: Add border with focus indication to text
More visible (and consistent with the rest of the UI changes) than just the cursor.
-rw-r--r--src/game.c2
-rw-r--r--src/menu.c8
-rw-r--r--src/tk.h2
-rw-r--r--src/tk/text.c65
4 files changed, 51 insertions, 26 deletions
diff --git a/src/game.c b/src/game.c
index 1768a1a..237e126 100644
--- a/src/game.c
+++ b/src/game.c
@@ -53,7 +53,7 @@ _mf_game_form(SDL_Renderer *renderer, TTF_Font *text_font,
struct _mf_game *game)
{
game->timer = mftk_text_new(MF_DIGITS ":", 5, "00:00", text_font,
- SDL_FALSE, NULL, NULL, NULL);
+ MF_BTN_B, SDL_FALSE, 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 c2c3b17..746347c 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -167,11 +167,11 @@ _mf_menu_form(SDL_Renderer *renderer, TTF_Font *text_font,
}
menu->seed_text = mftk_text_new(MF_DIGITS, rand_max_len, menu->seed_buf,
- text_font, SDL_TRUE, &_mf_menu_seed, &_mf_menu_play,
- menu);
+ text_font, MF_BTN_B, SDL_TRUE, &_mf_menu_seed,
+ &_mf_menu_play, menu);
menu->enemies_text = mftk_text_new(MF_DIGITS, 2, menu->enemies_buf,
- text_font, SDL_TRUE, &_mf_menu_enemies, &_mf_menu_play,
- menu);
+ text_font, MF_BTN_B, SDL_TRUE, &_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", renderer),
MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
diff --git a/src/tk.h b/src/tk.h
index e99d23f..b7b001c 100644
--- a/src/tk.h
+++ b/src/tk.h
@@ -73,7 +73,7 @@ mftk_radio_new(int butn_width, int butn_padding, int butn_border,
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);
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;
}