From 15ccf7173989c51530a966ee25c5d897dcd7d328 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 27 Jan 2025 21:50:02 +0100 Subject: [PATCH] begin context menu --- build_file.c | 4 +-- package/index.html | 1 + src/app/app_wasm.html | 1 + src/core/core_intrin.c | 2 +- src/ui/ui.c | 63 +++++++++++++++++++++++++++++++++++++++--- src/ui/ui.h | 6 ++++ todo.txt | 8 +++++- 7 files changed, 77 insertions(+), 8 deletions(-) diff --git a/build_file.c b/build_file.c index 14aaf6e..b930669 100644 --- a/build_file.c +++ b/build_file.c @@ -67,9 +67,9 @@ int main(int argc, char **argv) { if (ok != 0) return ok; } - if (win32_target && cache_code_modified(s8_lit("../src/app/app_win32_exe.c"), s8_lit("app.exe"))) { + if (win32_target && cache_code_modified(s8_lit("../src/app/app_win32_exe.c"), s8_lit("app_win32.exe"))) { ok = os_systemf( - "cl ../src/app/app_win32_exe.c -Fe:app.exe -Fd:app_win32.pdb" + "cl ../src/app/app_win32_exe.c -Fe:app_win32.exe -Fd:app_win32.pdb" " -I ../src" " /Zi /FC /nologo /Oi" " /WX /W3 /wd4200 /diagnostics:column" diff --git a/package/index.html b/package/index.html index a7c7af1..d5f0c69 100644 --- a/package/index.html +++ b/package/index.html @@ -175,6 +175,7 @@ const wasm_app_imports = { addEventListener("resize", (event) => { wake_up(); }); addEventListener("keydown", (event) => { + event.preventDefault(); wasm_exports["wasm_key_down"](mem.write_string_to_cglobal("wasm_temp_buff1", event.key), event.ctrlKey, event.shiftKey, event.altKey, event.metaKey); wake_up(); }); diff --git a/src/app/app_wasm.html b/src/app/app_wasm.html index a7c7af1..d5f0c69 100644 --- a/src/app/app_wasm.html +++ b/src/app/app_wasm.html @@ -175,6 +175,7 @@ const wasm_app_imports = { addEventListener("resize", (event) => { wake_up(); }); addEventListener("keydown", (event) => { + event.preventDefault(); wasm_exports["wasm_key_down"](mem.write_string_to_cglobal("wasm_temp_buff1", event.key), event.ctrlKey, event.shiftKey, event.altKey, event.metaKey); wake_up(); }); diff --git a/src/core/core_intrin.c b/src/core/core_intrin.c index 5eee3a3..2e87ab0 100644 --- a/src/core/core_intrin.c +++ b/src/core/core_intrin.c @@ -72,7 +72,7 @@ fn f64 f64_exp2(f64 x) { b * (U64_TO_F64(0x3F242C003E4A2000) + b * U64_TO_F64(0x3EF0B291F6C00000))))))); - const f64 m[8] = { + f64 m[8] = { U64_TO_F64(0x3FF0000000000000), U64_TO_F64(0x3FF172B83C7D517B), U64_TO_F64(0x3FF306FE0A31B715), diff --git a/src/ui/ui.c b/src/ui/ui.c index e321c4e..ff5f7db 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -279,6 +279,16 @@ fn ui_signal_t ui_signal_from_box(ui_box_t *box) { return result; } +fn void ui_set_to_be_determined(ui_box_t *box, ui_axis2_t axis) { + if (axis == ui_axis2_x) { + box->full_rect.min.x = box->rect.max.x; + box->full_rect.max.x = box->rect.min.x; + } else if (axis == ui_axis2_y) { + box->full_rect.min.y = box->rect.max.y; + box->full_rect.max.y = box->rect.min.y; + } else_is_invalid; +} + fn v2f32_t ui_aligned_text_pos(f32 offset, ui_text_align_t text_align, r2f32_t rect, s8_t string) { v2f32_t string_size = rn_measure_string(rn->main_font, string); v2f32_t rect_size = r2f32_get_size(rect); @@ -305,7 +315,7 @@ fn ui_draw_compute_t ui_draw_compute(ui_box_t *box) { ui_draw_compute_t co = {0}; co.rect = box->full_rect; - f32 appear_t = f32_ease_out_n(f32_clamp01(box->appear_t * 6), 10); + f32 appear_t = f32_ease_out_n(f32_clamp01(box->appear_t * 2), 10); if (!ui_id_is_null(box->id)) { v2f32_t size = v2f32_muls(r2f32_get_size(co.rect), 0.15f); r2f32_t smaller_rect = r2f32_shrink(co.rect, size); @@ -931,6 +941,7 @@ fn void ui_end_reversal(void) { break; } } + assert(connected); ui_box_t *first2 = NULL; @@ -950,7 +961,7 @@ fn void ui_end_reversal(void) { gb app_event_t ui_test_event; -gb i32 ui_g_panel = 2; +gb i32 ui_g_panel = 3; fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) { ui_begin_frame(frame); rn_begin_frame(frame); @@ -971,6 +982,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co ui_set_top(top_box) { ui_radio_button(&ui_g_panel, 1, "data"); ui_radio_button(&ui_g_panel, 2, "log"); + ui_radio_button(&ui_g_panel, 3, "menus"); } ui->top->rect = r2f32_shrinks(ui->top->rect, ui_em(1)); @@ -1273,6 +1285,51 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co } } + if (ui_g_panel == 3) { + ui_box_t *item_box = ui_boxf((ui_box_flags_t){.draw_rect = true, .clip_rect = true}, "item_box"); + ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max)); + ui_set_top(item_box) { + ui_label("right click to bring up the context menu!"); + } + + /////////////////////////////// + // context menu + { + + locl b32 context_menu_open; + locl v2f32_t menu_pos; + if (ev_right_down(ev)) { + context_menu_open = 1; + menu_pos = ev->mouse_pos; + } + + if (context_menu_open) { + ui_box_t *menu = ui_boxf((ui_box_flags_t){.draw_border = true, .draw_rect = true}, "context_menu"); + ui_set_rect(menu, r2f32_min_dim(menu_pos, v2f32(ui_em(10), ui_to_be_determined))); + + ui_set_top(menu) + ui_set_text_align(ui_text_align_left) + ui_set_string_pos_offset(ui_em(1)) + ui_set_padding(ui_em(0.2f)) { + ui_label_button("file"); + ui_label_button("memes"); + } + ui_set_to_be_determined(menu, ui_axis2_y); + + + if (ev_left_down(ev) && !r2f32_contains(menu->full_rect, ev->mouse_pos)) { + context_menu_open = 0; + } + if (ev_right_down(ev)) { + menu->appear_t = 0; + for (ui_box_t *it = menu->first; it; it = it->next) { + it->appear_t = 0; + } + } + } + } + } + ui_id_t lister_id = ui_id_from_loc(UILOC); locl b32 lister_open; @@ -1314,8 +1371,6 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co } } - - ui_end_build(); } diff --git a/src/ui/ui.h b/src/ui/ui.h index 62a6944..15e2f59 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -155,11 +155,17 @@ fn b32 ui_is_focused_box(ui_box_t *box) { return !ui_is_null_box(box) && box->id #define ev_left_up(ev) ((ev)->kind == app_event_kind_mouse_up && ev_left(ev)) #define ev_left_down(ev) ((ev)->kind == app_event_kind_mouse_down && ev_left(ev)) +#define ev_right(ev) ((ev)->mouse_button == app_mouse_button_right) +#define ev_right_up(ev) ((ev)->kind == app_event_kind_mouse_up && ev_right(ev)) +#define ev_right_down(ev) ((ev)->kind == app_event_kind_mouse_down && ev_right(ev)) + + fn void ui_set_rect(ui_box_t *box, r2f32_t rect) { box->rect = box->full_rect = rect; } #define UILOC (ui_code_loc_t){.file = __FILE__, .line = __LINE__, .counter = __COUNTER__} #define ui_em(x) ((x) * rn->main_font->size) #define ui_max 200000000.f +#define ui_to_be_determined 0 #define ui_box_flags(...) (ui_box_flag_t){__VA_ARGS__} fn ui_id_t ui_id(s8_t string); diff --git a/todo.txt b/todo.txt index 4be390a..8dc1834 100644 --- a/todo.txt +++ b/todo.txt @@ -22,10 +22,16 @@ [ ] maybe copy package stuff to build? [ ] ui - [ ] debug console, lines + [ ] table + [ ] vertical scroll + [ ] right click menu + [ ] hover tooltips + [ ] upper left context menu dynamics + [x] debug console, lines [x] fix elements getting offset when font large and they are on the edge [ ] text input [x] basic working + [ ] enter signal [ ] ctrl backspace, ctrl delete [ ] set mouse cursor on hover [ ] everything lister (with edits etc.)