diff options
author | P. J. McDermott <pj@pehjota.net> | 2022-01-27 18:46:46 (EST) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2022-01-27 18:51:31 (EST) |
commit | 92caa6344f71f3681ed255b56ecfa9294f497ab9 (patch) | |
tree | 04eb34658328afecbeb416d6c79e27003ae66164 | |
parent | 8ba3e62614e61e47ca7f8d79cd8c93d547206f74 (diff) | |
download | mazefight-92caa6344f71f3681ed255b56ecfa9294f497ab9.zip mazefight-92caa6344f71f3681ed255b56ecfa9294f497ab9.tar.gz mazefight-92caa6344f71f3681ed255b56ecfa9294f497ab9.tar.bz2 |
tk: Add border with focus indication to button
-rw-r--r-- | src/defs.h | 1 | ||||
-rw-r--r-- | src/game.c | 2 | ||||
-rw-r--r-- | src/menu.c | 6 | ||||
-rw-r--r-- | src/tk.h | 2 | ||||
-rw-r--r-- | src/tk/button.c | 29 | ||||
-rw-r--r-- | src/tk/style.h | 8 |
6 files changed, 41 insertions, 7 deletions
@@ -40,6 +40,7 @@ #define MF_CHK_ITM_P 8 /* Radio button and check box label pad */ #define MF_BTN_M 8 /* Margin between buttons */ #define MF_BTN_P 8 /* Button padding */ +#define MF_BTN_B 1 /* Button border */ #define MF_TITLE_FONT_S 48 /* Title font size */ #define MF_TEXT_FONT_S 16 /* Regular text font size */ @@ -57,7 +57,7 @@ _mf_game_form(SDL_Renderer *renderer, TTF_Font *text_font, return mftk_grid_new(2, 1, MF_ROW_M, MF_COL_M, game->timer, MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T, - mftk_button_new(text_font, "Exit", MF_BTN_P, + mftk_button_new(text_font, "Exit", MF_BTN_P, MF_BTN_B, &_mf_game_exit, NULL, renderer), MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_B ); @@ -203,10 +203,12 @@ _mf_menu_form(SDL_Renderer *renderer, TTF_Font *text_font, mftk_blank_new(), MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T, mftk_grid_new(1, 2, 0, MF_BTN_M, - mftk_button_new(text_font, "Quit", MF_BTN_P, + mftk_button_new(text_font, "Quit", + MF_BTN_P, MF_BTN_B, &_mf_menu_quit, NULL, renderer), MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T, - mftk_button_new(text_font, "Play", MF_BTN_P, + mftk_button_new(text_font, "Play", + MF_BTN_P, MF_BTN_B, &_mf_menu_play, menu, renderer), MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T ), @@ -42,7 +42,7 @@ struct mftk_widget * mftk_label_new(TTF_Font *font, const char *text, SDL_Renderer *renderer); struct mftk_widget * -mftk_button_new(TTF_Font *font, const char *text, int padding, +mftk_button_new(TTF_Font *font, const char *text, int padding, int border, int (*action)(void *), void *user_data, SDL_Renderer *renderer); #define MFTK_GRID_HALIGN_L 1 diff --git a/src/tk/button.c b/src/tk/button.c index c0a0c61..fa0cb65 100644 --- a/src/tk/button.c +++ b/src/tk/button.c @@ -26,6 +26,7 @@ struct mftk_button { struct mftk_widget parent; struct mftk_widget *label; int padding; + int border; int (*action)(void *); void *user_data; }; @@ -37,8 +38,8 @@ _mftk_button_layout(struct mftk_widget *w) mftk_widget_layout(b->label); - w->w = b->label->w + b->padding * 2; - w->h = b->label->h + b->padding * 2; + w->w = b->label->w + b->padding * 2 + b->border * 2; + w->h = b->label->h + b->padding * 2 + b->border * 2; } static void @@ -107,11 +108,32 @@ _mftk_button_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) { struct mftk_button *b = (struct mftk_button *) w; SDL_Rect rect; + SDL_Color border; rect.x = x; rect.y = y; rect.w = w->w; rect.h = w->h; + if (w->focused == SDL_TRUE) { + border.r = MFTK_COLOR_BDRF_R, border.g = MFTK_COLOR_BDRF_G; + border.b = MFTK_COLOR_BDRF_B, border.a = MFTK_COLOR_BDRF_A; + } else { + border.r = MFTK_COLOR_BDRN_R, border.g = MFTK_COLOR_BDRN_G; + border.b = MFTK_COLOR_BDRN_B, border.a = MFTK_COLOR_BDRN_A; + } + if (SDL_SetRenderDrawColor(renderer, + border.r, border.g, border.b, border.a) < 0 || + SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + return -1; + } + + rect.x += b->border; + rect.y += b->border; + rect.w -= b->border * 2; + rect.h -= b->border * 2; if (SDL_SetRenderDrawColor(renderer, MFTK_COLOR_BACK_R, MFTK_COLOR_BACK_G, MFTK_COLOR_BACK_B, MFTK_COLOR_BACK_A) < 0 || @@ -139,7 +161,7 @@ _mftk_button_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_button_new(TTF_Font *font, const char *text, int padding, +mftk_button_new(TTF_Font *font, const char *text, int padding, int border, int (*action)(void *), void *user_data, SDL_Renderer *renderer) { struct mftk_widget *w; @@ -154,6 +176,7 @@ mftk_button_new(TTF_Font *font, const char *text, int padding, } b->padding = padding; + b->border = border; b->action = action; b->user_data = user_data; diff --git a/src/tk/style.h b/src/tk/style.h index 14a1ea4..a8ff76f 100644 --- a/src/tk/style.h +++ b/src/tk/style.h @@ -32,6 +32,14 @@ #define MFTK_COLOR_BACK_G 0xAF #define MFTK_COLOR_BACK_B 0xAF #define MFTK_COLOR_BACK_A 0xFF +#define MFTK_COLOR_BDRN_R 0x00 /* Widget normal border color */ +#define MFTK_COLOR_BDRN_G 0x00 +#define MFTK_COLOR_BDRN_B 0x00 +#define MFTK_COLOR_BDRN_A 0xFF +#define MFTK_COLOR_BDRF_R 0x00 /* Widget focus border color */ +#define MFTK_COLOR_BDRF_G 0x00 +#define MFTK_COLOR_BDRF_B 0xAF +#define MFTK_COLOR_BDRF_A 0xFF #define MFTK_COLOR_FORE_R 0x00 /* Text color */ #define MFTK_COLOR_FORE_G 0x00 #define MFTK_COLOR_FORE_B 0x00 |