diff options
Diffstat (limited to 'src/editor.c')
| -rw-r--r-- | src/editor.c | 366 |
1 files changed, 0 insertions, 366 deletions
diff --git a/src/editor.c b/src/editor.c deleted file mode 100644 index c1d9463..0000000 --- a/src/editor.c +++ /dev/null @@ -1,366 +0,0 @@ -#include "editor.h" - -#include <stdbool.h> -#include <stddef.h> -#include <stdio.h> -#include <stdlib.h> - -#include "logger.h" -#include "nfd.h" -#include "raylib.h" - -#define RAYGUI_IMPLEMENTATION -#include "raygui.h" - -const char *EDITOR_MAIN_TABS[] = {"Build"}; -const int EDITOR_MAIN_TABS_SIZE = - sizeof(EDITOR_MAIN_TABS) / sizeof(EDITOR_MAIN_TABS[0]); - -void SE_UpdateEditor(Editor *editor) { - EditorState *state = &editor->state; - - // update selected layer - if (state->activeTilesetTile != NULL && - state->activeTilesetTile->type != state->activeTileLayerId) { - state->activeTileLayerId = state->activeTilesetTile->type; - SE_LOG_INFO("Changed selected layer"); - } - - // update tile rotation - if (IsKeyPressed(KEY_R)) { - state->tileRotation += 90.0f; - if (state->tileRotation >= 360.0f) { - state->tileRotation = 0.0f; - } - } -} - -void drawBuildTab(Editor *editor, Tileset *tileset, int x, int y, int width, - int height, int padding) { - GuiPanel((Rectangle){x, y + 24.0f, width, height}, NULL); - - // Displaying what tile is selected right now - TilesetTile *activeTilesetTile = editor->state.activeTilesetTile; - - const char *selectedTileLabel; - - if (activeTilesetTile != NULL) { - int type = activeTilesetTile->type; - if (type == TILE_FLOOR) { - selectedTileLabel = "FLOOR"; - } else if (type == TILE_WALL) { - selectedTileLabel = "WALL"; - } else { - selectedTileLabel = "SMTH"; - } - - DrawTextureEx(activeTilesetTile->texture, - (Vector2){x + padding, y + padding * 3.0f}, 0.0f, 6.0f, - WHITE); - - } else { - selectedTileLabel = "???"; - DrawRectangle(x + padding, y + padding * 3.0f, TILE_WIDTH * 6.0f, - TILE_HEIGHT * 6.0f, LIGHTGRAY); - } - - GuiLabel((Rectangle){x + padding, y + padding * 2.0f, width - padding * 2.0f, - 24.0f}, - selectedTileLabel); - - // Drawing rotation - GuiLabel((Rectangle){x + padding * 7.0f, y + padding * 2.0f, - width - padding * 2.0f, 24.0f}, - "Rotation"); - - DrawCircle(x + padding * 7.0f + 10.0f, y + padding * 3.0f + 10.0f, 10.0f, - LIGHTGRAY); - - DrawCircleSector( - (Vector2){x + padding * 7.0f + 10.0f, y + padding * 3.0f + 10.0f}, 10.0f, - 0.0f, editor->state.tileRotation, 4, GRAY); - - int rotationTextLength = - snprintf(NULL, 0, "%.0f", editor->state.tileRotation); - char *rotationText = malloc(rotationTextLength + 1); - snprintf(rotationText, rotationTextLength + 1, "%.0f", - editor->state.tileRotation); - - GuiLabel((Rectangle){x + padding * 7.0f + 25.0f, y + padding * 3.0f, - width - padding * 2.0f, 24.0f}, - rotationText); - - free(rotationText); - - // Rendering - Rectangle controlBounds = {x + padding, y + padding + 20.0f * 8.0f, - width - padding * 2.0f, 200.0f}; - controlBounds.height = height - controlBounds.y - padding * 4.0f; - Rectangle contentSize = {x + padding, y + padding, 10 * TILE_WIDTH, 1000}; - - GuiScrollPanel(controlBounds, NULL, contentSize, &editor->state.panelScroll, - &editor->state.panelView); - - BeginScissorMode(editor->state.panelView.x, editor->state.panelView.y, - editor->state.panelView.width, - editor->state.panelView.height); - - Vector2 mousePos = GetMousePosition(); - - // rendering floor tiles in grid - int row = 0, column = 0, texturesPerRow = 10; - float scale = 3.0f; - for (int i = 0; i < tileset->tileCount; i++) { - TilesetTile *tile = tileset->tiles[i]; - if (tile == NULL) continue; - - row = i / texturesPerRow; - column = i % texturesPerRow; - - int x = controlBounds.x + 5 + TILE_WIDTH * column * scale; - int y = controlBounds.y + editor->state.panelScroll.y + - TILE_HEIGHT * row * scale; - - DrawTextureEx(tile->texture, (Vector2){x, y}, 0.0f, scale, WHITE); - - bool isHovered = - (x <= mousePos.x && mousePos.x <= x + TILE_WIDTH * scale) && - (y <= mousePos.y && mousePos.y <= y + TILE_HEIGHT * scale); - - bool sameTile = - activeTilesetTile != NULL && activeTilesetTile->id == tile->id; - - // hover event - if (isHovered || sameTile) { - DrawRectangleLines(x, y, TILE_WIDTH * scale, TILE_HEIGHT * scale, YELLOW); - } - - // select tile - if (isHovered && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - editor->state.activeTilesetTile = tile; - } - } - - 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)); - state->isFloor = true; - state->isWall = false; - state->upFilePath = NULL; - editor->state.createBlockState = state; - } -} - -void drawTextures(EditorCreateBlockState *state, int wx, int wy, int ww, int wh, - int pad) { - if (state->isFloor) { - GuiLabel((Rectangle){wx + pad, wy + pad + 24.0f * 2.0f, ww, 24.0f}, "UP"); - DrawTexture(state->upTexture, wx + pad, wy + pad + 24.0f * 3.0f, WHITE); - } else { - GuiLabel((Rectangle){wx + pad, wy + pad + 24.0f * 2.0f, ww, 24.0f}, - "UP (2D)"); - DrawTexture(state->upTexture, wx + pad, wy + pad + 24.0f * 3.0f, WHITE); - - GuiLabel((Rectangle){wx + pad + 24.0f * 3.0f, wy + pad + 24.0f * 2.0f, ww, - 24.0f}, - "CORNER (2D)"); - DrawTexture(state->cornerTexture, wx + pad + 24.0f * 3.0f, - wy + pad + 24.0f * 3.0f, WHITE); - - GuiLabel((Rectangle){wx + pad + 24.0f * 7.0f, wy + pad + 24.0f * 2.0f, ww, - 24.0f}, - "SIDE (3D)"); - DrawTexture(state->sideTexture, wx + pad + 24.0f * 7.0f, - wy + pad + 24.0f * 3.0f, WHITE); - } -} - -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}, - "2D PREVIEW"); - for (int x = 0; x < 10; x++) { - for (int y = 0; y < 5; y++) { - if (state->isFloor) { - DrawTexture(state->upTexture, wx + pad + x * TILE_WIDTH, - wy + pad + 24.0f * 5.0f + y * TILE_HEIGHT, WHITE); - } else { - Vector2 position = {x * TILE_WIDTH, y * TILE_HEIGHT}; - position.x += wx + pad; - position.y += wy + pad + 24.0f * 5.0f; - - // --- CORNERS --- - // top left - if (x == 0 && y == 0) { - DrawTextureEx(state->cornerTexture, position, 0.0f, 1.0f, WHITE); - } - // top right - else if (x == 9 && y == 0) { - DrawTextureEx(state->cornerTexture, position, 90.0f, 1.0f, WHITE); - } - // bottom left - else if (x == 0 && y == 4) { - DrawTextureEx(state->cornerTexture, position, 270.0f, 1.0f, WHITE); - } - // bottom right - else if (x == 9 && y == 4) { - DrawTextureEx(state->cornerTexture, position, 180.0f, 1.0f, WHITE); - } - - // --- SIDES --- - // top - else if (y == 0) { - DrawTextureEx(state->upTexture, position, 0.0f, 1.0f, WHITE); - } - // left - else if (x == 0 && y > 1) { - DrawTextureEx(state->upTexture, position, 270.0f, 1.0f, WHITE); - } - // right - else if (x == 9) { - DrawTextureEx(state->upTexture, position, 90.0f, 1.0f, WHITE); - } - // bottom - else if (y == 4 && x > 1) { - DrawTextureEx(state->upTexture, position, 180.0f, 1.0f, WHITE); - } - } - } - } -} - -void loadTexture(EditorCreateBlockState *state) { - nfdu8char_t *outPath; - nfdu8filteritem_t filters[1] = {{"Images", "png"}}; - nfdopendialogu8args_t args = {0}; - args.filterList = filters; - args.filterCount = 1; - nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args); - - 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 != TILE_WIDTH || image.height != TILE_HEIGHT) { - ImageResizeNN(&image, TILE_WIDTH, TILE_HEIGHT); - } - - state->upTexture = LoadTextureFromImage(image); - - UnloadImage(image); - } -} - -void drawCreatingNewBlock(Editor *editor, Tileset *tileset) { - EditorCreateBlockState *state = editor->state.createBlockState; - - int ww = 300; - int wh = 280; - - int wx = GetScreenWidth() / 2.0f - ww / 2.0f; - int wy = GetScreenHeight() / 2.0f - wh / 2.0f; - int pad = 20; - - int close = GuiWindowBox((Rectangle){wx, wy, ww, wh}, "Creating a new block"); - - // --- Block type --- - GuiLabel((Rectangle){wx + pad, wy + pad, ww - pad * 2, 24.0f}, "Select type"); - - GuiCheckBox((Rectangle){wx + pad, wy + pad + 24.0f, 24.0f, 24.0f}, "Floor", - &state->isFloor); - if (state->isFloor) state->isWall = false; - - GuiCheckBox( - (Rectangle){wx + pad * 4.0f + 24.0f, wy + pad + 24.0f, 24.0f, 24.0f}, - "Wall", &state->isWall); - if (state->isWall) state->isFloor = false; - 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 + TILE_WIDTH, - wy + pad + 24.0f * 3.0f - TILE_WIDTH / 2.0f, - 24.0f, 24.0f}, - "#211#")) { - loadTexture(state); - } - } - - // --- Texture preview --- - // Up texture - drawTextures(state, wx, wy, ww, wh, pad); - - drawPreview(state, wx, wy, ww, wh, pad); - - int createButton = GuiButton( - (Rectangle){wx + pad, wy + wh - pad * 2.0f, ww - pad * 2.0f, 32.0f}, - "Create"); - - if (createButton) { - close = true; - - TilesetTileType type = state->isFloor ? TILE_FLOOR : TILE_WALL; - - SE_AddTilesetTile(tileset, state->upTexture, type); - } - - if (close) { - if (state->upFilePath != NULL) { - free(state->upFilePath); - state->upFilePath = NULL; - - if (!createButton) { - UnloadTexture(state->upTexture); - } - } - - free(state); - editor->state.createBlockState = NULL; - } -} - -void SE_DrawEditor(Editor *editor, Tileset *tileset) { - switch (editor->state.activeMainTab) { - case 0: { - drawBuildTab(editor, tileset, EDITOR_TOOLKIT_X, EDITOR_TOOLKIT_Y, - EDITOR_TOOLKIT_WIDTH, EDITOR_TOOLKIT_HEIGHT, 20); - break; - } - default: - break; - } - - GuiTabBar((Rectangle){EDITOR_TOOLKIT_X, EDITOR_TOOLKIT_Y, 100.0, 24.0f}, - EDITOR_MAIN_TABS, EDITOR_MAIN_TABS_SIZE, - &editor->state.activeMainTab); - - // Creating new block - if (editor->state.createBlockState != NULL) { - drawCreatingNewBlock(editor, tileset); - } -}
\ No newline at end of file |
