From ddf513909ce1742021b9c57ea7279a61aeda4899 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Wed, 04 Aug 2021 20:17:59 -0400 Subject: tk: Implement mouse button up event --- diff --git a/src/splash.c b/src/splash.c index b7d9185..9b94057 100644 --- a/src/splash.c +++ b/src/splash.c @@ -28,6 +28,13 @@ #include "tk.h" #include "util.h" +static int +_mf_splash_play(void *user_data) +{ + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Playing"); + return 0; +} + int mf_splash(SDL_Renderer *renderer) { @@ -70,7 +77,8 @@ mf_splash(SDL_Renderer *renderer) mftk_label_new(title_font, "Maze Fight", &text_color, renderer), mftk_button_new(text_font, "Play", &text_color, - &butn_color, 2, NULL, NULL, renderer)); + &butn_color, 2, &_mf_splash_play, NULL, + renderer)); /* TODO: Widgets */ mftk_widget_layout(grid); @@ -108,6 +116,8 @@ 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); SDL_RenderPresent(renderer); diff --git a/src/tk.h b/src/tk.h index 7a3c9c6..2a4706d 100644 --- a/src/tk.h +++ b/src/tk.h @@ -36,7 +36,7 @@ void mftk_widget_layout(struct mftk_widget *w); int -mftk_widget_event(struct mftk_widget *w, SDL_Event *e); +mftk_widget_event(struct mftk_widget *w, SDL_Event *e, int x, int y); int mftk_widget_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y); diff --git a/src/tk/button.c b/src/tk/button.c index 0db91a9..ffac9de 100644 --- a/src/tk/button.c +++ b/src/tk/button.c @@ -42,7 +42,9 @@ _mftk_button_layout(struct mftk_widget *w) } static int -_mftk_button_event(struct mftk_widget *w, SDL_Event *e) +_mftk_button_event(struct mftk_widget *w, SDL_Event *e, + int x __attribute__((__unused__)), + int y __attribute__((__unused__))) { struct mftk_button *b = (struct mftk_button *) w; diff --git a/src/tk/grid.c b/src/tk/grid.c index 5994929..67fa677 100644 --- a/src/tk/grid.c +++ b/src/tk/grid.c @@ -79,12 +79,33 @@ _mftk_grid_layout(struct mftk_widget *w) } static int -_mftk_grid_event(struct mftk_widget *w, SDL_Event *e) +_mftk_grid_event(struct mftk_widget *w, SDL_Event *e, int x, int y) { - struct mftk_grid *g = (struct mftk_grid *) w; + struct mftk_grid *g = (struct mftk_grid *) w; + SDL_Point p; + SDL_Rect rect; + int r; + int c; + struct mftk_widget *child; switch (e->type) { case SDL_MOUSEBUTTONUP: + p.x = e->button.x; + p.y = e->button.y; + for (r = 0; r < g->rows; ++r) { + for (c = 0; c < g->cols; ++c) { + child = g->children[r * g->cols + c]; + rect.x = x + g->children_x[c]; + rect.y = y + g->children_y[r]; + rect.w = child->w; + rect.h = child->h; + if (SDL_PointInRect(&p, &rect) + == SDL_TRUE) { + return mftk_widget_event(child, + e, x, y); + } + } + } break; default: break; diff --git a/src/tk/label.c b/src/tk/label.c index ec4400a..1fcfd84 100644 --- a/src/tk/label.c +++ b/src/tk/label.c @@ -37,7 +37,9 @@ _mftk_label_layout(struct mftk_widget *w __attribute__((__unused__))) static int _mftk_label_event(struct mftk_widget *w __attribute__((__unused__)), - SDL_Event *e __attribute__((__unused__))) + SDL_Event *e __attribute__((__unused__)), + int x __attribute__((__unused__)), + int y __attribute__((__unused__))) { /* No events */ return 0; diff --git a/src/tk/widget.c b/src/tk/widget.c index 1bf9407..ea4835e 100644 --- a/src/tk/widget.c +++ b/src/tk/widget.c @@ -61,9 +61,9 @@ mftk_widget_layout(struct mftk_widget *w) } int -mftk_widget_event(struct mftk_widget *w, SDL_Event *e) +mftk_widget_event(struct mftk_widget *w, SDL_Event *e, int x, int y) { - return w->event(w, e); + return w->event(w, e, x, y); } int diff --git a/src/tk/widget.h b/src/tk/widget.h index ae81553..4966749 100644 --- a/src/tk/widget.h +++ b/src/tk/widget.h @@ -28,7 +28,7 @@ struct mftk_widget { int vis; int can_focus; void (*layout)(struct mftk_widget *); - int (*event)(struct mftk_widget *, SDL_Event *); + int (*event)(struct mftk_widget *, SDL_Event *, int, int); int (*render)(struct mftk_widget *, SDL_Renderer *, int, int); void (*destroy)(struct mftk_widget *); }; -- cgit v0.9.1