summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c2
-rw-r--r--src/puzzles/collection.c14
-rw-r--r--src/puzzles/puzzle.h15
-rw-r--r--src/scenes/collectionScene.c50
-rw-r--r--src/scenes/scenes.h2
5 files changed, 67 insertions, 16 deletions
diff --git a/src/main.c b/src/main.c
index 39f6a68..6d563e2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,7 +31,7 @@ int main(void) {
return 1;
}
- if (!AddScene(CreatePuzzleCollectionScene("puzzles", &el))) {
+ if (!AddScene(CreatePuzzleCollectionScene(&el))) {
printf("Failed to create a collection scene\n");
return 1;
}
diff --git a/src/puzzles/collection.c b/src/puzzles/collection.c
index 71eaadf..ca2743c 100644
--- a/src/puzzles/collection.c
+++ b/src/puzzles/collection.c
@@ -151,7 +151,7 @@ void InitializeCollection(CollectionElement *el, void (*callback)(CollectionElem
initCollectionFromDir(el, callback);
break;
default:
- TraceLog(LOG_WARNING, "Invalid CollectionElement type");
+ TraceLog(LOG_WARNING, "Invalid CollectionElement type for %s", el->path);
break;
}
}
@@ -191,3 +191,15 @@ void UpdateCollectionElement(CollectionElement *el) {
}
}
}
+
+void DeinitCollectionElement(CollectionElement *el) {
+ if (el == NULL || !el->IsInitialized) return;
+ if (el->path != NULL) free(el->path);
+ if (el->name != NULL) free(el->name);
+ if (el->hasIcon) UnloadTexture(el->icon);
+
+ for (size_t i = 0; i < el->childCount; ++i) {
+ DeinitCollectionElement(&el->children[i]);
+ }
+ free(el);
+}
diff --git a/src/puzzles/puzzle.h b/src/puzzles/puzzle.h
index 21d8944..5af6d6e 100644
--- a/src/puzzles/puzzle.h
+++ b/src/puzzles/puzzle.h
@@ -100,17 +100,27 @@ typedef enum {
} CollectionElementType;
+/// A collection node. Corresponds to either a directory in the FS or a .pz file
typedef struct CollectionElement {
+ /// The icon used for the puzzles in the collection
Texture2D icon;
+ /// Used in the click detection. Do not modify from user space
Rectangle _previousFramePosition;
+ /// The path to the directory/puzzle
char *path;
+ /// The display name of an element
char *name;
+ /// The tree child nodes array
struct CollectionElement *children;
- void (*callback)(struct CollectionElement *);
+ /// The tree child nodes count
size_t childCount;
+ /// OnClick callback
+ void (*callback)(struct CollectionElement *);
+ /// If the element is initialized it has children and an icon loaded into mem
bool IsInitialized;
+ /// The element has an icon
bool hasIcon;
- bool isPressed;
+ /// The type of the element
CollectionElementType type;
} CollectionElement;
@@ -120,5 +130,6 @@ typedef struct CollectionElement {
void InitializeCollection(CollectionElement *el, void (*callback)(CollectionElement *));
void DrawCollectionElement(CollectionElement *el, CollectionElement *parent, Rectangle rec);
void UpdateCollectionElement(CollectionElement *el);
+void DeinitCollectionElement(CollectionElement *el);
#endif // PUZZLE_H
diff --git a/src/scenes/collectionScene.c b/src/scenes/collectionScene.c
index fff814a..508e4d5 100644
--- a/src/scenes/collectionScene.c
+++ b/src/scenes/collectionScene.c
@@ -12,11 +12,8 @@
static const size_t ARENA_SIZE = 1024 * 64;
typedef struct {
- Arena arena;
- char *DirPath;
- char *Name;
-
CollectionElement *collection;
+ Button *backButton;
size_t count;
} CollectionSceneData;
@@ -24,9 +21,20 @@ CollectionSceneData *getData(void *self) {
return ((Scene *)self)->data;
}
+void backButton_onClick(Scene *self) {
+ DeleteSceneFromStack();
+}
+
void collectionElement_onSelect(CollectionElement *caller) {
if (caller == NULL) return;
TraceLog(LOG_INFO, "Selected element %s", caller->name);
+ switch (caller->type) {
+ case COLLECTION_ELEMENT_PUZZLE: break;
+ case COLLECTION_ELEMENT_COLLECTION: AddScene(CreatePuzzleCollectionScene(caller)); break;
+ default:
+ TraceLog(LOG_WARNING, "Invalid CollectionElement type for %s", caller->path);
+ break;
+ }
}
void DrawCollectionName(char *name) {
@@ -54,14 +62,23 @@ bool updateCollectionScene(void *self) {
for (size_t i = 0; i < data->collection->childCount; ++i) {
UpdateCollectionElement(&data->collection->children[i]);
}
+ if (data->backButton != NULL) UpdateButton(data->backButton);
+
return true;
}
bool drawCollectionScene(void *self) {
CollectionSceneData *data = getData(self);
DrawBackground();
- DrawCollectionName(data->Name);
+ DrawCollectionName(data->collection->name);
DrawPageView(GetScreenHeight() - 60, 5, 2);
+ if (data->backButton != NULL)
+ DrawButton(data->backButton, &(Rectangle) {
+ .x = 10,
+ .y = GetScreenHeight() - 60,
+ .width = 36,
+ .height = 36, // TODO: make it match the page view scale
+ });
for (size_t i = 0; i < data->collection->childCount; ++i) {
DrawCollectionElement(&data->collection->children[i], data->collection, (Rectangle) {
@@ -78,13 +95,14 @@ void deinitCollectionScene(void *self) {
TraceLog(LOG_INFO, "Deleting a collection scene");
CollectionSceneData *data = getData(self);
if (data != NULL) {
- free(data->arena.mem);
+ if (data->collection != NULL)
+ DeinitCollectionElement(data->collection);
free(data);
}
free((Scene *)self);
}
-Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el) {
+Scene *CreatePuzzleCollectionScene(CollectionElement *el) {
TraceLog(LOG_INFO, "Creating a collection scene");
Scene *outp = malloc(sizeof(Scene));
if (outp == NULL) return NULL;
@@ -98,14 +116,24 @@ Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el) {
free(outp);
return NULL;
}
- data->arena = (Arena){0};
- ArenaInit(&data->arena, ARENA_SIZE);
- data->DirPath = ArenaStrdup(&data->arena, path);
- data->Name = getCollectionName(path);
if (!el->IsInitialized) InitializeCollection(el, collectionElement_onSelect);
data->collection = el;
+ Button *backButton = malloc(sizeof(Button));
+ if (backButton != NULL) {
+ backButton->parent = outp;
+ backButton->OnClick = backButton_onClick;
+ backButton->atlas = GetButtonsAtlas();
+ backButton->textrueLocation = (Rectangle) {
+ .x = 0,
+ .y = 0,
+ .width = 32,
+ .height = 32,
+ };
+ }
+ data->backButton = backButton;
+
outp->data = data;
return outp;
diff --git a/src/scenes/scenes.h b/src/scenes/scenes.h
index 659dcfd..f71c2ee 100644
--- a/src/scenes/scenes.h
+++ b/src/scenes/scenes.h
@@ -33,6 +33,6 @@ Scene *CreatePuzzleScene(Puzzle *puzzle);
Scene *CreateDistractionScene(DistractionPuzzle *puzzle, void (*addTime)(Scene *, size_t));
Scene *CreateResultView(SubmitionResult *result);
-Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el);
+Scene *CreatePuzzleCollectionScene(CollectionElement *el);
#endif // SCENES_H