colourpicker

Simple Colour Picker written in C
git clone anongit@rnpnr.xyz:colourpicker.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

Commit: 079fbf0bd3782aa4aa3d92a8d11b1dd793b12429
Parent: f31a43e859a02b9598acb09ff3f59a4327c76a09
Author: Randy Palamar
Date:   Wed, 12 Jun 2024 20:44:37 -0600

animate stack pushing

Diffstat:
Mcolourpicker.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 70 insertions(+), 31 deletions(-)

diff --git a/colourpicker.c b/colourpicker.c @@ -349,6 +349,42 @@ do_status_bar(ColourPickerCtx *ctx, Rect r, f32 dt) } static void +do_colour_stack_item(ColourPickerCtx *ctx, v2 mouse, Rect r, i32 item_idx, + f32 fade_param, f32 stack_offset_y, f32 dt) +{ + static f32 stack_scales[ARRAY_COUNT(ctx->colour_stack.items)] = { 1, 1, 1, 1, 1 }; + f32 stack_scale_target = 1.2f; + f32 stack_scale_delta = (stack_scale_target - 1) * 8 * dt; + + v4 colour = ctx->colour_stack.items[item_idx]; + + b32 stack_collides = CheckCollisionPointRec(mouse.rv, r.rr); + if (stack_collides && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if (ctx->mode == CPM_HSV) { + ctx->colour = rgb_to_hsv(colour); + ctx->flags |= CPF_REFILL_TEXTURE; + } else { + ctx->colour = colour; + } + } + + stack_scales[item_idx] = move_towards_f32(stack_scales[item_idx], + stack_collides? stack_scale_target : 1, + stack_scale_delta); + f32 scale = stack_scales[item_idx]; + Rect draw_rect = { + .pos = { + .x = r.pos.x - (scale - 1) * r.size.w / 2, + .y = r.pos.y - (scale - 1) * r.size.h / 2 + stack_offset_y, + }, + .size = { .x = r.size.w * scale, .y = r.size.h * scale }, + }; + Color disp = ColorFromNormalized(colour.rv); + DrawRectangleRounded(draw_rect.rr, 1, 0, Fade(disp, 1 - fade_param)); + DrawRectangleRoundedLinesEx(draw_rect.rr, 1, 0, 3.0, Fade(BLACK, 1 - fade_param)); +} + +static void do_colour_stack(ColourPickerCtx *ctx, Rect sa, f32 dt) { v2 mouse = { .rv = GetMousePosition() }; @@ -359,41 +395,40 @@ do_colour_stack(ColourPickerCtx *ctx, Rect sa, f32 dt) r.pos.x += sa.size.w * 0.15; r.pos.y += sa.size.h * 0.06; - static f32 stack_scales[ARRAY_COUNT(ctx->colour_stack.items)] = { 1, 1, 1, 1, 1 }; - f32 stack_scale_target = 1.2f; - f32 stack_scale_delta = (stack_scale_target - 1) * 8 * dt; - for (u32 i = 0; i < 5; i++) { - i32 cidx = (ctx->colour_stack.widx + i) % ARRAY_COUNT(ctx->colour_stack.items); - v4 colour = ctx->colour_stack.items[cidx]; - - b32 stack_collides = CheckCollisionPointRec(mouse.rv, r.rr); - if (stack_collides && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - if (ctx->mode == CPM_HSV) { - ctx->colour = rgb_to_hsv(colour); - ctx->flags |= CPF_REFILL_TEXTURE; - } else { - ctx->colour = colour; - } - } + static v4 last_colour = {0}; + static f32 fade_param = 1.0f; + static f32 stack_offset_y = 0; + f32 stack_off_target = -sa.size.h * 0.16; + f32 stack_off_delta = -stack_off_target * 5 * dt; - stack_scales[cidx] = move_towards_f32(stack_scales[cidx], - stack_collides? stack_scale_target : 1, - stack_scale_delta); - - f32 scale = stack_scales[cidx]; - Rect draw_rect = { - .pos = { - .x = r.pos.x - (scale - 1) * r.size.w / 2, - .y = r.pos.y - (scale - 1) * r.size.h / 2, - }, - .size = { .x = r.size.w * scale, .y = r.size.h * scale }, - }; - DrawRectangleRounded(draw_rect.rr, 1, 0, ColorFromNormalized(colour.rv)); - DrawRectangleRoundedLinesEx(draw_rect.rr, 1, 0, 3.0, Fade(BLACK, 0.8)); + b32 fade_stack = fade_param != 1.0f; + if (fade_stack) { + Rect draw_rect = r; + draw_rect.pos.y += stack_offset_y; + r.pos.y += sa.size.h * 0.16; + Color old = Fade(ColorFromNormalized(last_colour.rv), fade_param); + DrawRectangleRounded(draw_rect.rr, 1, 0, old); + DrawRectangleRoundedLinesEx(draw_rect.rr, 1, 0, 3.0, Fade(BLACK, fade_param)); + } + for (u32 i = 0; i < 4; i++) { + i32 cidx = (ctx->colour_stack.widx + i) % ARRAY_COUNT(ctx->colour_stack.items); + do_colour_stack_item(ctx, mouse, r, cidx, 0, stack_offset_y, dt); r.pos.y += sa.size.h * 0.16; } + i32 last_idx = (ctx->colour_stack.widx + 4) % ARRAY_COUNT(ctx->colour_stack.items); + do_colour_stack_item(ctx, mouse, r, last_idx, fade_stack? fade_param : 0, + stack_offset_y, dt); + + fade_param = move_towards_f32(fade_param, fade_stack? 0 : 1, 8 * dt); + stack_offset_y = move_towards_f32(stack_offset_y, fade_stack? stack_off_target : 0, + stack_off_delta); + if (stack_offset_y == stack_off_target) { + fade_param = 1.0f; + stack_offset_y = 0; + } + r.pos.y = sa.pos.y + sa.size.h - r.size.h; r.pos.x += r.size.w * 0.1; r.size.w *= 0.8; @@ -424,10 +459,14 @@ do_colour_stack(ColourPickerCtx *ctx, Rect sa, f32 dt) DrawTriangle(t_top.rv, t_left.rv, t_right.rv, ctx->fg); if (push_collides && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - v4 colour = ctx->colour; + fade_param -= 1e-6; + v4 colour = ctx->colour; if (ctx->mode == CPM_HSV) colour = hsv_to_rgb(colour); + + last_colour = ctx->colour_stack.items[ctx->colour_stack.widx]; ctx->colour_stack.items[ctx->colour_stack.widx++] = colour; + if (ctx->colour_stack.widx == ARRAY_COUNT(ctx->colour_stack.items)) ctx->colour_stack.widx = 0; }