diff options
Diffstat (limited to 'src/mgui/richtext.c')
-rw-r--r-- | src/mgui/richtext.c | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/src/mgui/richtext.c b/src/mgui/richtext.c index 9b1acfc..77ee3c1 100644 --- a/src/mgui/richtext.c +++ b/src/mgui/richtext.c @@ -105,6 +105,10 @@ static void mgui_richtext_vertices_free(mgui_richtext *self, size_t vertex_index } mgui_richtext* mgui_richtext_create(const char *str, size_t size, unsigned char character_size) { + /* TODO: Make this work for character size >= 100 */ + if(character_size >= 100) + character_size = 99; + mgui_richtext *richtext = mgui_alloc(sizeof(mgui_richtext)); mgui_widget_init(&richtext->widget, MGUI_WIDGET_RICHTEXT); richtext->str = mgui_alloc(size); @@ -120,6 +124,7 @@ mgui_richtext* mgui_richtext_create(const char *str, size_t size, unsigned char richtext->vertex_data[i].vertex_count = 0; } richtext->dirty = true; + richtext->vertices_dirty = true; return richtext; } @@ -177,7 +182,7 @@ static void mgui_richtext_append_glyph(mgui_richtext *self, size_t vertex_index, mgui_richtext_vertices_append(self, vertex_index, &top_right_vertex); } -static void mgui_richtext_update(mgui_richtext *self) { +static void mgui_richtext_update(mgui_richtext *self, bool build_vertices) { for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) { mgui_richtext_vertices_clear(self, i); } @@ -231,9 +236,11 @@ static void mgui_richtext_update(mgui_richtext *self) { if(position.x + glyph.size.x > self->width) { position.x = 0; position.y += self->character_size; + self->render_size.y += self->character_size; } - mgui_richtext_append_glyph(self, vertex_index, position, color, &glyph); + if(build_vertices) + mgui_richtext_append_glyph(self, vertex_index, position, color, &glyph); position.x += glyph.advance + mgl_font_get_kerning(font, prev_codepoint, codepoint); self->render_size.x = max_int(self->render_size.x, position.x); } @@ -249,13 +256,16 @@ void mgui_richtext_calculate_size(mgui_richtext *self, mgl_vec2i max_size) { /* TODO: Do not update if not visible on screen? */ if(max_size.x != self->width) { self->width = max_size.x; - self->dirty = true; + const bool is_multiple_lines = self->render_size.y > (int)self->character_size; + if(is_multiple_lines || self->width < self->render_size.x) + self->dirty = true; } /* TODO: Instead of updating richtext vertices, calculcate the richtext bounds only and update the vertices in the draw function if dirty */ if(self->dirty) { self->dirty = false; - mgui_richtext_update(self); + mgui_richtext_update(self, false); + self->vertices_dirty = true; self->widget.size.x = self->render_size.x; self->widget.size.y = min_int(self->render_size.y, max_size.y); } @@ -271,9 +281,9 @@ void mgui_richtext_on_event(mgui_richtext *self, mgl_window *window, mgl_event * void mgui_richtext_draw(mgui_richtext *self, mgl_window *window) { if(mgui_rectangle_intersects_with_scissor(self->position, self->render_size, window)) { /* This can happen when the item is first not visible in its scissor and then becomes visible */ - if(self->dirty) { - self->dirty = false; - mgui_richtext_update(self); + if(self->vertices_dirty) { + self->vertices_dirty = false; + mgui_richtext_update(self, true); } const mgui_font_type font_types[NUM_VERTEX_DATA] = { @@ -295,6 +305,6 @@ void mgui_richtext_draw(mgui_richtext *self, mgl_window *window) { for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) { mgui_richtext_vertices_free(self, i); } - self->dirty = true; + self->vertices_dirty = true; } } |