Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit a4533e4

Browse files
committed
Added file dialog, saving, loading, new maze
1 parent 908b09b commit a4533e4

File tree

9 files changed

+279
-42
lines changed

9 files changed

+279
-42
lines changed

Examples/normal_maze.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"mazeName": "Unnamed Maze",
3+
"junctions": [
4+
[ "E1", 26500, 47, 20, 0, 0, 1, 1 ],
5+
[ "N1", 6334, 40, 13, 0, 0, 1, 1 ],
6+
[ "S1", 15724, 32, 13, 0, 0, 1, 1 ],
7+
[ "S1", 19169, 40, 27, 0, 0, 1, 1 ],
8+
[ "W1", 18467, 32, 20, 0, 0, 1, 1 ],
9+
[ "H", 41, 40, 20, 0, 0, 1, 1 ]
10+
],
11+
"tunnels": [
12+
[ 6334, 15724 ],
13+
[ 41, 6334 ],
14+
[ 41, 26500 ],
15+
[ 41, 18467 ],
16+
[ 41, 19169 ],
17+
[ 15724, 18467 ]
18+
],
19+
"tags": [
20+
[ 32, 13, [ "supply_lift" ]],
21+
[ 40, 20, [ "home_base" ]]
22+
]
23+
}

Examples/test.json

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
{
22
"mazeName": "Test Maze",
33
"junctions": [
4-
[ "A", 11478, 38, 27, 0, 0, 1, 1 ],
5-
[ "B", 26500, 47, 15, 0, 0, 1, 1 ],
6-
[ "C", 6334, 38, 15, 0, 0, 1, 1 ],
7-
[ "D", 15724, 47, 27, 0, 0, 1, 1 ],
8-
[ "E", 19169, 47, 21, 0, 0, 3, 2 ],
4+
[ "G", 41, 38, 21, 0, 0, 1, 1 ],
95
[ "F", 18467, 29, 21, -1, -1, 1, 1 ],
10-
[ "G", 41, 38, 21, 0, 0, 1, 1 ]
6+
[ "E", 19169, 47, 21, 0, 0, 3, 2 ],
7+
[ "D", 15724, 47, 27, 0, 0, 1, 1 ],
8+
[ "C", 6334, 38, 15, 0, 0, 1, 1 ],
9+
[ "B", 26500, 47, 15, 0, 0, 1, 1 ],
10+
[ "A", 11478, 38, 27, 0, 0, 1, 1 ]
1111
],
1212
"tunnels": [
13-
[ 11478, 15724 ],
13+
[ 41, 18467 ],
14+
[ 41, 6334 ],
15+
[ 41, 11478 ],
1416
[ 6334, 26500 ],
15-
[ 15724, 19169 ],
1617
[ 19169, 26500 ],
17-
[ 41, 11478 ],
18-
[ 41, 6334 ],
19-
[ 41, 18467 ]
18+
[ 15724, 19169 ],
19+
[ 11478, 15724 ]
2020
],
2121
"tags": [
22-
[ 33, 21, [ "moreitems" ]],
22+
[ 29, 21, [ "foodsupply1", "outpost2", "largeplatform" ]],
2323
[ 38, 15, [ "spawn", "outpost1", "supplycrate" ]],
24-
[ 29, 21, [ "foodsupply1", "outpost2", "largeplatform" ]]
24+
[ 33, 21, [ "moreitems" ]]
2525
]
26-
}
27-
28-
29-
26+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Core features:
1212
- [x] Edge placement
1313
- [x] Tagging of any grid cell with strings
1414
- [x] JSON export/import function
15-
- [ ] File management/Export UI
16-
- [ ] Sector partitioning. Junction naming and coloring by sector.
15+
- [x] File dialog/Export UI
1716

1817
Extra:
18+
- [ ] Sector partitioning. Junction naming and coloring by sector.
1919
- [ ] Advanced junction moving features
2020
- [ ] Tag managing and coloring
2121
- [ ] Interactive maze generation

