diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-04 21:01:30 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-04 21:07:07 (EDT) |
commit | 4290eee6d45aab8e9f1c47d5d7d35e9ceccd78b2 (patch) | |
tree | c2e7871158c55a389055ba1d0a8e03244600fa79 /src | |
parent | ddf513909ce1742021b9c57ea7279a61aeda4899 (diff) | |
download | mazefight-4290eee6d45aab8e9f1c47d5d7d35e9ceccd78b2.zip mazefight-4290eee6d45aab8e9f1c47d5d7d35e9ceccd78b2.tar.gz mazefight-4290eee6d45aab8e9f1c47d5d7d35e9ceccd78b2.tar.bz2 |
tk: Add box widget
Diffstat (limited to 'src')
-rw-r--r-- | src/splash.c | 35 | ||||
-rw-r--r-- | src/tk.h | 4 | ||||
-rw-r--r-- | src/tk/box.c | 119 | ||||
-rw-r--r-- | src/tk/local.mk | 1 |
4 files changed, 145 insertions, 14 deletions
diff --git a/src/splash.c b/src/splash.c index 9b94057..dee2eb2 100644 --- a/src/splash.c +++ b/src/splash.c @@ -41,9 +41,10 @@ mf_splash(SDL_Renderer *renderer) char *font_path = NULL; TTF_Font *title_font = NULL; TTF_Font *text_font = NULL; + SDL_Color form_color; SDL_Color text_color; SDL_Color butn_color; - struct mftk_widget *grid; + struct mftk_widget *box; struct mf_maze *maze = NULL; SDL_Color maze_color; SDL_Event event; @@ -64,6 +65,10 @@ mf_splash(SDL_Renderer *renderer) goto err; } + form_color.r = MF_COLOR_FORM_R; + form_color.g = MF_COLOR_FORM_G; + form_color.b = MF_COLOR_FORM_B; + form_color.a = MF_COLOR_FORM_A; text_color.r = MF_COLOR_FORE_R; text_color.g = MF_COLOR_FORE_G; text_color.b = MF_COLOR_FORE_B; @@ -73,14 +78,18 @@ mf_splash(SDL_Renderer *renderer) butn_color.b = MF_COLOR_BUTN_B; butn_color.a = MF_COLOR_BUTN_A; - grid = mftk_grid_new(2, 1, MF_SPLASH_TITLE_M, 0, - mftk_label_new(title_font, "Maze Fight", &text_color, - renderer), - mftk_button_new(text_font, "Play", &text_color, - &butn_color, 2, &_mf_splash_play, NULL, - renderer)); + box = mftk_box_new(MF_WINDOW_W, MF_WINDOW_H, 0, 0, MF_SPLASH_FORM_P, + &form_color, + mftk_grid_new(2, 1, MF_SPLASH_TITLE_M, 0, + mftk_label_new(title_font, "Maze Fight", + &text_color, renderer), + mftk_button_new(text_font, "Play", &text_color, + &butn_color, 2, &_mf_splash_play, NULL, + renderer) + ) + ); /* TODO: Widgets */ - mftk_widget_layout(grid); + mftk_widget_layout(box); TTF_CloseFont(title_font); title_font = NULL; @@ -116,15 +125,13 @@ mf_splash(SDL_Renderer *renderer) SDL_RenderClear(renderer); mf_maze_render(maze, renderer, &maze_color, MF_SPLASH_MAZE_CELL_W); - mftk_widget_event(grid, &event, - MF_SPLASH_WINDOW_P, MF_SPLASH_WINDOW_P); - mftk_widget_render(grid, renderer, - MF_SPLASH_WINDOW_P, MF_SPLASH_WINDOW_P); + mftk_widget_event(box, &event, 0, 0); + mftk_widget_render(box, renderer, 0, 0); SDL_RenderPresent(renderer); } quit: - mftk_widget_destroy(&grid); + mftk_widget_destroy(&box); mf_maze_destroy(&maze); return 0; @@ -139,7 +146,7 @@ mf_splash(SDL_Renderer *renderer) if (text_font != NULL) { TTF_CloseFont(text_font); } - mftk_widget_destroy(&grid); + mftk_widget_destroy(&box); mf_maze_destroy(&maze); return -1; } @@ -56,4 +56,8 @@ mftk_button_new(TTF_Font *font, const char *text, SDL_Color *text_color, struct mftk_widget * 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, SDL_Color *bg_color, struct mftk_widget *child); + #endif /* MFTK_H_ */ diff --git a/src/tk/box.c b/src/tk/box.c new file mode 100644 index 0000000..1a45589 --- /dev/null +++ b/src/tk/box.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2021 P. J. McDermott + * + * This file is part of Maze Fight + * + * Maze Fight is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Maze Fight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Maze Fight. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <SDL.h> +#include "errno.h" +#include <stdlib.h> +#include <string.h> +#include "../tk.h" +#include "widget.h" + +struct mftk_box { + struct mftk_widget parent; + int container_w; + int container_h; + int padding; + SDL_Color *bg_color; + struct mftk_widget *child; + int child_x; + int child_y; +}; + +static void +_mftk_box_layout(struct mftk_widget *w) +{ + struct mftk_box *b = (struct mftk_box *) w; + + mftk_widget_layout(b->child); + if (w->w == 0) { + w->w = b->child->w + b->padding * 2; + } + if (w->h == 0) { + w->h = b->child->h + b->padding * 2; + } + b->child_x = (b->container_w - w->w) / 2 + b->padding; + b->child_y = (b->container_h - w->h) / 2 + b->padding; +} + +static int +_mftk_box_event(struct mftk_widget *w, SDL_Event *e, int x, int y) +{ + struct mftk_box *b = (struct mftk_box *) w; + SDL_Point p; + SDL_Rect rect; + + switch (e->type) { + case SDL_MOUSEBUTTONUP: + p.x = e->button.x; + p.y = e->button.y; + rect.x = x + b->child_x; + rect.y = y + b->child_y; + rect.w = b->child->w; + rect.h = b->child->h; + if (SDL_PointInRect(&p, &rect) == SDL_TRUE) { + return mftk_widget_event(b->child, e, x, y); + } + break; + default: + break; + } + + return 0; +} + +static int +_mftk_box_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) +{ + struct mftk_box *b = (struct mftk_box *) w; + + if (mftk_widget_render(b->child, renderer, + x + b->child_x, y + b->child_y) < 0) { + return -1; + } + + return 0; +} + +static void +_mftk_box_destroy(struct mftk_widget *w) +{ + struct mftk_box *b = (struct mftk_box *) w; + + mftk_widget_destroy(&b->child); +} + +struct mftk_widget * +mftk_box_new(int container_w, int container_h, int own_w, int own_h, + int padding, SDL_Color *bg_color, struct mftk_widget *child) +{ + struct mftk_widget *w; + struct mftk_box *b; + + mftk_widget_init(w, b, box); + + b->container_w = container_w; + b->container_h = container_h; + w->w = own_w; + w->h = own_h; + b->padding = padding; + b->bg_color = bg_color; + b->child = child; + + return w; +} diff --git a/src/tk/local.mk b/src/tk/local.mk index a15bd33..12c07d7 100644 --- a/src/tk/local.mk +++ b/src/tk/local.mk @@ -1,4 +1,5 @@ mazefight_SOURCES += \ + %reldir%/box.c \ %reldir%/button.c \ %reldir%/grid.c \ %reldir%/label.c \ |