string16 improvements

This commit is contained in:
Krzosa Karol
2025-04-04 08:25:47 +02:00
parent f6d5a1cf81
commit f3aa52ba3d
34 changed files with 637 additions and 699 deletions

View File

@@ -43,13 +43,13 @@ fn s8_t ui_tprint_loc(ui_code_loc_t loc) {
fn s8_t ui_get_display_string(s8_t string) {
s8_t result = string;
s8_seek(result, s8_lit("##"), s8_seek_none, &result.len);
s8_seek(result, s8("##"), s8_seek_none, &result.len);
return result;
}
fn s8_t ui_get_hash_string(s8_t string) {
i64 len = 0;
if (s8_seek(string, s8_lit("##"), s8_seek_none, &len)) {
if (s8_seek(string, s8("##"), s8_seek_none, &len)) {
string = s8_skip(string, len + 2);
}
return string;
@@ -435,14 +435,14 @@ fn void ui_text_input_draw(ui_box_t *box) {
rn_draw_rect(co.rect, co.background_color);
ui_text_input_t *ti = box->text_input;
s8_t string = s8(ti->str, ti->len);
s8_t string = s8_make(ti->str, ti->len);
v2f32_t pos = ui_aligned_text_pos(box->string_pos_offset, box->text_align, co.rect, string, rn_measure_string(rn->main_font, string));
rn_draw_string(rn->main_font, pos, co.text_color, string);
ti->caret = ui_caret_clamp(ti->caret, 0, (i32)ti->len);
{
s8_t string_min = s8(ti->str, ti->caret.range.min);
s8_t string_max = s8(ti->str, ti->caret.range.max);
s8_t string_min = s8_make(ti->str, ti->caret.range.min);
s8_t string_max = s8_make(ti->str, ti->caret.range.max);
v2f32_t size_min = rn_measure_string(rn->main_font, string_min);
v2f32_t size_max = rn_measure_string(rn->main_font, string_max);
@@ -507,19 +507,19 @@ fn_test void ui_test_text_replace(void) {
char buff[4] = {0};
ti.str = buff;
ti.cap = lengthof(buff);
ui_text_replace(&ti, r1i32(0,0), s8_lit("astfpo"));
assert(s8_are_equal(ti.string, s8_lit("astf")));
ui_text_replace(&ti, r1i32(0,0), s8("astfpo"));
assert(s8_are_equal(ti.string, s8("astf")));
ui_text_replace(&ti, r1i32(0,4), s8_lit("qwer"));
assert(s8_are_equal(ti.string, s8_lit("qwer")));
ui_text_replace(&ti, r1i32(0,4), s8("qwer"));
assert(s8_are_equal(ti.string, s8("qwer")));
ui_text_replace(&ti, r1i32(1,2), s8_lit("a"));
assert(s8_are_equal(ti.string, s8_lit("qaer")));
ui_text_replace(&ti, r1i32(1,2), s8("a"));
assert(s8_are_equal(ti.string, s8("qaer")));
}
#define ui_text_input(...) ui__text_input(UILOC, __VA_ARGS__)
fn ui_signal_t ui__text_input(ui_code_loc_t loc, ui_text_input_t *ti, b32 sim_even_if_no_focus) {
ui_box_t *box = ui_box(.loc = loc, .string = s8_lit("text_input"), .flags = { .draw_border = true, .draw_rect = true, .draw_text = true, .keyboard_nav = true });
ui_box_t *box = ui_box(.loc = loc, .string = s8("text_input"), .flags = { .draw_border = true, .draw_rect = true, .draw_text = true, .keyboard_nav = true });
box->text_input = ti;
box->custom_draw = ui_text_input_draw;
@@ -542,13 +542,13 @@ fn ui_signal_t ui__text_input(ui_code_loc_t loc, ui_text_input_t *ti, b32 sim_ev
signal.text_changed = true;
if (ev->ctrl) {
if (sel_size) {
ui_text_replace(ti, ti->caret.range, s8_lit(""));
ui_text_replace(ti, ti->caret.range, s8(""));
} else {
ui_text_replace(ti, r1i32(0, ti->caret.range.max), s8_lit(""));
ui_text_replace(ti, r1i32(0, ti->caret.range.max), s8(""));
}
} else {
if (sel_size) {
ui_text_replace(ti, ti->caret.range, s8_lit(""));
ui_text_replace(ti, ti->caret.range, s8(""));
} else {
ui_text_replace(ti, r1i32(ti->caret.e[0] - 1, ti->caret.e[0]), s8_null);
ti->caret = ui_carets(ti->caret.range.min - 1);
@@ -557,7 +557,7 @@ fn ui_signal_t ui__text_input(ui_code_loc_t loc, ui_text_input_t *ti, b32 sim_ev
} else if (ev->key == app_key_delete) {
signal.text_changed = true;
if (sel_size) {
ui_text_replace(ti, ti->caret.range, s8_lit(""));
ui_text_replace(ti, ti->caret.range, s8(""));
} else {
ui_text_replace(ti, r1i32(ti->caret.e[0], ti->caret.e[0] + 1), s8_null);
}
@@ -579,7 +579,7 @@ fn ui_signal_t ui__text_input(ui_code_loc_t loc, ui_text_input_t *ti, b32 sim_ev
signal.text_commit = true;
}
}
v2f32_t size = rn_measure_string(rn->main_font, s8_lit("_"));
v2f32_t size = rn_measure_string(rn->main_font, s8("_"));
v2f32_t pos = v2f32_sub(ev->mouse_pos, box->final_rect.min);
i32 p = (i32)f32_round(pos.x / size.x);
if (ev->kind == app_event_kind_mouse_down && ev->mouse_button == app_mouse_button_left) {
@@ -1262,9 +1262,9 @@ fn void ui_demo_everything_lister(void) {
}
s8_t cmds[] = {
s8_lit("show data tab"),
s8_lit("show log tab"),
s8_lit("show menus tab"),
s8("show data tab"),
s8("show log tab"),
s8("show menus tab"),
};
if (lister_open) {
@@ -1292,7 +1292,7 @@ fn void ui_demo_everything_lister(void) {
if (lister_just_opened) text_input.len = 0;
ma_temp_t scratch = ma_begin_scratch();
s8_t needle = s8(text_input.str, text_input.len);
s8_t needle = s8_make(text_input.str, text_input.len);
fuzzy_pair_t *pairs = fuzzy_rate_array(scratch.arena, needle, cmds, lengthof(cmds));
b32 set_focus = lister_just_opened || ti_sig.text_changed;
@@ -1370,7 +1370,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_top_rectp()[0] = r2f32_shrinks(ui_top_rect(), ui_em(1));
for (i32 i = 0; i < tweak_count; i += 1) {
mt_tweak_t *tweak = tweak_table + i;
if (s8_starts_with(tweak->name, s8_lit("_"), false)) {
if (s8_starts_with(tweak->name, s8("_"), false)) {
continue;
}

View File

@@ -1,19 +1,19 @@
// automatically generated using: C:\dev\wasm\src/ui/ui.meta.c
type_t type__ui_color_t = { type_kind_enum, s8_const_lit("ui_color_t"), sizeof(ui_color_t),
type_t type__ui_color_t = { type_kind_enum, s8_const("ui_color_t"), sizeof(ui_color_t),
.members = (type_member_t[]){
{.name = s8_const_lit("ui_color_rect"), .value = ui_color_rect},
{.name = s8_const_lit("ui_color_rect_hot"), .value = ui_color_rect_hot},
{.name = s8_const_lit("ui_color_rect_active"), .value = ui_color_rect_active},
{.name = s8_const_lit("ui_color_rect_turned_on"), .value = ui_color_rect_turned_on},
{.name = s8_const_lit("ui_color_border"), .value = ui_color_border},
{.name = s8_const_lit("ui_color_text"), .value = ui_color_text},
{.name = s8_const_lit("ui_color_text_hot"), .value = ui_color_text_hot},
{.name = s8_const_lit("ui_color_text_active"), .value = ui_color_text_active},
{.name = s8_const_lit("ui_color_focused_rect"), .value = ui_color_focused_rect},
{.name = s8_const_lit("ui_color_scroller"), .value = ui_color_scroller},
{.name = s8_const_lit("ui_color_scroller_hot"), .value = ui_color_scroller_hot},
{.name = s8_const_lit("ui_color_scroller_active"), .value = ui_color_scroller_active},
{.name = s8_const("ui_color_rect"), .value = ui_color_rect},
{.name = s8_const("ui_color_rect_hot"), .value = ui_color_rect_hot},
{.name = s8_const("ui_color_rect_active"), .value = ui_color_rect_active},
{.name = s8_const("ui_color_rect_turned_on"), .value = ui_color_rect_turned_on},
{.name = s8_const("ui_color_border"), .value = ui_color_border},
{.name = s8_const("ui_color_text"), .value = ui_color_text},
{.name = s8_const("ui_color_text_hot"), .value = ui_color_text_hot},
{.name = s8_const("ui_color_text_active"), .value = ui_color_text_active},
{.name = s8_const("ui_color_focused_rect"), .value = ui_color_focused_rect},
{.name = s8_const("ui_color_scroller"), .value = ui_color_scroller},
{.name = s8_const("ui_color_scroller_hot"), .value = ui_color_scroller_hot},
{.name = s8_const("ui_color_scroller_active"), .value = ui_color_scroller_active},
},
.count = 12,
};

View File

@@ -15,7 +15,7 @@ fn void mt_ui_colors(ma_arena_t *arena, sb8_t *c, sb8_t *h) {
{ scroller_active `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.2f, 0.5f, 1.0f})` }
));
mtt_serial_enum(c, h, table, s8_lit("ui_color"));
mtt_serial_enum(c, h, table, s8("ui_color"));
sb8_stmtf(c, "gb v4f32_t ui_color_table[] = {");
for (ast_t *it = table->first; it; it = it->next) {
@@ -55,20 +55,20 @@ fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) {
// create `stack` and `node` columns
for (ast_t *it = table->first; it; it = it->next) {
s8_t type = mtts(it, "type");
if (s8_ends_with(type, s8_lit("_t"))) {
if (s8_ends_with(type, s8("_t"))) {
type = s8_chop(type, 2);
}
s8_t ui_type = type;
if (!s8_starts_with(ui_type, s8_lit("ui_"))) {
if (!s8_starts_with(ui_type, s8("ui_"))) {
ui_type = s8_printf(arena, "ui_%S", ui_type);
}
s8_t node = s8_printf(arena, "%S_node_t", ui_type);
s8_t stack = s8_printf(arena, "%S_stack", mtts(it, "name"));
mt_ast_append(it, mt_kv(arena, s8_lit("node"), node));
mt_ast_append(it, mt_kv(arena, s8_lit("stack"), stack));
mt_ast_append(it, mt_kv(arena, s8("node"), node));
mt_ast_append(it, mt_kv(arena, s8("stack"), stack));
}
@@ -118,7 +118,7 @@ fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) {
sb8_stmtf(c, "fn void ui_push_init_values(void) {");
for (ast_t *it = table->first; it; it = it->next) {
if (s8_are_equal(mtts(it, "init"), s8_lit("x"))) {
if (s8_are_equal(mtts(it, "init"), s8("x"))) {
continue;
}
mt_stmtf(c, it, "ui_push_@name(@init);");
@@ -127,7 +127,7 @@ fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) {
sb8_stmtf(c, "fn void ui_pop_init_values(void) {");
for (ast_t *it = table->first; it; it = it->next) {
if (s8_are_equal(mtts(it, "init"), s8_lit("x"))) {
if (s8_are_equal(mtts(it, "init"), s8("x"))) {
continue;
}
mt_stmtf(c, it, "ui_pop_@name();");
@@ -136,7 +136,7 @@ fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) {
sb8_stmtf(c, "fn void ui_box_fill_with_colors(ui_box_t *box) {");
for (ast_t *it = table->first; it; it = it->next) {
if (!s8_ends_with(mtts(it, "name"), s8_lit("_color"))) {
if (!s8_ends_with(mtts(it, "name"), s8("_color"))) {
continue;
}
mt_stmtf(c, it, "box->@name = ui_top_@name();");