summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorphysick <mynameisgennadiy@vk.com>2026-07-31 18:36:22 +0500
committerphysick <mynameisgennadiy@vk.com>2026-07-31 18:36:22 +0500
commit722a9d1e729aff2eb64f3c064c33d547ea980eb0 (patch)
treefb03a4f5bf76d1274565ef261e7f2e3a579a0823
parente20b4c3a5dfe081fa2d6d772617307fae4689698 (diff)
Buttons GUI start
-rw-r--r--README.md12
-rwxr-xr-xassets/textures/buttons_atlas.pngbin0 -> 660 bytes
-rw-r--r--build.zig1
-rw-r--r--src/resources/TextruesAtlas.h18
-rw-r--r--src/resources/resources.c34
-rw-r--r--src/resources/resources.h6
-rw-r--r--src/scenes/puzzleScene.c64
-rw-r--r--src/ui_elements/Button.c17
-rw-r--r--src/ui_elements/UI.h13
9 files changed, 161 insertions, 4 deletions
diff --git a/README.md b/README.md
index 86aa376..fca07cf 100644
--- a/README.md
+++ b/README.md
@@ -16,3 +16,15 @@ I recommend using zig build system with `build.sh` or `build.bat` in
For windows you have to clone the dll's in the output lib yourself
You should also download raylib 5.5 manually.
Put the DLLs in `raylib/lib` and header in `raylib/include/`.
+
+# Development
+
+## Textures convention
+All the game colorscheme should contain 6 colors.
+The colorscheme is applied at the runtime. Follow the following colorscheme in the assets:
+- #FFFFFF - light
+- #FF0000 - red
+- #00FF00 - green
+- #FF7F00 - accent light
+- #0000FF - accent dark
+- #000000 - dark
diff --git a/assets/textures/buttons_atlas.png b/assets/textures/buttons_atlas.png
new file mode 100755
index 0000000..587b94e
--- /dev/null
+++ b/assets/textures/buttons_atlas.png
Binary files differ
diff --git a/build.zig b/build.zig
index 9c4d736..49c6e86 100644
--- a/build.zig
+++ b/build.zig
@@ -30,6 +30,7 @@ pub fn build(b: *std.Build) void {
"src/resources/config.c",
"src/ui_elements/UI.c",
"src/ui_elements/TextEditor.c",
+ "src/ui_elements/Button.c",
"src/scenes/sceneManager.c",
"src/scenes/mainMenu.c",
"src/scenes/resultView.c",
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 <raylib.h>
+
+#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 <raylib.h>
+
+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 <raylib.h>
#include <stddef.h>
+#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;