From 4bc03025c1240faae57baf0e4937eaee14f0a704 Mon Sep 17 00:00:00 2001 From: Randy McShandy Date: Sat, 7 Oct 2023 00:22:45 -0500 Subject: [PATCH] Initial mouse support for the interactive windows --- README.md | 3 ++- source/rendering.c | 54 +++++++++++++++++++++++++++++++++++++++++++--- source/structs.h | 3 +++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fc7b301..fc463b1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ A quick and easy color picker for visualizing RGB/HSL Adjust saturation in the big square with `H/L`, luminence with `J/K`. Or sat with `J/K` and luminence with `H/L`. I forgot which but now that I think about it, it could go in the config file. The hue spectrum slider adjusts with `N/B`. Holding either `shift` increases the adjust speed of any other input. -If it wasn't already obvious, the bottom portion displays the active color in a big square along with RGB and HSL value, in decimal and hex. There is no mouse support as of this release. +If it wasn't already obvious, the bottom portion displays the active color in a big square along with RGB and HSL value, in decimal and hex. +Now including mouse support -- click/drag around in the hue spectrum and L/S square to change values. ## Configure and build ### Dependencies diff --git a/source/rendering.c b/source/rendering.c index 99d929b..691786b 100644 --- a/source/rendering.c +++ b/source/rendering.c @@ -117,6 +117,41 @@ int32_t delay(int32_t delay_time) return 0; } +void grab_mouse_state(Runtime_Info* runtime) +{ + Uint32 buttons; + SDL_PumpEvents(); // make sure we have the latest mouse state. + buttons = SDL_GetMouseState(&runtime->mouse_click_pos.x, &runtime->mouse_click_pos.y); + + if ((buttons & SDL_BUTTON_LMASK) != 0) { + runtime->mouse_click = 1; + } +} + +void clear_mouse_state(Runtime_Info* runtime) +{ + runtime->mouse_click = 0; +} + +int32_t is_mouse_down(Runtime_Info* runtime) +{ + grab_mouse_state(runtime); + int32_t down = runtime->mouse_click; + runtime->mouse_click = 0; + return down; +} + +int32_t mouse_click_in_container(Runtime_Info* runtime, SDL_FRect* container) +{ + if (is_mouse_down(runtime) && + runtime->mouse_click_pos.x >= container->x && + runtime->mouse_click_pos.x <= container->x + container->w && + runtime->mouse_click_pos.y >= container->y && + runtime->mouse_click_pos.y <= container->y + container->h) + return 1; + return 0; +} + // Rename this eventually // It's actually HSL int32_t render_hsl_square(Runtime_Info* runtime, SDL_FRect* container) @@ -146,6 +181,12 @@ int32_t render_hsl_square(Runtime_Info* runtime, SDL_FRect* container) } } + if (mouse_click_in_container(runtime, container)) + { + runtime->active_hsl.s = 100.0f * (runtime->mouse_click_pos.x - container->x) / container->w; + runtime->active_hsl.l = 100.0f * (1.0f - ((float)(runtime->mouse_click_pos.y - container->y) / container->h)); + } + float s_norm = (float)runtime->active_hsl.s/100.0f; float l_norm = (float)runtime->active_hsl.l/100.0f; SDL_Rect cursor = { @@ -154,6 +195,7 @@ int32_t render_hsl_square(Runtime_Info* runtime, SDL_FRect* container) .w = 10, .h = 10 }; + SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(black)); SDL_RenderDrawLine(mgr.rend, container->x-16, cursor.y, container->x+container->w+16, cursor.y); SDL_RenderDrawLine(mgr.rend, cursor.x, container->y-16, cursor.x, container->y+container->h+16); @@ -217,6 +259,7 @@ int32_t render_text_container(Runtime_Info* runtime, Text_Container* tc) { NULL_CHECK(runtime); NULL_CHECK(tc); + // basics tc->surface = TTF_RenderText_Solid(runtime->font, tc->text, black); tc->texture = SDL_CreateTextureFromSurface(mgr.rend, tc->surface); @@ -265,13 +308,18 @@ int32_t render_vertical_hue_spectrum(Runtime_Info* runtime, SDL_FRect* container NULL_CHECK(runtime); NULL_CHECK(container); -#if 0 + if (mouse_click_in_container(runtime, container)) + { + runtime->active_hsl.h = ((runtime->mouse_click_pos.y - container->y) / container->h) * 360; + } + int bar_y = runtime->active_hsl.h/360.0f*container->h + container->y; int spectrum_shifter = 0; -#endif +#if 0 int spectrum_shifter = runtime->active_hsl.h + 180; int bar_y = container->h/2 + container->y; +#endif SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(black)); SDL_RenderDrawLine(mgr.rend, container->x-16, bar_y, container->w+container->x+16, bar_y); @@ -284,7 +332,6 @@ int32_t render_vertical_hue_spectrum(Runtime_Info* runtime, SDL_FRect* container SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(slice_color)); SDL_RenderDrawLine(mgr.rend, container->x, container->y + n, container->w+container->x, container->y+n); } - return 0; } @@ -386,6 +433,7 @@ int32_t check_inputs(Runtime_Info* runtime) break; } } + } return 0; diff --git a/source/structs.h b/source/structs.h index d818b5e..1cbf73d 100644 --- a/source/structs.h +++ b/source/structs.h @@ -82,6 +82,9 @@ typedef struct const char font_path[128]; TTF_Font* font; + SDL_Point mouse_click_pos; + int mouse_click; + } Runtime_Info; #endif // STRUCTS__ -- 2.49.0