From 4a0566ce98cb5dd9a719c486a87b18a23875db12 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 27 Jan 2022 01:58:09 -0500 Subject: tk: Add border to box --- diff --git a/src/defs.h b/src/defs.h index ba67c29..81b844a 100644 --- a/src/defs.h +++ b/src/defs.h @@ -29,6 +29,7 @@ /* Form dimensions */ #define MF_FORM_P 24 /* Form padding */ +#define MF_FORM_B 2 /* Form border width, same as maze wall thickness */ #define MF_TITLE_M 24 /* Margin under title */ #define MF_ROW_M 16 /* Margin between rows */ #define MF_COL_M 16 /* Margin between labels and entries */ @@ -69,7 +70,7 @@ #define MF_COLOR_BACK_B 0xAF #define MF_COLOR_BACK_A 0xFF #define MF_COLOR_MAZE_R 0x00 /* Maze wall color */ -#define MF_COLOR_MAZE_G 0x00 +#define MF_COLOR_MAZE_G 0x00 /* Sync with MFTK_COLOR_FBDR_* in tk/style.h */ #define MF_COLOR_MAZE_B 0x00 #define MF_COLOR_MAZE_A 0xFF #define MF_COLOR_FOGW_R 0x2F /* Fog of war color */ diff --git a/src/game.c b/src/game.c index 133eff0..6f9ea30 100644 --- a/src/game.c +++ b/src/game.c @@ -197,7 +197,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, win = mftk_window_new(MF_WINDOW_H, 0, mftk_box_new( MF_WINDOW_W - MF_WINDOW_H, MF_WINDOW_H, MF_WINDOW_W - MF_WINDOW_H, MF_WINDOW_H, - MF_FORM_P, + MF_FORM_P, MF_FORM_B / 2, _mf_game_form(renderer, text_font, &game))); game.beg = SDL_GetTicks(); diff --git a/src/menu.c b/src/menu.c index fa78853..b03ab8b 100644 --- a/src/menu.c +++ b/src/menu.c @@ -268,7 +268,7 @@ mf_menu(SDL_Renderer *renderer) font_path = NULL; win = mftk_window_new(0, 0, mftk_box_new(MF_WINDOW_W, MF_WINDOW_H, 0, 0, - MF_FORM_P, + MF_FORM_P, MF_FORM_B, mftk_grid_new(2, 1, MF_TITLE_M, 0, mftk_label_new(title_font, "Maze Fight", renderer), diff --git a/src/tk.h b/src/tk.h index 165e83e..60018f6 100644 --- a/src/tk.h +++ b/src/tk.h @@ -56,7 +56,7 @@ mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...); struct mftk_widget * mftk_box_new(int container_w, int container_h, int own_w, int own_h, - int padding, struct mftk_widget *child); + int padding, int border, struct mftk_widget *child); struct mftk_widget * mftk_check_new(int butn_width, int butn_padding, int state, int label_padding, diff --git a/src/tk/box.c b/src/tk/box.c index 003af66..fd6c071 100644 --- a/src/tk/box.c +++ b/src/tk/box.c @@ -30,6 +30,7 @@ struct mftk_box { int container_w; int container_h; int padding; + int border; struct mftk_widget *child; int child_x; int child_y; @@ -42,13 +43,13 @@ _mftk_box_layout(struct mftk_widget *w) mftk_widget_layout(b->child); if (w->w == 0) { - w->w = b->child->w + b->padding * 2; + w->w = b->child->w + b->padding * 2 + b->border * 2; } if (w->h == 0) { - w->h = b->child->h + b->padding * 2; + w->h = b->child->h + b->padding * 2 + b->border * 2; } - b->child_x = (b->container_w - w->w) / 2 + b->padding; - b->child_y = (b->container_h - w->h) / 2 + b->padding; + b->child_x = (b->container_w - w->w) / 2 + b->padding + b->border; + b->child_y = (b->container_h - w->h) / 2 + b->padding + b->border; } static void @@ -90,23 +91,54 @@ static int _mftk_box_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) { struct mftk_box *b = (struct mftk_box *) w; - SDL_Rect rect; + SDL_Rect outer; + SDL_Rect inner; + SDL_Rect edge; + + outer.x = x + b->child_x - b->padding - b->border; + outer.y = y + b->child_y - b->padding - b->border; + outer.w = w->w; + outer.h = w->h; + inner.x = x + b->child_x - b->padding; + inner.y = y + b->child_y - b->padding; + inner.w = w->w - b->border * 2; + inner.h = w->h - b->border * 2; + + /* Border */ + if (SDL_SetRenderDrawColor(renderer, + MFTK_COLOR_FBDR_R, MFTK_COLOR_FBDR_G, + MFTK_COLOR_FBDR_B, MFTK_COLOR_FBDR_A) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + return -1; + } +#define _mftk_edge(X, Y, W, H) \ + edge.x = X, edge.y = Y, edge.w = W, edge.h = H; \ + if (SDL_RenderFillRect(renderer, &edge) < 0) { \ + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ + "Couldn't render widget: %s", \ + SDL_GetError()); \ + return -1; \ + } + _mftk_edge(outer.x , outer.y , outer.w , b->border); + _mftk_edge(outer.x , inner.y + inner.h, outer.w , b->border); + _mftk_edge(outer.x , outer.y , b->border, outer.h ); + _mftk_edge(inner.x + inner.w, outer.y , b->border, outer.h ); +#undef _mftk_edge - rect.x = x + b->child_x - b->padding; - rect.y = y + b->child_y - b->padding; - rect.w = w->w; - rect.h = w->h; + /* Background */ if (SDL_SetRenderDrawColor(renderer, MFTK_COLOR_FORM_R, MFTK_COLOR_FORM_G, MFTK_COLOR_FORM_B, MFTK_COLOR_FORM_A) < 0 || - SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_RenderFillRect(renderer, &inner) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", SDL_GetError()); return -1; } - + /* Child widget */ if (mftk_widget_render(b->child, renderer, x + b->child_x, y + b->child_y) < 0) { return -1; @@ -125,7 +157,7 @@ _mftk_box_destroy(struct mftk_widget *w) struct mftk_widget * mftk_box_new(int container_w, int container_h, int own_w, int own_h, - int padding, struct mftk_widget *child) + int padding, int border, struct mftk_widget *child) { struct mftk_widget *w; struct mftk_box *b; @@ -137,6 +169,7 @@ mftk_box_new(int container_w, int container_h, int own_w, int own_h, w->w = own_w; w->h = own_h; b->padding = padding; + b->border = border; b->child = child; child->container = w; diff --git a/src/tk/style.h b/src/tk/style.h index 16b4e25..be4c2bb 100644 --- a/src/tk/style.h +++ b/src/tk/style.h @@ -24,6 +24,10 @@ #define MFTK_COLOR_FORM_G 0xDF #define MFTK_COLOR_FORM_B 0xDF #define MFTK_COLOR_FORM_A 0xDF +#define MFTK_COLOR_FBDR_R 0x00 /* Form border color */ +#define MFTK_COLOR_FBDR_G 0x00 +#define MFTK_COLOR_FBDR_B 0x00 +#define MFTK_COLOR_FBDR_A 0xFF #define MFTK_COLOR_FORE_R 0x00 /* Text color */ #define MFTK_COLOR_FORE_G 0x00 #define MFTK_COLOR_FORE_B 0x00 -- cgit v0.9.1