-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolourpicker.c
1263 lines (1069 loc) · 37 KB
/
colourpicker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* See LICENSE for copyright details */
#include <raylib.h>
#include <rlgl.h>
#include "util.c"
static s8 mode_labels[CM_LAST][4] = {
[CM_RGB] = { s8("R"), s8("G"), s8("B"), s8("A") },
[CM_HSV] = { s8("H"), s8("S"), s8("V"), s8("A") },
};
static void
mem_move(u8 *src, u8 *dest, size n)
{
if (dest < src) while (n) { *dest++ = *src++; n--; }
else while (n) { n--; dest[n] = src[n]; }
}
static f32
move_towards_f32(f32 current, f32 target, f32 delta)
{
if (target < current) {
current -= delta;
if (current < target)
current = target;
} else {
current += delta;
if (current > target)
current = target;
}
return current;
}
static Color
fade(Color a, f32 alpha)
{
a.a = (u8)((f32)a.a * alpha);
return a;
}
static f32
lerp(f32 a, f32 b, f32 t)
{
return a + t * (b - a);
}
static v4
lerp_v4(v4 a, v4 b, f32 t)
{
return (v4){
.x = a.x + t * (b.x - a.x),
.y = a.y + t * (b.y - a.y),
.z = a.z + t * (b.z - a.z),
.w = a.w + t * (b.w - a.w),
};
}
static v2
measure_text(Font font, s8 text)
{
v2 result = {.y = font.baseSize};
for (size i = 0; i < text.len; i++) {
/* NOTE: assumes font glyphs are ordered (they are in our embedded fonts) */
i32 idx = (i32)text.data[i] - 32;
result.x += font.glyphs[idx].advanceX;
if (font.glyphs[idx].advanceX == 0)
result.x += (font.recs[idx].width + font.glyphs[idx].offsetX);
}
return result;
}
static void
draw_text(Font font, s8 text, v2 pos, Color colour)
{
for (size i = 0; i < text.len; i++) {
/* NOTE: assumes font glyphs are ordered (they are in our embedded fonts) */
i32 idx = text.data[i] - 32;
Rectangle dst = {
pos.x + font.glyphs[idx].offsetX - font.glyphPadding,
pos.y + font.glyphs[idx].offsetY - font.glyphPadding,
font.recs[idx].width + 2.0f * font.glyphPadding,
font.recs[idx].height + 2.0f * font.glyphPadding
};
Rectangle src = {
font.recs[idx].x - font.glyphPadding,
font.recs[idx].y - font.glyphPadding,
font.recs[idx].width + 2.0f * font.glyphPadding,
font.recs[idx].height + 2.0f * font.glyphPadding
};
DrawTexturePro(font.texture, src, dst, (Vector2){0}, 0, colour);
pos.x += font.glyphs[idx].advanceX;
if (font.glyphs[idx].advanceX == 0)
pos.x += font.recs[idx].width;
}
}
static v2
left_align_text_in_rect(Rect r, s8 text, Font font)
{
v2 ts = measure_text(font, text);
v2 delta = { .h = r.size.h - ts.h };
return (v2) { .x = r.pos.x, .y = r.pos.y + 0.5 * delta.h, };
}
static Rect
scale_rect_centered(Rect r, v2 scale)
{
Rect or = r;
r.size.w *= scale.x;
r.size.h *= scale.y;
r.pos.x += (or.size.w - r.size.w) / 2;
r.pos.y += (or.size.h - r.size.h) / 2;
return r;
}
static v2
center_align_text_in_rect(Rect r, s8 text, Font font)
{
v2 ts = measure_text(font, text);
v2 delta = { .w = r.size.w - ts.w, .h = r.size.h - ts.h };
return (v2) {
.x = r.pos.x + 0.5 * delta.w,
.y = r.pos.y + 0.5 * delta.h,
};
}
static Rect
cut_rect_middle(Rect r, f32 left, f32 right)
{
ASSERT(left <= right);
r.pos.x += r.size.w * left;
r.size.w = r.size.w * (right - left);
return r;
}
static Rect
cut_rect_left(Rect r, f32 fraction)
{
r.size.w *= fraction;
return r;
}
static Rect
cut_rect_right(Rect r, f32 fraction)
{
r.pos.x += fraction * r.size.w;
r.size.w *= (1 - fraction);
return r;
}
static void
draw_cardinal_triangle(v2 midpoint, v2 size, v2 scale, enum cardinal_direction direction,
Color colour)
{
v2 t1, t2;
switch (direction) {
case NORTH:
t1.x = midpoint.x - scale.x * size.x;
t1.y = midpoint.y + scale.y * size.y;
t2.x = midpoint.x + scale.x * size.x;
t2.y = midpoint.y + scale.y * size.y;
break;
case EAST:
t1.x = midpoint.x - scale.x * size.y;
t1.y = midpoint.y - scale.y * size.x;
t2.x = midpoint.x - scale.x * size.y;
t2.y = midpoint.y + scale.y * size.x;
break;
case SOUTH:
t1.x = midpoint.x + scale.x * size.x;
t1.y = midpoint.y - scale.y * size.y;
t2.x = midpoint.x - scale.x * size.x;
t2.y = midpoint.y - scale.y * size.y;
break;
case WEST:
t1.x = midpoint.x + scale.x * size.y;
t1.y = midpoint.y + scale.y * size.x;
t2.x = midpoint.x + scale.x * size.y;
t2.y = midpoint.y - scale.y * size.x;
break;
default: ASSERT(0); return;
}
DrawTriangle(midpoint.rv, t1.rv, t2.rv, colour);
#if 0
DrawCircleV(midpoint.rv, 6, RED);
DrawCircleV(t1.rv, 6, BLUE);
DrawCircleV(t2.rv, 6, GREEN);
#endif
}
static v4
get_formatted_colour(ColourPickerCtx *ctx, enum colour_mode format)
{
switch (ctx->colour_mode) {
case CM_RGB:
switch (format) {
case CM_RGB: return ctx->colour;
case CM_HSV: return rgb_to_hsv(ctx->colour);
case CM_LAST: ASSERT(0); break;
}
break;
case CM_HSV:
switch (format) {
case CM_RGB: return hsv_to_rgb(ctx->colour);
case CM_HSV: return ctx->colour;
case CM_LAST: ASSERT(0); break;
}
break;
case CM_LAST: ASSERT(0); break;
}
return (v4){0};
}
static void
store_formatted_colour(ColourPickerCtx *ctx, v4 colour, enum colour_mode format)
{
switch (ctx->colour_mode) {
case CM_RGB:
switch (format) {
case CM_RGB: ctx->colour = colour; break;
case CM_HSV: ctx->colour = hsv_to_rgb(colour); break;
case CM_LAST: ASSERT(0); break;
}
break;
case CM_HSV:
switch (format) {
case CM_RGB: ctx->colour = rgb_to_hsv(colour); break;
case CM_HSV: ctx->colour = colour; break;
case CM_LAST: ASSERT(0); break;
}
ctx->flags |= CPF_REFILL_TEXTURE;
break;
case CM_LAST: ASSERT(0); break;
}
}
static void
step_colour_mode(ColourPickerCtx *ctx, i32 inc)
{
ASSERT(inc == 1 || inc == -1);
enum colour_mode last_mode = ctx->colour_mode;
ctx->colour_mode += inc;
CLAMP(ctx->colour_mode, CM_RGB, CM_LAST);
if (ctx->colour_mode == CM_LAST) {
if (inc == 1) ctx->colour_mode = 0;
else ctx->colour_mode = CM_LAST + inc;
}
store_formatted_colour(ctx, ctx->colour, last_mode);
}
static void
get_slider_subrects(Rect r, Rect *label, Rect *slider, Rect *value)
{
if (label) *label = cut_rect_left(r, 0.08);
if (value) *value = cut_rect_right(r, 0.83);
if (slider) {
*slider = cut_rect_middle(r, 0.1, 0.79);
slider->size.h *= 0.7;
slider->pos.y += r.size.h * 0.15;
}
}
static void
parse_and_store_text_input(ColourPickerCtx *ctx)
{
s8 input = {.len = ctx->is.buf_len, .data = ctx->is.buf};
v4 new_colour = {0};
enum colour_mode new_mode = CM_LAST;
if (ctx->is.idx == -1) {
return;
} else if (ctx->is.idx == INPUT_HEX) {
new_colour = normalize_colour(parse_hex_u32(input));
new_mode = CM_RGB;
} else {
new_mode = ctx->colour_mode;
new_colour = ctx->colour;
f32 fv = parse_f64(input);
CLAMP01(fv);
switch(ctx->is.idx) {
case INPUT_R: new_colour.r = fv; break;
case INPUT_G: new_colour.g = fv; break;
case INPUT_B: new_colour.b = fv; break;
case INPUT_A: new_colour.a = fv; break;
default: break;
}
}
if (new_mode != CM_LAST)
store_formatted_colour(ctx, new_colour, new_mode);
}
static void
set_text_input_idx(ColourPickerCtx *ctx, enum input_indices idx, Rect r, v2 mouse)
{
if (ctx->is.idx != (i32)idx)
parse_and_store_text_input(ctx);
Stream in = {.data = ctx->is.buf, .cap = ARRAY_COUNT(ctx->is.buf)};
if (idx == INPUT_HEX) {
stream_append_colour(&in, colour_from_normalized(get_formatted_colour(ctx, CM_RGB)));
} else {
f32 fv = 0;
switch (idx) {
case INPUT_R: fv = ctx->colour.r; break;
case INPUT_G: fv = ctx->colour.g; break;
case INPUT_B: fv = ctx->colour.b; break;
case INPUT_A: fv = ctx->colour.a; break;
default: break;
}
stream_append_f64(&in, fv, 100);
}
ctx->is.buf_len = in.widx;
ctx->is.idx = idx;
ctx->is.cursor = -1;
CLAMP(ctx->is.idx, -1, INPUT_A);
if (ctx->is.idx == -1)
return;
ASSERT(CheckCollisionPointRec(mouse.rv, r.rr));
ctx->is.cursor_hover_p = (mouse.x - r.pos.x) / r.size.w;
CLAMP01(ctx->is.cursor_hover_p);
}
static void
do_text_input(ColourPickerCtx *ctx, Rect r, Color colour, i32 max_disp_chars)
{
v2 ts = measure_text(ctx->font, (s8){.len = ctx->is.buf_len, .data = ctx->is.buf});
v2 pos = {.x = r.pos.x, .y = r.pos.y + (r.size.y - ts.y) / 2};
i32 buf_delta = ctx->is.buf_len - max_disp_chars;
if (buf_delta < 0) buf_delta = 0;
s8 buf = {.len = ctx->is.buf_len - buf_delta, .data = ctx->is.buf + buf_delta};
{
/* NOTE: drop a char if the subtext still doesn't fit */
v2 nts = measure_text(ctx->font, buf);
if (nts.w > 0.96 * r.size.w) {
buf.data++;
buf.len--;
}
}
draw_text(ctx->font, buf, pos, colour);
ctx->is.cursor_t = move_towards_f32(ctx->is.cursor_t, ctx->is.cursor_t_target,
1.5 * ctx->dt);
if (ctx->is.cursor_t == ctx->is.cursor_t_target) {
if (ctx->is.cursor_t_target == 0) ctx->is.cursor_t_target = 1;
else ctx->is.cursor_t_target = 0;
}
v4 bg = ctx->cursor_colour;
bg.a = 0;
Color cursor_colour = colour_from_normalized(lerp_v4(bg, ctx->cursor_colour,
ctx->is.cursor_t));
/* NOTE: guess a cursor position */
if (ctx->is.cursor == -1) {
/* NOTE: extra offset to help with putting a cursor at idx 0 */
#define TEXT_HALF_CHAR_WIDTH 10
f32 x_off = TEXT_HALF_CHAR_WIDTH, x_bounds = r.size.w * ctx->is.cursor_hover_p;
i32 i;
for (i = 0; i < ctx->is.buf_len && x_off < x_bounds; i++) {
/* NOTE: assumes font glyphs are ordered */
i32 idx = ctx->is.buf[i] - 32;
x_off += ctx->font.glyphs[idx].advanceX;
if (ctx->font.glyphs[idx].advanceX == 0)
x_off += ctx->font.recs[idx].width;
}
ctx->is.cursor = i;
}
buf.len = ctx->is.cursor - buf_delta;
v2 sts = measure_text(ctx->font, buf);
f32 cursor_x = r.pos.x + sts.x;
f32 cursor_width;
if (ctx->is.cursor == ctx->is.buf_len) cursor_width = MIN(ctx->window_size.w * 0.03, 20);
else cursor_width = MIN(ctx->window_size.w * 0.01, 6);
Rect cursor_r = {
.pos = {.x = cursor_x, .y = pos.y},
.size = {.w = cursor_width, .h = ts.h},
};
DrawRectangleRec(cursor_r.rr, cursor_colour);
/* NOTE: handle multiple input keys on a single frame */
i32 key = GetCharPressed();
while (key > 0) {
if (ctx->is.buf_len == ARRAY_COUNT(ctx->is.buf))
break;
mem_move(ctx->is.buf + ctx->is.cursor,
ctx->is.buf + ctx->is.cursor + 1,
ctx->is.buf_len - ctx->is.cursor + 1);
ctx->is.buf[ctx->is.cursor++] = key;
ctx->is.buf_len++;
key = GetCharPressed();
}
if ((IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) && ctx->is.cursor > 0)
ctx->is.cursor--;
if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) &&
ctx->is.cursor < ctx->is.buf_len)
ctx->is.cursor++;
if ((IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) &&
ctx->is.cursor > 0) {
ctx->is.cursor--;
mem_move(ctx->is.buf + ctx->is.cursor + 1,
ctx->is.buf + ctx->is.cursor,
ctx->is.buf_len - ctx->is.cursor);
ctx->is.buf_len--;
}
if ((IsKeyPressed(KEY_DELETE) || IsKeyPressedRepeat(KEY_DELETE)) &&
ctx->is.cursor < ctx->is.buf_len) {
mem_move(ctx->is.buf + ctx->is.cursor + 1,
ctx->is.buf + ctx->is.cursor,
ctx->is.buf_len - ctx->is.cursor);
ctx->is.buf_len--;
}
if (IsKeyPressed(KEY_ENTER)) {
parse_and_store_text_input(ctx);
ctx->is.idx = -1;
}
}
static i32
do_button(ButtonState *btn, v2 mouse, Rect r, f32 dt, f32 hover_speed)
{
b32 hovered = CheckCollisionPointRec(mouse.rv, r.rr);
i32 pressed_mask = 0;
pressed_mask |= MOUSE_LEFT * (hovered && IsMouseButtonPressed(MOUSE_BUTTON_LEFT));
pressed_mask |= MOUSE_RIGHT * (hovered && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT));
if (hovered) btn->hover_t += hover_speed * dt;
else btn->hover_t -= hover_speed * dt;
CLAMP01(btn->hover_t);
return pressed_mask;
}
static i32
do_rect_button(ButtonState *btn, v2 mouse, Rect r, Color bg, f32 dt, f32 hover_speed, f32 scale_target, f32 fade_t)
{
i32 pressed_mask = do_button(btn, mouse, r, dt, hover_speed);
f32 param = lerp(1, scale_target, btn->hover_t);
v2 bscale = (v2){
.x = param + RECT_BTN_BORDER_WIDTH / r.size.w,
.y = param + RECT_BTN_BORDER_WIDTH / r.size.h,
};
Rect sr = scale_rect_centered(r, (v2){.x = param, .y = param});
Rect sb = scale_rect_centered(r, bscale);
DrawRectangleRounded(sb.rr, SELECTOR_ROUNDNESS, 0, fade(SELECTOR_BORDER_COLOUR, fade_t));
DrawRectangleRounded(sr.rr, SELECTOR_ROUNDNESS, 0, fade(bg, fade_t));
return pressed_mask;
}
static i32
do_text_button(ColourPickerCtx *ctx, ButtonState *btn, v2 mouse, Rect r, s8 text, v4 fg, Color bg)
{
i32 pressed_mask = do_rect_button(btn, mouse, r, bg, ctx->dt, TEXT_HOVER_SPEED, 1, 1);
v2 tpos = center_align_text_in_rect(r, text, ctx->font);
v2 spos = {.x = tpos.x + 1.75, .y = tpos.y + 2};
v4 colour = lerp_v4(fg, ctx->hover_colour, btn->hover_t);
draw_text(ctx->font, text, spos, fade(BLACK, 0.8));
draw_text(ctx->font, text, tpos, colour_from_normalized(colour));
return pressed_mask;
}
static void
do_slider(ColourPickerCtx *ctx, Rect r, i32 label_idx, v2 relative_mouse)
{
Rect lr, sr, vr;
get_slider_subrects(r, &lr, &sr, &vr);
b32 hovering = CheckCollisionPointRec(relative_mouse.rv, sr.rr);
if (hovering && ctx->held_idx == -1)
ctx->held_idx = label_idx;
if (ctx->held_idx != -1) {
f32 current = ctx->colour.E[ctx->held_idx];
f32 wheel = GetMouseWheelMove();
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
current = (relative_mouse.x - sr.pos.x) / sr.size.w;
current += wheel / 255;
CLAMP01(current);
ctx->colour.E[ctx->held_idx] = current;
ctx->flags |= CPF_REFILL_TEXTURE;
}
if (IsMouseButtonUp(MOUSE_BUTTON_LEFT))
ctx->held_idx = -1;
f32 current = ctx->colour.E[label_idx];
{
f32 scale_delta = (SLIDER_SCALE_TARGET - 1.0) * SLIDER_SCALE_SPEED * ctx->dt;
b32 should_scale = (ctx->held_idx == -1 && hovering) ||
(ctx->held_idx != -1 && label_idx == ctx->held_idx);
f32 scale = ctx->ss.scale_t[label_idx];
scale = move_towards_f32(scale, should_scale? SLIDER_SCALE_TARGET: 1.0,
scale_delta);
ctx->ss.scale_t[label_idx] = scale;
v2 tri_scale = {.x = scale, .y = scale};
v2 tri_mid = {.x = sr.pos.x + current * sr.size.w, .y = sr.pos.y};
draw_cardinal_triangle(tri_mid, SLIDER_TRI_SIZE, tri_scale, SOUTH, ctx->fg);
tri_mid.y += sr.size.h;
draw_cardinal_triangle(tri_mid, SLIDER_TRI_SIZE, tri_scale, NORTH, ctx->fg);
}
{
SliderState *s = &ctx->ss;
b32 collides = CheckCollisionPointRec(relative_mouse.rv, vr.rr);
if (collides && ctx->is.idx != (label_idx + 1)) {
s->colour_t[label_idx] += TEXT_HOVER_SPEED * ctx->dt;
} else {
s->colour_t[label_idx] -= TEXT_HOVER_SPEED * ctx->dt;
}
CLAMP01(s->colour_t[label_idx]);
if (!collides && ctx->is.idx == (label_idx + 1) &&
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
set_text_input_idx(ctx, -1, vr, relative_mouse);
current = ctx->colour.E[label_idx];
}
v4 colour = lerp_v4(normalize_colour(pack_rl_colour(ctx->fg)),
ctx->hover_colour, s->colour_t[label_idx]);
Color colour_rl = colour_from_normalized(colour);
if (collides && IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
set_text_input_idx(ctx, label_idx + 1, vr, relative_mouse);
if (ctx->is.idx != (label_idx + 1)) {
u8 vbuf[4];
Stream vstream = {.data = vbuf, .cap = ARRAY_COUNT(vbuf)};
stream_append_f64(&vstream, current, 100);
s8 value = {.len = vstream.widx, .data = vbuf};
draw_text(ctx->font, value, left_align_text_in_rect(vr, value, ctx->font), colour_rl);
} else {
do_text_input(ctx, vr, colour_rl, 4);
}
}
s8 label = mode_labels[ctx->colour_mode][label_idx];
draw_text(ctx->font, label, center_align_text_in_rect(lr, label, ctx->font), ctx->fg);
}
static void
do_status_bar(ColourPickerCtx *ctx, Rect r, v2 relative_mouse)
{
Rect hex_r = cut_rect_left(r, 0.5);
Rect mode_r;
get_slider_subrects(r, 0, 0, &mode_r);
s8 mode_txt = s8("");
switch (ctx->colour_mode) {
case CM_RGB: mode_txt = s8("RGB"); break;
case CM_HSV: mode_txt = s8("HSV"); break;
case CM_LAST: ASSERT(0); break;
}
v2 mode_ts = measure_text(ctx->font, mode_txt);
mode_r.pos.y += (mode_r.size.h - mode_ts.h) / 2;
mode_r.size.w = mode_ts.w;
i32 mouse_mask = do_button(&ctx->sbs.mode, relative_mouse, mode_r, ctx->dt, TEXT_HOVER_SPEED);
if (mouse_mask & MOUSE_LEFT) step_colour_mode(ctx, 1);
if (mouse_mask & MOUSE_RIGHT) step_colour_mode(ctx, -1);
u8 hbuf[8];
Stream hstream = {.data = hbuf, .cap = ARRAY_COUNT(hbuf)};
stream_append_colour(&hstream, colour_from_normalized(get_formatted_colour(ctx, CM_RGB)));
s8 hex = {.len = hstream.widx, .data = hbuf};
s8 label = s8("RGB: ");
v2 label_size = measure_text(ctx->font, label);
v2 hex_size = measure_text(ctx->font, hex);
f32 extra_input_scale = 1.07;
hex_r.size.w = extra_input_scale * (label_size.w + hex_size.w);
Rect label_r = cut_rect_left(hex_r, label_size.x / hex_r.size.w);
hex_r = cut_rect_right(hex_r, label_size.x / hex_r.size.w);
i32 hex_collides = CheckCollisionPointRec(relative_mouse.rv, hex_r.rr);
if (!hex_collides && ctx->is.idx == INPUT_HEX &&
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
set_text_input_idx(ctx, -1, hex_r, relative_mouse);
hstream.widx = 0;
stream_append_colour(&hstream, colour_from_normalized(get_formatted_colour(ctx, CM_RGB)));
hex.len = hstream.widx;
}
if (hex_collides && IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
set_text_input_idx(ctx, INPUT_HEX, hex_r, relative_mouse);
if (hex_collides && ctx->is.idx != INPUT_HEX)
ctx->sbs.hex_hover_t += TEXT_HOVER_SPEED * ctx->dt;
else
ctx->sbs.hex_hover_t -= TEXT_HOVER_SPEED * ctx->dt;
CLAMP01(ctx->sbs.hex_hover_t);
v4 fg = normalize_colour(pack_rl_colour(ctx->fg));
v4 hex_colour = lerp_v4(fg, ctx->hover_colour, ctx->sbs.hex_hover_t);
v4 mode_colour = lerp_v4(fg, ctx->hover_colour, ctx->sbs.mode.hover_t);
draw_text(ctx->font, label, left_align_text_in_rect(label_r, label, ctx->font), ctx->fg);
Color hex_colour_rl = colour_from_normalized(hex_colour);
if (ctx->is.idx != INPUT_HEX) {
draw_text(ctx->font, hex, left_align_text_in_rect(hex_r, hex, ctx->font), hex_colour_rl);
} else {
do_text_input(ctx, hex_r, hex_colour_rl, 8);
}
draw_text(ctx->font, mode_txt, mode_r.pos, colour_from_normalized(mode_colour));
}
static void
do_colour_stack(ColourPickerCtx *ctx, Rect sa)
{
ColourStackState *css = &ctx->colour_stack;
/* NOTE: Small adjusment to align with mode text. TODO: Cleanup? */
sa = scale_rect_centered(sa, (v2){.x = 1, .y = 0.98});
sa.pos.y += 0.02 * sa.size.h;
Rect r = sa;
r.size.h *= 1.0 / (ARRAY_COUNT(css->items) + 3);
r.size.w *= 0.75;
r.pos.x += (sa.size.w - r.size.w) * 0.5;
f32 y_pos_delta = r.size.h * 1.2;
r.pos.y -= y_pos_delta * css->y_off_t;
/* NOTE: Stack is moving up; draw last top item as it moves up and fades out */
if (css->fade_param) {
ButtonState btn;
do_rect_button(&btn, ctx->mouse_pos, r, colour_from_normalized(css->last), ctx->dt,
0, 1, css->fade_param);
r.pos.y += y_pos_delta;
}
f32 stack_scale_target = (f32)(ARRAY_COUNT(css->items) + 1) / ARRAY_COUNT(css->items);
f32 fade_scale[ARRAY_COUNT(css->items)] = { [ARRAY_COUNT(css->items) - 1] = 1 };
for (u32 i = 0; i < ARRAY_COUNT(css->items); i++) {
i32 cidx = (css->widx + i) % ARRAY_COUNT(css->items);
Color bg = colour_from_normalized(css->items[cidx]);
b32 pressed = do_rect_button(css->buttons + cidx, ctx->mouse_pos, r, bg, ctx->dt,
BUTTON_HOVER_SPEED, stack_scale_target,
1 - css->fade_param * fade_scale[i]);
if (pressed) {
v4 hsv = rgb_to_hsv(css->items[cidx]);
store_formatted_colour(ctx, hsv, CM_HSV);
if (ctx->mode == CPM_PICKER) {
ctx->pms.base_hue = hsv.x;
ctx->pms.fractional_hue = 0;
}
}
r.pos.y += y_pos_delta;
}
css->fade_param -= BUTTON_HOVER_SPEED * ctx->dt;
css->y_off_t += BUTTON_HOVER_SPEED * ctx->dt;
if (css->fade_param < 0) {
css->fade_param = 0;
css->y_off_t = 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;
b32 push_pressed = do_button(&css->tri_btn, ctx->mouse_pos, r, ctx->dt, BUTTON_HOVER_SPEED);
f32 param = css->tri_btn.hover_t;
v2 tri_size = {.x = 0.25 * r.size.w, .y = 0.5 * r.size.h};
v2 tri_scale = {.x = 1 - 0.5 * param, .y = 1 + 0.3 * param};
v2 tri_mid = {.x = r.pos.x + 0.5 * r.size.w, .y = r.pos.y - 0.3 * r.size.h * param};
draw_cardinal_triangle(tri_mid, tri_size, tri_scale, NORTH, ctx->fg);
if (push_pressed) {
css->fade_param = 1.0;
css->last = css->items[css->widx];
css->items[css->widx++] = get_formatted_colour(ctx, CM_RGB);
if (css->widx == ARRAY_COUNT(css->items))
css->widx = 0;
}
}
static void
do_colour_selector(ColourPickerCtx *ctx, Rect r)
{
Color colour = colour_from_normalized(get_formatted_colour(ctx, CM_RGB));
Color pcolour = colour_from_normalized(ctx->previous_colour);
Rect cs[2] = {cut_rect_left(r, 0.5), cut_rect_right(r, 0.5)};
DrawRectangleRec(cs[0].rr, pcolour);
DrawRectangleRec(cs[1].rr, colour);
v4 fg = normalize_colour(pack_rl_colour(ctx->fg));
s8 labels[2] = {s8("Revert"), s8("Apply")};
i32 pressed_idx = -1;
for (u32 i = 0; i < ARRAY_COUNT(cs); i++) {
if (CheckCollisionPointRec(ctx->mouse_pos.rv, cs[i].rr) && ctx->held_idx == -1) {
ctx->selection_hover_t[i] += TEXT_HOVER_SPEED * ctx->dt;
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
pressed_idx = i;
} else {
ctx->selection_hover_t[i] -= TEXT_HOVER_SPEED * ctx->dt;
}
CLAMP01(ctx->selection_hover_t[i]);
v4 colour = lerp_v4(fg, ctx->hover_colour, ctx->selection_hover_t[i]);
v2 fpos = center_align_text_in_rect(cs[i], labels[i], ctx->font);
v2 pos = fpos;
pos.x += 1.75;
pos.y += 2;
draw_text(ctx->font, labels[i], pos, fade(BLACK, 0.8));
draw_text(ctx->font, labels[i], fpos, colour_from_normalized(colour));
}
DrawRectangleRoundedLinesEx(r.rr, SELECTOR_ROUNDNESS, 0, 4 * SELECTOR_BORDER_WIDTH, ctx->bg);
DrawRectangleRoundedLinesEx(r.rr, SELECTOR_ROUNDNESS, 0, SELECTOR_BORDER_WIDTH,
SELECTOR_BORDER_COLOUR);
v2 start = cs[1].pos;
v2 end = cs[1].pos;
end.y += cs[1].size.h;
DrawLineEx(start.rv, end.rv, SELECTOR_BORDER_WIDTH, SELECTOR_BORDER_COLOUR);
if (pressed_idx == 0) store_formatted_colour(ctx, ctx->previous_colour, CM_RGB);
else if (pressed_idx == 1) ctx->previous_colour = get_formatted_colour(ctx, CM_RGB);
if (pressed_idx != -1) {
ctx->pms.base_hue = get_formatted_colour(ctx, CM_HSV).x;
ctx->pms.fractional_hue = 0;
}
}
static void
do_slider_shader(ColourPickerCtx *ctx, Rect r, i32 colour_mode, f32 *regions, f32 *colours)
{
f32 border_thick = SLIDER_BORDER_WIDTH;
f32 radius = SLIDER_ROUNDNESS / 2;
/* NOTE: scale radius by rect width or height to adapt to window scaling */
radius *= (r.size.w > r.size.h)? r.size.h : r.size.w;
BeginShaderMode(ctx->picker_shader);
rlEnableShader(ctx->picker_shader.id);
rlSetUniform(ctx->mode_id, &ctx->mode, RL_SHADER_UNIFORM_INT, 1);
rlSetUniform(ctx->radius_id, &radius, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(ctx->border_thick_id, &border_thick, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(ctx->colour_mode_id, &colour_mode, RL_SHADER_UNIFORM_INT, 1);
rlSetUniform(ctx->colours_id, colours, RL_SHADER_UNIFORM_VEC4, 3);
rlSetUniform(ctx->regions_id, regions, RL_SHADER_UNIFORM_VEC4, 4);
DrawRectanglePro(r.rr, (Vector2){0}, 0, BLACK);
EndShaderMode();
}
static void
do_slider_mode(ColourPickerCtx *ctx, v2 relative_mouse)
{
BEGIN_CYCLE_COUNT(CC_DO_SLIDER);
Rect tr = {
.size = {
.w = ctx->picker_texture.texture.width,
.h = ctx->picker_texture.texture.height
}
};
Rect sb = tr;
Rect ss = tr;
sb.size.h *= 0.1;
ss.size.h *= 0.15;
ss.pos.y += 1.2 * sb.size.h;
BeginTextureMode(ctx->slider_texture);
ClearBackground(ctx->bg);
do_status_bar(ctx, sb, relative_mouse);
Rect sr;
get_slider_subrects(ss, 0, &sr, 0);
f32 r_bound = sr.pos.x + sr.size.w;
f32 y_step = 1.525 * ss.size.h;
for (i32 i = 0; i < 4; i++) {
do_slider(ctx, ss, i, relative_mouse);
ss.pos.y += y_step;
}
f32 start_y = sr.pos.y - 0.5 * ss.size.h;
f32 end_y = start_y + sr.size.h;
f32 regions[16] = {
sr.pos.x, start_y + 3 * y_step, r_bound, end_y + 3 * y_step,
sr.pos.x, start_y + 2 * y_step, r_bound, end_y + 2 * y_step,
sr.pos.x, start_y + 1 * y_step, r_bound, end_y + 1 * y_step,
sr.pos.x, start_y + 0 * y_step, r_bound, end_y + 0 * y_step
};
v4 colours[3] = {ctx->colour};
do_slider_shader(ctx, tr, ctx->colour_mode, regions, (f32 *)colours);
EndTextureMode();
END_CYCLE_COUNT(CC_DO_SLIDER);
}
#define PM_LEFT 0
#define PM_MIDDLE 1
#define PM_RIGHT 2
static v4
do_vertical_slider(ColourPickerCtx *ctx, v2 test_pos, Rect r, i32 idx,
v4 bot_colour, v4 top_colour, v4 colour)
{
b32 hovering = CheckCollisionPointRec(test_pos.rv, r.rr);
if (hovering && ctx->held_idx == -1) {
colour.x -= GetMouseWheelMove() * (bot_colour.x - top_colour.x) / 36;
CLAMP(colour.x, top_colour.x, bot_colour.x);
}
if (hovering && IsMouseButtonDown(MOUSE_BUTTON_LEFT) && ctx->held_idx == -1)
ctx->held_idx = idx;
if (ctx->held_idx == idx) {
CLAMP(test_pos.y, r.pos.y, r.pos.y + r.size.h);
f32 new_t = (test_pos.y - r.pos.y) / r.size.h;
colour.x = top_colour.x + new_t * (bot_colour.x - top_colour.x);
}
f32 param = (colour.x - top_colour.x) / (bot_colour.x - top_colour.x);
{
b32 should_scale = (ctx->held_idx == -1 && hovering) ||
(ctx->held_idx != -1 && ctx->held_idx == idx);
if (should_scale) ctx->pms.scale_t[idx] += SLIDER_SCALE_SPEED * ctx->dt;
else ctx->pms.scale_t[idx] -= SLIDER_SCALE_SPEED * ctx->dt;
CLAMP01(ctx->pms.scale_t[idx]);
f32 scale = lerp(1, SLIDER_SCALE_TARGET, ctx->pms.scale_t[idx]);
v2 tri_scale = {.x = scale, .y = scale};
v2 tri_mid = {.x = r.pos.x, .y = r.pos.y + (param * r.size.h)};
draw_cardinal_triangle(tri_mid, SLIDER_TRI_SIZE, tri_scale, EAST, ctx->fg);
tri_mid.x += r.size.w;
draw_cardinal_triangle(tri_mid, SLIDER_TRI_SIZE, tri_scale, WEST, ctx->fg);
}
return colour;
}
static void
do_picker_mode(ColourPickerCtx *ctx, v2 relative_mouse)
{
BEGIN_CYCLE_COUNT(CC_DO_PICKER);
v4 colour = get_formatted_colour(ctx, CM_HSV);
colour.x = ctx->pms.base_hue + ctx->pms.fractional_hue;
Rect tr = {
.size = {
.w = ctx->picker_texture.texture.width,
.h = ctx->picker_texture.texture.height
}
};
Rect hs1 = scale_rect_centered(cut_rect_left(tr, 0.2), (v2){.x = 0.5, .y = 0.95});
Rect hs2 = scale_rect_centered(cut_rect_middle(tr, 0.2, 0.4), (v2){.x = 0.5, .y = 0.95});
Rect sv = scale_rect_centered(cut_rect_right(tr, 0.4), (v2){.x = 1.0, .y = 0.95});
BeginTextureMode(ctx->picker_texture);
ClearBackground(ctx->bg);
v4 hsv[3] = {colour, colour, colour};
hsv[1].x = 0;
hsv[2].x = 1;
f32 last_hue = colour.x;
colour = do_vertical_slider(ctx, relative_mouse, hs1, PM_LEFT, hsv[2], hsv[1], colour);
if (colour.x != last_hue)
ctx->pms.base_hue = colour.x - ctx->pms.fractional_hue;
f32 fraction = 0.1;
if (ctx->pms.base_hue - 0.5 * fraction < 0) {
hsv[1].x = 0;
hsv[2].x = fraction;
} else if (ctx->pms.base_hue + 0.5 * fraction > 1) {
hsv[1].x = 1 - fraction;
hsv[2].x = 1;
} else {
hsv[1].x = ctx->pms.base_hue - 0.5 * fraction;
hsv[2].x = ctx->pms.base_hue + 0.5 * fraction;
}
colour = do_vertical_slider(ctx, relative_mouse, hs2, PM_MIDDLE, hsv[2], hsv[1], colour);
ctx->pms.fractional_hue = colour.x - ctx->pms.base_hue;
{
f32 regions[16] = {
hs1.pos.x, hs1.pos.y, hs1.pos.x + hs1.size.w, hs1.pos.y + hs1.size.h,
hs2.pos.x, hs2.pos.y, hs2.pos.x + hs2.size.w, hs2.pos.y + hs2.size.h,
sv.pos.x, sv.pos.y, sv.pos.x + sv.size.w, sv.pos.y + sv.size.h
};
do_slider_shader(ctx, tr, CM_HSV, regions, (f32 *)hsv);
}
b32 hovering = CheckCollisionPointRec(relative_mouse.rv, sv.rr);
if (hovering && IsMouseButtonDown(MOUSE_BUTTON_LEFT) && ctx->held_idx == -1)
ctx->held_idx = PM_RIGHT;
if (ctx->held_idx == PM_RIGHT) {
CLAMP(relative_mouse.x, sv.pos.x, sv.pos.x + sv.size.w);
CLAMP(relative_mouse.y, sv.pos.y, sv.pos.y + sv.size.h);
colour.y = (relative_mouse.x - sv.pos.x) / sv.size.w;
colour.z = (sv.pos.y + sv.size.h - relative_mouse.y) / sv.size.h;
}
f32 radius = 4;
v2 param = {.x = colour.y, .y = 1 - colour.z};
{
b32 should_scale = (ctx->held_idx == -1 && hovering) ||
(ctx->held_idx != -1 && ctx->held_idx == PM_RIGHT);
if (should_scale) ctx->pms.scale_t[PM_RIGHT] += SLIDER_SCALE_SPEED * ctx->dt;
else ctx->pms.scale_t[PM_RIGHT] -= SLIDER_SCALE_SPEED * ctx->dt;
CLAMP01(ctx->pms.scale_t[PM_RIGHT]);
f32 slider_scale = lerp(1, SLIDER_SCALE_TARGET, ctx->pms.scale_t[PM_RIGHT]);
f32 line_len = 8;
/* NOTE: North-East */
v2 start = {
.x = sv.pos.x + (param.x * sv.size.w) + 0.5 * radius,
.y = sv.pos.y + (param.y * sv.size.h) + 0.5 * radius,
};
v2 end = start;
end.x += line_len * slider_scale;
end.y += line_len * slider_scale;
DrawLineEx(start.rv, end.rv, 4, ctx->fg);
/* NOTE: North-West */
start.x -= radius;
end = start;
end.x -= line_len * slider_scale;
end.y += line_len * slider_scale;
DrawLineEx(start.rv, end.rv, 4, ctx->fg);
/* NOTE: South-West */
start.y -= radius;
end = start;
end.x -= line_len * slider_scale;
end.y -= line_len * slider_scale;
DrawLineEx(start.rv, end.rv, 4, ctx->fg);
/* NOTE: South-East */
start.x += radius;
end = start;
end.x += line_len * slider_scale;
end.y -= line_len * slider_scale;
DrawLineEx(start.rv, end.rv, 4, ctx->fg);
}
EndTextureMode();
if (IsMouseButtonUp(MOUSE_BUTTON_LEFT))
ctx->held_idx = -1;
store_formatted_colour(ctx, colour, CM_HSV);
END_CYCLE_COUNT(CC_DO_PICKER);
}
#ifdef _DEBUG
#include <stdio.h>
#endif
static void
debug_dump_info(ColourPickerCtx *ctx)