From e515240297e1d8dd6f23308f0bffcda7464486e7 Mon Sep 17 00:00:00 2001 From: physick Date: Mon, 31 Aug 2026 02:13:58 +0500 Subject: collection element callback --- src/puzzles/collection.c | 17 ++++++++++++++--- src/puzzles/puzzle.h | 10 +++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'src/puzzles') 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 -- cgit v1.3