diff --git a/build_file.c b/build_file.c index a1e3f63..13603b0 100644 --- a/build_file.c +++ b/build_file.c @@ -7,7 +7,9 @@ #include "src/meta/build_tool.c" #include "src/meta/parser.c" #include "src/meta/serialize.c" + #include "src/app/app.meta.c" +#include "src/wasm_app/ui.meta.c" void list_files_recursive(sb8_t *sb, s8_t path) { for (OS_FileIter iter = OS_IterateFiles(&Perm, path); OS_IsValid(iter); OS_Advance(&iter)) { @@ -24,6 +26,7 @@ int main(int argc, char **argv) { ma_arena_t *arena = ma_create(ma_default_reserve_size); meta_app(arena); + meta_ui(arena); b32 generate_math_library = false; // WARNING: be wary of this, cause it might break build file b32 execute_python_snippets = false; // make sure to not abuse just for quick maths diff --git a/src/meta/parser.c b/src/meta/parser.c index 5c2cbe6..8dce7a2 100644 --- a/src/meta/parser.c +++ b/src/meta/parser.c @@ -1,22 +1,24 @@ typedef enum { -#define AST_FLAG_XLIST \ - X(null) \ - X(string) \ - X(integer) \ - X(real) \ - X(binary) \ - X(enum) \ - X(enum_member) \ - X(struct) \ - X(struct_member) \ - X(var) \ - X(type_name) \ - X(type_pointer) \ - X(type_array) \ - X(dont_serialize) +#define AST_FLAG_XLIST \ + X(ast_flag_null) \ + X(ast_flag_string) \ + X(ast_flag_integer) \ + X(ast_flag_real) \ + X(ast_flag_binary) \ + X(ast_flag_enum) \ + X(ast_flag_enum_member) \ + X(ast_flag_struct) \ + X(ast_flag_struct_member) \ + X(ast_flag_var) \ + X(ast_flag_type_name) \ + X(ast_flag_type_pointer) \ + X(ast_flag_type_array) \ + X(ast_flag_dont_serialize) \ + X(ast_flag_flag_enum) \ -#define X(NAME) ast_flag_##NAME, + +#define X(NAME) NAME, AST_FLAG_XLIST #undef X } ast_flag_t; @@ -39,7 +41,7 @@ struct ast_t { s8_t s8_serial_ast_flag_t(ma_arena_t *arena, ast_flag_t flag) { ma_temp_t scratch = ma_begin_scratch1(arena); sb8_t *sb = sb8_serial_begin(scratch.arena); -#define X(NAME) if (flag & set_bit(ast_flag_##NAME)) sb8_printf(sb, #NAME); +#define X(NAME) if (flag & set_bit(NAME)) sb8_printf(sb, #NAME); AST_FLAG_XLIST #undef X s8_t result = sb8_serial_end(arena, sb); @@ -252,6 +254,12 @@ 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); + if (parser_match(par, lex_kind_tag) && parser_expecti(par, s8_lit("flags"))) { + n->flags |= set_bit(ast_flag_flag_enum); + } + + + matched = true; } diff --git a/src/meta/serialize.c b/src/meta/serialize.c index 66c1d3d..43af05e 100644 --- a/src/meta/serialize.c +++ b/src/meta/serialize.c @@ -46,7 +46,17 @@ s8_t s8_serial_ast(ma_arena_t *arena, ast_t *n) { s8_t s8_serial_ast_to_code(ma_arena_t *arena, ast_t *n); void sb8_serial_ast_to_code(sb8_t *sb, ast_t *n) { - if (n->flags & set_bit(ast_flag_enum)) { + if (n->flags & set_bit(ast_flag_flag_enum)) { + sb8_stmtf(sb, "typedef u32 %S;", n->string); + sb8_stmtf(sb, "enum {"); + sb->indent += 1; + int i = 0; + for (ast_t *it = n->first; it; it = it->next) { + sb8_stmtf(sb, "%S = 1 << %d,", it->string, i++); + } + sb->indent -= 1; + sb8_stmtf(sb, "};"); + } else if (n->flags & set_bit(ast_flag_enum)) { sb8_printf(sb, "typedef enum {"); sb->indent += 1; for (ast_t *it = n->first; it; it = it->next) { diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index dec5ba8..3909479 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -48,7 +48,7 @@ fn b32 app_update(app_frame_t *frame) { defer_block(ui_push_xcontainer(UI_CODE_LOC, ui_em(25), ui_em(30)), ui_pop_parent()) { - ui_box_t *item_box = ui_build_box_from_string(UI_CODE_LOC, flag3(ui_box_flag_scroll, ui_box_flag_draw_rect, ui_box_flag_draw_border), s8_lit("scrolled item_box")); + ui_box_t *item_box = ui_build_box_from_string(UI_CODE_LOC, (ui_box_flags_t){.scroll = true, .draw_rect = true}, s8_lit("scrolled item_box")); ui_set_semantic_size(item_box, ui_percent(0.97f), ui_percent(1)); item_box->grow_axis = ui_axis2_y; defer_block(ui_push_parent(item_box), ui_pop_parent()) { @@ -131,7 +131,6 @@ fn b32 app_update(app_frame_t *frame) { f32 scroller_size = CLAMP(item_box_size / all_items_size, 0, 1.0f); f32 scrollable_space = (1 - scroller_size); f32 scroller_percent = scroller_value * scrollable_space; - f32 inV_scroller_percent = 1.f / scroller_percent; f32 scroller_second = scrollable_space - scroller_percent; ui_push_container(UI_CODE_LOC, ui_percent(0.03f), ui_percent(1)); diff --git a/src/wasm_app/ui.c b/src/wasm_app/ui.c index c685d9c..1c85237 100644 --- a/src/wasm_app/ui.c +++ b/src/wasm_app/ui.c @@ -63,7 +63,7 @@ fn s8_t ui_tprint_loc(ui_code_loc_t loc) { return s8_printf(tcx.temp, "%s(%d)", loc.file, loc.line); } -fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flag_t flags, ui_id_t id) { +fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id_t id) { ui_box_t *box = ui_find_box(id); if (box) { expect (box->last_touched_event_id != ui->event->id) { @@ -83,18 +83,17 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flag_t flags, ui_id_ return box; } -fn ui_id_t ui_gen_id(ui_id_strategy_t strat, ui_code_loc_t loc, s8_t string) { - assert(strat != 0); +fn ui_id_t ui_gen_id(ui_id_flags_t flags, ui_code_loc_t loc, s8_t string) { u64 result = 42523423493; - if (is_flag_set(strat, ui_id_strategy_use_string)) { + if (flags.use_string) { u64 string_hash = hash_data(string); result = hash_mix(string_hash, result); } - if (is_flag_set(strat, ui_id_strategy_use_hierarchy)) { + if (flags.use_hierarchy) { ui_id_t top_id = ui_find_top_id(); result = hash_mix(top_id.value, result); } - if (is_flag_set(strat, ui_id_strategy_use_code_loc)) { + if (flags.use_code_loc) { u64 file_hash = hash_data(s8_from_char(loc.file)); u64 line_hash = hash_data(s8_struct(loc.line)); u64 cont_hash = hash_data(s8_struct(loc.counter)); @@ -106,8 +105,8 @@ fn ui_id_t ui_gen_id(ui_id_strategy_t strat, ui_code_loc_t loc, s8_t string) { return id; } -fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flag_t flags, s8_t string) { - ui_id_t id = ui_gen_id(ui->id_strategy, loc, ui_get_hash_string(string)); +fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s8_t string) { + ui_id_t id = ui_gen_id(ui->id_flags, loc, ui_get_hash_string(string)); ui_box_t *box = ui_build_box_from_id(loc, flags, id); box->string = ui_get_display_string(string); return box; @@ -148,7 +147,7 @@ fn void ui_init(ma_arena_t *arena) { ui = ma_push_type(arena, ui_t); ui->box_arena = arena; ui->root = ma_push_type(arena, ui_box_t); - ui->id_strategy = flag2(ui_id_strategy_use_string, ui_id_strategy_use_code_loc); + ui->id_flags = (ui_id_flags_t){.use_string = true, .use_code_loc = true}; ui->box_array = ma_push_type(arena, ui_box_t); SLLS_PUSH(ui->free_first, ui->box_array); @@ -200,21 +199,21 @@ fn ui_box_t *ui_pop_parent(void) { } fn ui_box_t *ui_spacer(ui_code_loc_t loc, ui_size_t x, ui_size_t y) { - ui_box_t *box = ui_build_box_from_id(loc, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_border), ui_null_id); + ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_rect = true, .draw_border = true}, ui_null_id); ui_set_semantic_size(box, x, y); return box; } fn ui_signal_t ui_scroller_button(ui_code_loc_t loc, ui_size_t x, ui_size_t y) { - ui_id_t id = ui_gen_id(flag2(ui_id_strategy_use_code_loc, ui_id_strategy_use_string), loc, s8_lit("spacer")); - ui_box_t *box = ui_build_box_from_id(loc, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_border), id); + ui_id_t id = ui_gen_id(ui->id_flags, loc, s8_lit("spacer")); + ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_rect = true, .draw_border = true}, id); ui_set_semantic_size(box, x, y); ui_signal_t signal = ui_signal_from_box(box); return signal; } fn ui_box_t *ui_push_container(ui_code_loc_t loc, ui_size_t x, ui_size_t y) { - ui_box_t *box = ui_build_box_from_id(loc, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_border), ui_null_id); + ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_rect = true}, ui_null_id); ui_push_parent(box); ui_set_semantic_size(box, x, y); box->grow_axis = ui_axis2_y; @@ -235,7 +234,7 @@ fn void ui_set_indented_string(ui_box_t *box, s8_t string) { box->string = s8_pr fn ui_signal_t ui_push_exp(ui_code_loc_t loc, char *str, ...) { S8_FMT(tcx.temp, str, string); - ui_box_t *box = ui_build_box_from_string(loc, flag1(ui_box_flag_draw_text), string); + ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){.draw_text = true}, string); ui_set_semantic_size(box, ui_percent(1), ui_text()); if (box->created_new) box->expanded = true; @@ -262,7 +261,7 @@ fn void ui_pop_exp(void) { fn ui_box_t *ui_label(ui_code_loc_t loc, char *fmt, ...) { S8_FMT(tcx.temp, fmt, string); - ui_box_t *box = ui_build_box_from_id(loc, flag1(ui_box_flag_draw_text), ui_null_id); + ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_text = true}, ui_null_id); ui_set_indented_string(box, string); ui_set_semantic_size(box, ui_text(), ui_text()); return box; @@ -343,7 +342,7 @@ fn void ui_layout(void) { f32 *iter_pos = &parent->iter_pos[axis]; *pos = parent_pos + *iter_pos; - if (is_flag_set(parent->flags, ui_box_flag_scroll)) { + if (parent->flags.scroll) { *pos -= parent->view_offset.e[axis]; } @@ -381,13 +380,13 @@ fn void ui_draw(void) { } rn_set_clip(clip_rect); - if (is_flag_set(box->flags, ui_box_flag_draw_rect)) { + if (box->flags.draw_rect) { rn_draw_rect(box->rect, rect_color); } - if (is_flag_set(box->flags, ui_box_flag_draw_border)) { + if (box->flags.draw_border) { rn_draw_rect_border(box->rect, accent2_color_global); } - if (is_flag_set(box->flags, ui_box_flag_draw_text)) { + if (box->flags.draw_text) { rn_draw_string(font, box->rect.min, text_color, box->string); } } diff --git a/src/wasm_app/ui.h b/src/wasm_app/ui.h index acf5afb..0830f44 100644 --- a/src/wasm_app/ui.h +++ b/src/wasm_app/ui.h @@ -46,20 +46,13 @@ struct ui_size_t { f32 strictness; }; -#define flag1(flag) set_bit(flag) -#define flag2(a, b) (set_bit(a) | set_bit(b)) -#define flag3(a, b, c) (set_bit(a) | set_bit(b) | set_bit(c)) -#define flag4(a, b, c, d) (set_bit(a) | set_bit(b) | set_bit(c) | set_bit(d)) -#define is_flag_set(x, FLAG) ((x) & flag1(FLAG)) -#define set_flag1(x, FLAG) ((x) |= flag1(FLAG)) +typedef struct ui_box_flags_t ui_box_flags_t; +struct ui_box_flags_t { + b8 draw_border : 1; + b8 draw_text : 1; + b8 draw_rect : 1; -typedef u32 ui_box_flag_t; -enum { - ui_box_flag_draw_border, - ui_box_flag_draw_text, - ui_box_flag_draw_rect, - - ui_box_flag_scroll, + b8 scroll : 1; }; typedef struct ui_box_t ui_box_t; @@ -72,7 +65,7 @@ struct ui_box_t { ui_box_t *last; i32 node_count; - ui_box_flag_t flags; + ui_box_flags_t flags; s8_t string; ui_size_t semantic_size[ui_axis2_count]; ui_axis2_t grow_axis; @@ -112,11 +105,12 @@ struct ui_signal_t { // b8 hovering; }; -typedef enum { - ui_id_strategy_use_hierarchy, - ui_id_strategy_use_code_loc, - ui_id_strategy_use_string, -} ui_id_strategy_t; +typedef struct ui_id_flags_t ui_id_flags_t; +struct ui_id_flags_t { + b8 use_hierarchy; + b8 use_code_loc; + b8 use_string; +}; typedef struct ui_t ui_t; struct ui_t { @@ -138,7 +132,8 @@ struct ui_t { // STACK(ui_id_t, 256) id_stack; int indent_stack; - ui_id_strategy_t id_strategy; + + ui_id_flags_t id_flags; }; gb ui_t *ui = NULL; diff --git a/src/wasm_app/ui.meta.c b/src/wasm_app/ui.meta.c new file mode 100644 index 0000000..2e26717 --- /dev/null +++ b/src/wasm_app/ui.meta.c @@ -0,0 +1,27 @@ +void meta_ui(ma_arena_t *arena) { + #if 0 + sb8_t *h = sb8_serial_begin(arena); + sb8_t *c = sb8_serial_begin(arena); + + ast_t *decls = parse_decls(arena, __FILE__, S8_CODE( + typedef enum { + ui_box_flag_draw_border, + ui_box_flag_draw_text, + ui_box_flag_draw_rect, + ui_box_flag_scroll, + } ui_box_flag_t; @flags + + typedef enum { + ui_id_strategy_use_hierarchy, + ui_id_strategy_use_code_loc, + ui_id_strategy_use_string, + } ui_id_strategy_t; @flags + )); + + sb8_serial_ast_to_code(h, decls); + sb8_serial_ast_to_type_info(c, decls); + + os_write_file(gen_c(arena), sb8_merge(arena, c)); + os_write_file(gen_h(arena), sb8_merge(arena, h)); + #endif +} \ No newline at end of file