diff options
| author | physick <mynameisgennadiy@vk.com> | 2026-09-01 05:45:22 +0500 |
|---|---|---|
| committer | physick <mynameisgennadiy@vk.com> | 2026-09-01 05:45:22 +0500 |
| commit | da295397942073453ed9f82cc88dafd083d4c782 (patch) | |
| tree | 43d207c0a6b819c5b3e67ec5ed85df7b4a246b6e /src/ui_elements/UI.c | |
| parent | e40581d3b3aa343e84fa9a9492d93597e8899c5b (diff) | |
Text wrapping fix
There was a bug where \n wasn't handled correctly and the text was displayed multiple times
Diffstat (limited to 'src/ui_elements/UI.c')
| -rw-r--r-- | src/ui_elements/UI.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/ui_elements/UI.c b/src/ui_elements/UI.c index 85c7a02..ea3463a 100644 --- a/src/ui_elements/UI.c +++ b/src/ui_elements/UI.c @@ -27,10 +27,19 @@ int CalculateSpans(Rectangle rec, char *text, Font *font, int height, int *spans float char_width = MeasureChar(font, height, symbol); acc += char_width + SPACING_H; - if (symbol == ' ' || symbol == '\n') { + if (symbol == '\n') { + spans[span_ptr] = ptr; + spans += 1; + ptr += 1; + acc = 0; + continue; + } + + if (symbol == ' ') { last_space_ptr = ptr; } - if (acc > rec.width || symbol == '\n') { + + if (acc > rec.width) { if (last_space_ptr < spans[span_ptr - 1] || symbol == '\n' && last_space_ptr == -1) { spans[span_ptr] = (ptr > spans[span_ptr - 1]) ? ptr : ptr + 1; last_space_ptr = spans[span_ptr]; @@ -77,7 +86,7 @@ void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color colo return; } - int vertical_acc = scroll == NULL? rec.y : rec.y - *scroll; + int vertical_acc = scroll == NULL ? rec.y : rec.y - *scroll; int text_height = lines * height; int max_scroll = text_height - rec.height; @@ -93,12 +102,13 @@ void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color colo Vector2 pos = { .x = rec.x, .y = vertical_acc }; char *line_start = text + spans[i-1]; int line_length = spans[i] - spans[i-1]; - // printf("Drawing text '%.*s' at %f;%f\n", line_length, line_start, pos.x, pos.y); + // printf("Drawing text '%.*s' at %f;%f\n", line_length - 1, line_start, pos.x, pos.y); DrawTextEx(*font, - TextFormat("%.*s", line_length, line_start), + TextFormat("%.*s", line_length - 1, line_start), pos, height, SPACING_H, color); vertical_acc += height; } + // TraceLog(LOG_FATAL, "done"); EndScissorMode(); } |
