From d2711061b06a95bdf0170e86c1c723a32a6527ba Mon Sep 17 00:00:00 2001 From: physick Date: Sun, 30 Aug 2026 20:12:17 +0500 Subject: collection view start --- src/puzzles/collection.c | 52 +++++++++++++++++++++++++++++++++++++++----- src/puzzles/puzzle.h | 2 ++ src/scenes/collectionScene.c | 9 ++++++++ src/ui_elements/UI.c | 34 ++++++++++++++++++++++++----- 4 files changed, 86 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/puzzles/collection.c b/src/puzzles/collection.c index bb95eba..8abc3fa 100644 --- a/src/puzzles/collection.c +++ b/src/puzzles/collection.c @@ -1,6 +1,9 @@ #include #include "puzzle.h" +#include "../resources/resources.h" +#include "../ui_elements/UI.h" #include +#include #include #include #include @@ -28,6 +31,15 @@ bool isIcon(struct dirent *entry) { void addIcon(CollectionElement *el, struct dirent *entry) { if (el == NULL || entry == NULL) return; char fullPath[1024]; + snprintf(fullPath, sizeof(fullPath), "%s/%s", el->path, entry->d_name); +} + +bool isPuzzle(char *path) { + size_t entry_name_len = strlen(path); + if (entry_name_len < 4) return false; + char *last_bytes = path + entry_name_len - 3; + if (strncmp(last_bytes, ".pz", 3) != 0) return false; + return true; } bool isElement(struct dirent *entry, char *basePath) { @@ -41,12 +53,23 @@ bool isElement(struct dirent *entry, char *basePath) { if (stat(full_path_buf, &st) != 0) return false; if (S_ISDIR(st.st_mode)) return true; - size_t entry_name_len = strlen(entry->d_name); - if (entry_name_len < 4) return false; - char *last_bytes = entry->d_name + entry_name_len - 3; - if (strncmp(last_bytes, ".pz", 3) != 0) return false; + return isPuzzle(entry->d_name); +} - return true; +char *getName(char *name) { + char buf[1024]; + strncpy(buf, name, sizeof(buf)); + size_t len = strlen(name); + + // Cutting a file extention + for (size_t i = 0; i < len; ++i) { + if (buf[i] == '.') { + buf[i] = 0x0; + break; + } + } + + return strdup(buf); } size_t getElementCount(DIR *dir, CollectionElement *el) { @@ -102,6 +125,8 @@ void initCollectionFromDir(CollectionElement *el) { } read_el->IsInitialized = false; read_el->path = strdup(full_path_buf); + read_el->name = getName(entry->d_name); + read_el->icon = NULL; idx += 1; } @@ -124,3 +149,20 @@ void InitializeCollection(CollectionElement *el) { break; } } + +void DrawCollectionElement(CollectionElement *el, Rectangle rec) { + if (el == NULL) return; + if (el->icon != NULL) { + printf("xdx\n"); + DrawTexturePro(*el->icon, (Rectangle) { + .x = 0, + .y = 0, + .width = el->icon->width, + .height = el->icon->height, + }, rec, (Vector2) { .x = 0, .y = 0 }, 0, WHITE); + } else { + DrawRecBordered(rec, COLORSCHEME_WHITE, COLORSCHEME_ORANGE, BORDER); + } + int scroll = 0; + DrawTextInRec(rec, el->name, GetMainFont(), 20, COLORSCHEME_BROWN, 1, &scroll); +} diff --git a/src/puzzles/puzzle.h b/src/puzzles/puzzle.h index 90ee160..a65ae6f 100644 --- a/src/puzzles/puzzle.h +++ b/src/puzzles/puzzle.h @@ -101,6 +101,7 @@ typedef enum { typedef struct CollectionElement { char *path; + char *name; struct CollectionElement *children; size_t childCount; Texture2D *icon; @@ -109,5 +110,6 @@ typedef struct CollectionElement { } CollectionElement; void InitializeCollection(CollectionElement *el); +void DrawCollectionElement(CollectionElement *el, Rectangle rec); #endif // PUZZLE_H diff --git a/src/scenes/collectionScene.c b/src/scenes/collectionScene.c index 2a9fc22..a5a650f 100644 --- a/src/scenes/collectionScene.c +++ b/src/scenes/collectionScene.c @@ -52,6 +52,15 @@ bool drawCollectionScene(void *self) { DrawBackground(); DrawCollectionName(data->Name); DrawPageView(GetScreenHeight() - 60, 5, 2); + + for (size_t i = 0; i < data->collection->childCount; ++i) { + DrawCollectionElement(&data->collection->children[i], (Rectangle) { + .x = 10 + 210 * i, + .y = 80, + .width = 200, + .height = 450, + }); + } return true; } 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 +#include #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); -- cgit v1.3