ui expander working!
This commit is contained in:
@@ -42,40 +42,37 @@ fn void app_init(void) {
|
||||
fn b32 app_update(app_frame_t *frame) {
|
||||
assert(frame != NULL);
|
||||
ui_begin_frame(frame);
|
||||
defer_block(ui_begin_build(), ui_end_build()) {
|
||||
defer_block(ui_push_list_container(), ui_pop_parent()) {
|
||||
defer_if (ui_push_expander("app_event_t"), ui_pop_expander()) {
|
||||
if (r2f32_contains(r2f32(0, 0, 200, 200), frame->mouse_pos)) {
|
||||
defer_if (ui_push_expander("mouse_wheel_delta: v3f64_t"), ui_pop_expander()) {
|
||||
ui_label("x: f64 = value");
|
||||
ui_label("y: f64 = value");
|
||||
ui_label("z: f64 = value");
|
||||
}
|
||||
}
|
||||
ui_label("kind: app_event_kind_t = value");
|
||||
ui_label("ctrl: b8 = value");
|
||||
ui_label("shift: b8 = value");
|
||||
defer_if (ui_push_expander("pos: v2f64_t"), ui_pop_expander()) {
|
||||
defer_if (ui_push_expander("inner_pos: v2f64_t"), ui_pop_expander()) {
|
||||
ui_label("x_: f64 = value");
|
||||
ui_label("y_: f64 = value");
|
||||
}
|
||||
ui_label("y__: f64 = value");
|
||||
defer_if (ui_push_expander("_inner_pos: v2f64_t"), ui_pop_expander()) {
|
||||
ui_label("x____: f64 = value");
|
||||
ui_label("y____: f64 = value");
|
||||
}
|
||||
}
|
||||
ui_label("alt: b8 = value");
|
||||
ui_label("meta: b8 = value");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
assert(frame->first_event);
|
||||
for (app_event_t *ev = frame->first_event; ev; ev = ev->next) {
|
||||
if (globals->event.kind == app_event_kind_null) {
|
||||
globals->event = *ev;
|
||||
defer_block(ui_begin_build(FILE_AND_LINE, ev), ui_end_build()) {
|
||||
defer_block(ui_push_list_container(FILE_AND_LINE), ui_pop_parent()) {
|
||||
defer_if (ui_push_expander(FILE_AND_LINE, "app_event_t").clicked, ui_pop_expander()) {
|
||||
defer_if (ui_push_expander(FILE_AND_LINE, "mouse_wheel_delta: v3f64_t").clicked, ui_pop_expander()) {
|
||||
ui_label(FILE_AND_LINE, "x: f64 = value");
|
||||
ui_label(FILE_AND_LINE, "y: f64 = value");
|
||||
ui_label(FILE_AND_LINE, "z: f64 = value");
|
||||
}
|
||||
ui_label(FILE_AND_LINE, "kind: app_event_kind_t = value");
|
||||
ui_label(FILE_AND_LINE, "ctrl: b8 = value");
|
||||
ui_label(FILE_AND_LINE, "shift: b8 = value");
|
||||
defer_if (ui_push_expander(FILE_AND_LINE, "pos: v2f64_t").clicked, ui_pop_expander()) {
|
||||
defer_if (ui_push_expander(FILE_AND_LINE, "inner_pos: v2f64_t").clicked, ui_pop_expander()) {
|
||||
ui_label(FILE_AND_LINE, "x_: f64 = value");
|
||||
ui_label(FILE_AND_LINE, "y_: f64 = value");
|
||||
}
|
||||
ui_label(FILE_AND_LINE, "y__: f64 = value");
|
||||
defer_if (ui_push_expander(FILE_AND_LINE, "_inner_pos: v2f64_t").clicked, ui_pop_expander()) {
|
||||
ui_label(FILE_AND_LINE, "x____: f64 = value");
|
||||
ui_label(FILE_AND_LINE, "y____: f64 = value");
|
||||
}
|
||||
}
|
||||
ui_label(FILE_AND_LINE, "alt: b8 = value");
|
||||
ui_label(FILE_AND_LINE, "meta: b8 = value");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,9 +36,12 @@ struct ui_box_t {
|
||||
ui_size_t semantic_size[ui_axis2_count];
|
||||
b32 grow_axis[ui_axis2_count];
|
||||
|
||||
// between frames, state saving
|
||||
ui_id_t id; // all is reset every frame up to this
|
||||
u64 last_frame_touched_index;
|
||||
// debug info
|
||||
char *file_and_line;
|
||||
|
||||
// preserving state
|
||||
ui_id_t id; // important position!: offset(id) used for partial zeroing
|
||||
u64 last_touched_event_id;
|
||||
ui_box_t *hash_next;
|
||||
ui_box_t *hash_prev;
|
||||
|
||||
@@ -47,10 +50,13 @@ struct ui_box_t {
|
||||
f32 computed_rel_pos[ui_axis2_count];
|
||||
f32 computed_size[ui_axis2_count];
|
||||
r2f32_t rect;
|
||||
|
||||
// state
|
||||
b32 expanded;
|
||||
};
|
||||
|
||||
typedef struct ui_signal ui_signal;
|
||||
struct ui_signal {
|
||||
typedef struct ui_signal_t ui_signal_t;
|
||||
struct ui_signal_t {
|
||||
b8 clicked;
|
||||
b8 double_clicked;
|
||||
b8 right_clicked;
|
||||
@@ -63,6 +69,7 @@ struct ui_signal {
|
||||
typedef struct ui_t ui_t;
|
||||
struct ui_t {
|
||||
ma_arena_t *box_arena; // required to be only used for boxes
|
||||
app_event_t *event;
|
||||
app_frame_t *frame;
|
||||
|
||||
i32 allocated_boxes;
|
||||
@@ -70,17 +77,20 @@ struct ui_t {
|
||||
ui_box_t *root;
|
||||
ui_box_t *top;
|
||||
|
||||
ui_box_t *first_free;
|
||||
ui_box_t *first_hash;
|
||||
ui_box_t *last_hash;
|
||||
ui_box_t *free_first;
|
||||
ui_box_t *hash_first;
|
||||
ui_box_t *hash_last;
|
||||
|
||||
ui_id_t hot;
|
||||
ui_id_t active;
|
||||
};
|
||||
|
||||
ui_t *ui = NULL;
|
||||
ui_id_t ui_id_null;
|
||||
gb ui_t *ui = NULL;
|
||||
gb_read_only ui_id_t ui_id_null;
|
||||
|
||||
ui_box_t *ui_find_box(ui_id_t id) {
|
||||
fn ui_box_t *ui_find_box(ui_id_t id) {
|
||||
if (id.value == 0) return NULL;
|
||||
for (ui_box_t *it = ui->first_hash; it; it = it->hash_next) {
|
||||
for (ui_box_t *it = ui->hash_first; it; it = it->hash_next) {
|
||||
if (it->id.value == id.value) {
|
||||
return it;
|
||||
}
|
||||
@@ -88,10 +98,10 @@ ui_box_t *ui_find_box(ui_id_t id) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ui_box_t *ui_alloc_box(void) {
|
||||
fn ui_box_t *ui_alloc_box(void) {
|
||||
ui_box_t *result = NULL;
|
||||
if (ui->first_free) {
|
||||
SLLS_POP_AND_STORE(ui->first_free, result);
|
||||
if (ui->free_first) {
|
||||
SLLS_POP_AND_STORE(ui->free_first, result);
|
||||
} else {
|
||||
result = ma_push_type(ui->box_arena, ui_box_t);
|
||||
ui->allocated_boxes += 1;
|
||||
@@ -99,67 +109,122 @@ ui_box_t *ui_alloc_box(void) {
|
||||
return result;
|
||||
}
|
||||
|
||||
ui_box_t *ui_build_box(ui_id_t id) {
|
||||
fn ui_box_t *ui_build_box(char *file_and_line, ui_id_t id) {
|
||||
ui_box_t *box = ui_find_box(id);
|
||||
if (box) {
|
||||
memory_zero(box, offsetof(ui_box_t, id));
|
||||
} else {
|
||||
box = ui_alloc_box();
|
||||
DLLQ_APPEND_EX(ui->first_hash, ui->last_hash, box, hash_next, hash_prev);
|
||||
DLLQ_APPEND_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
||||
}
|
||||
box->last_frame_touched_index = ui->frame->frame;
|
||||
box->file_and_line = file_and_line;
|
||||
box->last_touched_event_id = ui->event->id;
|
||||
box->id = id;
|
||||
return box;
|
||||
}
|
||||
|
||||
#define ui_hot(box) (box->id.value == ui->hot.value)
|
||||
#define ui_active(box) (box->id.value == ui->active.value)
|
||||
|
||||
void ui_init(ma_arena_t *arena) {
|
||||
fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
|
||||
ui_signal_t result = {0};
|
||||
app_event_t *ev = ui->event;
|
||||
|
||||
b32 left = ev->mouse_button == app_mouse_button_left;
|
||||
b32 left_up = ev->kind == app_event_kind_mouse_up && left;
|
||||
b32 left_down = ev->kind == app_event_kind_mouse_down && left;
|
||||
|
||||
b32 move = ev->kind == app_event_kind_mouse_move;
|
||||
b32 inside = r2f32_contains(box->rect, ev->mouse_pos);
|
||||
|
||||
if (ui_active(box)) {
|
||||
if (left_up) {
|
||||
if (ui_hot(box)) result.clicked = true;
|
||||
else ui->active.value = 0;
|
||||
}
|
||||
} else if (ui_hot(box) && left_down) {
|
||||
ui->active = box->id;
|
||||
}
|
||||
|
||||
if (inside) {
|
||||
ui->hot.value = box->id.value;
|
||||
} else if (!inside && ui_hot(box)) {
|
||||
ui->hot.value = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fn void ui_init(ma_arena_t *arena) {
|
||||
assert(arena != tcx.temp);
|
||||
|
||||
ui = ma_push_type(arena, ui_t);
|
||||
ui->box_arena = arena;
|
||||
|
||||
ui->box_array = ma_push_type(arena, ui_box_t);
|
||||
SLLS_PUSH(ui->first_free, ui->box_array);
|
||||
SLLS_PUSH(ui->free_first, ui->box_array);
|
||||
}
|
||||
|
||||
void ui_begin_build(void) {
|
||||
ui->top = ui->root = ui_build_box(ui_id_null);
|
||||
fn void ui_begin_build(char *file_and_line, app_event_t *event) {
|
||||
ui->event = event;
|
||||
|
||||
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
|
||||
next = box->hash_next;
|
||||
b32 is_null = box->id.value == 0;
|
||||
if (is_null) {
|
||||
DLLQ_REMOVE_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
||||
zero_struct(box);
|
||||
SLLS_PUSH(ui->free_first, box);
|
||||
}
|
||||
}
|
||||
|
||||
ui->top = ui->root = ui_build_box(file_and_line, ui_id_null);
|
||||
}
|
||||
|
||||
void ui_end_build(void) {
|
||||
fn void ui_end_build(void) {
|
||||
assert(ui->top == ui->root);
|
||||
|
||||
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
|
||||
next = box->hash_next;
|
||||
b32 touched_box_during_update = ui->event->id <= box->last_touched_event_id;
|
||||
if (!touched_box_during_update) {
|
||||
DLLQ_REMOVE_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
||||
zero_struct(box);
|
||||
SLLS_PUSH(ui->free_first, box);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ui_begin_frame(app_frame_t *frame) {
|
||||
fn void ui_begin_frame(app_frame_t *frame) {
|
||||
ui->frame = frame;
|
||||
}
|
||||
|
||||
ui_id_t ui_id_from_string(s8_t string) {
|
||||
fn ui_id_t ui_id_from_string(s8_t string) {
|
||||
u64 value = ht_hash_data(string);
|
||||
ui_id_t id = {value};
|
||||
return id;
|
||||
}
|
||||
|
||||
void ui_push_box(ui_box_t *box) {
|
||||
fn void ui_push_box(ui_box_t *box) {
|
||||
box->parent = ui->top;
|
||||
DLLQ_APPEND(ui->top->first, ui->top->last, box);
|
||||
}
|
||||
|
||||
void ui_push_parent(ui_box_t *box) {
|
||||
fn void ui_push_parent(ui_box_t *box) {
|
||||
ui_push_box(box);
|
||||
ui->top = box;
|
||||
}
|
||||
|
||||
ui_box_t *ui_pop_parent(void) {
|
||||
fn ui_box_t *ui_pop_parent(void) {
|
||||
ui_box_t *top = ui->top;
|
||||
ui->top = ui->top->parent;
|
||||
return top;
|
||||
}
|
||||
|
||||
ui_box_t *ui_spacer(char *spacing_char) {
|
||||
fn ui_box_t *ui_spacer(char *file_and_line, char *spacing_char) {
|
||||
ui_id_t id = ui_id_null;
|
||||
ui_box_t *box = ui_build_box(id);
|
||||
ui_box_t *box = ui_build_box(file_and_line, id);
|
||||
ui_push_box(box);
|
||||
v2f32_t spacing = rn_measure_string(&rn_state.main_font, s8_from_char(spacing_char));
|
||||
box->semantic_size[0] = (ui_size_t){ui_size_kind_pixels, spacing.x, 0.5};
|
||||
@@ -167,9 +232,9 @@ ui_box_t *ui_spacer(char *spacing_char) {
|
||||
return box;
|
||||
}
|
||||
|
||||
ui_box_t *ui_push_list_container(void) {
|
||||
fn ui_box_t *ui_push_list_container(char *file_and_line) {
|
||||
ui_id_t id = ui_id_null;
|
||||
ui_box_t *box = ui_build_box(id);
|
||||
ui_box_t *box = ui_build_box(file_and_line, id);
|
||||
ui_push_parent(box);
|
||||
box->semantic_size[0] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
|
||||
box->semantic_size[1] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
|
||||
@@ -177,34 +242,42 @@ ui_box_t *ui_push_list_container(void) {
|
||||
return box;
|
||||
}
|
||||
|
||||
ui_box_t *ui_push_expander(char *str, ...) {
|
||||
fn ui_signal_t ui_push_expander(char *file_and_line, char *str, ...) {
|
||||
S8_FMT(tcx.temp, str, string);
|
||||
ui_id_t id = ui_id_from_string(string);
|
||||
ui_box_t *box = ui_build_box(id);
|
||||
ui_push_box(box);
|
||||
ui_box_t *box = ui_build_box(file_and_line, id);
|
||||
box->string = string;
|
||||
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
|
||||
box->semantic_size[1] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
|
||||
{
|
||||
ui_box_t *container = ui_push_list_container();
|
||||
container->grow_axis[0] = 1;
|
||||
container->grow_axis[1] = 0;
|
||||
ui_spacer("-");
|
||||
ui_box_t *c = ui_push_list_container();
|
||||
ui_push_box(box);
|
||||
|
||||
ui_signal_t signal = ui_signal_from_box(box);
|
||||
if (signal.clicked) {
|
||||
box->expanded = !box->expanded;
|
||||
}
|
||||
signal.clicked = box->expanded;
|
||||
|
||||
return box;
|
||||
if (signal.clicked) {
|
||||
{
|
||||
ui_box_t *container = ui_push_list_container(file_and_line);
|
||||
container->grow_axis[0] = 1;
|
||||
container->grow_axis[1] = 0;
|
||||
ui_spacer(file_and_line, "-");
|
||||
ui_box_t *c = ui_push_list_container(file_and_line);
|
||||
}
|
||||
}
|
||||
return signal;
|
||||
}
|
||||
|
||||
void ui_pop_expander() {
|
||||
fn void ui_pop_expander() {
|
||||
ui_pop_parent();
|
||||
ui_pop_parent();
|
||||
}
|
||||
|
||||
ui_box_t *ui_label(char *fmt, ...) {
|
||||
fn ui_box_t *ui_label(char *file_and_line, char *fmt, ...) {
|
||||
S8_FMT(tcx.temp, fmt, string);
|
||||
ui_id_t id = ui_id_from_string(string);
|
||||
ui_box_t *box = ui_build_box(id);
|
||||
ui_box_t *box = ui_build_box(file_and_line, id);
|
||||
ui_push_box(box);
|
||||
box->string = string;
|
||||
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
|
||||
@@ -215,17 +288,17 @@ ui_box_t *ui_label(char *fmt, ...) {
|
||||
typedef struct ui_preorder_iter_t ui_preorder_iter_t;
|
||||
struct ui_preorder_iter_t { ui_box_t *box; };
|
||||
|
||||
ui_preorder_iter_t ui_iterate_preorder(ui_box_t *box) {
|
||||
fn ui_preorder_iter_t ui_iterate_preorder(ui_box_t *box) {
|
||||
ui_preorder_iter_t iter = {box};
|
||||
return iter;
|
||||
}
|
||||
|
||||
b32 ui_preorder_iter_is_valid(ui_preorder_iter_t iter) {
|
||||
fn b32 ui_preorder_iter_is_valid(ui_preorder_iter_t iter) {
|
||||
b32 result = iter.box != NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ui_iter_advance_preorder(ui_preorder_iter_t *iter) {
|
||||
fn void ui_iter_advance_preorder(ui_preorder_iter_t *iter) {
|
||||
if (iter->box->first) {
|
||||
iter->box = iter->box->first;
|
||||
return;
|
||||
@@ -243,7 +316,7 @@ void ui_iter_advance_preorder(ui_preorder_iter_t *iter) {
|
||||
typedef struct ui_postorder_iter_t ui_postorder_iter_t;
|
||||
struct ui_postorder_iter_t { ui_box_t *box; };
|
||||
|
||||
ui_postorder_iter_t ui_iterate_postorder(ui_box_t *box) {
|
||||
fn ui_postorder_iter_t ui_iterate_postorder(ui_box_t *box) {
|
||||
while (box->first) {
|
||||
box = box->first;
|
||||
}
|
||||
@@ -251,12 +324,12 @@ ui_postorder_iter_t ui_iterate_postorder(ui_box_t *box) {
|
||||
return iter;
|
||||
}
|
||||
|
||||
b32 ui_postorder_iter_is_valid(ui_postorder_iter_t iter) {
|
||||
fn b32 ui_postorder_iter_is_valid(ui_postorder_iter_t iter) {
|
||||
b32 result = iter.box != NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ui_iter_advance_postorder(ui_postorder_iter_t *iter) {
|
||||
fn void ui_iter_advance_postorder(ui_postorder_iter_t *iter) {
|
||||
while (iter->box) {
|
||||
if (iter->box->next) {
|
||||
iter->box = iter->box->next;
|
||||
@@ -271,35 +344,25 @@ void ui_iter_advance_postorder(ui_postorder_iter_t *iter) {
|
||||
}
|
||||
}
|
||||
|
||||
void ui_test_stringify_preorder(sb8_t *sb, ui_box_t *box) {
|
||||
fn void ui_test_stringify_preorder(sb8_t *sb, ui_box_t *box) {
|
||||
sb8_printf(sb, "%S\n", box->string);
|
||||
for (ui_box_t *it = box->first; it; it = it->next) {
|
||||
ui_test_stringify_preorder(sb, it);
|
||||
}
|
||||
}
|
||||
|
||||
void ui_test_stringify_postorder(sb8_t *sb, ui_box_t *box) {
|
||||
fn void ui_test_stringify_postorder(sb8_t *sb, ui_box_t *box) {
|
||||
for (ui_box_t *it = box->first; it; it = it->next) {
|
||||
ui_test_stringify_postorder(sb, it);
|
||||
}
|
||||
sb8_printf(sb, "%S\n", box->string);
|
||||
}
|
||||
|
||||
// @todo: rename DLLQ_REMOVE_[MOD] to [EX]
|
||||
void ui_end_frame(void) {
|
||||
for (ui_box_t *box = ui->first_hash, *next = NULL; box; box = next) {
|
||||
next = box->hash_next;
|
||||
b32 touched_this_frame = ui->frame->frame <= box->last_frame_touched_index;
|
||||
b32 is_null = box->id.value == 0;
|
||||
if (!touched_this_frame || is_null) {
|
||||
DLLQ_REMOVE_EX(ui->first_hash, ui->last_hash, box, hash_next, hash_prev);
|
||||
zero_struct(box);
|
||||
SLLS_PUSH(ui->first_free, box);
|
||||
}
|
||||
}
|
||||
fn void ui_end_frame(void) {
|
||||
|
||||
}
|
||||
|
||||
void ui_draw(void) {
|
||||
fn void ui_draw(void) {
|
||||
rn_font_t *font = &rn_state.main_font;
|
||||
|
||||
{
|
||||
@@ -372,7 +435,7 @@ void ui_draw(void) {
|
||||
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
|
||||
ui_box_t *box = it.box;
|
||||
ui_box_t *parent = box->parent;
|
||||
if (box == ui->root) continue; // @todo: remove
|
||||
if (ui->root == box) continue; // @todo: how to remove this?
|
||||
|
||||
for (i32 i = 0; i < ui_axis2_count; i += 1) {
|
||||
f32 *pos = &box->computed_rel_pos[i];
|
||||
|
||||
Reference in New Issue
Block a user