diff options
| author | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2026-07-08 18:39:59 +0500 |
|---|---|---|
| committer | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2026-07-08 18:39:59 +0500 |
| commit | 586942c66f7d4aa71f0b60bb5de9ae19266f0cad (patch) | |
| tree | d744f24b2bfc5d4568c909b9bfbfb5cf85344474 /src/ui_elements/UI.c | |
| parent | 59f2cf27aab77ebb2deb341ef5e9fa04a1971e6f (diff) | |
scrolling
Diffstat (limited to 'src/ui_elements/UI.c')
| -rw-r--r-- | src/ui_elements/UI.c | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/src/ui_elements/UI.c b/src/ui_elements/UI.c index 7e00aa3..139f746 100644 --- a/src/ui_elements/UI.c +++ b/src/ui_elements/UI.c @@ -3,6 +3,7 @@ const int MAX_LINES = 256; const int SPACING_H = 1; +const float SCROLL_SENC = 10; void DrawRecBordered(Rectangle rec, Color bg, Color border_color, int border_width) { DrawRectangleRounded(rec, 0.015f, 8, bg); @@ -19,10 +20,11 @@ int CalculateSpans(Rectangle rec, char *text, Font *font, int height, int *spans int ptr = 0, span_ptr = 1, last_space_ptr; float acc = 0; while (text[ptr] != 0x0) { - float char_width = MeasureChar(font, height, text[ptr]); + char symbol = text[ptr]; + float char_width = MeasureChar(font, height, symbol); acc += char_width + SPACING_H; - if (text[ptr] == ' ') last_space_ptr = ptr; - if (acc > rec.width) { + 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; span_ptr++; @@ -33,8 +35,17 @@ int CalculateSpans(Rectangle rec, char *text, Font *font, int height, int *spans return span_ptr + 1; } -void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color) { +bool IsMouseInRec(Rectangle rec) { + Vector2 MousePos = GetMousePosition(); + return CheckCollisionPointRec(MousePos, rec); +} + +void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color, float padding, int *scroll) { int spans[MAX_LINES]; + rec.x += padding; + rec.y += padding; + rec.width -= 2 * padding; + rec.height -= 2 * padding; int lines = CalculateSpans(rec, text, font, height, spans); if (lines == 2) { @@ -43,7 +54,17 @@ void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color colo return; } - int vertical_acc = rec.y; + int vertical_acc = rec.y - *scroll; + int text_height = lines * height; + int max_scroll = text_height - rec.height; + + if (text_height >= rec.height && IsMouseInRec(rec)) { + *scroll -= GetMouseWheelMove() * SCROLL_SENC; + if (*scroll < 0) *scroll = 0; + if (*scroll > max_scroll) *scroll = max_scroll; + } + + BeginScissorMode(rec.x, rec.y, rec.width, rec.height); for (int i = 1; i < lines; i++) { Vector2 pos = { .x = rec.x, .y = vertical_acc }; char *line_start = text + spans[i-1]; @@ -53,4 +74,5 @@ void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color colo pos, height, SPACING_H, color); vertical_acc += height; } + EndScissorMode(); } |
