]> git.mcshandy.xyz Git - picker/commitdiff
Improve and expand mouse control, most UI elements can do mouse things now
authorRandy McShandy <randy@mcshandy.xyz>
Sun, 8 Oct 2023 05:09:19 +0000 (00:09 -0500)
committerRandy McShandy <randy@mcshandy.xyz>
Sun, 8 Oct 2023 05:09:19 +0000 (00:09 -0500)
source/rendering.c
source/structs.h

index 498762d82df89668924b0e089afa3209950aa94f..f7353cb5f0b846224d1c3d6ba4839afcb823f43d 100644 (file)
@@ -131,26 +131,30 @@ void grab_mouse_state(Runtime_Info* runtime)
        buttons = SDL_GetMouseState(&runtime->mouse_click_pos.x, &runtime->mouse_click_pos.y);
 
        if ((buttons & SDL_BUTTON_LMASK) != 0) {
-               runtime->mouse_click = 1;
+               runtime->mouse_click[M_LEFT] = 1;
+       }
+       if ((buttons & SDL_BUTTON_RMASK) != 0) {
+               runtime->mouse_click[M_RIGHT] = 1;
        }
 }
 
 void clear_mouse_state(Runtime_Info* runtime)
 {
-       runtime->mouse_click = 0;
+       runtime->mouse_click[M_LEFT] = 0;
+       runtime->mouse_click[M_RIGHT] = 0;
 }
 
-int32_t is_mouse_down(Runtime_Info* runtime)
+int32_t is_mouse_down(Runtime_Info* runtime, mouse_button mb)
 {
        grab_mouse_state(runtime);
-       int32_t down = runtime->mouse_click;
-       runtime->mouse_click = 0;
+       int32_t down = runtime->mouse_click[mb];
+       clear_mouse_state(runtime);
        return down;
 }
 
-int32_t mouse_click_in_container(Runtime_Info* runtime, SDL_FRect* container)
+int32_t mouse_click_in_container(Runtime_Info* runtime, SDL_FRect* container, mouse_button mb)
 {
-       if (is_mouse_down(runtime) &&
+       if (is_mouse_down(runtime, mb) &&
                        runtime->mouse_click_pos.x >= container->x &&
                        runtime->mouse_click_pos.x <= container->x + container->w &&
                        runtime->mouse_click_pos.y >= container->y &&
@@ -175,6 +179,15 @@ int32_t render_palette(Runtime_Info* runtime, SDL_FRect* container)
                        item.h *= 1.2;
                }
                SDL_RenderFillRectF(mgr.rend, &item);
+
+               if (mouse_click_in_container(runtime, &item, M_LEFT))
+               {
+                       runtime->active_palette = p;
+               }
+               if (mouse_click_in_container(runtime, &item, M_RIGHT))
+               {
+                       runtime->layout.palette_color[p] = runtime->active_hsl;
+               }
        }
 
        return 0;
@@ -207,11 +220,17 @@ int32_t render_hsl_square(Runtime_Info* runtime, SDL_FRect* container)
                }
        }
 
-       if (mouse_click_in_container(runtime, container))
+       if (mouse_click_in_container(runtime, container, M_LEFT))
        {
                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));
        }
+       if (mouse_click_in_container(runtime, container, M_RIGHT))
+       {
+               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));
+               runtime->layout.palette_color[runtime->active_palette] = runtime->active_hsl;
+       }
 
        float s_norm = (float)runtime->active_hsl.s/100.0f;
        float l_norm = (float)runtime->active_hsl.l/100.0f;
@@ -334,9 +353,14 @@ int32_t render_vertical_hue_spectrum(Runtime_Info* runtime, SDL_FRect* container
        NULL_CHECK(runtime);
        NULL_CHECK(container);
 
-       if (mouse_click_in_container(runtime, container))
+       if (mouse_click_in_container(runtime, container, M_LEFT))
+       {
+               runtime->active_hsl.h = ((runtime->mouse_click_pos.y - container->y) / container->h) * 360;
+       }
+       if (mouse_click_in_container(runtime, container, M_RIGHT))
        {
                runtime->active_hsl.h = ((runtime->mouse_click_pos.y - container->y) / container->h) * 360;
+               runtime->layout.palette_color[runtime->active_palette] = runtime->active_hsl;
        }
 
        int bar_y = runtime->active_hsl.h/360.0f*container->h + container->y;
index 92053ecc9496b535c3bcd1fa26f8887e7809b716..affbaa3596433f67ac5aeae536e29c592f67dba2 100644 (file)
@@ -75,6 +75,13 @@ typedef struct
        HSL_Color               palette_color[PALETTE_SIZE];
 } Window_Layout;
 
+typedef enum
+{
+       M_LEFT = SDL_BUTTON_LMASK,
+       M_RIGHT = SDL_BUTTON_RMASK,
+       M_COUNT
+} mouse_button;
+
 typedef struct
 {
        SDL_Color active_rgb;
@@ -88,7 +95,7 @@ typedef struct
        TTF_Font* font;
 
        SDL_Point mouse_click_pos;
-       int mouse_click;
+       int mouse_click[M_COUNT];
 
        int active_palette;