Skip to content

Commit 9c6d455

Browse files
committed
Load User Settings
1 parent 6b5c0da commit 9c6d455

File tree

4 files changed

+64
-22
lines changed

4 files changed

+64
-22
lines changed

Libs/AST/Include/AST/Utils/Settings.h

+27-15
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,45 @@
22

33
#pragma once
44

5-
#include "AST/Tree.h"
6-
#include "AST/TypeRef.h"
7-
85
#include <Pipe/Core/String.h>
6+
#include <Pipe/Files/Files.h>
7+
#include <Pipe/Serialize/Formats/JsonFormat.h>
98
#include <Pipe/Serialize/Serialization.h>
109
#include <PipeECS.h>
1110

1211

13-
namespace rift::ast
12+
namespace rift
1413
{
1514
p::String GetUserSettingsPath();
1615

1716
template<typename T>
18-
T* GetUserSettings()
17+
T& GetUserSettings()
1918
{
20-
static TOwnPtr<T> instance;
19+
static p::TOwnPtr<T> instance;
2120
if (!instance)
2221
{
22+
instance = p::MakeOwned<T>();
23+
2324
p::String path = GetUserSettingsPath();
24-
String data;
25-
// if (p::LoadStringFile(path, data))
26-
//{
27-
// instance = MakeOwned<T>();
28-
// p::JsonFormatReader reader{data};
29-
// reader.GetReader().Serialize(*instance);
30-
// }
25+
if (!p::Exists(path))
26+
{
27+
p::CreateFolder(path);
28+
}
29+
30+
p::AppendToPath(path, p::GetTypeName<T>(false));
31+
path.append(".json");
32+
if (!p::Exists(path))
33+
{
34+
p::SaveStringFile(path, "{}");
35+
}
36+
37+
p::String data;
38+
if (p::LoadStringFile(path, data))
39+
{
40+
p::JsonFormatReader reader{data};
41+
reader.GetReader().Serialize(*instance);
42+
}
3143
}
32-
return instance.Get();
44+
return *instance.Get();
3345
}
34-
} // namespace rift::ast
46+
} // namespace rift

Libs/AST/Src/AST/Utils/Settings.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include <Pipe/Files/PlatformPaths.h>
77

88

9-
namespace rift::ast
9+
namespace rift
1010
{
1111
p::String GetUserSettingsPath()
1212
{
1313
static p::StringView relativeSettingsPath{"Rift"};
1414
return p::JoinPaths(p::PlatformPaths::GetUserSettingsPath(), relativeSettingsPath);
1515
}
16-
} // namespace rift::ast
16+
} // namespace rift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015-2023 Piperift - All rights reserved
2+
#pragma once
3+
4+
#include <Pipe/Reflect/Struct.h>
5+
6+
7+
namespace rift::editor
8+
{
9+
struct EditorSettings : public p::Struct
10+
{
11+
P_STRUCT(EditorSettings, p::Struct)
12+
13+
P_PROP(recentProjects)
14+
p::TArray<p::String> recentProjects;
15+
};
16+
} // namespace rift::editor

Libs/Editor/Src/Utils/ProjectManager.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright 2015-2023 Piperift - All rights reserved
22

33
#include "Editor.h"
4+
#include "Statics/EditorSettings.h"
45
#include "Utils/ElementsPanel.h"
56

7+
#include <AST/Utils/Settings.h>
68
#include <Pipe/Files/PlatformPaths.h>
79
#include <PipeFiles.h>
810
#include <UI/Notify.h>
@@ -56,16 +58,29 @@ namespace rift::editor
5658
UI::SetItemDefaultFocus();
5759
{
5860
UI::Text("Recent projects:");
61+
auto& editorSettings = rift::GetUserSettings<EditorSettings>();
5962
static const char* recentProjects[]{"Project.rift"};
6063
static int selectedN = 0;
6164
UI::SetNextItemWidth(-FLT_MIN);
6265

63-
for (int n = 0; n < IM_ARRAYSIZE(recentProjects); ++n)
66+
for (int n = 0; n < editorSettings.recentProjects.Size(); ++n)
6467
{
6568
const bool isSelected = (selectedN == n);
66-
UI::BulletText(recentProjects[n]);
69+
p::StringView path = editorSettings.recentProjects[n];
70+
UI::BulletText(path.data());
6771
UI::SameLine(ImGui::GetContentRegionAvail().x - 30.f);
68-
if (UI::SmallButton("open")) {}
72+
if (UI::SmallButton("open"))
73+
{
74+
if (Editor::Get().OpenProject(path))
75+
{
76+
UI::CloseCurrentPopup();
77+
}
78+
else
79+
{
80+
UI::AddNotification({UI::ToastType::Error, 1.f,
81+
p::Strings::Format("Failed to open project at '{}'", path)});
82+
}
83+
}
6984
}
7085
}
7186
UI::Dummy({10.f, 40.f});
@@ -84,9 +99,8 @@ namespace rift::editor
8499
UI::SameLine();
85100
if (UI::Button("...", p::v2{24.f, 0.f}))
86101
{
87-
p::String selectedFolder = p::SelectFolderDialog(
102+
folder = p::SelectFolderDialog(
88103
"Select project folder", p::PlatformPaths::GetCurrentPath());
89-
folder = p::ToString(selectedFolder);
90104
}
91105
}
92106

0 commit comments

Comments
 (0)