summaryrefslogtreecommitdiff
path: root/src/editor.c
blob: 73164f552188a3db49e9c40b0a21875ea398ac76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#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");
  }
}

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);

  // 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) {
  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 != 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);
  }
}