This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_ui_widgets.c
2374 lines (2052 loc) · 117 KB
/
pl_ui_widgets.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
#include "pl_ui_internal.h"
#include <float.h> // FLT_MAX
//-----------------------------------------------------------------------------
// [SECTION] stb_text mess
//-----------------------------------------------------------------------------
static plVec2
pl__input_text_calc_text_size_w(const plUiWChar* text_begin, const plUiWChar* text_end, const plUiWChar** remaining, plVec2* out_offset, bool stop_on_new_line)
{
plFont* font = gptCtx->ptFont;
const float line_height = gptCtx->tStyle.fFontSize;
const float scale = line_height / font->tConfig.fFontSize;
plVec2 text_size = {0};
float line_width = 0.0f;
const plUiWChar* s = text_begin;
while (s < text_end)
{
unsigned int c = (unsigned int)(*s++);
if (c == '\n')
{
text_size.x = plu_max(text_size.x, line_width);
text_size.y += line_height;
line_width = 0.0f;
if (stop_on_new_line)
break;
continue;
}
if (c == '\r')
continue;
const float char_width = font->sbtGlyphs[font->sbuCodePoints[(plUiWChar)c]].xAdvance * scale;
line_width += char_width;
}
if (text_size.x < line_width)
text_size.x = line_width;
if (out_offset)
*out_offset = (plVec2){line_width, text_size.y + line_height}; // offset allow for the possibility of sitting after a trailing \n
if (line_width > 0 || text_size.y == 0.0f) // whereas size.y will ignore the trailing \n
text_size.y += line_height;
if (remaining)
*remaining = s;
return text_size;
}
static int STB_TEXTEDIT_STRINGLEN(const plUiInputTextState* obj) { return obj->iCurrentLengthW; }
static plUiWChar STB_TEXTEDIT_GETCHAR(const plUiInputTextState* obj, int idx) { return obj->sbTextW[idx]; }
static float STB_TEXTEDIT_GETWIDTH(plUiInputTextState* obj, int line_start_idx, int char_idx) { plUiWChar c = obj->sbTextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; return gptCtx->ptFont->sbtGlyphs[gptCtx->ptFont->sbuCodePoints[c]].xAdvance * (gptCtx->tStyle.fFontSize / gptCtx->ptFont->tConfig.fFontSize); }
static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; }
static plUiWChar STB_TEXTEDIT_NEWLINE = '\n';
static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, plUiInputTextState* obj, int line_start_idx)
{
const plUiWChar* text = obj->sbTextW;
const plUiWChar* text_remaining = NULL;
const plVec2 size = pl__input_text_calc_text_size_w(text + line_start_idx, text + obj->iCurrentLengthW, &text_remaining, NULL, true);
r->x0 = 0.0f;
r->x1 = size.x;
r->baseline_y_delta = size.y;
r->ymin = 0.0f;
r->ymax = size.y;
r->num_chars = (int)(text_remaining - (text + line_start_idx));
}
static bool pl__is_separator(unsigned int c)
{
return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r' || c=='.' || c=='!';
}
static inline bool pl__char_is_blank_w(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; }
static int pl__is_word_boundary_from_right(plUiInputTextState* obj, int idx)
{
// When PL_UI_INPUT_TEXT_FLAGS_PASSWORD is set, we don't want actions such as CTRL+Arrow to leak the fact that underlying data are blanks or separators.
// if ((obj->tFlags & PL_UI_INPUT_TEXT_FLAGS_PASSWORD) || idx <= 0)
// return 0;
bool prev_white = pl__char_is_blank_w(obj->sbTextW[idx - 1]);
bool prev_separ = pl__is_separator(obj->sbTextW[idx - 1]);
bool curr_white = pl__char_is_blank_w(obj->sbTextW[idx]);
bool curr_separ = pl__is_separator(obj->sbTextW[idx]);
return ((prev_white || prev_separ) && !(curr_separ || curr_white)) || (curr_separ && !prev_separ);
}
static int pl__is_word_boundary_from_left(plUiInputTextState* obj, int idx)
{
// if ((obj->Flags & ImGuiInputTextFlags_Password) || idx <= 0)
// return 0;
bool prev_white = pl__char_is_blank_w(obj->sbTextW[idx]);
bool prev_separ = pl__is_separator(obj->sbTextW[idx]);
bool curr_white = pl__char_is_blank_w(obj->sbTextW[idx - 1]);
bool curr_separ = pl__is_separator(obj->sbTextW[idx - 1]);
return ((prev_white) && !(curr_separ || curr_white)) || (curr_separ && !prev_separ);
}
static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(plUiInputTextState* obj, int idx) { idx--; while (idx >= 0 && !pl__is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; }
static int STB_TEXTEDIT_MOVEWORDRIGHT_MAC(plUiInputTextState* obj, int idx) { idx++; int len = obj->iCurrentLengthW; while (idx < len && !pl__is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; }
static int STB_TEXTEDIT_MOVEWORDRIGHT_WIN(plUiInputTextState* obj, int idx) { idx++; int len = obj->iCurrentLengthW; while (idx < len && !pl__is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; }
// static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(plUiInputTextState* obj, int idx) { if (g.IO.ConfigMacOSXBehaviors) return STB_TEXTEDIT_MOVEWORDRIGHT_MAC(obj, idx); else return STB_TEXTEDIT_MOVEWORDRIGHT_WIN(obj, idx); }
static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(plUiInputTextState* obj, int idx) { return STB_TEXTEDIT_MOVEWORDRIGHT_WIN(obj, idx); }
#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h
#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL
static inline int pl__text_count_utf8_bytes_from_char(unsigned int c)
{
if (c < 0x80) return 1;
if (c < 0x800) return 2;
if (c < 0x10000) return 3;
if (c <= 0x10FFFF) return 4;
return 3;
}
static int pl__text_count_utf8_bytes_from_str(const plUiWChar* in_text, const plUiWChar* in_text_end)
{
int bytes_count = 0;
while ((!in_text_end || in_text < in_text_end) && *in_text)
{
unsigned int c = (unsigned int)(*in_text++);
if (c < 0x80)
bytes_count++;
else
bytes_count += pl__text_count_utf8_bytes_from_char(c);
}
return bytes_count;
}
static void STB_TEXTEDIT_DELETECHARS(plUiInputTextState* obj, int pos, int n)
{
plUiWChar* dst = obj->sbTextW + pos;
// We maintain our buffer length in both UTF-8 and wchar formats
obj->bEdited = true;
obj->iCurrentLengthA -= pl__text_count_utf8_bytes_from_str(dst, dst + n);
obj->iCurrentLengthW -= n;
// Offset remaining text (FIXME-OPT: Use memmove)
const plUiWChar* src = obj->sbTextW + pos + n;
plUiWChar c = *src++;
while (c)
{
*dst++ = c;
c = *src++;
}
*dst = '\0';
}
static bool STB_TEXTEDIT_INSERTCHARS(plUiInputTextState* obj, int pos, const plUiWChar* new_text, int new_text_len)
{
// const bool is_resizable = (obj->Flags & ImGuiInputTextFlags_CallbackResize) != 0;
const bool is_resizable = false;
const int text_len = obj->iCurrentLengthW;
PL_UI_ASSERT(pos <= text_len);
const int new_text_len_utf8 = pl__text_count_utf8_bytes_from_str(new_text, new_text + new_text_len);
if (!is_resizable && (new_text_len_utf8 + obj->iCurrentLengthA + 1 > obj->iBufferCapacityA))
return false;
// Grow internal buffer if needed
if (new_text_len + text_len + 1 > (int)plu_sb_size(obj->sbTextW))
{
if (!is_resizable)
return false;
PL_UI_ASSERT(text_len < (int)plu_sb_size(obj->sbTextW));
plu_sb_resize(obj->sbTextW, text_len + plu_clampi(32, new_text_len * 4, plu_max(256, new_text_len)) + 1);
}
plUiWChar* text = obj->sbTextW;
if (pos != text_len)
memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(plUiWChar));
memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(plUiWChar));
obj->bEdited = true;
obj->iCurrentLengthW += new_text_len;
obj->iCurrentLengthA += new_text_len_utf8;
obj->sbTextW[obj->iCurrentLengthW] = '\0';
return true;
}
// We don't use an enum so we can build even with conflicting symbols (if another user of stb_textedit.h leak their STB_TEXTEDIT_K_* symbols)
#define STB_TEXTEDIT_K_LEFT 0x200000 // keyboard input to move cursor left
#define STB_TEXTEDIT_K_RIGHT 0x200001 // keyboard input to move cursor right
#define STB_TEXTEDIT_K_UP 0x200002 // keyboard input to move cursor up
#define STB_TEXTEDIT_K_DOWN 0x200003 // keyboard input to move cursor down
#define STB_TEXTEDIT_K_LINESTART 0x200004 // keyboard input to move cursor to start of line
#define STB_TEXTEDIT_K_LINEEND 0x200005 // keyboard input to move cursor to end of line
#define STB_TEXTEDIT_K_TEXTSTART 0x200006 // keyboard input to move cursor to start of text
#define STB_TEXTEDIT_K_TEXTEND 0x200007 // keyboard input to move cursor to end of text
#define STB_TEXTEDIT_K_DELETE 0x200008 // keyboard input to delete selection or character under cursor
#define STB_TEXTEDIT_K_BACKSPACE 0x200009 // keyboard input to delete selection or character left of cursor
#define STB_TEXTEDIT_K_UNDO 0x20000A // keyboard input to perform undo
#define STB_TEXTEDIT_K_REDO 0x20000B // keyboard input to perform redo
#define STB_TEXTEDIT_K_WORDLEFT 0x20000C // keyboard input to move cursor left one word
#define STB_TEXTEDIT_K_WORDRIGHT 0x20000D // keyboard input to move cursor right one word
#define STB_TEXTEDIT_K_PGUP 0x20000E // keyboard input to move cursor up a page
#define STB_TEXTEDIT_K_PGDOWN 0x20000F // keyboard input to move cursor down a page
#define STB_TEXTEDIT_K_SHIFT 0x400000
#define STB_TEXTEDIT_IMPLEMENTATION
#include "stb_textedit.h"
// stb_textedit internally allows for a single undo record to do addition and deletion, but somehow, calling
// the stb_textedit_paste() function creates two separate records, so we perform it manually. (FIXME: Report to nothings/stb?)
static void stb_textedit_replace(plUiInputTextState* str, STB_TexteditState* state, const STB_TEXTEDIT_CHARTYPE* text, int text_len)
{
stb_text_makeundo_replace(str, state, 0, str->iCurrentLengthW, text_len);
STB_TEXTEDIT_DELETECHARS(str, 0, str->iCurrentLengthW);
state->cursor = state->select_start = state->select_end = 0;
if (text_len <= 0)
return;
if (STB_TEXTEDIT_INSERTCHARS(str, 0, text, text_len))
{
state->cursor = state->select_start = state->select_end = text_len;
state->has_preferred_x = 0;
return;
}
PL_UI_ASSERT(0); // Failed to insert character, normally shouldn't happen because of how we currently use stb_textedit_replace()
}
static void
pl__text_state_on_key_press(plUiInputTextState* ptState, int iKey)
{
stb_textedit_key(ptState, &ptState->tStb, iKey);
ptState->bCursorFollow = true;
pl__text_state_cursor_anim_reset(ptState);
}
// Based on stb_to_utf8() from github.com/nothings/stb/
static inline int
pl__text_char_to_utf8_inline(char* buf, int buf_size, unsigned int c)
{
if (c < 0x80)
{
buf[0] = (char)c;
return 1;
}
if (c < 0x800)
{
if (buf_size < 2) return 0;
buf[0] = (char)(0xc0 + (c >> 6));
buf[1] = (char)(0x80 + (c & 0x3f));
return 2;
}
if (c < 0x10000)
{
if (buf_size < 3) return 0;
buf[0] = (char)(0xe0 + (c >> 12));
buf[1] = (char)(0x80 + ((c >> 6) & 0x3f));
buf[2] = (char)(0x80 + ((c ) & 0x3f));
return 3;
}
if (c <= 0x10FFFF)
{
if (buf_size < 4) return 0;
buf[0] = (char)(0xf0 + (c >> 18));
buf[1] = (char)(0x80 + ((c >> 12) & 0x3f));
buf[2] = (char)(0x80 + ((c >> 6) & 0x3f));
buf[3] = (char)(0x80 + ((c ) & 0x3f));
return 4;
}
// Invalid code point, the max unicode is 0x10FFFF
return 0;
}
static inline plUiInputTextState*
pl__get_input_text_state(uint32_t id)
{
return (id != 0 && gptCtx->tInputTextState.uId == id) ? &gptCtx->tInputTextState : NULL;
}
static const plUiWChar*
pl__str_bol_w(const plUiWChar* buf_mid_line, const plUiWChar* buf_begin) // find beginning-of-line
{
while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n')
buf_mid_line--;
return buf_mid_line;
}
static int
pl__text_str_to_utf8(char* out_buf, int out_buf_size, const plUiWChar* in_text, const plUiWChar* in_text_end)
{
char* buf_p = out_buf;
const char* buf_end = out_buf + out_buf_size;
while (buf_p < buf_end - 1 && (!in_text_end || in_text < in_text_end) && *in_text)
{
unsigned int c = (unsigned int)(*in_text++);
if (c < 0x80)
*buf_p++ = (char)c;
else
buf_p += pl__text_char_to_utf8_inline(buf_p, (int)(buf_end - buf_p - 1), c);
}
*buf_p = 0;
return (int)(buf_p - out_buf);
}
static int
pl__text_str_from_utf8(plUiWChar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining)
{
plUiWChar* buf_out = buf;
plUiWChar* buf_end = buf + buf_size;
while (buf_out < buf_end - 1 && (!in_text_end || in_text < in_text_end) && *in_text)
{
unsigned int c;
in_text += plu_text_char_from_utf8(&c, in_text, in_text_end);
*buf_out++ = (plUiWChar)c;
}
*buf_out = 0;
if (in_text_remaining)
*in_text_remaining = in_text;
return (int)(buf_out - buf);
}
static int
pl__text_count_chars_from_utf8(const char* in_text, const char* in_text_end)
{
int char_count = 0;
while ((!in_text_end || in_text < in_text_end) && *in_text)
{
unsigned int c;
in_text += plu_text_char_from_utf8(&c, in_text, in_text_end);
char_count++;
}
return char_count;
}
static int
pl__input_text_calc_text_len_and_line_count(const char* text_begin, const char** out_text_end)
{
int line_count = 0;
const char* s = text_begin;
char c = *s++;
while (c) // We are only matching for \n so we can ignore UTF-8 decoding
{
if (c == '\n')
line_count++;
c = *s++;
}
s--;
if (s[0] != '\n' && s[0] != '\r')
line_count++;
*out_text_end = s;
return line_count;
}
bool
pl_button_behavior(const plRect* ptBox, uint32_t uHash, bool* pbOutHovered, bool* pbOutHeld)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
gptCtx->tPrevItemData.bActive = false;
bool bPressed = false;
bool bHovered = pl_is_item_hoverable(ptBox, uHash);
if(bHovered)
gptCtx->uNextHoveredId = uHash;
bool bHeld = bHovered && pl_is_mouse_down(PL_MOUSE_BUTTON_LEFT);
if(uHash == gptCtx->uActiveId)
{
gptCtx->tPrevItemData.bActive = true;
if(bHeld)
pl__set_active_id(uHash, ptWindow);
}
if(bHovered)
{
if(pl_is_mouse_clicked(PL_MOUSE_BUTTON_LEFT, false))
{
pl__set_active_id(uHash, ptWindow);
gptCtx->tPrevItemData.bActive = true;
}
else if(pl_is_mouse_released(PL_MOUSE_BUTTON_LEFT))
{
bPressed = uHash == gptCtx->uActiveId;
pl__set_active_id(0, ptWindow);
}
}
*pbOutHeld = bHeld;
*pbOutHovered = bHovered;
gptCtx->tPrevItemData.bHovered = bHovered;
return bPressed;
}
bool
pl_button(const char* pcText)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
bool bPressed = false;
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
plRect tBoundingBox = plu_calculate_rect(tStartPos, tWidgetSize);
const plRect* ptClipRect = pl_get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = plu_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
bool bHeld = false;
bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(gptCtx->uActiveId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonActiveCol);
else if(gptCtx->uHoveredId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonHoveredCol);
else pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonCol);
const plVec2 tTextSize = pl_ui_calculate_text_size(gptCtx->ptFont, gptCtx->tStyle.fFontSize, pcText, -1.0f);
const plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
plVec2 tTextStartPos = {
.x = tStartPos.x,
.y = tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y
};
if((plu_rect_width(&tBoundingBox) < tTextSize.x)) // clipping, so start at beginning of widget
tTextStartPos.x += gptCtx->tStyle.tFramePadding.x;
else // not clipping, so center on widget
tTextStartPos.x += tStartPos.x + tWidgetSize.x / 2.0f - tTextActualCenter.x;
pl_add_clipped_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, tBoundingBox.tMin, tBoundingBox.tMax, gptCtx->tColorScheme.tTextCol, pcText, 0.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
return bPressed;
}
bool
pl_selectable(const char* pcText, bool* bpValue)
{
// temporary hack
static bool bDummyState = true;
if(bpValue == NULL) bpValue = &bDummyState;
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
bool bPressed = false;
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
const plVec2 tTextStartPos = {
.x = tStartPos.x + gptCtx->tStyle.tFramePadding.x,
.y = tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y
};
const plVec2 tEndPos = plu_add_vec2(tStartPos, tWidgetSize);
plRect tBoundingBox = plu_calculate_rect(tStartPos, tWidgetSize);
const plRect* ptClipRect = pl_get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = plu_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
bool bHeld = false;
bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(bPressed)
*bpValue = !*bpValue;
if(gptCtx->uActiveId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tEndPos, gptCtx->tColorScheme.tHeaderActiveCol);
else if(gptCtx->uHoveredId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tEndPos, gptCtx->tColorScheme.tHeaderHoveredCol);
if(*bpValue)
pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tEndPos, gptCtx->tColorScheme.tHeaderCol);
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, gptCtx->tColorScheme.tTextCol, pcText, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
return bPressed;
}
bool
pl_checkbox(const char* pcText, bool* bpValue)
{
// temporary hack
static bool bDummyState = true;
if(bpValue == NULL) bpValue = &bDummyState;
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
bool bPressed = false;
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
const bool bOriginalValue = *bpValue;
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
const plVec2 tTextStartPos = {
.x = tStartPos.x + tWidgetSize.y + gptCtx->tStyle.tInnerSpacing.x,
.y = tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y
};
plRect tBoundingBox = plu_calculate_rect(tStartPos, (plVec2){tWidgetSize.y, tWidgetSize.y});
const plRect* ptClipRect = pl_get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = plu_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
bool bHeld = false;
bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(bPressed)
*bpValue = !bOriginalValue;
if(gptCtx->uActiveId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tFrameBgActiveCol);
else if(gptCtx->uHoveredId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tFrameBgHoveredCol);
else pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tFrameBgCol);
if(*bpValue)
pl_add_line(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tCheckmarkCol, 2.0f);
// add label
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, gptCtx->tColorScheme.tTextCol, pcText, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
return bPressed;
}
bool
pl_radio_button(const char* pcText, int* piValue, int iButtonValue)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
bool bPressed = false;
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
const plVec2 tTextSize = pl_ui_calculate_text_size(gptCtx->ptFont, gptCtx->tStyle.fFontSize, pcText, -1.0f);
plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
const plVec2 tSize = {tTextSize.x + 2.0f * gptCtx->tStyle.tFramePadding.x + gptCtx->tStyle.tInnerSpacing.x + tWidgetSize.y, tWidgetSize.y};
// tSize = plu_floor_vec2(tSize);
const plVec2 tTextStartPos = {
.x = tStartPos.x + tWidgetSize.y + gptCtx->tStyle.tInnerSpacing.x + gptCtx->tStyle.tFramePadding.x,
.y = tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y
};
plRect tBoundingBox = plu_rect_expand_vec2(&tTextBounding, (plVec2){0.5f * (gptCtx->tStyle.tFramePadding.x + gptCtx->tStyle.tInnerSpacing.x + tWidgetSize.y), 0.0f});
tBoundingBox = plu_rect_move_start_x(&tBoundingBox, tStartPos.x + gptCtx->tStyle.tFramePadding.x);
const plRect* ptClipRect = pl_get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = plu_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
bool bHeld = false;
bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(bPressed)
*piValue = iButtonValue;
if(gptCtx->uActiveId == uHash) pl_add_circle_filled(ptWindow->ptFgLayer, (plVec2){tStartPos.x + tWidgetSize.y / 2.0f, tStartPos.y + tWidgetSize.y / 2.0f}, gptCtx->tStyle.fFontSize / 1.5f, gptCtx->tColorScheme.tFrameBgActiveCol, 12);
else if(gptCtx->uHoveredId == uHash) pl_add_circle_filled(ptWindow->ptFgLayer, (plVec2){tStartPos.x + tWidgetSize.y / 2.0f, tStartPos.y + tWidgetSize.y / 2.0f}, gptCtx->tStyle.fFontSize / 1.5f, gptCtx->tColorScheme.tFrameBgHoveredCol, 12);
else pl_add_circle_filled(ptWindow->ptFgLayer, (plVec2){tStartPos.x + tWidgetSize.y / 2.0f, tStartPos.y + tWidgetSize.y / 2.0f}, gptCtx->tStyle.fFontSize / 1.5f, gptCtx->tColorScheme.tFrameBgCol, 12);
if(*piValue == iButtonValue)
pl_add_circle_filled(ptWindow->ptFgLayer, (plVec2){tStartPos.x + tWidgetSize.y / 2.0f, tStartPos.y + tWidgetSize.y / 2.0f}, gptCtx->tStyle.fFontSize / 2.5f, gptCtx->tColorScheme.tCheckmarkCol, 12);
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, gptCtx->tColorScheme.tTextCol, pcText, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
return bPressed;
}
bool
pl_collapsing_header(const char* pcText)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
bool* pbOpenState = pl_get_bool_ptr(&ptWindow->tStorage, uHash, false);
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
const plVec2 tTextStartPos = {
.x = tStartPos.x + tWidgetSize.y * 1.5f,
.y = tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y
};
plRect tBoundingBox = plu_calculate_rect(tStartPos, tWidgetSize);
const plRect* ptClipRect = pl_get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = plu_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
bool bHeld = false;
const bool bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(bPressed)
*pbOpenState = !*pbOpenState;
if(gptCtx->uActiveId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tHeaderActiveCol);
else if(gptCtx->uHoveredId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tHeaderHoveredCol);
else pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tHeaderCol);
if(*pbOpenState)
{
const plVec2 centerPoint = {tStartPos.x + 8.0f * 1.5f, tStartPos.y + tWidgetSize.y / 2.0f};
const plVec2 pointPos = plu_add_vec2(centerPoint, (plVec2){ 0.0f, 4.0f});
const plVec2 rightPos = plu_add_vec2(centerPoint, (plVec2){ 4.0f, -4.0f});
const plVec2 leftPos = plu_add_vec2(centerPoint, (plVec2){-4.0f, -4.0f});
pl_add_triangle_filled(ptWindow->ptFgLayer, pointPos, rightPos, leftPos, (plVec4){1.0f, 1.0f, 1.0f, 1.0f});
}
else
{
const plVec2 centerPoint = {tStartPos.x + 8.0f * 1.5f, tStartPos.y + tWidgetSize.y / 2.0f};
const plVec2 pointPos = plu_add_vec2(centerPoint, (plVec2){ 4.0f, 0.0f});
const plVec2 rightPos = plu_add_vec2(centerPoint, (plVec2){ -4.0f, -4.0f});
const plVec2 leftPos = plu_add_vec2(centerPoint, (plVec2){ -4.0f, 4.0f});
pl_add_triangle_filled(ptWindow->ptFgLayer, pointPos, rightPos, leftPos, (plVec4){1.0f, 1.0f, 1.0f, 1.0f});
}
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, gptCtx->tColorScheme.tTextCol, pcText, -1.0f);
}
if(*pbOpenState)
plu_sb_push(ptWindow->sbtRowStack, ptWindow->tTempData.tCurrentLayoutRow);
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
return *pbOpenState;
}
void
pl_end_collapsing_header(void)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
ptWindow->tTempData.tCurrentLayoutRow = plu_sb_pop(ptWindow->sbtRowStack);
}
bool
pl_tree_node(const char* pcText)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plu_sb_push(ptWindow->sbtRowStack, ptWindow->tTempData.tCurrentLayoutRow);
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
bool* pbOpenState = pl_get_bool_ptr(&ptWindow->tStorage, uHash, false);
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
plRect tBoundingBox = plu_calculate_rect(tStartPos, tWidgetSize);
const plRect* ptClipRect = pl_get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = plu_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
bool bHeld = false;
const bool bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(bPressed)
*pbOpenState = !*pbOpenState;
if(gptCtx->uActiveId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tHeaderActiveCol);
else if(gptCtx->uHoveredId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tHeaderHoveredCol);
if(*pbOpenState)
{
const plVec2 centerPoint = {tStartPos.x + 8.0f * 1.5f, tStartPos.y + tWidgetSize.y / 2.0f};
const plVec2 pointPos = plu_add_vec2(centerPoint, (plVec2){ 0.0f, 4.0f});
const plVec2 rightPos = plu_add_vec2(centerPoint, (plVec2){ 4.0f, -4.0f});
const plVec2 leftPos = plu_add_vec2(centerPoint, (plVec2){-4.0f, -4.0f});
pl_add_triangle_filled(ptWindow->ptFgLayer, pointPos, rightPos, leftPos, (plVec4){1.0f, 1.0f, 1.0f, 1.0f});
}
else
{
const plVec2 centerPoint = {tStartPos.x + 8.0f * 1.5f, tStartPos.y + tWidgetSize.y / 2.0f};
const plVec2 pointPos = plu_add_vec2(centerPoint, (plVec2){ 4.0f, 0.0f});
const plVec2 rightPos = plu_add_vec2(centerPoint, (plVec2){ -4.0f, -4.0f});
const plVec2 leftPos = plu_add_vec2(centerPoint, (plVec2){ -4.0f, 4.0f});
pl_add_triangle_filled(ptWindow->ptFgLayer, pointPos, rightPos, leftPos, (plVec4){1.0f, 1.0f, 1.0f, 1.0f});
}
const plVec2 tTextStartPos = {
.x = tStartPos.x + tWidgetSize.y * 1.5f,
.y = tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y
};
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, gptCtx->tColorScheme.tTextCol, pcText, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
if(*pbOpenState)
{
ptWindow->tTempData.uTreeDepth++;
plu_sb_push(ptWindow->sbtRowStack, ptWindow->tTempData.tCurrentLayoutRow);
plu_sb_push(gptCtx->sbuIdStack, uHash);
}
return *pbOpenState;
}
bool
pl_tree_node_f(const char* pcFmt, ...)
{
va_list args;
va_start(args, pcFmt);
bool bOpen = pl_tree_node_v(pcFmt, args);
va_end(args);
return bOpen;
}
bool
pl_tree_node_v(const char* pcFmt, va_list args)
{
static char acTempBuffer[1024];
plu_vsprintf(acTempBuffer, pcFmt, args);
return pl_tree_node(acTempBuffer);
}
void
pl_tree_pop(void)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
ptWindow->tTempData.uTreeDepth--;
plu_sb_pop(gptCtx->sbuIdStack);
ptWindow->tTempData.tCurrentLayoutRow = plu_sb_pop(ptWindow->sbtRowStack);
}
bool
pl_begin_tab_bar(const char* pcText)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
const float fFrameHeight = pl_get_frame_height();
const plVec2 tStartPos = pl__ui_get_cursor_pos();
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
plu_sb_push(ptWindow->sbtRowStack, ptWindow->tTempData.tCurrentLayoutRow);
ptWindow->tTempData.tCurrentLayoutRow.fRowStartX = tStartPos.x - ptWindow->tTempData.tRowPos.x;
ptWindow->tTempData.fAccumRowX += ptWindow->tTempData.tCurrentLayoutRow.fRowStartX;
pl_layout_dynamic(0.0f, 1);
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
plu_sb_push(gptCtx->sbuIdStack, uHash);
// check if tab bar existed
gptCtx->ptCurrentTabBar = NULL;
for(uint32_t i = 0; i < plu_sb_size(gptCtx->sbtTabBars); i++)
{
if(gptCtx->sbtTabBars[i].uId == uHash)
{
gptCtx->ptCurrentTabBar = &gptCtx->sbtTabBars[i];
break;
}
}
// new tab bar needs to be created
if(gptCtx->ptCurrentTabBar == NULL)
{
plUiTabBar tTabBar = {
.uId = uHash
};;
plu_sb_push(gptCtx->sbtTabBars, tTabBar);
gptCtx->ptCurrentTabBar = &plu_sb_top(gptCtx->sbtTabBars);
}
gptCtx->ptCurrentTabBar->tStartPos = tStartPos;
gptCtx->ptCurrentTabBar->tCursorPos = tStartPos;
gptCtx->ptCurrentTabBar->uCurrentIndex = 0u;
pl_add_line(ptWindow->ptFgLayer,
(plVec2){gptCtx->ptCurrentTabBar->tStartPos.x, gptCtx->ptCurrentTabBar->tStartPos.y + fFrameHeight},
(plVec2){gptCtx->ptCurrentTabBar->tStartPos.x + tWidgetSize.x, gptCtx->ptCurrentTabBar->tStartPos.y + fFrameHeight},
gptCtx->tColorScheme.tButtonActiveCol, 1.0f);
pl_advance_cursor(tWidgetSize.x, fFrameHeight);
return true;
}
void
pl_end_tab_bar(void)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
gptCtx->ptCurrentTabBar->uValue = gptCtx->ptCurrentTabBar->uNextValue;
plu_sb_pop(gptCtx->sbuIdStack);
ptWindow->tTempData.tCurrentLayoutRow = plu_sb_pop(ptWindow->sbtRowStack);
ptWindow->tTempData.fAccumRowX -= ptWindow->tTempData.tCurrentLayoutRow.fRowStartX;
}
bool
pl_begin_tab(const char* pcText)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plu_sb_push(ptWindow->sbtRowStack, ptWindow->tTempData.tCurrentLayoutRow);
pl_layout_dynamic(0.0f, 1);
plUiTabBar* ptTabBar = gptCtx->ptCurrentTabBar;
const float fFrameHeight = pl_get_frame_height();
const uint32_t uHash = plu_str_hash(pcText, 0, plu_sb_top(gptCtx->sbuIdStack));
plu_sb_push(gptCtx->sbuIdStack, uHash);
if(ptTabBar->uValue == 0u) ptTabBar->uValue = uHash;
const plVec2 tTextSize = pl_ui_calculate_text_size(gptCtx->ptFont, gptCtx->tStyle.fFontSize, pcText, -1.0f);
const plVec2 tStartPos = ptTabBar->tCursorPos;
plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, pcText, pl_find_renderered_text_end(pcText, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
const plVec2 tFinalSize = {tTextSize.x + 2.0f * gptCtx->tStyle.tFramePadding.x, fFrameHeight};
const plVec2 tTextStartPos = {
.x = tStartPos.x + tStartPos.x + tFinalSize.x / 2.0f - tTextActualCenter.x,
.y = tStartPos.y + tStartPos.y + fFrameHeight / 2.0f - tTextActualCenter.y
};
const plRect tBoundingBox = plu_calculate_rect(tStartPos, tFinalSize);
bool bHovered = false;
bool bHeld = false;
const bool bPressed = pl_button_behavior(&tBoundingBox, uHash, &bHovered, &bHeld);
if(uHash == gptCtx->uActiveId)
{
ptTabBar->uNextValue = uHash;
}
if(gptCtx->uActiveId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonActiveCol);
else if(gptCtx->uHoveredId == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonHoveredCol);
else if(ptTabBar->uValue == uHash) pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonActiveCol);
else pl_add_rect_filled(ptWindow->ptFgLayer, tStartPos, tBoundingBox.tMax, gptCtx->tColorScheme.tButtonCol);
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tTextStartPos, gptCtx->tColorScheme.tTextCol, pcText, -1.0f);
ptTabBar->tCursorPos.x += gptCtx->tStyle.tInnerSpacing.x + tFinalSize.x;
ptTabBar->uCurrentIndex++;
if(ptTabBar->uValue != uHash)
pl_end_tab();
return ptTabBar->uValue == uHash;
}
void
pl_end_tab(void)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plu_sb_pop(gptCtx->sbuIdStack);
ptWindow->tTempData.tCurrentLayoutRow = plu_sb_pop(ptWindow->sbtRowStack);
}
void
pl_separator(void)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tStartPos = pl__ui_get_cursor_pos();
const plVec2 tWidgetSize = pl_calculate_item_size(gptCtx->tStyle.tItemSpacing.y * 2.0f);
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
pl_add_line(ptWindow->ptFgLayer, tStartPos, (plVec2){tStartPos.x + tWidgetSize.x, tStartPos.y}, gptCtx->tColorScheme.tCheckmarkCol, 1.0f);
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
}
void
pl_vertical_spacing(void)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
ptWindow->tTempData.tRowPos.y += gptCtx->tStyle.tItemSpacing.y * 2.0f;
}
void
pl_indent(float fIndent)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
ptCurrentRow->fHorizontalOffset += fIndent == 0.0f ? gptCtx->tStyle.fIndentSize : fIndent;
ptWindow->tTempData.fExtraIndent += fIndent == 0.0f ? gptCtx->tStyle.fIndentSize : fIndent;
}
void
pl_unindent(float fIndent)
{
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
ptCurrentRow->fHorizontalOffset -= fIndent == 0.0f ? gptCtx->tStyle.fIndentSize : fIndent;
ptWindow->tTempData.fExtraIndent -= fIndent == 0.0f ? gptCtx->tStyle.fIndentSize : fIndent;
}
void
pl_text(const char* pcFmt, ...)
{
va_list args;
va_start(args, pcFmt);
pl_text_v(pcFmt, args);
va_end(args);
}
void
pl_text_v(const char* pcFmt, va_list args)
{
static char acTempBuffer[1024];
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
plUiLayoutRow* ptCurrentRow = &ptWindow->tTempData.tCurrentLayoutRow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
plu_vsprintf(acTempBuffer, pcFmt, args);
const plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, acTempBuffer, pl_find_renderered_text_end(acTempBuffer, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, (plVec2){tStartPos.x, tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y}, gptCtx->tColorScheme.tTextCol, acTempBuffer, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
}
void
pl_color_text(plVec4 tColor, const char* pcFmt, ...)
{
va_list args;
va_start(args, pcFmt);
pl_color_text_v(tColor, pcFmt, args);
va_end(args);
}
void
pl_color_text_v(plVec4 tColor, const char* pcFmt, va_list args)
{
static char acTempBuffer[1024];
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
plu_vsprintf(acTempBuffer, pcFmt, args);
const plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, acTempBuffer, pl_find_renderered_text_end(acTempBuffer, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, (plVec2){tStartPos.x, tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y}, tColor, acTempBuffer, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
}
void
pl_labeled_text(const char* pcLabel, const char* pcFmt, ...)
{
va_list args;
va_start(args, pcFmt);
pl_labeled_text_v(pcLabel, pcFmt, args);
va_end(args);
}
void
pl_labeled_text_v(const char* pcLabel, const char* pcFmt, va_list args)
{
static char acTempBuffer[1024];
plUiWindow* ptWindow = gptCtx->ptCurrentWindow;
const plVec2 tWidgetSize = pl_calculate_item_size(pl_get_frame_height());
const plVec2 tStartPos = pl__ui_get_cursor_pos();
if(pl__ui_should_render(&tStartPos, &tWidgetSize))
{
plu_vsprintf(acTempBuffer, pcFmt, args);
const plRect tTextBounding = pl_calculate_text_bb_ex(gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartPos, acTempBuffer, pl_find_renderered_text_end(acTempBuffer, NULL), -1.0f);
const plVec2 tTextActualCenter = plu_rect_center(&tTextBounding);
const plVec2 tStartLocation = {tStartPos.x, tStartPos.y + tStartPos.y + tWidgetSize.y / 2.0f - tTextActualCenter.y};
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, plu_add_vec2(tStartLocation, (plVec2){(tWidgetSize.x / 3.0f), 0.0f}), gptCtx->tColorScheme.tTextCol, acTempBuffer, -1.0f);
pl_ui_add_text(ptWindow->ptFgLayer, gptCtx->ptFont, gptCtx->tStyle.fFontSize, tStartLocation, gptCtx->tColorScheme.tTextCol, pcLabel, -1.0f);
}
pl_advance_cursor(tWidgetSize.x, tWidgetSize.y);
}
bool
pl_input_text_ex(const char* pcLabel, const char* pcHint, char* pcBuffer, size_t szBufferSize, plUiInputTextFlags tFlags);
bool
pl_input_text(const char* pcLabel, char* pcBuffer, size_t szBufferSize)
{
return pl_input_text_ex(pcLabel, NULL, pcBuffer, szBufferSize, 0);
}
bool
pl_input_text_hint(const char* pcLabel, const char* pcHint, char* pcBuffer, size_t szBufferSize)