Implement loading an image when file is dropped
This commit is contained in:
		| @@ -2,13 +2,17 @@ | ||||
| #define DROP_AREA_H | ||||
|  | ||||
| #include "colour.h" | ||||
| #include "image.h" | ||||
| #include "window.h" | ||||
| #include <stdbool.h> | ||||
|  | ||||
| typedef struct { | ||||
|   render_rect_t rect; | ||||
|   render_rect_t image_rect; | ||||
|   colour_t border_colour; | ||||
|   image_t image; | ||||
|   bool mouseover; | ||||
|   bool image_loaded; | ||||
| } drop_area_t; | ||||
|  | ||||
| void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect, | ||||
|   | ||||
| @@ -1,18 +1,23 @@ | ||||
| #include "drop_area.h" | ||||
| #include "SDL_events.h" | ||||
| #include "colour.h" | ||||
| #include "image.h" | ||||
| #include "window.h" | ||||
| #include <stdbool.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| 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); | ||||
| render_rect_t image_render_rect(drop_area_t *area); | ||||
|  | ||||
| void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect, | ||||
|                     colour_t border_colour) { | ||||
|   area->rect = rect; | ||||
|   area->image_rect = (render_rect_t){0}; | ||||
|   area->border_colour = border_colour; | ||||
|   area->mouseover = false; | ||||
|   area->image_loaded = false; | ||||
|  | ||||
|   add_event_listener(wnd, area, handle_hover); | ||||
|   add_event_listener(wnd, area, handle_drop_file); | ||||
| @@ -40,7 +45,14 @@ void handle_drop_file(const window_t *wnd, void *obj, const SDL_Event *ev) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   printf("%s\n", ev->drop.file); | ||||
|   printf("Loading: %s\n", ev->drop.file); | ||||
|  | ||||
|   area->image_loaded = load_image(wnd, &(area->image), ev->drop.file); | ||||
|  | ||||
|   if (area->image_loaded) { | ||||
|     printf("Width: %ld, Height: %ld\n", area->image.width, area->image.height); | ||||
|     area->image_rect = image_render_rect(area); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void render_drop_area(const window_t *wnd, void *obj) { | ||||
| @@ -50,5 +62,37 @@ void render_drop_area(const window_t *wnd, void *obj) { | ||||
|  | ||||
|   drop_area_t *area = (drop_area_t *)obj; | ||||
|  | ||||
|   if (area->image_loaded) { | ||||
|     render_texture(wnd, area->image.texture, NULL, &(area->image_rect)); | ||||
|   } | ||||
|  | ||||
|   draw_rect(wnd, &(area->rect), area->border_colour); | ||||
| } | ||||
|  | ||||
| render_rect_t image_render_rect(drop_area_t *area) { | ||||
|   if (!area || !(area->image_loaded)) { | ||||
|     return (render_rect_t){0}; | ||||
|   } | ||||
|  | ||||
|   u64 w = 0; | ||||
|   u64 h = 0; | ||||
|  | ||||
|   if (area->image.width < area->rect.width && | ||||
|       area->image.height < area->rect.height) { | ||||
|     w = area->image.width; | ||||
|     h = area->image.height; | ||||
|   } else { | ||||
|     f32 w_factor = (f32)area->rect.width / area->image.width; | ||||
|     f32 h_factor = (f32)area->rect.height / area->image.height; | ||||
|  | ||||
|     f32 factor = w_factor < h_factor ? w_factor : h_factor; | ||||
|  | ||||
|     w = area->image.width * factor; | ||||
|     h = area->image.height * factor; | ||||
|   } | ||||
|  | ||||
|   u64 x = area->rect.x + (area->rect.width - w) / 2; | ||||
|   u64 y = area->rect.y + (area->rect.height - h) / 2; | ||||
|  | ||||
|   return (render_rect_t){x, y, w, h}; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user