Untitled
Loading...
Searching...
No Matches
ui.c
Go to the documentation of this file.
1
2#include "utilities/types.h"
4#include <math.h>
5#include <omp.h>
6#include "ui/ui.h"
7#include "ui/fonts.h"
8
9
10
11int TEXT_WIDTH = 1280;
12int TEXT_HEIGHT = 720;
14
15
17{
18 for (int i = 0; i < MAX_UI_ELEMENTS; i++)
19 {
20
21 if (m->elements[i].free) return i;
22
23 }
24 return -1;
25}
26
28{
29 for (int i = 0; i < MAX_UI_CONTAINERS; i++)
30 {
31
32 if (m->containers[i].free) return i;
33
34 }
35 return -1;
36}
37
38
39int create_ui_element(int menu_id)
40{
41
42 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
43
44 UIMenu * m = &ms[menu_id];
45
46
47
48 int empty = find_empty_element_slot(m);
49
50 if (empty == -1)
51 {
52 printf("No empty element slot found\n");
53 return -1;
54 }
55 UIElement * el = &m->elements[empty];
57 el->uID = empty;
60 el->menu = menu_id;
61
62 return empty;
63}
64
65
66
67
69{
70
71 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
72 if (el->menu == -1) return;
73 UIMenu * m = &ms[el->menu];
74
75 UIContainer * cs = m->containers;
76 UIContainer * c = &cs[el->container];
77
78 UIElement * els = m->elements;
79
80 for (int i = 0; i < MAX_UI_ELEMENTS; i++)
81 {
82 UIElement * el = &els[i];
83 if (el->free) continue;
84 if (el->parent_container == c->uID)
85 {
86 el->parent_container = -1;
87 }
88 }
89
90 TextBox * boxes = m->text_boxes;
91
92 for (int i = 0; i < MAX_TEXT_BOXES; i++)
93 {
94 TextBox * box = &boxes[i];
95 if (box->free) continue;
96 if (box->parent_container == c->uID)
97 {
98 box->parent_container = -1;
99 }
100 }
101
102
103 el->container = -1;
104
105
107 c->free = true;
108
109 // write function that shifts indices on deletion. for now this will just create stale entries but shouldn't matter for now
110
111}
112
113
115{
116 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
117
118 UIMenu * m = &ms[el->menu];
119 if (el->container == -1) return;
120 UIContainer * c = &m->containers[el->container];
121
122 for (int i = 0; i < c->child_count; i++)
123 {
124 UIElement * e = &m->elements[c->children[i]];
125 e->parent_container = c->uID;
126 }
127
128 for (int i = 0; i < c->box_count; i++)
129 {
130 TextBox * tb = &m->text_boxes[c->box_children[i]];
131 tb->parent_container = c->uID;
132 }
133
134}
135
136
137
138
140{
141 int id = -1;
142 for (int i = 0; i < c->child_count; i++)
143 {
144
145 if (c->children[i] == el->uID)
146 {
147 id = i;
148 }
149 }
150 if (id != -1)
151 {
153 }
154 el->parent_container = -1;
155
156}
158{
159 int id = -1;
160 for (int i = 0; i < c->box_count; i++)
161 {
162
163 if (c->box_children[i] == tb->uID)
164 {
165 id = i;
166 }
167 }
168 if (id != -1)
169 {
171 }
172
173}
174
175
176void delete_ui_element(int menu, int id)
177{
178
179 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
180 UIMenu * m = &ms[menu];
181
182 UIElement * el = &m->elements[id];
183 if (el->parent_container != -1)
184 {
186 }
187 if (el->container != -1)
188 {
190 }
191
192 // handle label here
193
194 el->free = true;
195}
196
198{
199
200 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
201 UIMenu * m = &ms[menu_id];
202
203
204
205 int empty = find_empty_container_slot(m);
206 if (empty == -1)
207 {
208 printf("No empty container slot found\n");
209 return NULL;
210 }
211
212 UIContainer * container = &m->containers[empty];
213
214 *container = DEFAULT_UI_CONTAINER;
215 container->uID = empty;
216 el->container = container->uID;
217 container->element = el->uID;
218
219 return container;
220
221}
222
223
225{
226
227 vec2 new_scroll_offset = vec2_scale((vec2){c->scroll_dir.x, c->scroll_dir.y}, lerp(slider->min_val, slider->max_val, slider->val) * slider->scroll_scale);
228
229
230 c->delta_scroll_offset = vec2_sub(new_scroll_offset, c->scroll_offset);
231 c->scroll_offset = new_scroll_offset;
232
233 UIElement * els = m->elements;
234 for(int i = 0; i < c->child_count; i++) {
235
236 int el_idx = c->children[i];
237 UIElement * el = &els[el_idx];
239 }
240}
241
242int find_ui_element_in_menu(UIMenu * m, const char * id)
243{
244
245 UIElement * els = m->elements;
246 for (int i = 0; i < MAX_UI_ELEMENTS; i++)
247 {
248
249 UIElement * el = &els[i];
250 if (el->free) continue;
251
252 if (strcmp(el->id, id) == 0)
253 {
254 return el->uID;
255 }
256
257 }
258 return -1;
259}
260
261int find_text_box_in_menu(UIMenu * m, const char * id)
262{
263
264 TextBox * tbs = m->text_boxes;
265 for (int i = 0; i < MAX_TEXT_BOXES; i++)
266 {
267
268 TextBox * tb = &tbs[i];
269 if (tb->free) continue;
270
271 if (strcmp(tb->id, id) == 0)
272 {
273 return tb->uID;
274 }
275
276 }
277 return -1;
278}
279
280int find_container_in_menu(UIMenu * m, const char * id)
281{
282
283 UIContainer * cs = m->containers;
284 for (int i = 0; i < MAX_UI_CONTAINERS; i++)
285 {
286
287 UIContainer* c = &cs[i];
288 if (c->free) continue;
289
290 if (strcmp(c->id, id) == 0)
291 {
292 return c->uID;
293 }
294
295 }
296 return -1;
297}
298
299int move_element_to_menu(int element_idx, int current_menu, int target_menu)
300{
301
302 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
303 if (current_menu == -1 || element_idx == -1 || target_menu == -1) return -1;
304 UIMenu * m = &ms[current_menu];
305 UIElement * el = &m->elements[element_idx];
306 UIMenu * target = &ms[target_menu];
307
308 int target_slot = find_empty_element_slot(target);
309 if (target_slot == -1) return -1;
310 if (el->container != -1)
311 {
313 }
314
315 UIElement * new_el = &target->elements[target_slot];
316 int uID = new_el->uID;
317 memcpy(new_el, el, sizeof(UIElement));
318
319 // need to maybe copy the text box if label to new menu
320 new_el->parent_container = -1;
321 new_el->menu = target_menu;
322 new_el->uID = target_slot;
323
324 delete_ui_element(current_menu, element_idx);
325 return target_slot;
326
327}
328
329int move_text_box_to_menu(int tb_idx, int current_menu, int target_menu)
330{
331
332 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
333 if (current_menu == -1 || tb_idx == -1 || target_menu == -1) return -1;
334
335 UIMenu * m = &ms[current_menu];
336 TextBox * tb = &m->text_boxes[tb_idx];
337 UIMenu * target = &ms[target_menu];
338 int target_slot = find_empty_text_box_slot(target);
339 if (target_slot == -1) return -1;
340
341 TextBox * new_tb = &target->text_boxes[target_slot];
342 int uID = new_tb->uID;
343 memcpy(new_tb, tb, sizeof(TextBox));
344
345 if (tb->text)
346 {
347 new_tb->text = malloc(sizeof(char) * tb->text_cap);
348 memcpy(new_tb->text, tb->text, sizeof(char) * tb->text_cap);
349 }
350 else
351 {
352 new_tb->text = NULL;
353 new_tb->text_cap = 0;
354 }
355
356 delete_text_box(current_menu, tb_idx);
357 new_tb->menu = target_menu;
358 new_tb->uID = target_slot;
359
360
361 return target_slot;
362
363
364}
365
366
368{
369
370 bool already_assigned = false;
371 for (int j = 0; j < c->child_count; j++)
372 {
373 if (c->children[j] == el->uID)
374 {
375 already_assigned = true;
376 break;
377 }
378
379 }
380 if (!already_assigned)
381 {
382 c->children[c->child_count++] = el->uID;
383 }
384 el->parent_container = c->uID;
385
386}
387
389{
390
391 bool already_assigned = false;
392 for (int j = 0; j < c->box_count; j++)
393 {
394 if (c->box_children[j] == tb->uID)
395 {
396 already_assigned = true;
397 break;
398 }
399
400 }
401 if (!already_assigned)
402 {
403 c->box_children[c->box_count++] = tb->uID;
404 }
405 tb->parent_container = c->uID;
406
407}
408
409
411{
412
413 UILabel * l = &el->as.label;
414 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
415 UIMenu * m = &ms[el->menu];
416 TextBox * tb = &m->text_boxes[l->box];
417 float hw = tb->w/2;
418 float hh = tb->h/2;
419 float el_hw = el->scale.x;
420 float el_hh = el->scale.y;
421
422 float scale_x = (float)TEXT_WIDTH / UI_SCALE_WIDTH;
423 float scale_y = (float)TEXT_HEIGHT / UI_SCALE_HEIGHT;
424 float top_start = el->translation.y - el_hh;
425 float bottom_start = el->translation.y + el_hh;
426 float left_start = el->translation.x - el_hw;
427 float right_start = el->translation.x + el_hw;
428 float left_tr = left_start + hw;
429 float right_tr = right_start - hw;
430 float top_tr = top_start;
431 float bottom_tr = bottom_start;
432
433
434 switch(l->align)
435 {
436 case JUSTIFY_TOP_LEFT:
437 tb->translation = (vec2){left_tr, top_tr};
438 break;
440 tb->translation = (vec2){el->translation.x, top_tr};
441 break;
443 tb->translation = (vec2){right_tr, top_tr};
444 break;
445 case JUSTIFY_LEFT:
446 tb->translation = (vec2){left_tr, el->translation.y};
447 break;
448 case JUSTIFY_CENTER:
449 tb->translation = el->translation;
450 break;
451 case JUSTIFY_RIGHT:
452 tb->translation = (vec2){right_tr, el->translation.y};
453 break;
455 tb->translation = (vec2){left_tr, bottom_tr};
456 break;
458 tb->translation = (vec2){el->translation.x, bottom_tr};
459 break;
461 tb->translation = (vec2){right_tr, bottom_tr};
462 break;
463 default:
464 break;
465 }
466
467
468}
469
471{
472
473
474 float d = 0.0f;
475 switch(el->type)
476 {
477 case UIELEMENT_RECT:
478 d = SDF_rect(pos, el->center, el->size, el->radius);
479 return d <= 0.0f;
480 break;
481 case UIELEMENT_CIRCLE:
482 d = SDF_circle(pos, el->center, el->radius);
483 return d <= 0.0f;
484 break;
485 default:
486 return false;
487 break;
488 }
489
490}
491
492vec2 screenspace_to_ui_element_local(UIElement * el, float scale_x, float scale_y, vec2 pos)
493{
494
495 vec2 text_tr = el->translation;
496 text_tr.x *= scale_x;
497 text_tr.y *= scale_y;
498 vec2 local = vec2_sub(pos, text_tr);
499 local.x *= 1.0f/el->scale.x;
500 local.y *= 1.0f/el->scale.y;
501 return local;
502}
503
504
505
507{
508 float scale_x = (float)TEXT_WIDTH / UI_SCALE_WIDTH;
509 float scale_y = (float)TEXT_HEIGHT / UI_SCALE_HEIGHT;
510 vec2 local = screenspace_to_ui_element_local(el, scale_x, scale_y, pos);
511 return point_inside_ui_element(el, local);
512
513}
514
515int get_text_box_from_coords(vec2 pos, int * menu_idx)
516{
517
518 float scale_x = (float)TEXT_WIDTH / UI_SCALE_WIDTH;
519 float scale_y = (float)TEXT_HEIGHT / UI_SCALE_HEIGHT;
520
521 int highest_layer = -INFINITY;
522 int highest_tb_index = -1;
523
524 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
525
526 vec2 ui_coords = screenspace_to_ui(pos);
527 // vec2 ui_coords = pos;
528 for (int i = 0; i < MENUS.count; i++)
529 {
530
531 UIMenu * m = &ms[i];
532 TextBox * tbs = m->text_boxes;
533 for (int j = 0; j < MAX_TEXT_BOXES; j++)
534 {
535 TextBox * tb = &tbs[j];
536 if (!tb->visible || tb->free) continue;
537
538 vec2 scale = tb->scale;
539 // vec2 start = tb->start;
540 vec2 start = screenspace_to_ui(tb->start);
541 start.x *= tb->scale.x;
542 start.y *= tb->scale.y;
543 // vec2 bounds = (vec2){tb->w, tb->h};
544 vec2 bounds = screenspace_to_ui((vec2){tb->w, tb->h});
545
546 bounds.x *= tb->scale.x;
547 bounds.y *= tb->scale.y;
548 vec2 min = vec2_add(start, tb->translation);
549 vec2 max = vec2_add(min, bounds);
550 bool inside = vec2_inside_bounds(ui_coords, min, max);
551 if (inside)
552 {
553
554 if (highest_layer < (int)tb->layer)
555 {
556 highest_layer = tb->layer;
557 highest_tb_index = j;
558 *menu_idx = i;
559 }
560 }
561
562 }
563 }
564
565 return highest_tb_index;
566
567}
568
569
570int get_ui_element_from_coords(vec2 pos, int * menu_idx)
571{
572
573
574 float scale_x = (float)TEXT_WIDTH / UI_SCALE_WIDTH;
575 float scale_y = (float)TEXT_HEIGHT / UI_SCALE_HEIGHT;
576
577 int highest_layer = -INFINITY;
578 int highest_element_index = -1;
579
580 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
581
582 for (int i = 0; i < MENUS.count; i++)
583 {
584
585 UIMenu * m = &ms[i];
586 UIElement * els = m->elements;
587 for (int j = 0; j < MAX_UI_ELEMENTS; j++)
588 {
589
590 UIElement * el = &els[j];
591 if (!el->visible || el->free) continue;
592
593 vec2 local = screenspace_to_ui_element_local(el, scale_x, scale_y, pos);
594 if (point_inside_ui_element(el, local))
595 {
596
597 if (highest_layer < (int)el->layer)
598 {
599 highest_layer = el->layer;
600 highest_element_index = j;
601 *menu_idx = i;
602 }
603
604 }
605
606 }
607 }
608
609 return highest_element_index;
610}
612{
613
614 UIValue * val = &callback->val;
615 switch(callback->val.type)
616 {
617 case UIVAL_NONE:
618 break;
619 case UIVAL_BOOL:
620 *(bool*)callback->val.data = !(*(bool*)callback->val.data);
621 break;
622 case UIVAL_INT:
623 {
624 switch(val->behavior)
625 {
627 *(int*)val->data = (*(int*)val->data) + val->inc_val.inc_int;
628 break;
630 *(int*)val->data = ((*(int*)val->data) + val->inc_val.inc_int + val->max_val.max_int) % val->max_val.max_int;
631 break;
633 *(int*)val->data = MAX(MIN(((*(int*)val->data) + val->inc_val.inc_int), val->max_val.max_int), val->min_val.min_int);
634 break;
635 }
636 }
637 break;
638 case UIVAL_FLOAT:
639 {
640
641 switch(val->behavior)
642 {
644 *(float*)val->data = ((*(float*)val->data) + val->inc_val.inc_flt);
645 break;
647 *(float*)val->data = fmodf(((*(float*)val->data) + val->inc_val.inc_flt + val->max_val.max_flt), val->max_val.max_flt);
648 break;
650 *(float*)val->data = fmaxf(fminf(((*(float*)val->data) + val->inc_val.inc_flt), val->max_val.max_flt), val->min_val.min_flt);
651 break;
652 }
653 }
654 break;
655 case UIVAL_CALLBACK:
656 if (callback->action_fn) {
657 callback->action_fn(callback->val.data);
658 }
659 break;
660 }
661}
662
664 UISlider * slider = &element->as.slider;
665
666 float r = slider->len / 2;
667 slider->min = vec2_sub(element->translation, vec2_scale(slider->dir, r));
668 slider->max = vec2_add(element->translation, vec2_scale(slider->dir, r));
669 slider->max_proj = vec2_length(vec2_sub(slider->max, slider->min));
670
671}
672
673void create_label(UIElement * element, int box, int parent_dropdown, Justify justify)
674{
675
676 element->as.label.box = box;
677 element->as.label.parent_dropdown = parent_dropdown;
678 element->as.label.align = justify;
679
680}
681
682void create_slider(UIElement * element, vec2 dir, float len)
683{
684
685 element->as.slider.container = -1;
687 element->as.slider.len = len;
688 element->as.slider.dir = dir;
689 element->as.slider.scroll_scale = 1;
690 element->as.slider.min_val = 0;
691 element->as.slider.max_val = 100;
692 compute_slider_bounds(element);
693
694}
695
696void handle_ui_event(SDL_Event event, int mouse_x, int mouse_y)
697{
698
699 int menu_idx = -1;
700 int highest_ui_element_idx = get_ui_element_from_coords((vec2){mouse_x, mouse_y}, &menu_idx);
701
702 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
703
704
705 UIElement * highest_el = NULL;
706 if (menu_idx != -1 && highest_ui_element_idx != -1)
707 {
708 UIElement * els = ms[menu_idx].elements;
709 highest_el = &els[highest_ui_element_idx];
710 }
711
712
713 for (int i = 0; i < MENUS.count; i++)
714 {
715 UIMenu * m = &ms[i];
716
717 UIElement * els = m->elements;
718
719 for (int j = 0; j < MAX_UI_ELEMENTS; j++)
720 {
721
722 UIElement * el = &els[j];
723
724 if (!el->visible || el->free) continue;
725
726 bool hovering = is_hovering_ui_element(el, (vec2){mouse_x, mouse_y});
727 bool processed_press = false;
728 if (highest_el)
729 {
730 if (highest_el->uID == el->uID && menu_idx == i && event.type == SDL_MOUSEBUTTONDOWN)
731 {
732 processed_press = true;
734 {
735
736 if (el->state == UISTATE_PRESSED)
737 {
738 if (hovering)
739 {
742 }
743 else
744 {
745 el->state = UISTATE_NONE;
746 }
747 }
748 else
749 {
750
752 el->state = UISTATE_NONE;
753 }
754 }
755 else
756 {
759 }
760 }
761
762 }
763 if (!processed_press)
764 {
765
766 if (hovering)
767 {
768 if (event.type != SDL_MOUSEBUTTONUP && el->state == UISTATE_PRESSED && !el->action_callback.one_shot) continue;
769 if (el->already_hovered) continue;
770
771 if (el->hover_callback.one_shot)
772 {
773
774 if (el->state == UISTATE_HOVERED)
775 {
776
777 el->already_hovered = true;
778 el->state = UISTATE_NONE;
779 }
780 else
781 {
782
784 el->state = UISTATE_NONE;
785 }
786 }
787 else
788 {
789
792 }
793 }
794 else
795 {
796
797 if (event.type != SDL_MOUSEBUTTONUP && el->state == UISTATE_PRESSED && !el->action_callback.one_shot) continue;
798
799 if (el->state == UISTATE_HOVERED)
800 {
801 el->already_hovered = false;
802 }
803 el->state = UISTATE_NONE;
804
805 }
806 }
807
808 }
809 }
810
811
812}
813
814
815
816void update_ui_state(float mouse_x, float mouse_y)
817{
818
819 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
820 for (int i = 0; i < MENUS.count; i++)
821 {
822 UIMenu * m = &ms[i];
823 UIElement * els = m->elements;
824
825 for (int j = 0; j < MAX_UI_ELEMENTS; j++)
826 {
827
828 UIElement * el = &els[j];
829 if (el->free) continue;
830
831 switch(el->state)
832 {
833 case UISTATE_NONE:
834 break;
835 case UISTATE_PRESSED:
836 {
837
838 float scale_x = (float)UI_SCALE_WIDTH / TEXT_WIDTH;
839 float scale_y = (float)UI_SCALE_HEIGHT / TEXT_HEIGHT;
840 vec2 mouse = {mouse_x * scale_x, mouse_y * scale_y};
842 UISlider * slider = &el->as.slider;
843 if (slider->dragging){
844
845 vec2 delta = vec2_sub(mouse, slider->drag_mouse_start);
846 float t = vec2_dot(delta, slider->dir);
847 float new_scalar = t + slider->drag_start_scalar;
848 new_scalar = fmaxf(0.0f, fminf(slider->max_proj, new_scalar));
849 vec2 new_pos = vec2_add(slider->min, vec2_scale(slider->dir, new_scalar));
850 slider->prev_val = slider->val;
851 slider->val = new_scalar / slider->max_proj;
852 el->translation = new_pos;
853
854 } else {
855
856 slider->dragging = true;
857 vec2 from_min = vec2_sub(el->translation, slider->min);
858 float start_scalar = vec2_dot(from_min, slider->dir);
859 start_scalar = fmaxf(0.0f, fminf(slider->max_proj, start_scalar));
860 slider->prev_val = start_scalar / slider->max_proj;
861 slider->val = start_scalar / slider->max_proj;
862 slider->drag_start_scalar = start_scalar;
863 slider->drag_center_start = el->translation;
864 slider->drag_mouse_start = mouse;
865
866 }
867
868 if (slider->container != -1)
869 {
870
871 UIContainer * cs = m->containers;
872 UIContainer * c = &cs[slider->container];
873 scroll_container(c, slider, m);
874
875
876 }
877
878 }
879 else
880 {
881
883 }
884 }
885
886
887 break;
888 case UISTATE_HOVERED:
890 break;
891 default:
892 break;
893 }
894
895
896 }
897
898 }
899
900}
901
902
903
int find_empty_text_box_slot(UIMenu *m)
Finds an empty slot for a TextBox that is marked as free.
Definition fonts.c:21
void delete_text_box(int menu, int id)
Deletes a TextBox and its text and frees it from the UIMenu.
Definition fonts.c:184
Functions that modify TextBoxes and load Fonts.
uint state
Definition text.vert:40
int uID
The ID inside of UIMenu.containers.
Definition ui.h:227
vec2 drag_center_start
The translation of the UISlider on initial drag.
Definition ui.h:431
bool free
If this UIContainer is empty or can be replaced.
Definition ui.h:228
vec2 max
The max in pixel space.
Definition ui.h:410
vec2 delta_scroll_offset
The difference between this frame's scroll offset and the current.
Definition ui.h:231
int box
The TextBox index inside of the UIMenu.
Definition ui.h:492
char id[256]
The name of this TextBox.
Definition ui.h:291
uint type
Definition text.vert:41
vec2 scroll_dir
The direction this container moves in when scrolled.
Definition ui.h:232
float prev_val
The previous value.
Definition ui.h:425
float h
The height of the box after pseudo-rendering in determine_text_box_dimensions().
Definition ui.h:269
UIInteractableType interactable
The special interactable type this UIElement may be.
Definition ui.h:544
bool dragging
If we are dragging this UISlider.
Definition ui.h:429
bool already_hovered
To avoid triggering hover events multiple times if not necessary.
Definition ui.h:535
float w
The width of the box after pseudo-rendering in determine_text_box_dimensions().
Definition ui.h:268
UIValueType type
The type of value that is inside of data.
Definition ui.h:345
char id[256]
The name of this UIContainer.
Definition ui.h:245
float min_val
The minimum value of the slider.
Definition ui.h:426
int container
The UIContainer this UIElement may control.
Definition ui.h:539
union UIElement::@017317244344356306005252125357265110320354311371 as
TextBox text_boxes[1024]
The TextBoxes.
Definition ui.h:567
int container
The UIContainer that this UISlider scrolls.
Definition ui.h:454
vec2 size
Size of the SDF.
Definition text.vert:38
union UIValue::@252221077172357350267050072076221061202345253170 max_val
vec2 min
The min in pixel space.
Definition ui.h:409
float scroll_scale
How much the scroll is supposed to influence the UIContainer.
Definition ui.h:428
vec2 start
The start of the text in relation to .
Definition ui.h:284
UIElement elements[1024]
The UIElements.
Definition ui.h:566
float val
The current value between min and max val.
Definition ui.h:424
vec2 translation
X and Y coords where Y+ is down.
Definition text.vert:45
union UIValue::@033233014044001351273032017075321074265046133265 inc_val
vec2 translation
Where to place the center of the TextBox on the GPU.
Definition ui.h:278
UICallback action_callback
The event to occur on action (press or hold) or unaction.
Definition ui.h:533
int text_cap
The amount of characters allocated.
Definition ui.h:266
int parent_container
The UIContainer that contains this UIElement.
Definition ui.h:540
char id[256]
The name of this UIElement.
Definition ui.h:542
int menu
The UIMenu this UIElement is inside.
Definition ui.h:538
Justify align
How the TextBox is justified within this UIElement.
Definition ui.h:493
float len
The length in pixel space.
Definition ui.h:411
vec2 scale
Scale.
Definition text.vert:46
uint32_t layer
The layer of the UIElement.
Definition ui.h:532
UIContainer containers[1024]
The UIContainers.
Definition ui.h:568
vec2 center
The center.
Definition ui.h:527
bool one_shot
If this event only occurs on initial event.
Definition ui.h:385
uint32_t layer
Rendering sort key (lower drawn first).
Definition ui.h:287
UICallback hover_callback
The event to occur on hover or unhover.
Definition ui.h:534
int parent_container
The UIContainer that contains this TextBox.
Definition ui.h:290
uint32_t uID
The ID inside of UIMenu.text_boxes.
Definition ui.h:263
char * text
The characters to render.
Definition ui.h:265
vec2 scroll_offset
The current offset from .
Definition ui.h:230
size_t box_count
The amount of TextBoxes contained.
Definition ui.h:241
int element
Which UIElement owns this UIContainer.
Definition ui.h:243
vec2 scale
The scale for this box on the GPU.
Definition ui.h:280
vec2 drag_mouse_start
The mouse position on initial drag.
Definition ui.h:430
void(*) action_fn(void *)
The function called on action (if there is one).
Definition ui.h:387
int children[1024]
The IDs for UIElements contained.
Definition ui.h:238
size_t child_count
The amount of UIElements contained.
Definition ui.h:239
UIValueBehavior behavior
The control upon reaching min or max.
Definition ui.h:346
bool free
If this TextBox is empty and can be overwritten.
Definition ui.h:261
bool free
If the UIElement can be replaced or is empty.
Definition ui.h:512
uint32_t uID
The ID inside of UIMenu.
Definition ui.h:514
uint visible
Definition text.vert:33
vec2 dir
The direction it slides in.
Definition ui.h:408
int box_children[1024]
The IDs of TextBoxes contained.
Definition ui.h:240
UIValue val
The value to change.
Definition ui.h:386
int menu
The UIMenu that this box is within.
Definition ui.h:289
float max_proj
The maximum projected scalar value.
Definition ui.h:452
vec2 base_pos
Where the element was before it was scrolled or manipulated somehow.
Definition ui.h:522
float radius
The radius of the circle OR the radius of a rectangle's corners.
Definition text.vert:47
float drag_start_scalar
The scalar value the slider starts at based on initial drag position.
Definition ui.h:443
uint visible
Definition text.vert:7
union UIValue::@221204347253153306173144256317051244116101305363 min_val
void * data
The data, which may be simply a pointer that is passed into a function instead of manipulated directl...
Definition ui.h:372
float max_val
The maximum value of the slider.
Definition ui.h:427
int get_ui_element_from_coords(vec2 pos, int *menu_idx)
Finds a UIElement based on a screen position.
Definition ui.c:570
void assign_element_to_container(UIElement *el, UIContainer *c)
Assigns a UIElement to be parented to a UIContainer.
Definition ui.c:367
#define MAX_UI_ELEMENTS
Definition ui.h:33
#define MAX_TEXT_BOXES
Definition ui.h:37
int move_text_box_to_menu(int tb_idx, int current_menu, int target_menu)
Moves a TextBox from its current UIMenu to another UIMenu.
Definition ui.c:329
int move_element_to_menu(int element_idx, int current_menu, int target_menu)
Moves a UIElement from its current UIMenu to another UIMenu.
Definition ui.c:299
void delete_ui_element(int menu, int id)
Deletes a UIElement, removing it from its possible parent UIContainer and deleting its own possible U...
Definition ui.c:176
void remove_element_from_container(UIContainer *c, UIElement *el)
Removes a UIElement from a UIContainer.
Definition ui.c:139
int create_ui_element(int menu_id)
Creates a UIElement on a given UIMenu.
Definition ui.c:39
#define MAX_UI_CONTAINERS
Definition ui.h:34
int find_text_box_in_menu(UIMenu *m, const char *id)
Finds a TextBox inside of a UIMenu via name.
Definition ui.c:261
UICallback DEFAULT_CALLBACK
Definition ui.h:579
UIElement DEFAULT_UI_ELEMENT
Definition ui.h:593
UIContainer DEFAULT_UI_CONTAINER
Definition ui.h:642
void update_ui_state(float mouse_x, float mouse_y)
Handles the persistent UI state from previous frames.
Definition ui.c:816
void align_text_box_label(UIElement *el)
Aligns a TextBox inside of a UILabel based on its Justify.
Definition ui.c:410
Vector MENUS
All UIMenus.
Definition ui.c:13
void create_slider(UIElement *element, vec2 dir, float len)
Creates a UISlider from a UIElement.
Definition ui.c:682
void delete_ui_container(UIElement *el)
Deletes a UIContainer, removing all references to it from existing TextBoxes and UIElements on the sa...
Definition ui.c:68
int TEXT_HEIGHT
Definition ui.c:12
int find_container_in_menu(UIMenu *m, const char *id)
Finds a UIContainer inside of a UIMenu via name.
Definition ui.c:280
void remove_text_box_from_container(UIContainer *c, TextBox *tb)
Removes a TextBox from a UIContainer.
Definition ui.c:157
void reassign_elements_to_container(UIElement *el)
Assigns all children based on current UIContainer children.
Definition ui.c:114
UIContainer * create_ui_container(UIElement *el, int menu_id)
Creates a UIContainer from a given UIElement a given UIMenu.
Definition ui.c:197
#define UI_SCALE_HEIGHT
Definition ui.h:36
#define UI_SCALE_WIDTH
Definition ui.h:35
Justify
Options for Justifying TextBoxes inside of containers but also around the origin .
Definition ui.h:152
void assign_text_box_to_container(TextBox *tb, UIContainer *c)
Assigns a TextBox to be parented to a UIContainer.
Definition ui.c:388
int TEXT_WIDTH
Definition ui.c:11
int find_ui_element_in_menu(UIMenu *m, const char *id)
Finds a UIElement inside of a UIMenu via name.
Definition ui.c:242
void compute_slider_bounds(UIElement *element)
Computes the minimum and maximum in UI space for a UISlider.
Definition ui.c:663
void handle_ui_event(SDL_Event event, int mouse_x, int mouse_y)
Handles an incoming UI event.
Definition ui.c:696
vec2 screenspace_to_ui(vec2 pos)
Transfers a position to local UI space by multiplying it by a ratio of the two resolutions.
Definition ui.h:934
int get_text_box_from_coords(vec2 pos, int *menu_idx)
Finds a TextBox based on a screen position.
Definition ui.c:515
@ UISTATE_NONE
Definition ui.h:188
@ UISTATE_PRESSED
Definition ui.h:190
@ UISTATE_HOVERED
Definition ui.h:189
@ UIVAL_INT
Definition ui.h:317
@ UIVAL_CALLBACK
Definition ui.h:319
@ UIVAL_NONE
Definition ui.h:315
@ UIVAL_FLOAT
Definition ui.h:318
@ UIVAL_BOOL
Definition ui.h:316
@ UIVALBEHAVIOR_WRAP
Definition ui.h:332
@ UIVALBEHAVIOR_NONE
Definition ui.h:331
@ UIVALBEHAVIOR_CLAMP
Definition ui.h:333
@ UI_INTERACTABLE_SLIDER
UISlider that can interpolate on a plane.
Definition ui.h:201
@ JUSTIFY_RIGHT
Definition ui.h:156
@ JUSTIFY_TOP_LEFT
Definition ui.h:158
@ JUSTIFY_LEFT
Definition ui.h:155
@ JUSTIFY_BOTTOM_LEFT
Definition ui.h:161
@ JUSTIFY_TOP_RIGHT
Definition ui.h:159
@ JUSTIFY_CENTER
Definition ui.h:154
@ JUSTIFY_BOTTOM_CENTER
Definition ui.h:160
@ JUSTIFY_BOTTOM_RIGHT
Definition ui.h:162
@ JUSTIFY_TOP_CENTER
Definition ui.h:157
@ UIELEMENT_CIRCLE
Definition ui.h:303
@ UIELEMENT_RECT
Definition ui.h:303
A structure holding a Font ID and text to be rendered and transformed with its properties.
Definition text.vert:5
Describes function pointers for UIElements that determine behavior on events.
Definition ui.h:382
UIContainer that contains both TextBoxes and UIElements and can mask and scroll them.
Definition ui.h:225
UIElements that are contained within a UIMenu that may receive events, be contained within UIContaine...
Definition text.vert:31
UIElement UILabels that contain a TextBox reference.
Definition ui.h:490
The top-most UI structure that contains UIElements, TextBoxes, and UIContainers.
Definition ui.h:562
UIElement sliders that interpolate between two points on a line.
Definition ui.h:406
A value that is manipulated in some way when a UIElement receives an event.
Definition ui.h:343
float vec2_dot(vec2 a, vec2 b)
Definition mathf.h:325
#define MIN(a, b)
Definition mathf.h:435
vec2 vec2_add(vec2 a, vec2 b)
Definition mathf.h:274
vec2 vec2_scale(vec2 a, float b)
Definition mathf.h:244
vec2 vec2_sub(vec2 a, vec2 b)
Definition mathf.h:270
float lerp(float a, float b, float t)
Definition mathf.h:372
float vec2_length(vec2 v)
Definition mathf.h:336
#define MAX(a, b)
Definition mathf.h:433
float SDF_rect(vec2 p, vec2 center, vec2 size, float r)
Definition text.frag:35
float SDF_circle(vec2 p, vec2 center, float radius)
Definition text.frag:30
float x
Definition types.h:11
float y
Definition types.h:11
size_t count
Definition types.h:209
Definition types.h:11
void process_ui_event(UICallback *callback)
Definition ui.c:611
int find_empty_container_slot(UIMenu *m)
Definition ui.c:27
void create_label(UIElement *element, int box, int parent_dropdown, Justify justify)
Definition ui.c:673
vec2 screenspace_to_ui_element_local(UIElement *el, float scale_x, float scale_y, vec2 pos)
Definition ui.c:492
void scroll_container(UIContainer *c, UISlider *slider, UIMenu *m)
Definition ui.c:224
bool point_inside_ui_element(UIElement *el, vec2 pos)
Definition ui.c:470
int find_empty_element_slot(UIMenu *m)
Definition ui.c:16
bool is_hovering_ui_element(UIElement *el, vec2 pos)
Definition ui.c:506
The standalone UI library that controls text rendering, ui rendering, and events.
#define CAST_VEC(vec, type)
Definition utilities.h:134
bool vec2_inside_bounds(vec2 p, vec2 min, vec2 max)
Definition utilities.h:100
#define REMOVE_FROM_ARR(arr, index, count)
Definition utilities.h:292