From 722a9d1e729aff2eb64f3c064c33d547ea980eb0 Mon Sep 17 00:00:00 2001 From: physick Date: Fri, 31 Jul 2026 18:36:22 +0500 Subject: Buttons GUI start --- src/resources/TextruesAtlas.h | 18 ++++++++++++ src/resources/resources.c | 34 +++++++++++++++++++++++ src/resources/resources.h | 6 ++++ src/scenes/puzzleScene.c | 64 ++++++++++++++++++++++++++++++++++++++++--- src/ui_elements/Button.c | 17 ++++++++++++ src/ui_elements/UI.h | 13 +++++++++ 6 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 src/resources/TextruesAtlas.h create mode 100644 src/ui_elements/Button.c (limited to 'src') diff --git a/src/resources/TextruesAtlas.h b/src/resources/TextruesAtlas.h new file mode 100644 index 0000000..5fdafc3 --- /dev/null +++ b/src/resources/TextruesAtlas.h @@ -0,0 +1,18 @@ +/* + * This header file binds texture type to its location on the atlas. + * They are hardcoded for now until I find a better way to do that. + */ + +#include + +#ifndef TEXTURES_ATLAS_H +#define TEXTURES_ATLAS_H + +static const Rectangle SUBMIT_BUTTON_LOCATION = { + .x = 0, + .y = 0, + .width = 32, + .height = 32, +}; + +#endif // TEXTURES_ATLAS_H diff --git a/src/resources/resources.c b/src/resources/resources.c index e82b6e0..c1b78b5 100644 --- a/src/resources/resources.c +++ b/src/resources/resources.c @@ -5,9 +5,43 @@ Font MainFont; Font MonoFont; +Texture ButtonsAtlas; + +Texture PrepareTexture(char *location) { + Image img = LoadImage(location); + // Applying colorscheme + ImageColorReplace(&img, + (Color) { .r = 0xFF, .g = 0x00, .b = 0x00, .a = 0xFF }, + COLORSCHEME_RED); + ImageColorReplace(&img, + (Color) { .r = 0x00, .g = 0xFF, .b = 0x00, .a = 0xFF }, + COLORSCHEME_GREEN); + ImageColorReplace(&img, + (Color) { .r = 0XFF, .g = 0x7F, .b = 0x00, .a = 0xFF }, + COLORSCHEME_YELLOW); + ImageColorReplace(&img, + (Color) { .r = 0x00, .g = 0x00, .b = 0xFF, .a = 0xFF }, + COLORSCHEME_ORANGE); + ImageColorReplace(&img, + (Color) { .r = 0x00, .g = 0x00, .b = 0x00, .a = 0xFF }, + COLORSCHEME_BROWN); + ImageColorReplace(&img, + (Color) { .r = 0xFF, .g = 0xFF, .b = 0xFF, .a = 0xFF }, + COLORSCHEME_WHITE); + Texture outp = LoadTextureFromImage(img); + UnloadImage(img); + return outp; +} + void LoadResources() { MainFont = LoadFontEx("assets/fonts/coolvetica.ttf", 100, NULL, 0); MonoFont = LoadFontEx("assets/fonts/monofont.ttf", 100, NULL, 0); + + ButtonsAtlas = PrepareTexture("assets/textures/buttons_atlas.png"); +} + +Texture *GetButtonsAtlas() { + return &ButtonsAtlas; } Font *GetMainFont() { return &MainFont; } diff --git a/src/resources/resources.h b/src/resources/resources.h index 8f6cd1d..0837317 100644 --- a/src/resources/resources.h +++ b/src/resources/resources.h @@ -38,4 +38,10 @@ void LoadResources(); Font *GetMainFont(); Font *GetMonoFont(); +typedef enum { + SUBMIT_BUTTON, +} TEXTURE_TYPE ; + +Texture *GetButtonsAtlas(); + #endif // RESOURCES_H diff --git a/src/scenes/puzzleScene.c b/src/scenes/puzzleScene.c index feeada8..67d3c86 100644 --- a/src/scenes/puzzleScene.c +++ b/src/scenes/puzzleScene.c @@ -7,6 +7,7 @@ typedef struct { Puzzle *PuzzlePtr; TextArea *EditorInfo; + Button *submitButton; int scroll; } puzzleSceneData; @@ -22,6 +23,32 @@ void DrawPuzzleName(char *name) { 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() + UpdateButton(data->submitButton, &submit_trg); + DrawButton(data->submitButton, &submit_trg); + acc -= (panel.height + PADDING); + } +} + Rectangle GetBodyRect() { int HeaderOffset = PADDING * 2 + HEADER_FONT_SIZE; Rectangle outp = { @@ -79,9 +106,26 @@ bool drawPuzzleScene(void *self) { GetMainFont(), 20, COLORSCHEME_BROWN, 3, &data->scroll); DrawTextArea(data->EditorInfo, pane2); + Rectangle controls_rect = { + .x = PADDING, + .y = body.y + body.height + PADDING, + .width = body.width, + .height = FOOTER_HEIGHT, + }; + DrawControls(self, controls_rect); + 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)); +} + bool updatePuzzleScene(void *self) { puzzleSceneData *data = ((Scene *)self)->data; @@ -91,10 +135,7 @@ bool updatePuzzleScene(void *self) { UpdateTextArea(data->EditorInfo, pane2); if (IsKeyPressed(KEY_F5)) { - char *script = GetText(data->EditorInfo); - SubmitionResult *res = SubmitSolution(data->PuzzlePtr, script); - if (res != NULL) - AddScene(CreateResultView(res)); + SubmitPuzzle(self); } return true; @@ -104,6 +145,7 @@ 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); } @@ -128,6 +170,20 @@ Scene *CreatePuzzleScene(Puzzle *puzzle) { 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; + if (puzzle->InitialCode != NULL) { data->EditorInfo = CreateTextAreaFromText(EditorDisplayParams, puzzle->InitialCode); } else { diff --git a/src/ui_elements/Button.c b/src/ui_elements/Button.c new file mode 100644 index 0000000..f3ab8fa --- /dev/null +++ b/src/ui_elements/Button.c @@ -0,0 +1,17 @@ +#include "UI.h" +#include + +void UpdateButton(Button *btn, Rectangle *location) { + if (IsMouseInRec(*location) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + btn->OnClick(btn->parent); + } +} + +void DrawButton(Button *btn, Rectangle *location) { + DrawTexturePro(*btn->atlas, + btn->textrueLocation, + *location, + (Vector2) { 0 , 0 }, + 0, + WHITE); +} diff --git a/src/ui_elements/UI.h b/src/ui_elements/UI.h index 06d127f..c23faf7 100644 --- a/src/ui_elements/UI.h +++ b/src/ui_elements/UI.h @@ -1,5 +1,6 @@ #include #include +#include "../scenes/scenes.h" #ifndef UI_ELEMENTS_H #define UI_ELEMENTS_H @@ -17,6 +18,18 @@ void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color colo float MeasureChar(Font *font, int height, char symbol); bool IsMouseInRec(Rectangle rec); +// ========== BUTTON ========== + +typedef struct { + Scene *parent; + void (*OnClick)(Scene *); + Texture *atlas; + Rectangle textrueLocation; +} Button; + +void UpdateButton(Button *btn, Rectangle *location); +void DrawButton(Button *btn, Rectangle *location); + // ========== TEXT EDITOR ========== static const size_t DEFAULT_TEXT_NODE_SIZE = 256; -- cgit v1.3