summaryrefslogtreecommitdiff
path: root/src/scenes/collectionScene.c
blob: cda6fd1bd55c0f0dd0d445af9d52595d21ba2098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <dirent.h>
#include <raylib.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scenes.h"
#include "../ui_elements/UI.h"
#include "../resources/resources.h"
#include "../utils/arena.h"
#include "../resources/TextruesAtlas.h"

static const size_t ARENA_SIZE = 1024 * 64;

typedef struct {
    CollectionElement *collection;
    Button *backButton;
    size_t count;
} CollectionSceneData;

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: 
            AddScene(CreatePuzzlePreview(ReadPuzzleFromFile(caller->path))); 
            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) {
    Vector2 width = MeasureTextEx(*GetMainFont(), name, 50, 1);
    int screen_width = GetScreenWidth();
    Vector2 pos = { .y = 20, .x = (screen_width - width.x) / 2, };
    DrawTextEx(*GetMainFont(), name, pos, HEADER_FONT_SIZE, 1, COLORSCHEME_BROWN);
}

// Does not allocate the memory
char *getCollectionName(char *path) {
    char *outp = path;
    size_t len = strlen(path);
    for (size_t i = 0; i < len; ++i) {
        if ((path[i] == '/' || path[i] == '\\') && path[i+1] != 0x0) {
            outp = path + i + 1;
        }
    }
    return outp;
}

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]);
        }
    if (data->backButton != NULL) UpdateButton(data->backButton);

    return true;
}

bool drawCollectionScene(void *self) {
    CollectionSceneData *data = getData(self);
    DrawBackground();
    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) {
                .x = 10 + 210 * i,
                .y = 80,
                .width = 200,
                .height = 450,
                });
    }
    return true;
}

void deinitCollectionScene(void *self) {
    TraceLog(LOG_INFO, "Deleting a collection scene");
    CollectionSceneData *data = getData(self);
    if (data != NULL) {
        if (data->collection != NULL) 
            DeinitCollectionElement(data->collection);
        free(data);
    }
    free((Scene *)self);
}

Scene *CreatePuzzleCollectionScene(CollectionElement *el) {
    TraceLog(LOG_INFO, "Creating a collection scene");
    Scene *outp = malloc(sizeof(Scene));
    if (outp == NULL) return NULL;
    outp->Update = updateCollectionScene;
    outp->Draw = drawCollectionScene;
    outp->Deint = deinitCollectionScene;
    outp->IsTransparent = false;
    
    CollectionSceneData *data = malloc(sizeof(CollectionSceneData));
    if (data == NULL) {
        free(outp);
        return NULL;
    }

    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 = BACK_BUTTON_SMALL_LOCATION;
    }
    data->backButton = backButton;

    outp->data = data;

    return outp;
}