diff options
| -rw-r--r-- | include/saffron_api.h | 1 | ||||
| -rw-r--r-- | src/saffron_widget.c | 28 | ||||
| -rw-r--r-- | src/saffron_window.c | 19 | ||||
| -rw-r--r-- | tests/test_main.c | 13 |
4 files changed, 49 insertions, 12 deletions
diff --git a/include/saffron_api.h b/include/saffron_api.h index a993ae6..c2e6c9c 100644 --- a/include/saffron_api.h +++ b/include/saffron_api.h @@ -16,5 +16,6 @@ SaffronWindow* saffron_window_new(const char* title, int w, int h); void saffron_window_free(SaffronWindow* window); void saffron_window_show(SaffronWindow* window); void saffron_window_main(SaffronWindow* window); +SaffronWidget* saffron_widget_hit_test(SaffronWidget* widget, int x, int y); #endif diff --git a/src/saffron_widget.c b/src/saffron_widget.c index 85b2013..932b7a5 100644 --- a/src/saffron_widget.c +++ b/src/saffron_widget.c @@ -1,8 +1,7 @@ #include <SDL3/SDL.h> +#include <stdio.h> #include <stdlib.h> -#include <saffron_widget.h> -#include <saffron_api.h> -#include <saffron_window.h> +#include <saffron.h> SaffronWidget* saffron_widget_new(void) { /* This function is a generic primitive for creating widgets. You wouldn't want to do this manually unless you're a lunatic. It is meant to be wrapped around by other functions that change the default parameters, for example, what sane person makes a widget 0x0x0x0? you LUNATIC! */ @@ -57,3 +56,26 @@ void saffron_widget_add_child(SaffronWidget *parent, SaffronWidget *child) { parent->child_count++; } +SaffronWidget* saffron_widget_hit_test(SaffronWidget* widget, int x, int y) { + if (!widget) { + printf("[saffron] hit test: widget is NULL!\n"); + return NULL; + } + if (x < widget->x || x > widget->x + widget->w) { + printf("[saffron] hit test: widget failed X bounds check!\n"); + return NULL; + } + if (y < widget->y || y > widget->y + widget->h) { + printf("[saffron] hit test: widget failed Y bounds check!\n"); + return NULL; + } + + // check children front to back + for (int i = widget->child_count - 1; i >= 0; i--) { + printf("[saffron] recursing to child (%i)\n", i); + SaffronWidget* hit = saffron_widget_hit_test(widget->children[i], x, y); + if (hit) return hit; + } + + return widget; +} diff --git a/src/saffron_window.c b/src/saffron_window.c index 4bdae76..a25fa0e 100644 --- a/src/saffron_window.c +++ b/src/saffron_window.c @@ -1,17 +1,16 @@ +#include <stdio.h> #include <SDL3/SDL_events.h> #include <stdlib.h> #include <SDL3/SDL.h> #include <SDL3_ttf/SDL_ttf.h> -#include <saffron_api.h> -#include <saffron_widget.h> -#include <saffron_window.h> +#include <saffron.h> SaffronWindow* saffron_window_new(const char* title, int w, int h) { SaffronWindow* window = malloc(sizeof(SaffronWindow)); window->root = saffron_widget_new(); // frick, need to implement this. window->title = title; - window->w = w; - window->h = h; + window->w = window->root->w = w; + window->h = window->root->h = h; window->sdl_window = SDL_CreateWindow(title, w, h, SDL_WINDOW_RESIZABLE); window->renderer = SDL_CreateRenderer(window->sdl_window, NULL); return window; @@ -35,7 +34,15 @@ void saffron_window_main(SaffronWindow *window) { if (event.type == SDL_EVENT_QUIT) { running = false; } - // TODO: send events to widgets and stuff + if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { + printf("[saffron] mouse down!\n"); + if (event.button.button == SDL_BUTTON_LEFT) { + printf("[saffron] left click at %f, %f!\n", event.button.x, event.button.y); + printf("[saffron] calling hit test at coordinates\n"); + SaffronWidget* hit = saffron_widget_hit_test(window->root, (int)event.button.x, (int)event.button.y); + if (hit && hit->on_click) hit->on_click(hit); + } + } } SDL_SetRenderDrawColor(window->renderer, 0, 0, 0, 255); diff --git a/tests/test_main.c b/tests/test_main.c index c171192..5bef6d5 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -1,10 +1,10 @@ +#include <stdio.h> +#include <saffron.h> #include <SDL3/SDL.h> -#include <saffron_api.h> #include <SDL3/SDL_rect.h> #include <SDL3/SDL_render.h> -#include <saffron.h> -/* PLEASE don't do this in production +/* PLEASE don't do this in production! * when wrappers are implemented, * THEY will make draw functions for you * THIS IS A BAD IDEA */ @@ -17,6 +17,12 @@ void my_test_draw(SaffronWidget* self, SDL_Renderer* renderer) { SDL_RenderRect(renderer, &rect); } +/* YET AGAIN DONT DO THIS IN PROD PLEASE + * this is very lunatic */ +void my_test_onclick(SaffronWidget* self) { + printf("clicked!\n"); +} + int main(void) { saffron_init(); @@ -30,6 +36,7 @@ int main(void) { test->w = 200; test->h = 150; test->draw = my_test_draw; + test->on_click = my_test_onclick; /* lunatic method 2 */ SaffronWidget* test2 = saffron_widget_new(); |
