#include "../graphics.c" b8 button_0_down = 0; b8 button_1_down = 0; b8 button_1_checked = 0; i64 text_len = 0; static c32 text[512] = {0}; i64 cursor = 0; i64 selection = 0; void update_and_render_frame(void) { p_wait_events(); for (i32 j = 0; j < g_platform.frame_height; ++j) for (i32 i = 0; i < g_platform.frame_width; ++i) g_platform.pixels[j * g_platform.frame_width + i] = (vec3_f32) { .2f, .1f, 0.f }; if (g_platform.cursor_x >= 40 && g_platform.cursor_x < 100 && g_platform.cursor_y >= 40 && g_platform.cursor_y < 100) { button_0_down = g_platform.key_down[BUTTON_LEFT]; if (button_0_down) fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, 40, 40, 60, 60); else fill_rectangle(OP_SET, (vec3_f32) { 0.f, 1.f, 0.f }, 40, 40, 60, 60); } else { button_0_down = 0; fill_rectangle(OP_SET, (vec3_f32) { .1f, .5f, .1f }, 40, 40, 60, 60); } if (g_platform.cursor_x >= 40 && g_platform.cursor_x < 100 && g_platform.cursor_y >= 120 && g_platform.cursor_y < 180) { button_1_down = g_platform.key_down[BUTTON_LEFT]; if (g_platform.key_pressed[BUTTON_LEFT]) button_1_checked = !button_1_checked; if (button_1_down) fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, 40, 120, 60, 60); else if (button_1_checked) fill_rectangle(OP_SET, (vec3_f32) { 1.f, .5f, .5f }, 40, 120, 60, 60); else fill_rectangle(OP_SET, (vec3_f32) { .5f, 1.f, .5f }, 40, 120, 60, 60); } else { button_1_down = 0; if (button_1_checked) fill_rectangle(OP_SET, (vec3_f32) { 1.f, 0.f, 0.f }, 40, 120, 60, 60); else fill_rectangle(OP_SET, (vec3_f32) { 0.f, 1.f, 0.f }, 40, 120, 60, 60); } i64 w = g_platform.frame_width / 2; i64 h = g_platform.frame_height / 2; i64 x0 = w / 2; i64 y0 = h / 2; vec3_f32 color = { 1.f, .5f, .5f }; if (g_platform.cursor_x >= x0 && g_platform.cursor_x < x0 + w && g_platform.cursor_y >= y0 && g_platform.cursor_y < y0 + h) color = (vec3_f32) { 1.f, 1.f, 1.f }; for (i64 i = 0; i < g_platform.input_size; ++i) if (g_platform.input[i].ctrl) switch (g_platform.input[i].key) { case KEY_V: { if (selection != 0) { i64 i0 = selection < 0 ? cursor + selection : cursor; i64 i1 = selection < 0 ? cursor : cursor + selection; for (i64 i = 0; i1 + i < text_len; ++i) text[i0 + i] = text[i1 + i]; selection = 0; cursor = i0; text_len -= i1 - i0; } for (i64 n = 0; n < g_platform.clipboard_size;) { c32 c = utf8_read(g_platform.clipboard_size - n, g_platform.clipboard + n); if (text_len < (i64) (sizeof text / sizeof *text)) { for (i64 j = text_len; j > cursor; --j) text[j] = text[j - 1]; text[cursor++] = c; ++text_len; } n += utf8_size(c); } } break; case KEY_X: { i64 len = 0; static c8 buf[1024]; i64 i0 = selection < 0 ? cursor + selection : cursor; i64 i1 = selection < 0 ? cursor : cursor + selection; for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i) len += utf8_write(text[i0 + i], buf + len); if (len > 0) p_clipboard_write(len, buf); for (i64 i = 0; i1 + i < text_len; ++i) text[i0 + i] = text[i1 + i]; selection = 0; cursor = i0; text_len -= i1 - i0; } break; case KEY_C: { i64 len = 0; static c8 buf[1024]; i64 i0 = selection < 0 ? cursor + selection : cursor; i64 i1 = selection < 0 ? cursor : cursor + selection; for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i) len += utf8_write(text[i0 + i], buf + len); if (len > 0) p_clipboard_write(len, buf); } break; default:; } else switch (g_platform.input[i].key) { case KEY_LEFT: if (g_platform.key_down[MOD_SHIFT]) { if (cursor > 0) ++selection; } else if (selection != 0) { cursor = selection < 0 ? cursor + selection + 1 : cursor + 1; selection = 0; } if (cursor > 0) --cursor; break; case KEY_RIGHT: if (g_platform.key_down[MOD_SHIFT]) { if (cursor < text_len) --selection; } else if (selection != 0) { cursor = selection < 0 ? cursor - 1 : cursor + selection - 1; selection = 0; } if (cursor < text_len) ++cursor; break; case KEY_BACKSPACE: if (selection != 0) { i64 i0 = selection < 0 ? cursor + selection : cursor; i64 i1 = selection < 0 ? cursor : cursor + selection; for (i64 i = 0; i1 + i < text_len; ++i) text[i0 + i] = text[i1 + i]; selection = 0; cursor = i0; text_len -= i1 - i0; } else if (cursor > 0 && text_len > 0) { for (i64 i = cursor; i < text_len; ++i) text[i - 1] = text[i]; --cursor; --text_len; } break; case KEY_DELETE: if (selection != 0) { i64 i0 = selection < 0 ? cursor + selection : cursor; i64 i1 = selection < 0 ? cursor : cursor + selection; for (i64 i = 0; i1 + i < text_len; ++i) text[i0 + i] = text[i1 + i]; selection = 0; cursor = i0; text_len -= i1 - i0; } else if (cursor < text_len) { for (i64 i = cursor + 1; i < text_len; ++i) text[i - 1] = text[i]; --text_len; } break; case KEY_ENTER: case KEY_TAB: g_platform.input[i].c = g_platform.input[i].key; // fallthrough default: if (g_platform.input[i].c) { if (selection != 0) { i64 i0 = selection < 0 ? cursor + selection : cursor; i64 i1 = selection < 0 ? cursor : cursor + selection; for (i64 i = 0; i1 + i < text_len; ++i) text[i0 + i] = text[i1 + i]; selection = 0; cursor = i0; text_len -= i1 - i0; } if (text_len < (i64) (sizeof text / sizeof *text)) { for (i64 i = text_len; i > cursor; --i) text[i] = text[i - 1]; text[cursor++] = g_platform.input[i].c; ++text_len; } } } draw_text_area((vec3_f32) {0}, x0 + 8, y0 - 8, w, h, 10., 10., text_len, text); draw_text_area(color, x0, y0, w, h, 10., 10., text_len, text); draw_selection_cursor((vec3_f32) { 1.f, 1.f, 1.f }, x0, y0, w, h, 10., 10., cursor, selection, text_len, text); p_render_frame(); } i32 main(i32 argc, c8 **argv) { (void) argc; (void) argv; g_platform = (Platform) { .title = "UI", .frame_width = 960, .frame_height = 720, .exact_resolution = 1, }; p_event_loop(); return 0; }