create elements?

main
Patrick Cleavelin 2024-03-16 17:56:00 -05:00
parent 92aa0af44c
commit e806637b10
2 changed files with 60 additions and 3 deletions

View File

@ -23,7 +23,6 @@ void T ## PushArray(array(T) *arr, T value) {\
arr->data[arr->size] = value;\ arr->data[arr->size] = value;\
arr->size += 1;\ arr->size += 1;\
} else {\ } else {\
// TODO: resize array
fprintf(stderr, "failed to push to u8 array, size+num > capacity\n");\ fprintf(stderr, "failed to push to u8 array, size+num > capacity\n");\
}\ }\
}; };

View File

@ -3,6 +3,8 @@
#ifndef ED_UI_INCLUDED #ifndef ED_UI_INCLUDED
#define ED_UI_INCLUDED #define ED_UI_INCLUDED
#define MAX_UI_ELEMENTS 2048
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -34,7 +36,7 @@ typedef struct {
ui_axis axis; ui_axis axis;
ui_semantic_size semantic_size[2]; ui_semantic_size semantic_size[2];
uint32_t computed_size[2]; uint32_t computed_size[2];
uint32_t computed_pos[2]; uint32_t computed_pos[2];
} ui_size; } ui_size;
// UI Element data persisted across frames // UI Element data persisted across frames
@ -77,18 +79,74 @@ arrayTemplate(ui_element_frame_data);
typedef struct { typedef struct {
ed_ht cached_elements; ed_ht cached_elements;
array(ui_element_frame_data) frame_elements; array(ui_element_frame_data) frame_elements;
size_t frame_index;
size_t current_parent;
} ui_context; } ui_context;
#ifdef ED_UI_IMPLEMENTATION #ifdef ED_UI_IMPLEMENTATION
ui_context init_ui_context() { ui_context init_ui_context() {
ed_ht cached_elements = ht_create(32, sizeof(ui_element_cache_data)); ed_ht cached_elements = ht_create(MAX_UI_ELEMENTS, sizeof(ui_element_cache_data));
array(ui_element_frame_data) frame_elements = newArray(ui_element_frame_data, MAX_UI_ELEMENTS);
return (ui_context) { return (ui_context) {
.cached_elements = cached_elements, .cached_elements = cached_elements,
.frame_elements = frame_elements,
.frame_index = 0,
}; };
} }
static ui_element_frame_data *_ui_element_last(ui_context *cx, size_t parent_index) {
size_t last_index = cx->frame_elements.data[parent_index].last;
if (last_index) {
return &cx->frame_elements.data[last_index];
}
return NULL;
}
size_t ui_element(ui_context *cx, string label) {
ui_element_frame_data frame_data = (ui_element_frame_data) {
.index = cx->frame_elements.size,
// TODO: don't just set this to label, because then elements
// with the same label can't be created together
.key = label,
.label = label,
.first = 0,
.last = 0,
.next = 0,
.prev = cx->frame_elements.data[cx->current_parent].last,
.parent = cx->current_parent,
};
// Get cached element data
ui_element_cache_data *cache_data = ht_get(&cx->cached_elements, label);
if (cache_data) {
cache_data->last_instantiated_index = cx->frame_index;
frame_data.size = cache_data->size;
} else {
assert("couldn't insert into ui element cache" && ht_set(&cx->cached_elements, label, &(ui_element_cache_data) {
.label = label,
.size = { 0 },
.last_instantiated_index = cx->frame_index,
}));
}
pushArray(ui_element_frame_data, &cx->frame_elements, frame_data);
if (frame_data.prev) {
cx->frame_elements.data[frame_data.prev].next = frame_data.index;
}
if (cx->frame_elements.data[cx->current_parent].first == 0) {
cx->frame_elements.data[cx->current_parent].first = frame_data.index;
}
cx->frame_elements.data[cx->current_parent].last = frame_data.index;
}
#endif #endif
#endif #endif