create elements?
parent
92aa0af44c
commit
e806637b10
|
@ -23,7 +23,6 @@ void T ## PushArray(array(T) *arr, T value) {\
|
|||
arr->data[arr->size] = value;\
|
||||
arr->size += 1;\
|
||||
} else {\
|
||||
// TODO: resize array
|
||||
fprintf(stderr, "failed to push to u8 array, size+num > capacity\n");\
|
||||
}\
|
||||
};
|
||||
|
|
60
src/ui.h
60
src/ui.h
|
@ -3,6 +3,8 @@
|
|||
#ifndef ED_UI_INCLUDED
|
||||
#define ED_UI_INCLUDED
|
||||
|
||||
#define MAX_UI_ELEMENTS 2048
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -77,18 +79,74 @@ arrayTemplate(ui_element_frame_data);
|
|||
typedef struct {
|
||||
ed_ht cached_elements;
|
||||
array(ui_element_frame_data) frame_elements;
|
||||
|
||||
size_t frame_index;
|
||||
|
||||
size_t current_parent;
|
||||
} ui_context;
|
||||
|
||||
|
||||
#ifdef ED_UI_IMPLEMENTATION
|
||||
|
||||
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) {
|
||||
.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
|
||||
|
|
Loading…
Reference in New Issue