From 7686e759186519d6230c7f32c815bad16223c547 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sat, 10 Aug 2024 19:31:37 +0200 Subject: Text area --- examples/ui.c | 59 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/examples/ui.c b/examples/ui.c index afb9dbf..95a3235 100755 --- a/examples/ui.c +++ b/examples/ui.c @@ -264,13 +264,19 @@ i64 enum_text_columns(i64 num_chars, c8 *text) { for (i64 i = 0; i < num_chars; ++i) { if (text[i] <= ' ') { - if (text[i] == '\n') + if (text[i] == '\n') { + if (cols < n) + cols = n; n = 0; - else if (text[i] == '\b' && i > 0) + } else if (text[i] == '\b' && i > 0) { + if (cols < n) + cols = n; n -= char_width(text[i - 1]) + char_spacing(num_chars, text, i - 1); - else if (text[i] == '\r') + } else if (text[i] == '\r') { + if (cols < n) + cols = n; n = 0; - else + } else n += char_width(' ') + char_spacing(num_chars, text, i); continue; } @@ -288,30 +294,30 @@ i64 enum_text_rows(i64 num_chars, c8 *text) { i64 rows = 0; - for (i64 i = 0; i < num_chars; ++i) - if (text[i] == '\n') - ++rows; - - if (num_chars > 0 && text[num_chars - 1] != '\n') - ++rows; + for (i64 i = 0; i <= num_chars; ++i) + if (i == num_chars || text[i] == '\n') { + if (rows > 0) + ++rows; + rows += CHAR_NUM_BITS_Y; + } return rows; } -void print_text(u32 color, i64 x0, i64 y0, f64 scale_x, f64 scale_y, i64 num_chars, c8 *text) { +void print_text(u32 color, f64 x0, f64 y0, f64 scale_x, f64 scale_y, i64 num_chars, c8 *text) { assert(text != NULL); f64 x = x0; f64 y = y0; - f64 kx = scale_x / (CHAR_NUM_BITS_X + 1); - f64 h = (scale_y * CHAR_NUM_BITS_Y) / (CHAR_NUM_BITS_Y + 1); + f64 kx = scale_x; + f64 h = scale_y * CHAR_NUM_BITS_Y; for (i64 n = 0; n < num_chars; ++n) { if (text[n] <= ' ') { if (text[n] == '\n') { x = x0; - y += scale_y; + y += scale_y * (CHAR_NUM_BITS_Y + 1); } else if (text[n] == '\b' && n > 0) x -= kx * (char_width(text[n - 1]) + char_spacing(num_chars, text, n - 1)); @@ -350,6 +356,16 @@ void print_text(u32 color, i64 x0, i64 y0, f64 scale_x, f64 scale_y, i64 num_cha } } +void draw_text_area(u32 color, f64 x0, f64 y0, f64 width, f64 height, i64 num_chars, c8 *text) { + i64 num_columns = enum_text_columns(num_chars, text); + i64 num_rows = enum_text_rows(num_chars, text); + + f64 scale_x = width / num_columns; + f64 scale_y = height / num_rows; + + print_text(color, x0, y0, scale_x, scale_y, num_chars, text); +} + i32 main(i32 argc, c8 **argv) { (void) argc; (void) argv; @@ -369,17 +385,18 @@ i32 main(i32 argc, c8 **argv) { for (i32 i = 0; i < platform.frame_width; ++i) platform.pixels[j * platform.frame_width + i] = 0x302000; + i64 w = platform.frame_width / 2; + i64 h = platform.frame_height / 2; + i64 x0 = w / 2; + i64 y0 = h / 2; + i64 color = 0xff7f7f; - if (platform.cursor_x >= 80 && platform.cursor_x < 80 + (enum_text_columns(14, "Hello, Sailor!") * 80) / (CHAR_NUM_BITS_X + 1) && - platform.cursor_y >= 80 && platform.cursor_y < 180) + if (platform.cursor_x >= x0 && platform.cursor_x < x0 + w && + platform.cursor_y >= y0 && platform.cursor_y < y0 + h) color = 0xffffff; - print_text(color, 80, 80, 80., 80., 14, "Hello, Sailor!"); - - c8 chars[] = "\"#$%&'()*+,-./\n0123456789:;<=>?@\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n[\\]^_`\nabcdefghijklmnopqrstuvwxyz\n{|}~"; - - print_text(0x7fffff, 80, 200, 80., 80., sizeof chars - 1, chars); + draw_text_area(color, x0, y0, w, h, 27, "Hello, Sailor!\nUI edit text"); p_render_frame(); } -- cgit v1.2.3