diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/saffron_widget.c | 28 | ||||
| -rw-r--r-- | src/saffron_window.c | 19 |
2 files changed, 38 insertions, 9 deletions
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); |
