diff options
Diffstat (limited to 'src/scenes/distractionScene.c')
| -rw-r--r-- | src/scenes/distractionScene.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/scenes/distractionScene.c b/src/scenes/distractionScene.c new file mode 100644 index 0000000..1f12aae --- /dev/null +++ b/src/scenes/distractionScene.c @@ -0,0 +1,116 @@ +#include "scenes.h" +#include <raylib.h> +#include <stdio.h> +#include "../ui_elements/UI.h" +#include "../resources/resources.h" + +static const float BODY_PADDING = 0.05f; +static const int BORDER = 2; +static const int EDITOR_PADDING = 3; + +typedef struct { + DistractionPuzzle *puzzle; + TextArea *EditorInfo; + void (*addTime_cb)(Scene *, size_t); + int scroll; +} distractionSceneData; + +Rectangle getBodyRect() { + float screen_h = GetScreenHeight(); + float screen_w = GetScreenWidth(); + return (Rectangle) { + .x = screen_w * BODY_PADDING, + .y = screen_h * BODY_PADDING, + .height = screen_h * (1 - BODY_PADDING * 2), + .width = screen_w * (1 - BODY_PADDING * 2), + }; +} + +bool updateDisctractionScene(void *self) { + distractionSceneData *data = ((Scene *)self)->data; + Rectangle pane1, pane2; + GetPanesSplit(&pane1, &pane2, getBodyRect(), 0.35, 0.4); + UpdateTextArea(data->EditorInfo, pane2); + + return true; +} + +bool drawDisctractionScene(void *self) { + distractionSceneData *data = ((Scene *)self)->data; + + DrawRectangle(0, 0, + GetScreenWidth(), GetScreenHeight(), + (Color) {0, 0, 0, 127 }); + Rectangle body = getBodyRect(); + Rectangle pane1, pane2; + GetPanesSplit(&pane1, &pane2, body, 0.35, 0.4); + DrawRecBordered(pane1, WHITE, COLORSCHEME_ORANGE, BORDER); + DrawRecBordered(pane2, WHITE, COLORSCHEME_ORANGE, BORDER); + + if (data->puzzle == NULL) { + return true; + } + DrawTextInRec(pane1, data->puzzle->puzzle->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); + + return true; +} + +void deinitDisctractionScene(void *self) { + distractionSceneData *data = ((Scene *)self)->data; + if (data->puzzle != NULL) { + FreeDistrationPuzzle(data->puzzle); + } +} + +Scene *CreateDistractionScene(DistractionPuzzle *puzzle, void (*addTime)(Scene *, size_t)) { + if (puzzle == NULL) { + TraceLog(LOG_ERROR, "Failed to create a distraction scene: puzzle struct is NULL"); + return NULL; + } + + Scene *outp = malloc(sizeof(Scene)); + if (outp == NULL) { + TraceLog(LOG_ERROR, "Failed to create a distraction scene: scene allocation failed"); + return NULL; + } + outp->Update = updateDisctractionScene; + outp->Draw = drawDisctractionScene; + outp->Deint = deinitDisctractionScene; + outp->IsTransparent = true; + + distractionSceneData *data = malloc(sizeof(distractionSceneData)); + if (data == NULL) { + free(outp); + TraceLog(LOG_ERROR, "Failed to create a distraction scene: data allocation failed"); + return NULL; + } + data->puzzle = puzzle; + data->addTime_cb = addTime; + + 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; + } + + if (puzzle->puzzle->InitialCode != NULL) { + data->EditorInfo = CreateTextAreaFromText(EditorDisplayParams, puzzle->puzzle->InitialCode); + } else { + data->EditorInfo = CreateTextArea(EditorDisplayParams); + } + + outp->data = data; + + return outp; +} |