Source/arclib.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <raylib.h>
1010
#include <raygui.h>
1111
#include <raymath.h>
12+
#include <vector>
13+
#include <string>
14+
using namespace std;
1215

1316
struct ArcGlobal {
1417
Camera2D camera;
@@ -40,6 +43,7 @@ bool PositionGizmo(Vector2 &position, Vector2 &anchor, bool &grabbed, float size
4043
Color LerpColor(Color from, Color to, float factor);
4144
Color ColorFromNormalized3(float arr[3]);
4245
void ColorToFloat3(Color col, float arr[3]);
46+
void StringsToPointers(vector<string> &vec, const char **pointers, int n);
4347

4448
#endif
4549

@@ -195,6 +199,12 @@ void ColorToFloat3(Color col, float arr[3])
195199
arr[1] = ((float)col.g)/255.0;
196200
arr[2] = ((float)col.b)/255.0;
197201
}
202+
void StringsToPointers(vector<string> &vec, const char **pointers, int n)
203+
{
204+
for (int i = 0; i < vec.size() && i < n; i++) {
205+
pointers[i] = vec[i].c_str();
206+
}
207+
}
198208

199209
#undef ARCLIB_IMPLEMENTATION
200210
#endif

Source/editor.h

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef MAZE_GUI_H
22
#define MAZE_GUI_H
33

4-
#include "arclib.h"
5-
#include "maze.h"
6-
#include "imgui.h"
4+
#include <imgui.h>
75
#include <string>
86
#include <vector>
7+
#include "arclib.h"
8+
#include "maze.h"
9+
#include "file_dialog.h"
10+
911

1012
#define KEY_SELECT IsMouseButtonPressed(MOUSE_LEFT_BUTTON)
1113
#define KEY_REMOVE IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)
@@ -18,10 +20,15 @@
1820
using namespace std;
1921
namespace Gui = ImGui;
2022

23+
// TODO: Clean up this class with states.
24+
// TODO: Split file loading from Maze class to editor.
25+
2126
// This class is basically the entire editor.
2227
class MazeEditor {
2328
public:
24-
Maze &maze;
29+
Maze maze;
30+
FileDialog fileDialog;
31+
fs::path filePath = "";
2532

2633
float tileSize = 16.0f;
2734
int fontSize = 20;
@@ -60,6 +67,7 @@ class MazeEditor {
6067
vector<string> tagsList;
6168
bool mazeHasFocus = false;
6269
bool editingCorners = false;
70+
bool loadOnFile = false;
6371

6472
bool showLabels = true;
6573
bool showJunctions = true;
@@ -69,14 +77,16 @@ class MazeEditor {
6977
bool showIdMap = false;
7078
bool showTags = true;
7179

72-
MazeEditor(Maze &targetMaze): maze(targetMaze)
80+
MazeEditor()
7381
{
7482
raylibFont = GetFontDefault();
7583
CenterHome();
7684
ColorToFloat3(junctionColor, junctionColorArr);
7785
ColorToFloat3(gridColor, gridColorArr);
7886
ColorToFloat3(clearColor, clearColorArr);
7987
ColorToFloat3(tunnelColor, tunnelColorArr);
88+
strcpy(mazeNameBuf, maze.name.c_str());
89+
LoadMaze("Examples/test.json");
8090
}
8191

8292
//
@@ -228,6 +238,35 @@ class MazeEditor {
228238
arcGlobal.camera.offset.y = GetScreenHeight() / 2;
229239
}
230240
}
241+
void SaveMaze(fs::path path)
242+
{
243+
if (maze.ExportJson(path)) {
244+
filePath = path;
245+
cout << "Saved " << filePath << endl;
246+
} else {
247+
cout << "Invalid file " << filePath << endl;
248+
}
249+
}
250+
void LoadMaze(fs::path path)
251+
{
252+
if (maze.ImportJson(path)) {
253+
filePath = path;
254+
cout << "Loaded " << filePath << endl;
255+
} else {
256+
cout << "Invalid file " << filePath << endl;
257+
}
258+
}
259+
void NewMaze()
260+
{
261+
maze = Maze();
262+
filePath = "";
263+
cout << "New Maze" << endl;
264+
}
265+
bool HasValidFile()
266+
{
267+
ifstream stream(filePath);
268+
return stream.good();
269+
}
231270

232271
//
233272
// Drawing methods.
@@ -257,7 +296,6 @@ class MazeEditor {
257296
if (showLabels) DrawJunctionLabels();
258297
DrawFPS(5, GetScreenHeight() - 15);
259298

260-
261299
Gui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once);
262300
Gui::SetNextWindowSize(ImVec2(350, 300), ImGuiCond_Once);
263301
Gui::Begin("Control Panel");
@@ -266,6 +304,13 @@ class MazeEditor {
266304
DrawGuiEditorSettings();
267305
DrawGuiMazeSettings();
268306
DrawGuiInspector();
307+
if (fileDialog.Update()) {
308+
if (loadOnFile) {
309+
LoadMaze(fileDialog.targetPath);
310+
} else {
311+
SaveMaze(fileDialog.targetPath);
312+
}
313+
}
269314
Gui::PopItemWidth();
270315
Gui::End();
271316
}
@@ -441,6 +486,7 @@ class MazeEditor {
441486
{
442487
if (Gui::TreeNode("Maze")) {
443488
Gui::InputText("Maze Name", mazeNameBuf, IM_ARRAYSIZE(mazeNameBuf));
489+
maze.name = string(mazeNameBuf);
444490

445491
Gui::Text("View Toggles");
446492
if (Gui::BeginTable("Split", 3)) {
@@ -475,7 +521,7 @@ class MazeEditor {
475521
Gui::InputText("Junction Name", nameBuf, IM_ARRAYSIZE(nameBuf));
476522
// The two buttons.
477523
if (hasSelectedCoord) {
478-
Gui::Text(TextFormat("Tag Position (%d, %d)", selectedCoord.x, selectedCoord.y));
524+
Gui::Text("Tag Position (%d, %d)", selectedCoord.x, selectedCoord.y);
479525
Gui::InputText("Tag", tagBuf, IM_ARRAYSIZE(tagBuf));
480526
Gui::SameLine();
481527
if (Gui::Button("Add")) {
@@ -503,18 +549,41 @@ class MazeEditor {
503549
// The top bar.
504550
if (Gui::BeginMainMenuBar()) {
505551
if (Gui::BeginMenu("File")) {
506-
if (Gui::MenuItem("New")) {}
507-
if (Gui::MenuItem("Save", "Ctrl+S")) {}
508-
if (Gui::MenuItem("Load")) {}
552+
if (Gui::MenuItem("New")) {
553+
NewMaze();
554+
}
555+
if (Gui::MenuItem("Save", "")) {
556+
if (HasValidFile())
557+
SaveMaze(filePath);
558+
else {
559+
fileDialog.Open();
560+
loadOnFile = false;
561+
}
562+
}
563+
if (Gui::MenuItem("Save As", "")) {
564+
fileDialog.Open();
565+
loadOnFile = false;
566+
}
567+
if (Gui::MenuItem("Load")) {
568+
fileDialog.Open();
569+
loadOnFile = true;
570+
}
509571
Gui::EndMenu();
510572
}
511573
Gui::PushStyleColor(ImGuiCol_Text, ImVec4(0.3, 0.3, 0.3, 1));
512574
Gui::Text(statusBarText.c_str());
513575
Gui::PopStyleColor();
514576
Gui::EndMainMenuBar();
515577
}
578+
579+
int w = GetScreenWidth();
580+
Gui::SetNextWindowPos(ImVec2(w-400, 19));
581+
Gui::SetNextWindowSize(ImVec2(400, 0));
582+
Gui::Begin("Info bar", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground);
583+
Gui::TextWrapped("%s", filePath.string().c_str());
584+
Gui::End();
516585
}
517-
586+
518587
//
519588
// Utility methods.
520589
//

0 commit comments

Comments
 (0)