diff --git a/src/app/app.gen.c b/src/app/app.gen.c index 030ec7c..91b7a37 100644 --- a/src/app/app.gen.c +++ b/src/app/app.gen.c @@ -255,18 +255,19 @@ type_t type__app_event_kind_t = { type_kind_enum, s8_const_lit("app_event_kind_t }; type_t type__app_event_t = { type_kind_struct, s8_const_lit("app_event_t"), sizeof(app_event_t), .members = (type_member_t[]){ + {.name = s8_const_lit("id"), .type = &type__u64, .offset = offsetof(app_event_t, id), .dont_serialize = 0}, {.name = s8_const_lit("next"), .type = &(type_t){type_kind_pointer, s8_const_lit("app_event_t*"), sizeof(void *), .base = &type__app_event_t}, .offset = offsetof(app_event_t, next), .dont_serialize = 0}, {.name = s8_const_lit("kind"), .type = &type__app_event_kind_t, .offset = offsetof(app_event_t, kind), .dont_serialize = 0}, {.name = s8_const_lit("mouse_button"), .type = &type__app_mouse_button_t, .offset = offsetof(app_event_t, mouse_button), .dont_serialize = 0}, {.name = s8_const_lit("key"), .type = &type__app_key_t, .offset = offsetof(app_event_t, key), .dont_serialize = 0}, {.name = s8_const_lit("text"), .type = &type__s8_t, .offset = offsetof(app_event_t, text), .dont_serialize = 0}, {.name = s8_const_lit("mouse_wheel_delta"), .type = &type__v3f32_t, .offset = offsetof(app_event_t, mouse_wheel_delta), .dont_serialize = 0}, - {.name = s8_const_lit("mouse_pos"), .type = &type__v2f32_t, .offset = offsetof(app_event_t, mouse_pos), .dont_serialize = 0}, {.name = s8_const_lit("ctrl"), .type = &type__b8, .offset = offsetof(app_event_t, ctrl), .dont_serialize = 0}, {.name = s8_const_lit("shift"), .type = &type__b8, .offset = offsetof(app_event_t, shift), .dont_serialize = 0}, {.name = s8_const_lit("alt"), .type = &type__b8, .offset = offsetof(app_event_t, alt), .dont_serialize = 0}, + {.name = s8_const_lit("mouse_pos"), .type = &type__v2f32_t, .offset = offsetof(app_event_t, mouse_pos), .dont_serialize = 0}, }, - .count = 10, + .count = 11, }; type_t type__app_frame_t = { type_kind_struct, s8_const_lit("app_frame_t"), sizeof(app_frame_t), .members = (type_member_t[]){ diff --git a/src/app/app.gen.h b/src/app/app.gen.h index c829e0e..605b8cb 100644 --- a/src/app/app.gen.h +++ b/src/app/app.gen.h @@ -93,16 +93,17 @@ typedef enum { typedef struct app_event_t app_event_t; struct app_event_t { + u64 id; app_event_t* next; app_event_kind_t kind; app_mouse_button_t mouse_button; app_key_t key; s8_t text; v3f32_t mouse_wheel_delta; - v2f32_t mouse_pos; b8 ctrl; b8 shift; b8 alt; + v2f32_t mouse_pos; }; typedef struct app_frame_t app_frame_t; diff --git a/src/app/app.meta.c b/src/app/app.meta.c index 655fe55..b7b11d4 100644 --- a/src/app/app.meta.c +++ b/src/app/app.meta.c @@ -158,7 +158,9 @@ void meta_app(ma_arena_t *arena) { app_event_kind_count, } app_event_kind_t; + typedef struct app_event_t app_event_t; struct app_event_t { + u64 id; app_event_t *next; app_event_kind_t kind; @@ -167,14 +169,15 @@ void meta_app(ma_arena_t *arena) { app_key_t key; // @key_down @key_up s8_t text; // @text v3f32_t mouse_wheel_delta; // @mouse_wheel - v2f32_t mouse_pos; // @mouse_move // always present data b8 ctrl; b8 shift; b8 alt; + v2f32_t mouse_pos; }; + typedef struct app_frame_t app_frame_t; struct app_frame_t { app_frame_t *next; f64 delta; diff --git a/src/app/app_win32.c b/src/app/app_win32.c index 62baabc..7ab2f42 100644 --- a/src/app/app_win32.c +++ b/src/app/app_win32.c @@ -59,10 +59,13 @@ fn f64 w32_seconds_now(void) { gb app_frame_t *w32_frame; gb ma_arena_t *w32_event_arena; +gb u64 w32_event_id; fn void w32_push_event(app_frame_t *frame, app_event_t event) { app_event_t *ev = ma_push_type(w32_event_arena, app_event_t); *ev = event; + ev->id = ++w32_event_id; + ev->mouse_pos = w32_get_mouse_pos(w32_window_handle); if (GetKeyState(VK_CONTROL) & 0x8000) ev->ctrl = true; if (GetKeyState(VK_SHIFT) & 0x8000) ev->shift = true; @@ -156,6 +159,11 @@ fn LRESULT CALLBACK w32_window_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lp }); } break; + // case WM_MOUSEMOVE: { + // w32_push_event(w32_frame, (app_event_t){ + // .kind = app_event_kind_mouse_move, + // }); + // } break; case WM_MOUSEWHEEL: { int zDelta = GET_WHEEL_DELTA_WPARAM(wparam); w32_push_event(w32_frame, (app_event_t){ diff --git a/src/meta/parser.c b/src/meta/parser.c index ab29c68..5c2cbe6 100644 --- a/src/meta/parser.c +++ b/src/meta/parser.c @@ -228,7 +228,11 @@ ast_t *parse_decls(ma_arena_t *arena, char *file, s8_t code) { result->string = s8_copy_char(arena, file); for (;par->at->kind != lex_kind_eof;) { lex_t *pos = par->at; - if (parser_matchi(par, s8_lit("enum"))) { + + b32 matched = false; + b32 is_typedef = !!parser_matchi(par, s8_lit("typedef")); + + if (is_typedef && parser_matchi(par, s8_lit("enum"))) { ast_t *n = create_ast(par, pos, set_bit(ast_flag_string) | set_bit(ast_flag_enum)); ast_append(result, n); @@ -248,7 +252,17 @@ ast_t *parse_decls(ma_arena_t *arena, char *file, s8_t code) { parser_expect(par, lex_kind_close_brace); n->string = parser_expect(par, lex_kind_ident)->string; parser_expect(par, lex_kind_semicolon); - } else if (parser_matchi(par, s8_lit("struct"))) { + matched = true; + } + + if (is_typedef && parser_matchi(par, s8_lit("struct"))) { + parser_match(par, lex_kind_ident); + parser_match(par, lex_kind_ident); + parser_match(par, lex_kind_semicolon); + matched = true; + } + + if (parser_matchi(par, s8_lit("struct"))) { ast_t *n = create_ast(par, pos, set_bit(ast_flag_string) | set_bit(ast_flag_struct)); ast_append(result, n); n->string = parser_expect(par, lex_kind_ident)->string; @@ -270,10 +284,10 @@ ast_t *parse_decls(ma_arena_t *arena, char *file, s8_t code) { } parser_expect(par, lex_kind_close_brace); parser_expect(par, lex_kind_semicolon); - - } else { - parser_next(par); + matched = true; } + + if (!matched) parser_next(par); } return result; diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index edc5887..7c4d050 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -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"); + } + } } } diff --git a/src/wasm_app/ui.c b/src/wasm_app/ui.c index 29e6de3..2878244 100644 --- a/src/wasm_app/ui.c +++ b/src/wasm_app/ui.c @@ -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];