From 2a59e9274104a7e38c791926fc19f8161f6893b9 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 28 Jan 2025 00:33:20 +0500 Subject: feat: load textures from file dialog rather than from dropping files --- src/editor.c | 164 ++++++++++++++++++++++------------------------------------- 1 file changed, 62 insertions(+), 102 deletions(-) (limited to 'src/editor.c') diff --git a/src/editor.c b/src/editor.c index d12d31b..8733143 100644 --- a/src/editor.c +++ b/src/editor.c @@ -5,6 +5,8 @@ #include #include +#include "logger.h" +#include "nfd.h" #include "raylib.h" #define RAYGUI_IMPLEMENTATION @@ -113,36 +115,10 @@ void SE_DrawEditor(Editor *editor, Camera2D *camera) { removeNullTiles(editor); } -void drawBuildTab(Editor *editor, int x, int y, int width, int height) { - int padding = 20; +void drawBuildTab(Editor *editor, int x, int y, int width, int height, + int padding) { GuiPanel((Rectangle){x, y + 24.0f, width, height}, NULL); - if (IsFileDropped() && editor->state.createBlockState == NULL) { - EditorCreateBlockState *state = malloc(sizeof(EditorCreateBlockState)); - - FilePathList droppedFiles = LoadDroppedFiles(); - - if (!IsFileExtension(droppedFiles.paths[0], ".png")) { - free(state); - UnloadDroppedFiles(droppedFiles); - return; - } - - state->upFilePath = droppedFiles.paths[0]; - UnloadDroppedFiles(droppedFiles); - - // loading textures - Image image = LoadImage(state->upFilePath); - if (image.width != TEXTURE_WIDTH || image.height != TEXTURE_HEIGHT) { - ImageResize(&image, TEXTURE_WIDTH, TEXTURE_HEIGHT); - } - state->upTexture = LoadTextureFromImage(image); - - editor->state.createBlockState = state; - - UnloadImage(image); - } - // Rendering Rectangle controlBounds = {x + padding, y + padding + 20.0f, 200, 200}; Rectangle contentSize = {x + padding, y + padding, 10 * TEXTURE_WIDTH, 1000}; @@ -189,6 +165,16 @@ void drawBuildTab(Editor *editor, int x, int y, int width, int height) { } EndScissorMode(); + + // create tile button + if (GuiButton( + (Rectangle){x + padding, height - padding - 12.0f, 24.0f, 24.0f}, + "#08#") && + editor->state.createBlockState == NULL) { + SE_LOG_INFO("Creating a new block"); + EditorCreateBlockState *state = malloc(sizeof(EditorCreateBlockState)); + editor->state.createBlockState = state; + } } void drawTextures(EditorCreateBlockState *state, int wx, int wy, int ww, int wh, @@ -215,74 +201,6 @@ void drawTextures(EditorCreateBlockState *state, int wx, int wy, int ww, int wh, } } -void loadWallTextures(EditorCreateBlockState *state, int wx, int wy, int ww, - int wh, int pad) { - if (state->sideFilePath == NULL) { - int box = GuiMessageBox((Rectangle){wx - 20, wy + 100, ww + 40, 200}, - "Waiting for side texture", - "Drop side texture here", "No"); - - if (box >= 1) { - state->isWall = false; - state->isFloor = true; - } - - if (IsFileDropped()) { - FilePathList droppedFiles = LoadDroppedFiles(); - - if (!IsFileExtension(droppedFiles.paths[0], ".png")) { - UnloadDroppedFiles(droppedFiles); - return; - } - - state->sideFilePath = droppedFiles.paths[0]; - UnloadDroppedFiles(droppedFiles); - - Image image = LoadImage(state->sideFilePath); - if (image.width != TEXTURE_WIDTH || image.height != TEXTURE_HEIGHT) { - ImageResize(&image, TEXTURE_WIDTH, TEXTURE_HEIGHT); - } - state->sideTexture = LoadTextureFromImage(image); - } - - return; - } - - if (state->cornerFilePath == NULL) { - int box = GuiMessageBox((Rectangle){wx - 20, wy + 100, ww + 40, 200}, - "Waiting for corner texture", - "Drop corner texture here", "No"); - - if (box >= 1) { - UnloadTexture(state->sideTexture); - free(state->sideFilePath); - state->sideFilePath = NULL; - state->isWall = false; - state->isFloor = true; - } - - if (IsFileDropped()) { - FilePathList droppedFiles = LoadDroppedFiles(); - - if (!IsFileExtension(droppedFiles.paths[0], ".png")) { - UnloadDroppedFiles(droppedFiles); - return; - } - - state->cornerFilePath = droppedFiles.paths[0]; - UnloadDroppedFiles(droppedFiles); - - Image image = LoadImage(state->cornerFilePath); - if (image.width != TEXTURE_WIDTH || image.height != TEXTURE_HEIGHT) { - ImageResize(&image, TEXTURE_WIDTH, TEXTURE_HEIGHT); - } - state->cornerTexture = LoadTextureFromImage(image); - } - - return; - } -} - void drawPreview(EditorCreateBlockState *state, int wx, int wy, int ww, int wh, int pad) { GuiLabel((Rectangle){wx + pad, wy + pad + 24.0f * 4.0f, ww, 24.0f}, @@ -337,6 +255,35 @@ void drawPreview(EditorCreateBlockState *state, int wx, int wy, int ww, int wh, } } +void loadTexture(EditorCreateBlockState *state) { + nfdchar_t *outPath = NULL; + nfdresult_t result = NFD_OpenDialog(NULL, NULL, &outPath); + + if (result == NFD_OKAY) { + if (!IsFileExtension(outPath, ".png")) { + SE_LOG_WARN("The editor only supports .png files"); + return; + } + + // unloading old texture + if (state->upFilePath != NULL) { + UnloadTexture(state->upTexture); + free(state->upFilePath); + } + + state->upFilePath = outPath; + Image image = LoadImage(state->upFilePath); + + if (image.width != TEXTURE_WIDTH || image.height != TEXTURE_HEIGHT) { + ImageResizeNN(&image, TEXTURE_WIDTH, TEXTURE_HEIGHT); + } + + state->upTexture = LoadTextureFromImage(image); + + UnloadImage(image); + } +} + void drawCreatingNewBlock(Editor *editor) { EditorCreateBlockState *state = editor->state.createBlockState; @@ -363,14 +310,27 @@ void drawCreatingNewBlock(Editor *editor) { if (!state->isWall && !state->isFloor) state->isFloor = true; // --- Texture loading --- + // Loading UP texture + if (state->upFilePath == NULL) { + if (GuiButton((Rectangle){wx + pad, wy + pad + 24.0f * 3.0f, 64.0f, 24.0f}, + "Load")) { + loadTexture(state); + } + } + // Changing the texture + else { + if (GuiButton((Rectangle){wx + pad * 1.5f + TEXTURE_WIDTH, + wy + pad + 24.0f * 3.0f - TEXTURE_WIDTH / 2.0f, + 24.0f, 24.0f}, + "#211#")) { + loadTexture(state); + } + } + + // --- Texture preview --- // Up texture drawTextures(state, wx, wy, ww, wh, pad); - // Side texture - if (state->isWall) { - loadWallTextures(state, wx, wy, ww, wh, pad); - } - drawPreview(state, wx, wy, ww, wh, pad); int createButton = GuiButton( @@ -406,7 +366,7 @@ void SE_DrawEditorToolkit(Editor *editor) { switch (editor->state.activeMainTab) { case 0: { drawBuildTab(editor, EDITOR_TOOLKIT_X, EDITOR_TOOLKIT_Y, - EDITOR_TOOLKIT_WIDTH, EDITOR_TOOLKIT_HEIGHT); + EDITOR_TOOLKIT_WIDTH, EDITOR_TOOLKIT_HEIGHT, 20); break; } default: -- cgit v1.2.3