diff --git a/build_file.c b/build_file.c index a53a68b..9c0fcec 100644 --- a/build_file.c +++ b/build_file.c @@ -11,23 +11,29 @@ #include "src/meta/cgen.c" #include "src/app/app.meta.c" +#include "src/ui/ui.meta.c" #include "src/wasm_app/wasm_app.meta.c" int main(int argc, char **argv) { + SRC_SearchPaths.include_path = (char*[]){OS_GetAbsolutePath(&Perm, s8_lit("../src")).str}; + SRC_SearchPaths.include_path_count = 1; cache_init(&Perm, s8_lit("cache_build_file")); + ma_arena_t *arena = ma_create(ma_default_reserve_size); int ok = 0; - ma_arena_t *arena = ma_create(ma_default_reserve_size); - if (cache_code_modified(s8_lit("../src/core/core_math_gen.py"), s8_null)) { + b32 generate_math = true; + + if (generate_math && cache_code_modified(s8_lit("../src/core/core_math_gen.py"), s8_null)) { os_set_working_dir("../src/core"); os_systemf("py core_math_gen.py"); os_set_working_dir("../../build"); } + mt_ui(arena); mt_app(arena); mt_wasm_app(arena); b32 run_server = false; - b32 core_test_target = false; + b32 core_test_target = true; b32 win32_target = true; b32 wasm_target = false; @@ -35,7 +41,7 @@ int main(int argc, char **argv) { os_systemf("start /D ..\\package ..\\package\\run_server.bat"); } - if (win32_target) { + if (win32_target && cache_code_modified(s8_lit("../src/wasm_app/main.c"), s8_lit("app_win32.exe"))) { os_delete_file(s8_lit("app_win32.pdb")); ok = os_systemf( "cl ../src/wasm_app/main.c -Fe:app_win32.exe -Fd:app_win32.pdb" @@ -47,7 +53,7 @@ int main(int argc, char **argv) { if (ok != 0) return ok; } - if (core_test_target) { + if (core_test_target && cache_code_modified(s8_lit("../src/core/core_test_entry.c"), s8_lit("core_test.exe"))) { os_delete_file(s8_lit("core_test.pdb")); ok = os_systemf( "cl ../src/core/core_test_entry.c -Fe:core_test.exe -Fd:core_test.pdb" @@ -59,11 +65,11 @@ int main(int argc, char **argv) { if (ok != 0) return ok; ok = os_systemf("core_test.exe"); - return ok; + if (ok != 0) return ok; } - if (wasm_target) { + if (wasm_target && cache_code_modified(s8_lit("../src/wasm_app/main.c"), s8_lit("main.wasm"))) { os_copy("../src/app/app_wasm.html", "../package/index.html", os_copy_overwrite); ok = os_systemf( "clang.exe ../src/wasm_app/main.c -o main.wasm" diff --git a/src/app/app.meta.c b/src/app/app.meta.c index 979f58c..5778697 100644 --- a/src/app/app.meta.c +++ b/src/app/app.meta.c @@ -199,6 +199,6 @@ void mt_app(ma_arena_t *arena) { sb8_serial_ast_to_type_info(c, decls); } - os_write_file(cg_cpath(arena), sb8_merge(arena, c)); - os_write_file(cg_hpath(arena), sb8_merge(arena, h)); + os_write_file(cg_cpath(arena), sb8_serial_end(arena, c)); + os_write_file(cg_hpath(arena), sb8_serial_end(arena, h)); } \ No newline at end of file diff --git a/src/core/core.h b/src/core/core.h index d5ff668..b52cd25 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -170,6 +170,7 @@ typedef double f64; #define SLLS_PUSH(stack_base, new_stack_base) \ SLLS_PUSH_EX(stack_base, new_stack_base, next) +#define SLLS_POP(stack_base) ((stack_base) = (stack_base)->next) #define SLLS_POP_AND_STORE(stack_base, out_node) \ do { \ if (stack_base) { \ diff --git a/src/core/core_math.gen.c b/src/core/core_math.gen.c index 616a672..d7ff30e 100644 --- a/src/core/core_math.gen.c +++ b/src/core/core_math.gen.c @@ -359,6 +359,7 @@ r2f32_t r2f32_get_bottom(const r2f32_t *r, f32 value) { /* Y is down */ } r2f32_t r2f32_shrink(r2f32_t rect, v2f32_t value) { return (r2f32_t){ v2f32_add(rect.min, value), v2f32_sub(rect.max, value) }; } +r2f32_t r2f32_shrinks(r2f32_t rect, f32 value) { return (r2f32_t){ v2f32_adds(rect.min, value), v2f32_subs(rect.max, value) }; } v2f32_t r2f32_get_size(r2f32_t r) { return (v2f32_t){r.max.x - r.min.x, r.max.y - r.min.y}; } v2f32_t r2f32_get_mid(r2f32_t r) { return v2f32_add(r.min, v2f32_divs(r2f32_get_size(r), 2)); } b32 r2f32_contains(r2f32_t rec, v2f32_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); } @@ -469,6 +470,7 @@ r2f64_t r2f64_get_bottom(const r2f64_t *r, f64 value) { /* Y is down */ } r2f64_t r2f64_shrink(r2f64_t rect, v2f64_t value) { return (r2f64_t){ v2f64_add(rect.min, value), v2f64_sub(rect.max, value) }; } +r2f64_t r2f64_shrinks(r2f64_t rect, f64 value) { return (r2f64_t){ v2f64_adds(rect.min, value), v2f64_subs(rect.max, value) }; } v2f64_t r2f64_get_size(r2f64_t r) { return (v2f64_t){r.max.x - r.min.x, r.max.y - r.min.y}; } v2f64_t r2f64_get_mid(r2f64_t r) { return v2f64_add(r.min, v2f64_divs(r2f64_get_size(r), 2)); } b32 r2f64_contains(r2f64_t rec, v2f64_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); } @@ -579,6 +581,7 @@ r2i32_t r2i32_get_bottom(const r2i32_t *r, i32 value) { /* Y is down */ } r2i32_t r2i32_shrink(r2i32_t rect, v2i32_t value) { return (r2i32_t){ v2i32_add(rect.min, value), v2i32_sub(rect.max, value) }; } +r2i32_t r2i32_shrinks(r2i32_t rect, i32 value) { return (r2i32_t){ v2i32_adds(rect.min, value), v2i32_subs(rect.max, value) }; } v2i32_t r2i32_get_size(r2i32_t r) { return (v2i32_t){r.max.x - r.min.x, r.max.y - r.min.y}; } v2i32_t r2i32_get_mid(r2i32_t r) { return v2i32_add(r.min, v2i32_divs(r2i32_get_size(r), 2)); } b32 r2i32_contains(r2i32_t rec, v2i32_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); } @@ -689,6 +692,7 @@ r2i64_t r2i64_get_bottom(const r2i64_t *r, i64 value) { /* Y is down */ } r2i64_t r2i64_shrink(r2i64_t rect, v2i64_t value) { return (r2i64_t){ v2i64_add(rect.min, value), v2i64_sub(rect.max, value) }; } +r2i64_t r2i64_shrinks(r2i64_t rect, i64 value) { return (r2i64_t){ v2i64_adds(rect.min, value), v2i64_subs(rect.max, value) }; } v2i64_t r2i64_get_size(r2i64_t r) { return (v2i64_t){r.max.x - r.min.x, r.max.y - r.min.y}; } v2i64_t r2i64_get_mid(r2i64_t r) { return v2i64_add(r.min, v2i64_divs(r2i64_get_size(r), 2)); } b32 r2i64_contains(r2i64_t rec, v2i64_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); } @@ -755,3 +759,19 @@ v3i32_t v3i64_to_v3i32(v3i64_t v) { return (v3i32_t){ (i32)v.x, (i32)v.y, (i32)v v4i32_t v4i64_to_v4i32(v4i64_t v) { return (v4i32_t){ (i32)v.x, (i32)v.y, (i32)v.z, (i32)v.w }; } r2i32_t r2i64_to_r2i32(r2i64_t v) { return (r2i32_t){ (i32)v.x0, (i32)v.y0, (i32)v.x1, (i32)v.y1 }; } r3i32_t r3i64_to_r3i32(r3i64_t v) { return (r3i32_t){ (i32)v.x0, (i32)v.y0, (i32)v.z0, (i32)v.x1, (i32)v.y1, (i32)v.z1 }; } +r2f32_t r2f32_add_v2f32(r2f32_t a, v2f32_t b) { return (r2f32_t){ v2f32_add(a.min, b), v2f32_add(a.max, b) }; } +r2f32_t r2f32_sub_v2f32(r2f32_t a, v2f32_t b) { return (r2f32_t){ v2f32_sub(a.min, b), v2f32_sub(a.max, b) }; } +r2f32_t r2f32_mul_v2f32(r2f32_t a, v2f32_t b) { return (r2f32_t){ v2f32_mul(a.min, b), v2f32_mul(a.max, b) }; } +r2f32_t r2f32_div_v2f32(r2f32_t a, v2f32_t b) { return (r2f32_t){ v2f32_div(a.min, b), v2f32_div(a.max, b) }; } +r2f64_t r2f64_add_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_add(a.min, b), v2f64_add(a.max, b) }; } +r2f64_t r2f64_sub_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_sub(a.min, b), v2f64_sub(a.max, b) }; } +r2f64_t r2f64_mul_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_mul(a.min, b), v2f64_mul(a.max, b) }; } +r2f64_t r2f64_div_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_div(a.min, b), v2f64_div(a.max, b) }; } +r2i32_t r2i32_add_v2i32(r2i32_t a, v2i32_t b) { return (r2i32_t){ v2i32_add(a.min, b), v2i32_add(a.max, b) }; } +r2i32_t r2i32_sub_v2i32(r2i32_t a, v2i32_t b) { return (r2i32_t){ v2i32_sub(a.min, b), v2i32_sub(a.max, b) }; } +r2i32_t r2i32_mul_v2i32(r2i32_t a, v2i32_t b) { return (r2i32_t){ v2i32_mul(a.min, b), v2i32_mul(a.max, b) }; } +r2i32_t r2i32_div_v2i32(r2i32_t a, v2i32_t b) { return (r2i32_t){ v2i32_div(a.min, b), v2i32_div(a.max, b) }; } +r2i64_t r2i64_add_v2i64(r2i64_t a, v2i64_t b) { return (r2i64_t){ v2i64_add(a.min, b), v2i64_add(a.max, b) }; } +r2i64_t r2i64_sub_v2i64(r2i64_t a, v2i64_t b) { return (r2i64_t){ v2i64_sub(a.min, b), v2i64_sub(a.max, b) }; } +r2i64_t r2i64_mul_v2i64(r2i64_t a, v2i64_t b) { return (r2i64_t){ v2i64_mul(a.min, b), v2i64_mul(a.max, b) }; } +r2i64_t r2i64_div_v2i64(r2i64_t a, v2i64_t b) { return (r2i64_t){ v2i64_div(a.min, b), v2i64_div(a.max, b) }; } diff --git a/src/core/core_math_gen.py b/src/core/core_math_gen.py index cb57032..3892093 100644 --- a/src/core/core_math_gen.py +++ b/src/core/core_math_gen.py @@ -307,6 +307,7 @@ r2f64_t r2f64_get_bottom(const r2f64_t *r, f64 value) { /* Y is down */ } r2f64_t r2f64_shrink(r2f64_t rect, v2f64_t value) { return (r2f64_t){ v2f64_add(rect.min, value), v2f64_sub(rect.max, value) }; } +r2f64_t r2f64_shrinks(r2f64_t rect, f64 value) { return (r2f64_t){ v2f64_adds(rect.min, value), v2f64_subs(rect.max, value) }; } v2f64_t r2f64_get_size(r2f64_t r) { return (v2f64_t){r.max.x - r.min.x, r.max.y - r.min.y}; } v2f64_t r2f64_get_mid(r2f64_t r) { return v2f64_add(r.min, v2f64_divs(r2f64_get_size(r), 2)); } b32 r2f64_contains(r2f64_t rec, v2f64_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); } @@ -334,4 +335,17 @@ for at in basic_types: result += ", " return result - print(f"{btype}_t {atype}_to_{btype}({atype}_t v) {{ return ({btype}_t){{ {cast_members(members, bt)} }}; }}") \ No newline at end of file + print(f"{btype}_t {atype}_to_{btype}({atype}_t v) {{ return ({btype}_t){{ {cast_members(members, bt)} }}; }}") + + +####### +# generate r2f64_sub_v2f64 for +-*/ and types +for bt in basic_types: + for sym in symbols: + word_op = sym[1] + s = """ +r2f64_t r2f64_sub_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_sub(a.min, b), v2f64_sub(a.max, b) }; } +""".strip() + s = s.replace("f64", bt) + s = s.replace("sub", word_op) + print(s) \ No newline at end of file diff --git a/src/meta/build_tool.c b/src/meta/build_tool.c index f4400d6..944f18e 100644 --- a/src/meta/build_tool.c +++ b/src/meta/build_tool.c @@ -5031,6 +5031,8 @@ SRC_CacheEntry *cache_hash_file(S8_String file, char *parent_file) { if (token.is_system_include) continue; S8_String file_it = S8_MakeFromChar(token.string_literal); + if (s8_are_equal(file_it, file)) continue; // prevent recursing, file includes itself (stb_truetype) + SRC_CacheEntry *cache = cache_hash_file(file_it, resolved_file); if (!cache) { // error was reported already IO_Printf("Missing cache for: %.*s\n", S8_Expand(file_it)); diff --git a/src/render/render_opengl.c b/src/render/render_opengl.c index 20476c0..ae8bb04 100644 --- a/src/render/render_opengl.c +++ b/src/render/render_opengl.c @@ -147,11 +147,11 @@ fn void rn_draw_rect(r2f32_t rect, v4f32_t color) { rn_push_quad(rect, rn_state.main_font->white_texture_bounding_box, color); } -fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color) { - r2f32_t left = r2f32_cut_left(&rect, 1); - r2f32_t right = r2f32_cut_right(&rect, 1); - r2f32_t top = r2f32_cut_top(&rect, 1); - r2f32_t bottom = r2f32_cut_bottom(&rect, 1); +fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) { + r2f32_t left = r2f32_cut_left(&rect, border_thickness); + r2f32_t right = r2f32_cut_right(&rect, border_thickness); + r2f32_t top = r2f32_cut_top(&rect, border_thickness); + r2f32_t bottom = r2f32_cut_bottom(&rect, border_thickness); rn_push_quad(left, rn_state.main_font->white_texture_bounding_box, color); rn_push_quad(right, rn_state.main_font->white_texture_bounding_box, color); rn_push_quad(top, rn_state.main_font->white_texture_bounding_box, color); diff --git a/src/ui/ui.c b/src/ui/ui.c index 468ebf3..55b6002 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -41,8 +41,8 @@ fn ui_id_t ui_id_from_code_loc(ui_code_loc_t loc) { fn u64 ui_hash_from_stack(void) { u64 result = 34294923; - for (i32 i = 0; i < ui->id_stack.len; i += 1) { - result = hash_mix(result, ui->id_stack.data[i].value); + for (ui_id_node_t *it = ui->id_stack; it; it = it->next) { + result = hash_mix(result, it->value.value); } return result; } @@ -60,11 +60,10 @@ fn ui_id_t ui_idf(char *str, ...) { return ui_id(string); } -fn void ui_push_id(ui_id_t id) { STACK_PUSH(ui->id_stack, id); } -fn void ui_pop_id(void) { STACK_POP(ui->id_stack); } fn void ui_push_id_string(s8_t string) { ui_push_id(ui_id(string)); } fn void ui_push_top(ui_box_t *new_top) { assert(new_top->parent == ui->top); ui->top = new_top; ui_push_id(new_top->id); } fn void ui_pop_top(void) { ui->top = ui->top->parent; ui_pop_id(); } +#define ui_set_top(x) defer_block(ui_push_top(x), ui_pop_top()) fn ui_box_t *ui_alloc_box(void) { ui_box_t *box = NULL; @@ -93,18 +92,23 @@ fn void ui_push_box(ui_box_t *parent, ui_box_t *box) { parent->node_count += 1; } -fn r2f32_t ui_next_rect(ui_op_t op, r2f32_t *rect, v2f32_t required_size) { +fn r2f32_t ui_next_rect(ui_lop_t op, r2f32_t *rect, v2f32_t required_size) { switch (op) { - case ui_op_cut_top: return r2f32_cut_top_no_squash(rect, required_size.y); break; - case ui_op_cut_bottom: return r2f32_cut_bottom_no_squash(rect, required_size.y); break; - case ui_op_cut_left: return r2f32_cut_left_no_squash(rect, required_size.x); break; - case ui_op_cut_right: return r2f32_cut_right_no_squash(rect, required_size.x); break; - case ui_op_idle: break; + case ui_lop_cut_top: return r2f32_cut_top_no_squash(rect, required_size.y); break; + case ui_lop_cut_bottom: return r2f32_cut_bottom_no_squash(rect, required_size.y); break; + case ui_lop_cut_left: return r2f32_cut_left_no_squash(rect, required_size.x); break; + case ui_lop_cut_right: return r2f32_cut_right_no_squash(rect, required_size.x); break; + case ui_lop_idle: break; default_is_invalid; } return (r2f32_t){0}; } +fn void ui_offset_box(ui_box_t *box, v2f32_t offset) { + box->full_rect = r2f32_sub_v2f32(box->full_rect, offset); + box->rect = r2f32_sub_v2f32(box->rect, offset); +} + 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) { @@ -121,7 +125,8 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id box->last_touched_event_id = ui->event->id; box->id = id; box->flags = flags; - box->text_align = ui->pref_text_align; + box->text_align = ui->text_align_stack->value; + box->border_thickness = ui->border_thickness_stack->value; ui_push_box(ui->top, box); return box; } @@ -131,19 +136,30 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s ui_box_t *box = ui_build_box_from_id(loc, flags, id); box->string = ui_get_display_string(string); v2f32_t string_size = rn_measure_string(rn_state.main_font, box->string); - string_size.x += ui_em(1); - r2f32_t rect = ui_next_rect(ui->top->op, &ui->top->rect, string_size); + if (ui->required_width_stack) string_size.x = ui->required_width_stack->value; + if (ui->required_height_stack) string_size.y = ui->required_height_stack->value; + if (ui->padding_stack) string_size = v2f32_add(string_size, ui->padding_stack->value); + r2f32_t rect = ui_next_rect(ui->lop_stack->value, &ui->top->rect, string_size); ui_set_rect(box, rect); return box; } -#define ui_boxf(flags, ...) ui__boxf(UILOC, flags, __VA_ARGS__) +#define ui_boxf(...) ui__boxf(UILOC, __VA_ARGS__) fn ui_box_t *ui__boxf(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...) { S8_FMT(tcx.temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, flags, string); return box; } +#define ui_box0(...) ui_build_box_from_id(UILOC, __VA_ARGS__, ui_null_id) +#define ui_box0f(...) ui__box0f(UILOC, __VA_ARGS__) +fn ui_box_t *ui__box0f(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...) { + S8_FMT(tcx.temp, str, string); + ui_box_t *box = ui_build_box_from_id(loc, flags, ui_null_id); + box->string = string; + return box; +} + fn ui_signal_t ui_signal_from_box(ui_box_t *box) { ui_signal_t result = {box}; app_event_t *ev = ui->event; @@ -181,6 +197,14 @@ fn ui_signal_t ui__button(ui_code_loc_t loc, char *str, ...) { return signal; } +#define ui_label_button(...) ui__label_button(UILOC, __VA_ARGS__) +fn ui_signal_t ui__label_button(ui_code_loc_t loc, char *str, ...) { + S8_FMT(tcx.temp, str, string); + ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_text = true }, string); + ui_signal_t signal = ui_signal_from_box(box); + return signal; +} + #define ui_label(...) ui__label(UILOC, __VA_ARGS__) fn ui_box_t *ui__label(ui_code_loc_t loc, char *str, ...) { S8_FMT(tcx.temp, str, string); @@ -188,6 +212,7 @@ fn ui_box_t *ui__label(ui_code_loc_t loc, char *str, ...) { return box; } +#define ui_expander(...) defer_if (ui_begin_expander(__VA_ARGS__).clicked, ui_end_expander()) #define ui_begin_expander(...) ui__begin_expander(UILOC, __VA_ARGS__) fn ui_signal_t ui__begin_expander(ui_code_loc_t loc, char *str, ...) { S8_FMT(tcx.temp, str, string); @@ -222,15 +247,26 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect) } } + ui_push_lop(ui_lop_cut_top); + ui_push_id(ui_idf("root")); + ui_push_border_thickness(1.0f); + ui_push_text_align(ui_text_align_left); + zero_struct(&ui->root); ui->root.full_rect = ui->root.rect = window_rect; ui->top = &ui->root; ui->root.loc = loc; + + } fn void ui_end_build(void) { assert(ui->top == &ui->root); + SLLS_POP(ui->id_stack); + SLLS_POP(ui->border_thickness_stack); + SLLS_POP(ui->text_align_stack); + 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; @@ -266,7 +302,7 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) { rn_draw_rect(rect, rect_color); } if (box->flags.draw_border) { - rn_draw_rect_border(rect, accent2_color_global); + rn_draw_rect_border(rect, accent2_color_global, box->border_thickness); } if (box->flags.draw_text) { v2f32_t string_size = rn_measure_string(rn_state.main_font, box->string); @@ -295,15 +331,22 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) { } } -fn void ui_draw(app_frame_t *frame) { - ui->clip_rect = window_rect_from_frame(frame); +fn void ui_draw(void) { + ui->clip_rect = window_rect_from_frame(ui->frame); rn_set_clip(ui->clip_rect); - ui__draw_box(frame, &ui->root); + ui__draw_box(ui->frame, &ui->root); } -fn void ui_demo_init(ma_arena_t *arena) { - ui = ma_push_type(arena, ui_t); - ui->box_arena = ma_push_arena(arena, mib(1)); +fn void ui_begin_frame(app_frame_t *frame) { + ui->frame = frame; +} + +fn void ui_end_frame(void) { + for (app_event_t *ev = ui->frame->first_event; ev; ev = ev->next) { + if (ev_left_up(ev)) { + ui->active = ui_null_id; + } + } } fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { @@ -312,7 +355,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { switch(type->kind) { case type_kind_i8: { i8 *n = (i8 *)p; - if (ui_button("%S: %d##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i8)delta; } @@ -320,7 +363,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_i16: { i16 *n = (i16 *)p; - if (ui_button("%S: %d##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i16)delta; } @@ -328,7 +371,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_i32: { i32 *n = (i32 *)p; - if (ui_button("%S: %d##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i32)delta; } @@ -336,7 +379,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_i64: { i64 *n = (i64 *)p; - if (ui_button("%S: %lld##slider%S", name, (long long)*n, name).dragging) { + if (ui_label_button("%S: %lld##slider%S", name, (long long)*n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i64)delta; } @@ -344,7 +387,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_u8: { u8 *n = (u8 *)p; - if (ui_button("%S: %u##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %u##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u8)delta; } @@ -352,7 +395,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_u16: { u16 *n = (u16 *)p; - if (ui_button("%S: %u##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %u##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u16)delta; } @@ -360,7 +403,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_u32: { u32 *n = (u32 *)p; - if (ui_button("%S: %u##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %u##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u32)delta; } @@ -368,7 +411,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_u64: { u64 *n = (u64 *)p; - if (ui_button("%S: %llu##slider%S", name, (unsigned long long)*n, name).dragging) { + if (ui_label_button("%S: %llu##slider%S", name, (unsigned long long)*n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u64)delta; } @@ -376,35 +419,35 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_b8: { b8 *n = (b8 *)p; - if (ui_button("%S: %s", name, *n ? "true" : "false").clicked) { + if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_b16: { b16 *n = (b16 *)p; - if (ui_button("%S: %s", name, *n ? "true" : "false").clicked) { + if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_b32: { b32 *n = (b32 *)p; - if (ui_button("%S: %s", name, *n ? "true" : "false").clicked) { + if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_b64: { b64 *n = (b64 *)p; - if (ui_button("%S: %s", name, *n ? "true" : "false").clicked) { + if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_f32: { f32 *n = (f32 *)p; - if (ui_button("%S: %f##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %f##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += delta; } @@ -412,7 +455,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_f64: { f64 *n = (f64 *)p; - if (ui_button("%S: %f##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %f##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += delta; } @@ -420,7 +463,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_isize: { isize *n = (isize *)p; - if (ui_button("%S: %lld##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %lld##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (isize)delta; } @@ -428,7 +471,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_usize: { usize *n = (usize *)p; - if (ui_button("%S: %llu##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %llu##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (usize)delta; } @@ -436,7 +479,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_int: { int *n = (int *)p; - if (ui_button("%S: %d##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (int)delta; } @@ -444,7 +487,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } break; case type_kind_char: { char *n = (char *)p; - if (ui_button("%S: %c##slider%S", name, *n, name).dragging) { + if (ui_label_button("%S: %c##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (char)delta; } @@ -460,7 +503,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { if (type->kind == type_kind_enum) { i64 value = ti_enum_read_value(p, type); s8_t string_value = ti_enum_value_to_name(value, type); - if (ui_button("%S: %S = %S", name, type->name, string_value).clicked) { + if (ui_label_button("%S: %S = %S", name, type->name, string_value).clicked) { i64 new_value = ti_enum_next_value(p, value, type); ti_enum_write_value(p, new_value, type); } @@ -468,7 +511,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } if (type->kind == type_kind_array) { - defer_if (ui_begin_expander("%S:", name).clicked, ui_end_expander()) { + ui_expander("%S:", name) { for (i32 i = 0; i < type->count; i += 1) { ma_temp_t scratch = ma_begin_scratch(); s8_t index_name = s8_printf(scratch.arena, "[%d]", i); @@ -481,7 +524,7 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { } if (type->kind == type_kind_struct) { - defer_if (ui_begin_expander("%S:", name).clicked, ui_end_expander()) { + ui_expander("%S:", name) { for (i32 i = 0; i < type->count; i += 1) { type_member_t *tm = type->members + i; void *tmp = ti_extract_member(p, tm); @@ -498,33 +541,41 @@ fn void ui_serial_type(void *p, type_t *type) { ui_serial_subtype(p, type, type->name); } + +fn void ui_demo_init(ma_arena_t *arena) { + ui = ma_push_type(arena, ui_t); + ui->box_arena = ma_push_arena(arena, mib(1)); +} + gb app_event_t ui_test_event; fn void ui_demo_update(app_frame_t *frame) { + ui_begin_frame(frame); for (app_event_t *ev = frame->first_event; ev; ev = ev->next) { ui_begin_build(UILOC, ev, window_rect_from_frame(frame)); - ui->top->op = ui_op_idle; + ui->top->rect = r2f32_shrinks(ui->top->rect, 200); - ui_box_t *scroller_box = ui_boxf((ui_box_flags_t){.draw_rect = true, .clip_rect = true}, "scroller"); + ui_box_t *scroller_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_set_rect(scroller_box, r2f32_cut_right(&ui->top->rect, ui_em(0.5f))); - ui_box_t *item_box = ui_boxf((ui_box_flags_t){.draw_rect = true, .clip_rect = true}, "item_box"); + ui_box_t *item_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max)); static f32 scroller_value; - defer_block(ui_push_top(item_box), ui_pop_top()) { + ui_set_text_align(ui_text_align_center) + ui_set_top(item_box) { for (i32 i = 0; i < lengthof(tweak_table); i += 1) { mt_tweak_t *tweak = tweak_table + i; if (s8_starts_with(tweak->name, s8_lit("_"), false)) continue; if (tweak->type->kind == type_kind_b32) { b32 *n = (b32 *)tweak->ptr; - if (ui_button("%S: %s##slider%S", tweak->name, *n ? "true" : "false", tweak->name).clicked) { + if (ui_label_button("%S: %s##slider%S", tweak->name, *n ? "true" : "false", tweak->name).clicked) { *n = !*n; } } else if (tweak->type->kind == type_kind_f32) { f32 *n = (f32 *)tweak->ptr; - ui_signal_t signal = ui_button("%S: %f##slider%S", tweak->name, *n, tweak->name); + ui_signal_t signal = ui_label_button("%S: %f##slider%S", tweak->name, *n, tweak->name); if (signal.dragging) { f32 size = tweak->max - tweak->min; v2f32_t string_size = rn_measure_string(rn_state.main_font, signal.box->string); @@ -534,17 +585,17 @@ fn void ui_demo_update(app_frame_t *frame) { } else_is_invalid; } ui_serial_type(&ui_test_event, type(app_event_t)); - defer_if (ui_begin_expander("app_event_t").clicked, ui_end_expander()) { + ui_expander("app_event_t") { for (int i = 0; i < 2; i += 1) { ui_label("kind: app_event_kind_t##a%d", i); ui_label("ctrl: b8##ds%d", i); ui_label("shift: b8##f%d", i); - defer_if (ui_begin_expander("pos: v3f32_t##gg%d", i).clicked, ui_end_expander()) { + ui_expander("pos: v3f32_t##gg%d", i) { ui_label("x: f64 = value##hgt%d", i); ui_label("y: f64 = value##r%d", i); ui_label("z: f64 = value##yr%d", i); } - defer_if (ui_begin_expander("mouse_pos: v2f32_t##i2%d", i).clicked, ui_end_expander()) { + ui_expander("mouse_pos: v2f32_t##i2%d", i) { ui_label("x: f64 = value##i3%d", i); ui_label("y: f64 = value##i4%d", i); } @@ -578,10 +629,7 @@ fn void ui_demo_update(app_frame_t *frame) { } for (ui_box_t *it = item_box->first; it; it = it->next) { - it->full_rect.min.y -= scroller_value; - it->full_rect.max.y -= scroller_value; - it->rect.min.y -= scroller_value; - it->rect.max.y -= scroller_value; + ui_offset_box(it, v2f32(0, scroller_value)); } } @@ -589,15 +637,9 @@ fn void ui_demo_update(app_frame_t *frame) { } rn_begin(); - ui_draw(frame); + ui_draw(); rn_draw_stringf(rn_state.main_font, v2f32(0, frame->window_size.y - rn_state.main_font->size), black_color_global, "boxes: %d", ui->allocated_boxes); rn_end(frame->window_size, white_color_global); - - // End frame - for (app_event_t *ev = frame->first_event; ev; ev = ev->next) { - if (ev_left_up(ev)) { - ui->active = ui_null_id; - } - } + ui_end_frame(); } \ No newline at end of file diff --git a/src/ui/ui.gen.c b/src/ui/ui.gen.c new file mode 100644 index 0000000..2c9ca7b --- /dev/null +++ b/src/ui/ui.gen.c @@ -0,0 +1,31 @@ + +fn void ui_push_id(ui_id_t v) { ui_id_node_t *n = ma_push_type(tcx.temp, ui_id_node_t); n->value = v; SLLS_PUSH(ui->id_stack, n); } +fn void ui_pop_id(void) { SLLS_POP(ui->id_stack); } +#define ui_set_id(x) defer_block(ui_push_id(x), ui_pop_id()) +fn void ui_push_lop(ui_lop_t v) { ui_lop_node_t *n = ma_push_type(tcx.temp, ui_lop_node_t); n->value = v; SLLS_PUSH(ui->lop_stack, n); } +fn void ui_pop_lop(void) { SLLS_POP(ui->lop_stack); } +#define ui_set_lop(x) defer_block(ui_push_lop(x), ui_pop_lop()) +fn void ui_push_border_thickness(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->border_thickness_stack, n); } +fn void ui_pop_border_thickness(void) { SLLS_POP(ui->border_thickness_stack); } +#define ui_set_border_thickness(x) defer_block(ui_push_border_thickness(x), ui_pop_border_thickness()) +fn void ui_push_text_align(ui_text_align_t v) { ui_text_align_node_t *n = ma_push_type(tcx.temp, ui_text_align_node_t); n->value = v; SLLS_PUSH(ui->text_align_stack, n); } +fn void ui_pop_text_align(void) { SLLS_POP(ui->text_align_stack); } +#define ui_set_text_align(x) defer_block(ui_push_text_align(x), ui_pop_text_align()) +fn void ui_push_required_width(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->required_width_stack, n); } +fn void ui_pop_required_width(void) { SLLS_POP(ui->required_width_stack); } +#define ui_set_required_width(x) defer_block(ui_push_required_width(x), ui_pop_required_width()) +fn void ui_push_required_height(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->required_height_stack, n); } +fn void ui_pop_required_height(void) { SLLS_POP(ui->required_height_stack); } +#define ui_set_required_height(x) defer_block(ui_push_required_height(x), ui_pop_required_height()) +fn void ui_push_padding(v2f32_t v) { ui_v2f32_node_t *n = ma_push_type(tcx.temp, ui_v2f32_node_t); n->value = v; SLLS_PUSH(ui->padding_stack, n); } +fn void ui_pop_padding(void) { SLLS_POP(ui->padding_stack); } +#define ui_set_padding(x) defer_block(ui_push_padding(x), ui_pop_padding()) +fn void ui_assert_stacks_are_null(void) { +assert(ui->id_stack == NULL); +assert(ui->lop_stack == NULL); +assert(ui->border_thickness_stack == NULL); +assert(ui->text_align_stack == NULL); +assert(ui->required_width_stack == NULL); +assert(ui->required_height_stack == NULL); +assert(ui->padding_stack == NULL); +} \ No newline at end of file diff --git a/src/ui/ui.gen.h b/src/ui/ui.gen.h new file mode 100644 index 0000000..7927e06 --- /dev/null +++ b/src/ui/ui.gen.h @@ -0,0 +1,22 @@ + +typedef struct ui_id_node_t ui_id_node_t; struct ui_id_node_t { ui_id_t value; ui_id_node_t *next; }; +typedef struct ui_lop_node_t ui_lop_node_t; struct ui_lop_node_t { ui_lop_t value; ui_lop_node_t *next; }; +typedef struct ui_f32_node_t ui_f32_node_t; struct ui_f32_node_t { f32 value; ui_f32_node_t *next; }; +typedef struct ui_text_align_node_t ui_text_align_node_t; struct ui_text_align_node_t { ui_text_align_t value; ui_text_align_node_t *next; }; +typedef struct ui_v2f32_node_t ui_v2f32_node_t; struct ui_v2f32_node_t { v2f32_t value; ui_v2f32_node_t *next; }; + +#define UI_DECL_BOX_MEMBERS \ +f32 border_thickness;\ +ui_text_align_t text_align;\ +f32 required_width;\ +f32 required_height;\ +v2f32_t padding;\ + +#define UI_DECL_STACKS \ +ui_id_node_t *id_stack;\ +ui_lop_node_t *lop_stack;\ +ui_f32_node_t *border_thickness_stack;\ +ui_text_align_node_t *text_align_stack;\ +ui_f32_node_t *required_width_stack;\ +ui_f32_node_t *required_height_stack;\ +ui_v2f32_node_t *padding_stack;\ diff --git a/src/ui/ui.h b/src/ui/ui.h index a555896..4b428bc 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -25,13 +25,14 @@ typedef enum { } ui_text_align_t; typedef enum { - ui_op_cut_top, - ui_op_cut_bottom, - ui_op_cut_left, - ui_op_cut_right, - ui_op_idle, -} ui_op_t; + ui_lop_cut_top, + ui_lop_cut_bottom, + ui_lop_cut_left, + ui_lop_cut_right, + ui_lop_idle, +} ui_lop_t; +#include "ui.gen.h" typedef struct ui_box_t ui_box_t; struct ui_box_t { ui_box_t *next; @@ -46,9 +47,8 @@ struct ui_box_t { ui_box_flags_t flags; b8 created_new; - ui_text_align_t text_align; + UI_DECL_BOX_MEMBERS - ui_op_t op; r2f32_t full_rect; r2f32_t rect; @@ -71,12 +71,14 @@ struct ui_signal_t { typedef struct ui_t ui_t; struct ui_t { + app_frame_t *frame; + // building ui_box_t root; ui_box_t *top; app_event_t *event; - stack_t(ui_id_t, 32) id_stack; - ui_text_align_t pref_text_align; + + UI_DECL_STACKS // allocation ma_arena_t *box_arena; diff --git a/src/ui/ui.meta.c b/src/ui/ui.meta.c new file mode 100644 index 0000000..efb6099 --- /dev/null +++ b/src/ui/ui.meta.c @@ -0,0 +1,76 @@ +void mt_ui(ma_arena_t *arena) { + typedef struct cg_ui_stacks_t cg_ui_stacks_t; + struct cg_ui_stacks_t { + s8_t type; + s8_t name; + b32 skip_box_member; + + s8_t stack; + s8_t node; + }; + + cg_ui_stacks_t stacks[] = { + {s8_lit("ui_id_t") , s8_lit("id") , .skip_box_member = true} , + {s8_lit("ui_lop_t") , s8_lit("lop") , .skip_box_member = true} , + {s8_lit("f32") , s8_lit("border_thickness")} , + {s8_lit("ui_text_align_t") , s8_lit("text_align")} , + {s8_lit("f32") , s8_lit("required_width")} , + {s8_lit("f32") , s8_lit("required_height")} , + {s8_lit("v2f32_t") , s8_lit("padding")} , + }; + + /////////////////////////////// + // fill stack and node + for (cg_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { + s8_t core_type = it->type; + if (s8_ends_with(core_type, s8_lit("_t"), false)) core_type = s8_chop(core_type, 2); + it->stack = s8_printf(arena, "%S_stack", it->name); + it->node = s8_printf(arena, "%S_node_t", core_type); + if (!s8_starts_with(it->node, s8_lit("ui_"), false)) it->node = s8_printf(arena, "ui_%S", it->node); + } + + sb8_t *h_sb = sb8_serial_begin(arena); + sb8_t *c_sb = sb8_serial_begin(arena); + + /////////////////////////////// + // generate types + for (cg_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { + for (cg_ui_stacks_t *jt = it - 1; jt >= stacks; jt -= 1) if (s8_are_equal(it->type, jt->type)) goto type_already_gened; + sb8_stmtf(h_sb, "typedef struct %S %S; struct %S { %S value; %S *next; };", it->node, it->node, it->node, it->type, it->node); + type_already_gened:; + } + sb8_printf(h_sb, "\n"); + + /////////////////////////////// + // generate field embeds + sb8_stmtf(h_sb, "#define UI_DECL_BOX_MEMBERS \\"); + for (cg_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { + if (it->skip_box_member) continue; + sb8_stmtf(h_sb, "%S %S;\\", it->type, it->name); + } + sb8_printf(h_sb, "\n"); + + sb8_stmtf(h_sb, "#define UI_DECL_STACKS \\"); + for (cg_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { + sb8_stmtf(h_sb, "%S *%S;\\", it->node, it->stack); + } + sb8_printf(h_sb, "\n"); + + /////////////////////////////// + // generate stack functions + for (cg_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { + sb8_stmtf(c_sb, "fn void ui_push_%S(%S v) { %S *n = ma_push_type(tcx.temp, %S); n->value = v; SLLS_PUSH(ui->%S, n); }", it->name, it->type, it->node, it->node, it->stack); + sb8_stmtf(c_sb, "fn void ui_pop_%S(void) { SLLS_POP(ui->%S); }", it->name, it->stack); + sb8_stmtf(c_sb, "#define ui_set_%S(x) defer_block(ui_push_%S(x), ui_pop_%S())", it->name, it->name, it->name); + } + + sb8_stmtf(c_sb, "fn void ui_assert_stacks_are_null(void) {"); + for (cg_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { + sb8_stmtf(c_sb, "assert(ui->%S == NULL);", it->stack); + } + sb8_stmtf(c_sb, "}"); + + + os_write_file(cg_cpath(arena), sb8_serial_end(arena, c_sb)); + os_write_file(cg_hpath(arena), sb8_serial_end(arena, h_sb)); +} \ No newline at end of file diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index 1829757..ea82520 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -25,6 +25,7 @@ #include "render/render_opengl.c" #include "wasm_app.gen.c" #include "ui/ui_iter.c" +#include "ui/ui.gen.c" #include "ui/ui.c" fn void app_init(f32 dpr) { @@ -41,7 +42,6 @@ fn b32 app_update(app_frame_t *frame) { if (!f32_are_equal(font_size, _font_size)) { _font_size = font_size; rn_reload_font(font_data, font_size); - debugf("reloading font"); } ui_demo_update(frame); return true; diff --git a/todo.txt b/todo.txt index 26c0ac0..1b7fc8c 100644 --- a/todo.txt +++ b/todo.txt @@ -23,10 +23,11 @@ [x] remove dates and time from core [ ] meta [x] search for python snippets and execute meta.py script on that file + [ ] simplify logging!!!! [ ] somehow index properly the meta files and ignore generated files - [ ] extract declarations from c files meta(introspect) + [x] extract declarations from c files meta(introspect) [ ] extract all globals from c files - [ ] tweak variables and embeds declared in c code + [x] tweak variables and embeds declared in c code [ ] new simple format with tags [x] revisit api [ ] s8_bin