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)
1820using namespace std;
1921namespace 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.
2227class MazeEditor {
2328public:
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