From d2711061b06a95bdf0170e86c1c723a32a6527ba Mon Sep 17 00:00:00 2001 From: physick Date: Sun, 30 Aug 2026 20:12:17 +0500 Subject: collection view start --- src/ui_elements/UI.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'src/ui_elements') diff --git a/src/ui_elements/UI.c b/src/ui_elements/UI.c index 9f17826..85c7a02 100644 --- a/src/ui_elements/UI.c +++ b/src/ui_elements/UI.c @@ -1,5 +1,6 @@ #include "UI.h" #include +#include #include "../resources/resources.h" const int MAX_LINES = 8192; @@ -17,22 +18,42 @@ float MeasureChar(Font *font, int height, char symbol) { int CalculateSpans(Rectangle rec, char *text, Font *font, int height, int *spans) { spans[0] = 0; - int ptr = 0, span_ptr = 1, last_space_ptr; + int ptr = 0, span_ptr = 1; + int last_space_ptr = -1; float acc = 0; + while (text[ptr] != 0x0) { char symbol = text[ptr]; float char_width = MeasureChar(font, height, symbol); acc += char_width + SPACING_H; - if (symbol == ' ' || symbol == '\n') last_space_ptr = ptr; + + if (symbol == ' ' || symbol == '\n') { + last_space_ptr = ptr; + } if (acc > rec.width || symbol == '\n') { - spans[span_ptr] = last_space_ptr + 1; - ptr = last_space_ptr; + 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]; + ptr = spans[span_ptr] - 1; + } else { + if (spans[span_ptr - 1] == last_space_ptr) { + spans[span_ptr] = ptr; + last_space_ptr = ptr; + } else { + spans[span_ptr] = last_space_ptr + 1; + } + ptr = last_space_ptr; + } + // printf("span %d = '%.*s'\n", span_ptr, spans[span_ptr] - spans[span_ptr - 1], text + spans[span_ptr - 1]); span_ptr++; acc = 0; } ptr++; - if (ptr >= MAX_LINES) break; + if (span_ptr >= MAX_LINES) break; } + + spans[span_ptr] = ptr; + // printf("span %d = '%.*s'\n", span_ptr, spans[span_ptr] - spans[span_ptr - 1], text + spans[span_ptr - 1]); return span_ptr + 1; } @@ -56,7 +77,7 @@ void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color colo return; } - int vertical_acc = 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; @@ -72,6 +93,7 @@ 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); DrawTextEx(*font, TextFormat("%.*s", line_length, line_start), pos, height, SPACING_H, color); -- cgit v1.3