From 41829434c29da9cb1d59654bca7c02016dbd4d85 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 05 Aug 2021 00:29:01 -0400 Subject: tk: Add check widget --- diff --git a/src/defs.h b/src/defs.h index 67de967..f35c53b 100644 --- a/src/defs.h +++ b/src/defs.h @@ -32,8 +32,9 @@ #define MF_SPLASH_ROW_M 16 /* Margin between rows */ #define MF_SPLASH_COL_M 16 /* Margin between labels and entries */ #define MF_SPLASH_LBL_M 8 /* Margin between radio buttons and labels */ -#define MF_SPLASH_BOX_W 16 /* Radio button and check box width */ -#define MF_SPLASH_BOX_P 2 /* Radio button and check box padding */ +#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_BTN_M 8 /* Margin between buttons */ #define MF_SPLASH_BTN_P 8 /* Button padding */ #define MF_SPLASH_TITLE_FONT_S 48 /* Title font size */ @@ -60,5 +61,13 @@ #define MF_COLOR_BUTN_G 0xAF #define MF_COLOR_BUTN_B 0xAF #define MF_COLOR_BUTN_A 0xFF +#define MF_COLOR_CHKB_R 0xAF /* Radio button and check box button fill color */ +#define MF_COLOR_CHKB_G 0xAF +#define MF_COLOR_CHKB_B 0xAF +#define MF_COLOR_CHKB_A 0xFF +#define MF_COLOR_CHKM_R 0x00 /* Radio button and check box mark fill color */ +#define MF_COLOR_CHKM_G 0x00 +#define MF_COLOR_CHKM_B 0x00 +#define MF_COLOR_CHKM_A 0xFF #endif /* MF_DEFS_H_ */ diff --git a/src/splash.c b/src/splash.c index 40d9f1e..403d0dd 100644 --- a/src/splash.c +++ b/src/splash.c @@ -29,6 +29,14 @@ #include "util.h" static int +_mf_splash_fow(void *user_data, int state) +{ + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Fog of war: %s", + state ? "enabled" : "disabled"); + return 0; +} + +static int _mf_splash_quit(void *user_data) { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Quitting"); @@ -51,6 +59,8 @@ mf_splash(SDL_Renderer *renderer) SDL_Color form_color; SDL_Color text_color; SDL_Color butn_color; + SDL_Color chkb_color; + SDL_Color chkm_color; struct mftk_widget *box; struct mf_maze *maze = NULL; SDL_Color maze_color; @@ -84,12 +94,26 @@ mf_splash(SDL_Renderer *renderer) butn_color.g = MF_COLOR_BUTN_G; butn_color.b = MF_COLOR_BUTN_B; butn_color.a = MF_COLOR_BUTN_A; + chkb_color.r = MF_COLOR_CHKB_R; + chkb_color.g = MF_COLOR_CHKB_G; + chkb_color.b = MF_COLOR_CHKB_B; + chkb_color.a = MF_COLOR_CHKB_A; + chkm_color.r = MF_COLOR_CHKM_R; + chkm_color.g = MF_COLOR_CHKM_G; + chkm_color.b = MF_COLOR_CHKM_B; + chkm_color.a = MF_COLOR_CHKM_A; 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_grid_new(3, 1, MF_SPLASH_TITLE_M, 0, mftk_label_new(title_font, "Maze Fight", &text_color, renderer), + mftk_check_new(MF_SPLASH_CHK_BTN_W, + MF_SPLASH_CHK_BTN_P, &chkb_color, + &chkm_color, SDL_TRUE, + MF_SPLASH_CHK_LBL_P, text_font, + "Fog of war", &text_color, + _mf_splash_fow, NULL, renderer), mftk_grid_new(1, 2, 0, MF_SPLASH_BTN_M, mftk_button_new(text_font, "Quit", &text_color, &butn_color, diff --git a/src/tk.h b/src/tk.h index 8cc65ce..31589f8 100644 --- a/src/tk.h +++ b/src/tk.h @@ -60,4 +60,11 @@ 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 * +mftk_check_new(int butn_width, int butn_padding, SDL_Color *butn_color, + SDL_Color *mark_color, int state, int label_padding, + TTF_Font *font, const char *text, SDL_Color *text_color, + int (*action)(void *, int), void *user_data, + SDL_Renderer *renderer); + #endif /* MFTK_H_ */ diff --git a/src/tk/check.c b/src/tk/check.c new file mode 100644 index 0000000..b5f91b8 --- /dev/null +++ b/src/tk/check.c @@ -0,0 +1,159 @@ +/* + * 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 "../tk.h" +#include "widget.h" + +struct mftk_check { + struct mftk_widget parent; + int butn_width; + int butn_padding; + SDL_Color *butn_color; + SDL_Color *mark_color; + int state; + int label_padding; + struct mftk_widget *label; + int (*action)(void *, int); + void *user_data; +}; + +static void +_mftk_check_layout(struct mftk_widget *w) +{ + struct mftk_check *c = (struct mftk_check *) w; + + mftk_widget_layout(c->label); + + w->w = c->butn_width; + if (c->label != NULL) { + w->w += c->label_padding + c->label->w; + } + w->h = c->butn_width; + if (c->label != NULL && c->label->h > w->h) { + w->h = c->label->h; + } +} + +static int +_mftk_check_event(struct mftk_widget *w, SDL_Event *e, + int x __attribute__((__unused__)), + int y __attribute__((__unused__))) +{ + struct mftk_check *c = (struct mftk_check *) w; + + switch (e->type) { + case SDL_MOUSEBUTTONUP: + if (e->button.button == SDL_BUTTON_LEFT) { + c->state = !c->state; + return c->action(c->user_data, c->state); + } + break; + default: + break; + } + + return 0; +} + +static int +_mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) +{ + struct mftk_check *c = (struct mftk_check *) w; + SDL_Rect rect; + + rect.x = x; + rect.y = y; + rect.w = c->butn_width; + rect.h = c->butn_width; + if (SDL_SetRenderDrawColor(renderer, + c->butn_color->r, c->butn_color->g, + c->butn_color->b, c->butn_color->a) < 0 || + SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + return -1; + } + + if (c->state == SDL_TRUE) { + rect.x += c->butn_padding; + rect.y += c->butn_padding; + rect.w -= c->butn_padding * 2; + rect.h -= c->butn_padding * 2; + if (SDL_SetRenderDrawColor(renderer, + c->mark_color->r, c->mark_color->g, + c->mark_color->b, c->mark_color->a) < 0 + || SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + return -1; + } + } + + if (c->label != NULL && mftk_widget_render(c->label, renderer, + x + c->butn_width + c->label_padding, y) < 0) { + return -1; + } + + return 0; +} + +static void +_mftk_check_destroy(struct mftk_widget *w) +{ + struct mftk_check *c = (struct mftk_check *) w; + + if (c->label != NULL) { + mftk_widget_destroy(&c->label); + } +} + +struct mftk_widget * +mftk_check_new(int butn_width, int butn_padding, SDL_Color *butn_color, + SDL_Color *mark_color, int state, int label_padding, + TTF_Font *font, const char *text, SDL_Color *text_color, + int (*action)(void *, int), void *user_data, + SDL_Renderer *renderer) +{ + struct mftk_widget *w; + struct mftk_check *c; + + mftk_widget_init(w, c, check); + + if (font != NULL && text != NULL && text_color != NULL) { + c->label = mftk_label_new(font, text, text_color, renderer); + if (c->label == NULL) { + free(c); + return NULL; + } + } + + c->butn_width = butn_width; + c->butn_padding = butn_padding; + c->butn_color = butn_color; + c->mark_color = mark_color; + c->state = state; + c->label_padding = label_padding; + c->action = action; + c->user_data = user_data; + + return w; +} diff --git a/src/tk/local.mk b/src/tk/local.mk index 12c07d7..679e2a4 100644 --- a/src/tk/local.mk +++ b/src/tk/local.mk @@ -1,6 +1,7 @@ mazefight_SOURCES += \ %reldir%/box.c \ %reldir%/button.c \ + %reldir%/check.c \ %reldir%/grid.c \ %reldir%/label.c \ %reldir%/widget.c \ -- cgit v0.9.1