summaryrefslogtreecommitdiff
path: root/src/scenes/puzzlePreviewScene.c
blob: 27b0541dca45e5b32b7acef238853d7697789e50 (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
#include <raylib.h>
#include <stdint.h>
#include <stdlib.h>
#include "scenes.h"
#include "../ui_elements/UI.h"
#include "../resources/resources.h"
#include "../resources/TextruesAtlas.h"

static const uint8_t MAX_TINT = 96;
static const float BODY_BUTTON_RATIO = 0.9f;
static const float BUTTON_PADDING_RATIO = 0.015f;

typedef struct {
    Puzzle *puzzle;
    Button *backButton;
    Button *proceedButton;
    float scroll_up;
    float hiding_state;
    int scroll;
    PopUpState state;
}  puzzlePreviewData;

void confirmSelemetion(Scene *self) {
    if (self == NULL) return;
    puzzlePreviewData *data = ((Scene *)self)->data;
    if (data == NULL) return;
    TraceLog(LOG_INFO, "Loading a puzzle %s", data->puzzle->Name);
    data->state = POPUP_HIDING;
}


void closePreviewWindow(Scene *self) {
    TraceLog(LOG_INFO, "Closing a preview window");
    if (self == NULL) return;
    puzzlePreviewData *data = ((Scene *)self)->data;
    if (data == NULL) return;
    data->state = POPUP_HIDING_OVERLAY;
}

static PopUpState getCurrentState(void *self) {
    if (self == NULL || ((Scene *)self)->data == NULL) 
        return POPUP_ACTIVE;
    puzzlePreviewData *data = ((Scene *)self)->data;
    return data->state;
}

bool updatePuzzlePreview(void *self) {
    puzzlePreviewData *data = ((Scene *)self)->data;
    switch (data->state) {
        case POPUP_REVEALING:
            data->scroll_up += SCROLL_SPEED * GetFrameTime();
            if (data->scroll_up >= 1) {
                data->state = POPUP_ACTIVE;
                data->scroll_up = 1;
            }
            return true;

        case POPUP_HIDING_OVERLAY:
            data->scroll_up -= SCROLL_SPEED * GetFrameTime();
            if (data->scroll_up <= 0) {
                if (data->puzzle != NULL) FreePuzzle(data->puzzle);
                DeleteSceneFromStack();
            }
            return true;

        case POPUP_HIDING:
            data->hiding_state += BLACKOUT_SPEED * GetFrameTime();
            if (data->hiding_state >= 2) {
                DeleteSceneFromStack();
                AddScene(CreatePuzzleScene(data->puzzle));
            }
            return true;

        default: break;
    }
    if (data->backButton != NULL) UpdateButton(data->backButton);
    if (data->proceedButton != NULL) UpdateButton(data->proceedButton);
    return true;
}

static Rectangle getBodyRect(puzzlePreviewData *data) {
    float screen_h = GetScreenHeight();
    float screen_w = GetScreenWidth();
    return (Rectangle) {
        .x = PADDING,
        .y = PADDING + (screen_h * (1 - data->scroll_up)),
        .width = screen_w - PADDING * 2,
        .height = screen_h - PADDING * 2,
    };
}

bool drawPuzzlePreview(void *self) {
    puzzlePreviewData *data = ((Scene *)self)->data;
    DrawRectangle(0, 0, 
            GetScreenWidth(), GetScreenHeight(), 
            (Color) {0, 0, 0, MAX_TINT * data->scroll_up });

    Rectangle body = getBodyRect(data);
    Rectangle TextArea = {
            .x = body.x,
            .y = body.y,
            .width = body.width,
            .height = body.height * BODY_BUTTON_RATIO,
            };

    DrawRecBordered(TextArea, COLORSCHEME_WHITE, COLORSCHEME_ORANGE, BORDER);
    if (data->puzzle != NULL) {
        DrawTextInRec(TextArea, data->puzzle->Description, 
                GetMainFont(), TEXT_FONT_SIZE, COLORSCHEME_BROWN,
                3, &data->scroll);
    }

    float btn_space_height = body.height * (1 - BODY_BUTTON_RATIO);
    float btn_padding = body.height * BUTTON_PADDING_RATIO;
    float btn_height = btn_space_height - 2 * btn_padding;
    float btn_width = btn_height * 2;

    if (data->backButton != NULL) {
        DrawButton(data->backButton, &(Rectangle) {
                .x = PADDING,
                .y = TextArea.y + TextArea.height + btn_padding,
                .height = btn_height,
                .width = btn_height * 2,
                });
    }

    if (data->proceedButton != NULL) {
        DrawButton(data->proceedButton, &(Rectangle) {
                .x = body.x + body.width - btn_width,
                .y = TextArea.y + TextArea.height + btn_padding,
                .height = btn_height,
                .width = btn_height * 2,
                });
    }

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

    return true;
}

/// DOES NOT FREE THE PUZZLE! This should be handled separatly
void deinitPuzzlePreview(void *self) {
    TraceLog(LOG_INFO, "Deleting a puzzle preview");
    if (self == NULL) return;
    puzzlePreviewData *data = ((Scene *)self)->data;
    if (data != NULL) {
        if (data->backButton != NULL) free(data->backButton);
        if (data->proceedButton != NULL) free(data->proceedButton);
    }
    free(self);
    TraceLog(LOG_INFO, "Deleting a puzzle preview - success");
}

Scene *CreatePuzzlePreview(Puzzle *puzzle) {
    if (puzzle == NULL) return NULL;
    TraceLog(LOG_INFO, "Creating a puzzle preview for a puzzle %s",
            puzzle->Name);
    Scene *outp = malloc(sizeof(Scene));
    outp->Update = updatePuzzlePreview;
    outp->Draw = drawPuzzlePreview;
    outp->Deint = deinitPuzzlePreview;
    outp->IsTransparent = true;

    outp->GetCurrentState = getCurrentState;
    outp->Reopen = NULL;

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

    data->puzzle = puzzle;
    data->state = POPUP_REVEALING;
    data->scroll_up = 0;
    data->scroll = 0;
    
    Button *backButton = malloc(sizeof(Button));
    if (backButton != NULL) {
        backButton->parent = outp;
        backButton->OnClick = closePreviewWindow;
        backButton->atlas = GetButtonsAtlas();
        backButton->textrueLocation = BACK_BUTTON_WIDE_LOCATION;
    }
    data->backButton = backButton;

    Button *proceedButton = malloc(sizeof(Button));
    if (proceedButton != NULL) {
        proceedButton->parent = outp;
        proceedButton->OnClick = confirmSelemetion;
        proceedButton->atlas = GetButtonsAtlas();
        proceedButton->textrueLocation = PROCEED_BUTTON_WIDE_LOCATION;
    }
    data->proceedButton = proceedButton;

    outp->data = data;

    return outp;
}