summaryrefslogtreecommitdiff
path: root/src/scenes/collectionScene.c
blob: b8c1bc3513c2154e9b28541b2c9df9b475d18bc5 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <dirent.h>
#include <raylib.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.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 float MENU_SCROLL_SENC = 75.0f;
static const size_t ARENA_SIZE = 1024 * 64;
static const int SCENE_PADDING = 30;
static const int FOOTER_PADDING = 60;
static const float ARROWS_HEIGHT = 0.4f;
static const uint8_t ARROWS_ALPHA = 128;
/// Height / width
static const float ELEMENT_WIDTH_RATIO = 0.45f;

typedef struct {
    /// The collection we are displaying
    CollectionElement *collection;
    /// The collection we selected. Used in the transitions
    CollectionElement *selected;
    Button *backButton;
    float blackout_state;
    PopUpState state;
    float scroll;
} CollectionSceneData;


CollectionSceneData *getData(void *self) {
    return ((Scene *)self)->data;
}

PopUpState getCollectionSceneState(void *self) {
    if (self == NULL) return POPUP_ACTIVE;
    CollectionSceneData *data = getData(self);
    if (data == NULL) return POPUP_ACTIVE;
    return data->state;
}


void reopenCollectionScene(void *self, PopUpState PreviousSceneState) {
    if (self == NULL) return;
    CollectionSceneData *data = getData(self);
    if (data == NULL) return;
    if (data->state != POPUP_HIDING) printf("xdx\n");
    switch (PreviousSceneState) {
        case POPUP_HIDING:
            data->state = POPUP_REVEALING;
            data->blackout_state = 1.0f;
            break;
        default:
            data->state = POPUP_ACTIVE;
            break;
    }
}

static void backButton_onClick(Scene *self) {
    CollectionSceneData *data = getData(self);
    data->state = POPUP_HIDING;
    data->selected = NULL;
    data->blackout_state = 0;
    // 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))); 
        case COLLECTION_ELEMENT_COLLECTION: 
            if (caller->callbackData == NULL) {
                TraceLog(LOG_WARNING, "The callback data is NULL. Cannot initiate the smooth transition");
                AddScene(CreatePuzzlePreview(ReadPuzzleFromFile(caller->path))); 
            } else {
                TraceLog(LOG_WARNING, "The callback data is not NULL");
                CollectionSceneData *data = ((Scene *)caller->callbackData)->data;
                data->selected = caller;
                data->state = POPUP_HIDING;
                data->blackout_state = 0;
            }
            // AddScene(CreatePuzzleCollectionScene(caller, false)); 
            break;
        default:
            TraceLog(LOG_WARNING, "Invalid CollectionElement type for %s", caller->path);
            break;
    }
}

void DrawCollectionName(char *name) {
    Vector2 width = MeasureTextEx(*GetMainFont(), name, HEADER_FONT_SIZE, 1);
    int screen_width = GetScreenWidth();
    Vector2 pos = { .y = SCENE_PADDING , .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->state == POPUP_REVEALING) {
        data->blackout_state -= BLACKOUT_SPEED * GetFrameTime();
        if (data->blackout_state <= 0) {
            data->blackout_state = 0;
            data->state = POPUP_ACTIVE;
            TraceLog(LOG_INFO, "Done revealing");
        }
        return true;
    } 

    if (data->state == POPUP_HIDING) {
        data->blackout_state += BLACKOUT_SPEED * GetFrameTime();
        if (data->blackout_state >= 1) {
            TraceLog(LOG_INFO, "Done hiding");
            if (data->selected == NULL)
                DeleteSceneFromStack();
            else
                AddScene(CreatePuzzleCollectionScene(data->selected, true));
        }
        return true;
    } 

    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);
    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
                });

    float element_height = GetScreenHeight() - SCENE_PADDING * 2 - HEADER_FONT_SIZE - FOOTER_PADDING;
    float element_width = element_height * ELEMENT_WIDTH_RATIO;
    for (size_t i = 0; i < data->collection->childCount; ++i) {
        DrawCollectionElement(&data->collection->children[i], data->collection, (Rectangle) {
                .x = SCENE_PADDING + (element_width + PADDING) * i - data->scroll,
                .y = SCENE_PADDING + HEADER_FONT_SIZE,
                .width = element_width,
                .height = element_height,
                });
    }

    data->scroll -= GetMouseWheelMove() * MENU_SCROLL_SENC;
    if (data->scroll < 0) data->scroll = 0;
    float max_scroll = (element_width + PADDING) * data->collection->childCount + SCENE_PADDING * 2 - GetScreenWidth();
    if (max_scroll < 0) max_scroll = 0;
    if (data->scroll > max_scroll) data->scroll = max_scroll;

    float arrows_height_px = GetScreenHeight() * ARROWS_HEIGHT;
    Color arrows_color = { .r = 255, .g = 255, .b = 255, .a = ARROWS_ALPHA, };


    if (data->scroll != 0)
        DrawTexturePro(*GetButtonsAtlas(), 
                LEFT_ARROW_ICON_LOCATION, 
                (Rectangle) {
                .x = 0,
                .y = (GetScreenHeight() - arrows_height_px) / 2,
                .width = SCENE_PADDING,
                .height = arrows_height_px
                }, (Vector2) {0}, 0, arrows_color);

    if (data->scroll != max_scroll)
        DrawTexturePro(*GetButtonsAtlas(), 
                RIGHT_ARROW_ICON_LOCATION, 
                (Rectangle) {
                .x = GetScreenWidth() - SCENE_PADDING,
                .y = (GetScreenHeight() - arrows_height_px) / 2,
                .width = SCENE_PADDING,
                .height = arrows_height_px
                }, (Vector2) {0}, 0, arrows_color);

    if (data->state != POPUP_ACTIVE) {
        DrawRectangle(0, 0, 
                GetScreenWidth(), GetScreenHeight(), 
                (Color) {0, 0, 0, 255 * (data->blackout_state >= 1 ? 1 : data->blackout_state) });
    }

    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, bool smooothEntry) {
    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;
    
    outp->Reopen = reopenCollectionScene;
    outp->GetCurrentState = getCollectionSceneState;

    CollectionSceneData *data = malloc(sizeof(CollectionSceneData));
    if (data == NULL) {
        free(outp);
        return NULL;
    }

    if (!el->IsInitialized) InitializeCollection(el, collectionElement_onSelect, outp);
    data->collection = el;
    data->selected = NULL;
    data->state = smooothEntry? POPUP_REVEALING : POPUP_ACTIVE;
    data->blackout_state = 1.0f;
    data->scroll = 0.0f;

    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;
}