diff options
Diffstat (limited to 'src/ui_elements')
| -rw-r--r-- | src/ui_elements/UI.c | 32 | ||||
| -rw-r--r-- | src/ui_elements/UI.h | 2 |
2 files changed, 28 insertions, 6 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(); } diff --git a/src/ui_elements/UI.h b/src/ui_elements/UI.h index b98ece0..5868efe 100644 --- a/src/ui_elements/UI.h +++ b/src/ui_elements/UI.h @@ -4,6 +4,6 @@ #define UI_ELEMENTS_H void DrawRecBordered(Rectangle rec, Color bg, Color border_color, int border_width); -void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color); +void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color, float padding, int *scroll); #endif // UI_ELEMENTS_H |
