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
|
#include <raylib.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "scenes.h"
#include "../resources/resources.h"
#include "../ui_elements/UI.h"
typedef struct {
Puzzle *PuzzlePtr;
TextArea *EditorInfo;
Button *submitButton;
int scroll;
// Time until the next distraction puzzle in seconds
size_t distraction_timer;
// Miliseconds elapsed
float time_acc;
float blackout_state;
PopUpState state;
} puzzleSceneData;
// Time in seconds
const size_t BASE_TIME_LEFT = 5 * 60;
void DrawPuzzleName(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);
}
void DrawControls(Scene *self, Rectangle rect) {
DrawRecBordered(rect, WHITE, COLORSCHEME_ORANGE, BORDER);
puzzleSceneData *data = ((Scene *)self)->data;
Rectangle panel = {
.x = rect.x + BORDER * 2,
.y = rect.y + BORDER * 2,
.width = rect.width - BORDER * 4,
.height = rect.height - BORDER * 4,
};
int acc = panel.width - panel.height + BORDER * 4;
if (data->submitButton != NULL) {
Rectangle submit_trg = {
.x = acc,
.y = panel.y,
.width = panel.height,
.height = panel.height,
};
// Hacky. Should find a way to update in update()
DrawButton(data->submitButton, &submit_trg);
acc -= (panel.height + PADDING);
}
}
Rectangle GetBodyRect() {
int HeaderOffset = PADDING * 2 + HEADER_FONT_SIZE;
Rectangle outp = {
.x = PADDING,
.y = HeaderOffset,
.width = GetScreenWidth() - PADDING * 2,
.height = GetScreenHeight() - HeaderOffset - PADDING * 3 - FOOTER_HEIGHT,
};
return outp;
}
bool drawPuzzleScene(void *self) {
puzzleSceneData *data = ((Scene *)self)->data;
DrawBackground();
DrawPuzzleName(data->PuzzlePtr->Name);
Rectangle body = GetBodyRect();
// DrawRecBordered(body, WHITE, COLORSCHEME_ORANGE, BORDER);
// DrawTextArea(data->EditorInfo, body, GetMonoFont(), 20, 2, &data->editor_scroll);
// return true;
Rectangle pane1, pane2;
GetPanesSplit(&pane1, &pane2, body, 0.35, 0.4);
DrawRecBordered(pane1, WHITE, COLORSCHEME_ORANGE, BORDER);
DrawRecBordered(pane2, WHITE, COLORSCHEME_ORANGE, BORDER);
DrawTextInRec(pane1, data->PuzzlePtr->Description,
GetMainFont(), 20, COLORSCHEME_BROWN, 3, &data->scroll);
Rectangle editor_rect = {
.x = pane2.x + EDITOR_PADDING,
.y = pane2.y + EDITOR_PADDING,
.width = pane2.width - EDITOR_PADDING * 2,
.height = pane2.height - EDITOR_PADDING * 2,
};
DrawTextArea(data->EditorInfo, editor_rect);
Rectangle controls_rect = {
.x = PADDING,
.y = body.y + body.height + PADDING,
.width = body.width,
.height = FOOTER_HEIGHT,
};
DrawControls(self, controls_rect);
if (data->state == POPUP_REVEALING) {
DrawRectangle(0, 0,
GetScreenWidth(), GetScreenHeight(),
(Color) {0, 0, 0, 255 * (data->blackout_state >= 1 ? 1 : data->blackout_state) });
}
return true;
}
void SubmitPuzzle(Scene *self) {
puzzleSceneData *data = ((Scene *)self)->data;
char *script = GetText(data->EditorInfo);
SubmitionResult *res = SubmitSolution(data->PuzzlePtr, script);
if (res != NULL)
AddScene(CreateResultView(res));
}
void AddTime(Scene *self, size_t additionalTime) {
puzzleSceneData *data = ((Scene *)self)->data;
data->distraction_timer += additionalTime;
}
void createDistraction() {
AddScene(CreateDistractionScene(GetRandomDistraction(), AddTime));
}
bool updatePuzzleScene(void *self) {
puzzleSceneData *data = ((Scene *)self)->data;
if (data->state == POPUP_REVEALING) {
data->blackout_state -= BLACKOUT_SPEED * GetFrameTime();
if (data->blackout_state <= 0) {
data->blackout_state = 0;
data->state = POPUP_ACTIVE;
}
return true;
}
Rectangle body = GetBodyRect();
Rectangle pane1, pane2;
GetPanesSplit(&pane1, &pane2, body, 0.35, 0.4);
UpdateTextArea(data->EditorInfo, pane2);
if (IsKeyPressed(KEY_F5))
SubmitPuzzle(self);
if (data->submitButton != NULL)
UpdateButton(data->submitButton);
data->time_acc += GetFrameTime();
while (data->time_acc >= 1) {
data->distraction_timer -= 1;
data->time_acc -= 1;
// TraceLog(LOG_INFO, "Time left: %u", data->distraction_timer);
if (data->distraction_timer == 0) {
createDistraction();
return true;
}
}
return true;
}
void deinitPuzzleScene(void *self) {
puzzleSceneData *data = ((Scene *)self)->data;
TraceLog(LOG_INFO, "Deleting a puzzle scene");
FreeTextArea(data->EditorInfo);
if (data->submitButton != NULL) free(data->submitButton);
free(data);
free((Scene *)self);
}
Scene *CreatePuzzleScene(Puzzle *puzzle) {
Scene *outp = malloc(sizeof(Scene));
outp->Update = updatePuzzleScene;
outp->Draw = drawPuzzleScene;
outp->Deint = deinitPuzzleScene;
outp->IsTransparent = false;
outp->Reopen = NULL;
outp->GetCurrentState = NULL;
puzzleSceneData *data = malloc(sizeof(puzzleSceneData));
data->PuzzlePtr = puzzle;
data->scroll = 0;
TextAreaDisplayParams *EditorDisplayParams = malloc(sizeof(TextAreaDisplayParams));
if (EditorDisplayParams != NULL) {
EditorDisplayParams->font = GetMonoFont();
EditorDisplayParams->spacing = 1;
EditorDisplayParams->font_size = 20; // this will change
EditorDisplayParams->scroll_h = 0;
EditorDisplayParams->scroll_v = 0;
}
Button *submitButton = malloc(sizeof(Button));
if (submitButton != NULL) {
submitButton->parent = outp;
submitButton->OnClick = SubmitPuzzle;
submitButton->atlas = GetButtonsAtlas();
submitButton->textrueLocation = (Rectangle) {
.x = 0,
.y = 0,
.width = 32,
.height = 32,
};
}
data->submitButton = submitButton;
data->distraction_timer = BASE_TIME_LEFT;
data->time_acc = 0;
data->state = POPUP_REVEALING;
data->blackout_state = 1;
if (puzzle->InitialCode != NULL) {
data->EditorInfo = CreateTextAreaFromText(EditorDisplayParams, puzzle->InitialCode);
} else {
data->EditorInfo = CreateTextArea(EditorDisplayParams);
}
outp->data = data;
return outp;
}
|