From 4554db338221f8fd1c7b4855b8d5bc50780815d2 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 27 Jan 2021 17:45:04 -0500 Subject: more warn overwrite --- buffer.c | 2 +- main.c | 23 ++++------------------- menu.c | 7 +++++-- text.c | 9 ++++++++- text.h | 1 + ui.c | 36 ++++++++++++++++++++++++++++++++++-- 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/buffer.c b/buffer.c index 3a808c7..2f656e7 100644 --- a/buffer.c +++ b/buffer.c @@ -656,7 +656,7 @@ bool buffer_save(TextBuffer *buffer) { } return success; } else { - buffer_seterr(buffer, "Couldn't create file %s.", buffer->filename); + buffer_seterr(buffer, "Couldn't write to file %s.", buffer->filename); return false; } } else { diff --git a/main.c b/main.c index f7ed8fa..2e92a18 100644 --- a/main.c +++ b/main.c @@ -494,8 +494,6 @@ int main(int argc, char **argv) { *ted->error_shown = '\0'; } else { Rect r = error_box_rect(ted); - float char_width = text_font_char_width(font); - float char_height = text_font_char_height(font); float padding = settings->padding; glBegin(GL_QUADS); @@ -510,24 +508,11 @@ int main(int argc, char **argv) { float text_x1 = rect_x1(r) + padding, text_x2 = rect_x2(r) - padding; float text_y1 = rect_y1(r) + padding; + // (make sure text wraps) 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); + .min_x = text_x1, .max_x = text_x2, .min_y = -FLT_MAX, .max_y = FLT_MAX, + .render = true, .wrap = true}; + text_render_with_state(font, &text_state, ted->error_shown, text_x1, text_y1); } } diff --git a/menu.c b/menu.c index 9a326de..a307a62 100644 --- a/menu.c +++ b/menu.c @@ -102,9 +102,12 @@ static void menu_render(Ted *ted, Menu menu) { glEnd(); if (*ted->warn_overwrite) { - char body[256] = {0}; + char const *filename = ted->warn_overwrite; + char title[32] = {0}, body[256] = {0}; + char const *last_path_sep = strrchr(filename, PATH_SEPARATOR); + strbuf_printf(title, "Overwrite %s?", last_path_sep ? last_path_sep + 1 : filename); strbuf_printf(body, "Are you sure you want to overwrite %s?", ted->warn_overwrite); - popup_render(ted, "Save as...", body); + popup_render(ted, title, body); return; } diff --git a/text.c b/text.c index 094db15..c4b88d3 100644 --- a/text.c +++ b/text.c @@ -180,6 +180,7 @@ void text_chars_end(Font *font) { } void text_render_char(Font *font, TextRenderState *state, char32_t c) { +top: if (c >= 0x40000 && c < 0xE0000){ // these Unicode code points are currently unassigned. replace them with a Unicode box. // (specifically, we don't want to use extra memory for pages which @@ -192,7 +193,7 @@ void text_render_char(Font *font, TextRenderState *state, char32_t c) { if (state->render) text_render_with_page(font, (int)page); stbtt_bakedchar *char_data = font->char_pages[page]; - float char_height = font->char_height; + float const char_height = font->char_height; if (char_data) { // if page was successfully loaded stbtt_aligned_quad q = {0}; state->y += char_height * 0.75f; @@ -205,6 +206,12 @@ void text_render_char(Font *font, TextRenderState *state, char32_t c) { float x1 = q.x1, y1 = q.y1; float const min_x = state->min_x, max_x = state->max_x; float const min_y = state->min_y, max_y = state->max_y; + if (state->wrap && x1 >= max_x) { + state->x = min_x; + state->y += char_height; + goto top; + } + if (x0 > max_x || y0 > max_y || x1 < min_x || y1 < min_y) return; if (x0 < min_x) { diff --git a/text.h b/text.h index ad2a3b0..ae865a9 100644 --- a/text.h +++ b/text.h @@ -13,6 +13,7 @@ typedef struct Font Font; typedef struct { // should the text actually be rendered (set to false to get text size) bool render; + bool wrap; // should the text wrap around to min_x when it reaches max_x? NOTE: this is character-by-character wrapping, not word wrap float x, y; // points at which the text should be cut off diff --git a/ui.c b/ui.c index b09c8f6..05e4110 100644 --- a/ui.c +++ b/ui.c @@ -476,15 +476,47 @@ static PopupOption popup_update(Ted *ted) { static void popup_render(Ted *ted, char const *title, char const *body) { float window_width = ted->window_width; float window_height = ted->window_height; + Font *font = ted->font; + Font *font_bold = ted->font_bold; Rect r = rect_centered(V2(window_width * 0.5f, window_height * 0.5f), V2(300, 200)); Settings const *settings = &ted->settings; u32 const *colors = settings->colors; + float const char_height_bold = text_font_char_height(font_bold); + float const padding = settings->padding; glBegin(GL_QUADS); gl_color_rgba(colors[COLOR_MENU_BG]); rect_render(r); + gl_color_rgba(colors[COLOR_BORDER]); + rect_render_border(r, 1); + glEnd(); + + float y = r.pos.y; + // title text + v2 title_size = {0}; + text_get_size(font_bold, title, &title_size.x, &title_size.y); + v2 title_pos = v2_sub(V2(window_width * 0.5f, y), V2(title_size.x * 0.5f, 0)); + gl_color_rgba(colors[COLOR_TEXT]); + text_render(font_bold, title, title_pos.x, title_pos.y); + + y += char_height_bold; + // line separating text from body + glBegin(GL_LINES); + gl_color_rgba(colors[COLOR_BORDER]); + glVertex2f(rect_x1(r), y); + glVertex2f(rect_x2(r), y); glEnd(); - (void)title; - (void)body; + y += padding; + + // body text + gl_color_rgba(colors[COLOR_TEXT]); + float text_x1 = rect_x1(r) + padding; + float text_x2 = rect_x2(r) - padding; + TextRenderState state = { + .render = true, .wrap = true, + .min_x = text_x1, .max_x = text_x2, + .min_y = -FLT_MAX, .max_y = +FLT_MAX + }; + text_render_with_state(font, &state, body, text_x1, y); } -- cgit v1.2.3