summaryrefslogtreecommitdiff
path: root/src/ui_elements/UI.c
diff options
context:
space:
mode:
authorphysick <mynameisgennadiy@vk.com>2026-08-30 20:12:17 +0500
committerphysick <mynameisgennadiy@vk.com>2026-08-30 20:12:17 +0500
commitd2711061b06a95bdf0170e86c1c723a32a6527ba (patch)
tree13ec59966abdbef2672448300d2dd7c8f6e89a8c /src/ui_elements/UI.c
parent1e7ec08dc9c62df43940d00c1fae4a6e54464b86 (diff)
collection view start
Diffstat (limited to 'src/ui_elements/UI.c')
-rw-r--r--src/ui_elements/UI.c34
1 files changed, 28 insertions, 6 deletions
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 <raylib.h>
+#include <stdio.h>
#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);