diff options
| author | physick <mynameisgennadiy@vk.com> | 2026-08-31 02:13:58 +0500 |
|---|---|---|
| committer | physick <mynameisgennadiy@vk.com> | 2026-08-31 02:13:58 +0500 |
| commit | e515240297e1d8dd6f23308f0bffcda7464486e7 (patch) | |
| tree | 3f684236b27b99a8712456f844cba01503d2b360 | |
| parent | ce9cbb6c0dbd8c920fdb87389332028cdd60ccc1 (diff) | |
collection element callback
| -rw-r--r-- | src/puzzles/collection.c | 17 | ||||
| -rw-r--r-- | src/puzzles/puzzle.h | 10 | ||||
| -rw-r--r-- | src/scenes/collectionScene.c | 12 | ||||
| -rw-r--r-- | src/ui_elements/UI.h | 1 |
4 files changed, 35 insertions, 5 deletions
diff --git a/src/puzzles/collection.c b/src/puzzles/collection.c index c382259..71eaadf 100644 --- a/src/puzzles/collection.c +++ b/src/puzzles/collection.c @@ -87,7 +87,7 @@ size_t getElementCount(DIR *dir, CollectionElement *el) { return outp; } -void initCollectionFromDir(CollectionElement *el) { +void initCollectionFromDir(CollectionElement *el, void (*callback)(CollectionElement *)) { if (el == NULL) return; TraceLog(LOG_INFO, "Initializing a collection element from %s/", el->path); @@ -132,6 +132,7 @@ void initCollectionFromDir(CollectionElement *el) { read_el->IsInitialized = false; read_el->path = strdup(full_path_buf); read_el->name = getName(entry->d_name); + read_el->callback = callback; idx += 1; } @@ -139,7 +140,7 @@ void initCollectionFromDir(CollectionElement *el) { closedir(dir); } -void InitializeCollection(CollectionElement *el) { +void InitializeCollection(CollectionElement *el, void (*callback)(CollectionElement *)) { if (el == NULL) { TraceLog(LOG_ERROR, "Failed to initialize collection element: the element is NULL"); return; @@ -147,7 +148,7 @@ void InitializeCollection(CollectionElement *el) { switch (el->type) { case COLLECTION_ELEMENT_PUZZLE: break; case COLLECTION_ELEMENT_COLLECTION: - initCollectionFromDir(el); + initCollectionFromDir(el, callback); break; default: TraceLog(LOG_WARNING, "Invalid CollectionElement type"); @@ -157,6 +158,7 @@ void InitializeCollection(CollectionElement *el) { void DrawCollectionElement(CollectionElement *el, CollectionElement *parent, Rectangle rec) { if (el == NULL || parent == NULL) return; + el->_previousFramePosition = rec; DrawRecBordered(rec, COLORSCHEME_WHITE, COLORSCHEME_ORANGE, BORDER); if (el->type == COLLECTION_ELEMENT_PUZZLE && parent->hasIcon) { Rectangle texture_rec = { @@ -180,3 +182,12 @@ void DrawCollectionElement(CollectionElement *el, CollectionElement *parent, Rec .height = rec.height * NAME_IN_CARD_RATIO - BORDER * 2, }, el->name, GetMainFont(), 20, COLORSCHEME_BROWN, 1, &scroll); } + +void UpdateCollectionElement(CollectionElement *el) { + if (el == NULL) return; + if (IsMouseInRec(el->_previousFramePosition)) { + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { + el->callback(el); + } + } +} diff --git a/src/puzzles/puzzle.h b/src/puzzles/puzzle.h index cea0dba..21d8944 100644 --- a/src/puzzles/puzzle.h +++ b/src/puzzles/puzzle.h @@ -99,18 +99,26 @@ typedef enum { COLLECTION_ELEMENT_UNKNOWN } CollectionElementType; + typedef struct CollectionElement { Texture2D icon; + Rectangle _previousFramePosition; char *path; char *name; struct CollectionElement *children; + void (*callback)(struct CollectionElement *); size_t childCount; bool IsInitialized; bool hasIcon; + bool isPressed; CollectionElementType type; } CollectionElement; -void InitializeCollection(CollectionElement *el); +/// Accepts an element which children will be created and a generic callback that +/// will be called when the collection enement is selected. +/// The element passes a selected element as a callback argummment +void InitializeCollection(CollectionElement *el, void (*callback)(CollectionElement *)); void DrawCollectionElement(CollectionElement *el, CollectionElement *parent, Rectangle rec); +void UpdateCollectionElement(CollectionElement *el); #endif // PUZZLE_H diff --git a/src/scenes/collectionScene.c b/src/scenes/collectionScene.c index 17d36c9..fff814a 100644 --- a/src/scenes/collectionScene.c +++ b/src/scenes/collectionScene.c @@ -24,6 +24,11 @@ CollectionSceneData *getData(void *self) { return ((Scene *)self)->data; } +void collectionElement_onSelect(CollectionElement *caller) { + if (caller == NULL) return; + TraceLog(LOG_INFO, "Selected element %s", caller->name); +} + void DrawCollectionName(char *name) { Vector2 width = MeasureTextEx(*GetMainFont(), name, 50, 1); int screen_width = GetScreenWidth(); @@ -44,6 +49,11 @@ char *getCollectionName(char *path) { } bool updateCollectionScene(void *self) { + CollectionSceneData *data = getData(self); + if (data->collection != NULL) + for (size_t i = 0; i < data->collection->childCount; ++i) { + UpdateCollectionElement(&data->collection->children[i]); + } return true; } @@ -93,7 +103,7 @@ Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el) { data->DirPath = ArenaStrdup(&data->arena, path); data->Name = getCollectionName(path); - if (!el->IsInitialized) InitializeCollection(el); + if (!el->IsInitialized) InitializeCollection(el, collectionElement_onSelect); data->collection = el; outp->data = data; diff --git a/src/ui_elements/UI.h b/src/ui_elements/UI.h index 52d2222..3665de8 100644 --- a/src/ui_elements/UI.h +++ b/src/ui_elements/UI.h @@ -31,6 +31,7 @@ bool IsMouseInRec(Rectangle rec); typedef struct { Scene *parent; + void *userData; void (*OnClick)(Scene *); Texture *atlas; Rectangle textrueLocation; |
