summaryrefslogtreecommitdiff
path: root/src/ui_elements/UI.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui_elements/UI.c')
-rw-r--r--src/ui_elements/UI.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/ui_elements/UI.c b/src/ui_elements/UI.c
index fb469e3..7e00aa3 100644
--- a/src/ui_elements/UI.c
+++ b/src/ui_elements/UI.c
@@ -1,7 +1,56 @@
#include "UI.h"
#include <raylib.h>
+const int MAX_LINES = 256;
+const int SPACING_H = 1;
+
void DrawRecBordered(Rectangle rec, Color bg, Color border_color, int border_width) {
DrawRectangleRounded(rec, 0.015f, 8, bg);
DrawRectangleRoundedLinesEx(rec, 0.015f, 8, border_width, border_color);
}
+
+float MeasureChar(Font *font, int height, char symbol) {
+ char buf[2] = { symbol, 0 };
+ return MeasureTextEx(*font, buf, height, SPACING_H).x;
+}
+
+int CalculateSpans(Rectangle rec, char *text, Font *font, int height, int *spans) {
+ spans[0] = 0;
+ int ptr = 0, span_ptr = 1, last_space_ptr;
+ float acc = 0;
+ while (text[ptr] != 0x0) {
+ float char_width = MeasureChar(font, height, text[ptr]);
+ acc += char_width + SPACING_H;
+ if (text[ptr] == ' ') last_space_ptr = ptr;
+ if (acc > rec.width) {
+ spans[span_ptr] = last_space_ptr + 1;
+ ptr = last_space_ptr;
+ span_ptr++;
+ acc = 0;
+ }
+ ptr++;
+ }
+ return span_ptr + 1;
+}
+
+void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color) {
+ int spans[MAX_LINES];
+ int lines = CalculateSpans(rec, text, font, height, spans);
+
+ if (lines == 2) {
+ Vector2 pos = {.x = rec.x, .y = rec.y};
+ DrawTextEx(*font, text, pos, height, SPACING_H, color);
+ return;
+ }
+
+ int vertical_acc = rec.y;
+ for (int i = 1; i < lines; i++) {
+ Vector2 pos = { .x = rec.x, .y = vertical_acc };
+ char *line_start = text + spans[i-1];
+ int line_length = spans[i] - spans[i-1];
+ DrawTextEx(*font,
+ TextFormat("%.*s", line_length, line_start),
+ pos, height, SPACING_H, color);
+ vertical_acc += height;
+ }
+}