From 261c177480a0379e77af6ad36a6259f751a504c7 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 25 Jan 2021 19:50:44 -0500 Subject: dismiss error box --- command.c | 5 ++++- main.c | 71 ++++++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/command.c b/command.c index a8dc7e2..2364442 100644 --- a/command.c +++ b/command.c @@ -158,7 +158,10 @@ void command_execute(Ted *ted, Command c, i64 argument) { } break; case CMD_ESCAPE: - if (ted->menu) { + if (*ted->error_shown) { + // dismiss error box + *ted->error_shown = '\0'; + } else if (ted->menu) { menu_close(ted, true); } else if (buffer) { buffer_disable_selection(buffer); diff --git a/main.c b/main.c index aed49c9..142942b 100644 --- a/main.c +++ b/main.c @@ -478,45 +478,56 @@ int main(int argc, char **argv) { double time_passed = t - ted->error_time; if (time_passed > settings->error_display_time) { // stop showing error - ted->error_shown[0] = '\0'; + *ted->error_shown = '\0'; } else { - // @TODO: middle click to dismiss float padding = settings->padding; float char_width = text_font_char_width(font); float char_height = text_font_char_height(font); Rect r = rect_centered(V2(window_width * 0.5f, window_height * 0.9f), V2(menu_get_width(ted), 3 * char_height + 2 * padding)); - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_ERROR_BG]); - rect_render(r); - gl_color_rgba(colors[COLOR_ERROR_BORDER]); - rect_render_border(r, settings->border_thickness); - glEnd(); - gl_color_rgba(colors[COLOR_ERROR_TEXT]); - - - float text_x1 = rect_x1(r) + padding, text_x2 = rect_x2(r) - padding; - float text_y1 = rect_y1(r) + padding; - - TextRenderState text_state = {.x = text_x1, .y = text_y1, - .min_x = -FLT_MAX, .max_x = FLT_MAX, .min_y = -FLT_MAX, .max_y = FLT_MAX, - .render = true}; - char *p = ted->error_shown, *end = p + strlen(p); - - text_chars_begin(font); - while (p != end) { - char32_t c = 0; - size_t n = unicode_utf8_to_utf32(&c, p, (size_t)(end - p)); - if (n == (size_t)-1) { ++p; continue; } // invalid UTF-8; this shouldn't happen - p += n; - if (text_state.x + char_width >= text_x2) { - text_state.x = text_x1; - text_state.y += char_height; + bool dismissed = false; + for (u32 i = 0; i < ted->nmouse_clicks[SDL_BUTTON_MIDDLE]; ++i) { + if (rect_contains_point(r, ted->mouse_clicks[SDL_BUTTON_MIDDLE][i])) { + dismissed = true; + break; } - text_render_char(font, &text_state, c); } - text_chars_end(font); + + if (dismissed) { + *ted->error_shown = '\0'; + } else { + glBegin(GL_QUADS); + gl_color_rgba(colors[COLOR_ERROR_BG]); + rect_render(r); + gl_color_rgba(colors[COLOR_ERROR_BORDER]); + rect_render_border(r, settings->border_thickness); + glEnd(); + gl_color_rgba(colors[COLOR_ERROR_TEXT]); + + + float text_x1 = rect_x1(r) + padding, text_x2 = rect_x2(r) - padding; + float text_y1 = rect_y1(r) + padding; + + TextRenderState text_state = {.x = text_x1, .y = text_y1, + .min_x = -FLT_MAX, .max_x = FLT_MAX, .min_y = -FLT_MAX, .max_y = FLT_MAX, + .render = true}; + char *p = ted->error_shown, *end = p + strlen(p); + + text_chars_begin(font); + while (p != end) { + char32_t c = 0; + size_t n = unicode_utf8_to_utf32(&c, p, (size_t)(end - p)); + if (n == (size_t)-1) { ++p; continue; } // invalid UTF-8; this shouldn't happen + p += n; + if (text_state.x + char_width >= text_x2) { + text_state.x = text_x1; + text_state.y += char_height; + } + text_render_char(font, &text_state, c); + } + text_chars_end(font); + } } } -- cgit v1.2.3