From 66abe8585c312f89974e043ae7a86c14ea521157 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Mon, 8 Jan 2024 00:16:47 +0000 Subject: [PATCH] Add drop_area_t --- include/drop_area.h | 17 ++++++++++++++ src/drop_area.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 include/drop_area.h create mode 100644 src/drop_area.c diff --git a/include/drop_area.h b/include/drop_area.h new file mode 100644 index 0000000..a89d08a --- /dev/null +++ b/include/drop_area.h @@ -0,0 +1,17 @@ +#ifndef DROP_AREA_H +#define DROP_AREA_H + +#include "colour.h" +#include "window.h" +#include + +typedef struct { + render_rect_t rect; + colour_t border_colour; + bool mouseover; +} drop_area_t; + +void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect, + colour_t border_colour); + +#endif // !DROP_AREA_H diff --git a/src/drop_area.c b/src/drop_area.c new file mode 100644 index 0000000..3737c35 --- /dev/null +++ b/src/drop_area.c @@ -0,0 +1,54 @@ +#include "drop_area.h" +#include "SDL_events.h" +#include "colour.h" +#include "window.h" +#include + +void handle_hover(const window_t *wnd, void *obj, const SDL_Event *ev); +void handle_drop_file(const window_t *wnd, void *obj, const SDL_Event *ev); +void render_drop_area(const window_t *wnd, void *obj); + +void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect, + colour_t border_colour) { + area->rect = rect; + area->border_colour = border_colour; + area->mouseover = false; + + add_event_listener(wnd, area, handle_hover); + add_event_listener(wnd, area, handle_drop_file); + add_renderer_func(wnd, area, render_drop_area); +} + +void handle_hover(const window_t *wnd, void *obj, const SDL_Event *ev) { + if (!obj || ev->type != SDL_MOUSEMOTION) { + return; + } + + drop_area_t *area = (drop_area_t *)obj; + + area->mouseover = aabb(ev->motion.x, ev->motion.y, &(area->rect)); +} + +void handle_drop_file(const window_t *wnd, void *obj, const SDL_Event *ev) { + if (!obj || ev->type != SDL_DROPFILE) { + return; + } + + drop_area_t *area = (drop_area_t *)obj; + + if (!(area->mouseover)) { + return; + } + + printf("%s\n", ev->drop.file); +} + +void render_drop_area(const window_t *wnd, void *obj) { + if (!obj) { + return; + } + + drop_area_t *area = (drop_area_t *)obj; + + draw_rect(wnd, &(area->rect), area->border_colour); +}