From 586942c66f7d4aa71f0b60bb5de9ae19266f0cad Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Wed, 8 Jul 2026 18:39:59 +0500 Subject: scrolling --- src/puzzles/puzzle.c | 2 +- src/scenes/puzzleScene.c | 3 ++- src/ui_elements/UI.c | 32 +++++++++++++++++++++++++++----- src/ui_elements/UI.h | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/puzzles/puzzle.c b/src/puzzles/puzzle.c index 8c2c3de..1bfb6be 100644 --- a/src/puzzles/puzzle.c +++ b/src/puzzles/puzzle.c @@ -7,7 +7,7 @@ Puzzle *GetTestPuzzle() { if (outp == NULL) return NULL; outp->Name = strdup("Test puzzle"); - outp->Description = strdup("In this puzzle you will have to return the input string"); + outp->Description = strdup("In this puzzle you will have to return the input string\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-\nThis is a line that should be somewhere down"); return outp; } diff --git a/src/scenes/puzzleScene.c b/src/scenes/puzzleScene.c index 1706ff2..70a3667 100644 --- a/src/scenes/puzzleScene.c +++ b/src/scenes/puzzleScene.c @@ -6,6 +6,7 @@ typedef struct { Puzzle *PuzzlePtr; + int scroll; } puzzleSceneData; @@ -75,7 +76,7 @@ bool drawPuzzleScene(void *self) { DrawRecBordered(pane2, WHITE, COLORSCHEME_ORANGE, BORDER); DrawTextInRec(pane1, data->PuzzlePtr->Description, - GetMainFont(), 20, COLORSCHEME_BROWN); + GetMainFont(), 20, COLORSCHEME_BROWN, 3, &data->scroll); return true; } 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 -- cgit v1.3