]> git.mcshandy.xyz Git - picker/commitdiff
Initial mouse support for the interactive windows
authorRandy McShandy <randy@mcshandy.xyz>
Sat, 7 Oct 2023 05:22:45 +0000 (00:22 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Sat, 7 Oct 2023 05:22:45 +0000 (00:22 -0500)
README.md
source/rendering.c
source/structs.h

index fc7b301075623a22c1b344d5f0aa56e484fb6549..fc463b19ce0bebcc0c34dc71ca92849f41526cc7 100644 (file)
--- 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
index 99d929bfd2fa285d8033859c244b2723f2bc9aed..691786b71943207a6cf39c200a14dae79509bdad 100644 (file)
@@ -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;
index d818b5eaca87e5e04eab50dc5f48d88967c670fd..1cbf73d682b3b2215ce6f469f42256b6cab61ee4 100644 (file)
@@ -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__