From fa7a4af184162fabb03e90998b0388672fc354d9 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 05 Aug 2021 14:18:09 -0400 Subject: tk: Add radio widget Also fix an #include directive in the grid widget. --- diff --git a/src/defs.h b/src/defs.h index f35c53b..732630c 100644 --- a/src/defs.h +++ b/src/defs.h @@ -35,6 +35,7 @@ #define MF_SPLASH_CHK_BTN_W 16 /* Radio button and check box width */ #define MF_SPLASH_CHK_BTN_P 2 /* Radio button and check box padding */ #define MF_SPLASH_CHK_LBL_P 8 /* Radio button and check box label pad */ +#define MF_SPLASH_CHK_ITM_P 8 /* Radio button and check box label pad */ #define MF_SPLASH_BTN_M 8 /* Margin between buttons */ #define MF_SPLASH_BTN_P 8 /* Button padding */ #define MF_SPLASH_TITLE_FONT_S 48 /* Title font size */ diff --git a/src/splash.c b/src/splash.c index c6ee9c8..93dbc3d 100644 --- a/src/splash.c +++ b/src/splash.c @@ -29,6 +29,30 @@ #include "util.h" static int +_mf_splash_size(void *user_data, int state) +{ + switch (state) { + case 0: + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Size: 15x15"); + break; + case 1: + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Size: 20x20"); + break; + case 2: + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Size: 30x30"); + break; + default: + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Size: unknown"); + break; + } + return 0; +} + +static int _mf_splash_fow(void *user_data, int state) { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Fog of war: %s", @@ -103,9 +127,16 @@ mf_splash(SDL_Renderer *renderer) box = mftk_box_new(MF_WINDOW_W, MF_WINDOW_H, 0, 0, MF_SPLASH_FORM_P, &form_color, - mftk_grid_new(4, 1, MF_SPLASH_TITLE_M, 0, + mftk_grid_new(5, 1, MF_SPLASH_TITLE_M, 0, mftk_label_new(title_font, "Maze Fight", &text_color, renderer), + mftk_radio_new(MF_SPLASH_CHK_BTN_W, + MF_SPLASH_CHK_BTN_P, &chkb_color, + &chkm_color, MF_SPLASH_CHK_LBL_P, + MF_SPLASH_CHK_ITM_P, text_font, + &text_color, _mf_splash_size, NULL, + renderer, 0, 3, "15x15", "20x20", + "30x30"), mftk_check_new(MF_SPLASH_CHK_BTN_W, MF_SPLASH_CHK_BTN_P, &chkb_color, &chkm_color, SDL_TRUE, diff --git a/src/tk.h b/src/tk.h index 31589f8..fa782b2 100644 --- a/src/tk.h +++ b/src/tk.h @@ -67,4 +67,11 @@ mftk_check_new(int butn_width, int butn_padding, SDL_Color *butn_color, int (*action)(void *, int), void *user_data, SDL_Renderer *renderer); +struct mftk_widget * +mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color, + SDL_Color *mark_color, int label_padding, int item_padding, + TTF_Font *font, SDL_Color *text_color, + int (*action)(void *, int), void *user_data, + SDL_Renderer *renderer, int state, int options, ...); + #endif /* MFTK_H_ */ diff --git a/src/tk/grid.c b/src/tk/grid.c index 8b6b99a..dc25f75 100644 --- a/src/tk/grid.c +++ b/src/tk/grid.c @@ -18,7 +18,7 @@ */ #include -#include "errno.h" +#include #include #include #include "../tk.h" diff --git a/src/tk/local.mk b/src/tk/local.mk index 679e2a4..9cea068 100644 --- a/src/tk/local.mk +++ b/src/tk/local.mk @@ -4,5 +4,6 @@ mazefight_SOURCES += \ %reldir%/check.c \ %reldir%/grid.c \ %reldir%/label.c \ + %reldir%/radio.c \ %reldir%/widget.c \ %reldir%/widget.h diff --git a/src/tk/radio.c b/src/tk/radio.c new file mode 100644 index 0000000..fcf63b3 --- /dev/null +++ b/src/tk/radio.c @@ -0,0 +1,125 @@ +/* + * 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 . + */ + +#include +#include +#include +#include +#include "../tk.h" +#include "widget.h" + +struct mftk_radio { + struct mftk_widget parent; + struct mftk_widget *grid; + int state; + int (*action)(void *, int); + void *user_data; +}; + +static void +_mftk_radio_layout(struct mftk_widget *w) +{ + struct mftk_radio *r = (struct mftk_radio *) w; + + mftk_widget_layout(r->grid); + w->w = r->grid->w; + w->h = r->grid->h; +} + +static int +_mftk_radio_event(struct mftk_widget *w, SDL_Event *e, int x, int y) +{ + struct mftk_radio *r = (struct mftk_radio *) w; + + return mftk_widget_event(r->grid, e, x, y); +} + +static int +_mftk_radio_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) +{ + struct mftk_radio *r = (struct mftk_radio *) w; + + return mftk_widget_render(r->grid, renderer, x, y); +} + +static void +_mftk_radio_destroy(struct mftk_widget *w) +{ + struct mftk_radio *r = (struct mftk_radio *) w; + + mftk_widget_destroy(&r->grid); +} + +struct mftk_widget * +mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color, + SDL_Color *mark_color, int label_padding, int item_padding, + TTF_Font *font, SDL_Color *text_color, + int (*action)(void *, int), void *user_data, + SDL_Renderer *renderer, int state, int options, ...) +{ + struct mftk_widget **children; + va_list ap; + int i; + struct mftk_widget *w; + struct mftk_radio *r; + + children = calloc(options, sizeof(*children)); + if (children == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create widget: %s", + strerror(errno)); + return NULL; + } + + va_start(ap, options); + for (i = 0; i < options; ++i) { + children[i] = mftk_check_new(butn_width, butn_padding, + butn_color, mark_color, (i == state), + label_padding, font, va_arg(ap, const char *), + text_color, NULL/*TODO*/, NULL/*TODO*/, + renderer); + if (children[i] == NULL) { + for (; i >= 0; --i) { + mftk_widget_destroy(&children[i]); + } + free(children); + va_end(ap); + return NULL; + } + mftk_check_set_shape(children[i], MFTK_CHECK_SHAPE_CIR); + } + va_end(ap); + + mftk_widget_init(w, r, radio); + + r->grid = mftk_grid_new_a(options, 1, item_padding, 0, children); + if (r->grid == NULL) { + for (i = 0; i < options; ++i) { + mftk_widget_destroy(&children[i]); + } + free(children); + free(r); + } + + r->state = state; + r->action = action; + r->user_data = user_data; + + return w; +} -- cgit v0.9.1