summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/tk.h5
-rw-r--r--src/tk/button.c125
-rw-r--r--src/tk/local.mk1
3 files changed, 131 insertions, 0 deletions
diff --git a/src/tk.h b/src/tk.h
index 1c32e32..c41b194 100644
--- a/src/tk.h
+++ b/src/tk.h
@@ -48,4 +48,9 @@ struct mftk_widget *
mftk_label_new(TTF_Font *font, const char *text, SDL_Color *color,
SDL_Renderer *renderer);
+struct mftk_widget *
+mftk_button_new(TTF_Font *font, const char *text, SDL_Color *text_color,
+ SDL_Color *fill_color, int padding,
+ int (*action)(void *), void *user_data, SDL_Renderer *renderer);
+
#endif /* MFTK_H_ */
diff --git a/src/tk/button.c b/src/tk/button.c
new file mode 100644
index 0000000..c7d0c5f
--- /dev/null
+++ b/src/tk/button.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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <SDL.h>
+#include "../tk.h"
+#include "widget.h"
+
+struct mftk_button {
+ struct mftk_widget parent;
+ struct mftk_widget *label;
+ SDL_Color *fill_color;
+ int padding;
+ int (*action)(void *);
+ void *user_data;
+};
+
+static void
+_mftk_button_layout(struct mftk_widget *w)
+{
+ struct mftk_button *b = (struct mftk_button *) w;
+
+ mftk_widget_layout(b->label);
+
+ w->w = b->label->w + b->padding * 2;
+ w->h = b->label->h + b->padding * 2;
+}
+
+static int
+_mftk_button_event(struct mftk_widget *w, SDL_Event *e)
+{
+ struct mftk_button *b = (struct mftk_button *) w;
+
+ switch (e->type) {
+ case SDL_MOUSEBUTTONUP:
+ if (e->button.button == SDL_BUTTON_LEFT) {
+ return b->action(b->user_data);
+ }
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+static int
+_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;
+
+ rect.x = x;
+ rect.y = y;
+ rect.w = w->w;
+ rect.h = w->h;
+ if (SDL_SetRenderDrawColor(renderer,
+ b->fill_color->r, b->fill_color->g,
+ b->fill_color->b, b->fill_color->a) < 0 ||
+ SDL_RenderFillRect(renderer, &rect) < 0) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't render widget: %s",
+ SDL_GetError());
+ return -1;
+ }
+
+ if (mftk_widget_render(b->label, renderer,
+ x + b->padding, y + b->padding) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static void
+_mftk_button_destroy(struct mftk_widget *w)
+{
+ struct mftk_button *b = (struct mftk_button *) w;
+
+ mftk_widget_destroy(&b->label);
+}
+
+struct mftk_widget *
+mftk_button_new(TTF_Font *font, const char *text, SDL_Color *text_color,
+ SDL_Color *fill_color, int padding,
+ int (*action)(void *), void *user_data, SDL_Renderer *renderer)
+{
+ struct mftk_widget *w;
+ struct mftk_button *b;
+
+ if ((w = mftk_widget_new(sizeof(*b))) == NULL) return NULL;
+ b = (struct mftk_button *) w;
+ w->layout = &_mftk_button_layout;
+ w->event = &_mftk_button_event;
+ w->render = &_mftk_button_render;
+ w->destroy = &_mftk_button_destroy;
+
+ b->label = mftk_label_new(font, text, text_color, renderer);
+ if (b->label == NULL) {
+ free(b);
+ return NULL;
+ }
+
+ b->fill_color = fill_color;
+ b->padding = padding;
+ b->action = action;
+ b->user_data = user_data;
+
+ return w;
+}
diff --git a/src/tk/local.mk b/src/tk/local.mk
index 0c37299..cb71e9f 100644
--- a/src/tk/local.mk
+++ b/src/tk/local.mk
@@ -1,4 +1,5 @@
mazefight_SOURCES += \
+ %reldir%/button.c \
%reldir%/label.c \
%reldir%/widget.c \
%reldir%/widget.h