forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.cpp
More file actions
289 lines (259 loc) · 7.97 KB
/
Copy pathProject.cpp
File metadata and controls
289 lines (259 loc) · 7.97 KB
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
#include "Game/PCH.h"
#include "Project.h"
#include "EditorWorld.h"
#include "EditorResource.h"
#include "AssetDatabase.h"
#include "Game/ModelLoader.h"
#include "Core/FileSystem.h"
#include "Core/FileLoader.h"
namespace sh::editor
{
bool Project::bInitResource = false;
Project::Project(game::ImGUImpl& imgui, EditorWorld& world) :
UI(imgui), world(world),
rootPath(std::filesystem::current_path()),
currentPath(rootPath)
{
invisibleExtensions.push_back(".meta");
InitResources();
GetAllFiles(currentPath);
}
void Project::InitResources()
{
if (bInitResource)
return;
bInitResource = true;
auto resource = EditorResource::GetInstance();
folderIcon = resource->GetIcon(EditorResource::Icon::Folder);
fileIcon = resource->GetIcon(EditorResource::Icon::File);
meshIcon = resource->GetIcon(EditorResource::Icon::Mesh);
}
void Project::GetAllFiles(const std::filesystem::path& path)
{
foldersPath.clear();
filesPath.clear();
for (const auto& entry : std::filesystem::directory_iterator(path))
{
if (std::filesystem::is_directory(entry.path()))
foldersPath.push_back(entry.path());
else if (std::filesystem::is_regular_file(entry.path()))
filesPath.push_back(entry.path());
}
std::sort(foldersPath.begin(), foldersPath.end());
std::sort(filesPath.begin(), filesPath.end());
}
auto Project::GetElideFileName(const std::filesystem::path& path, float maxSize) const -> std::string
{
std::string result = path.filename().u8string();
float currentSize = ImGui::CalcTextSize(result.c_str()).x;
while (currentSize > maxSize)
{
result.pop_back();
currentSize = ImGui::CalcTextSize(result.c_str()).x;
}
return result;
}
inline void Project::RenderParentFolder()
{
ImGui::BeginGroup();
ImGui::ImageButton("../", *folderIcon, ImVec2{ iconSize, iconSize }, ImVec2{ 0, 0 }, ImVec2{ 1, 1 }, iconBackgroundColor);
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_::ImGuiMouseButton_Left))
{
currentPath = currentPath.parent_path();
GetAllFiles(currentPath);
}
float textWidth = ImGui::CalcTextSize("../").x;
float textOffset = (iconSize - textWidth) / 2.f;
if (textOffset >= 0.0f)
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + textOffset);
ImGui::Text("../");
ImGui::EndGroup();
}
inline void Project::SetDragItem(const std::filesystem::path& path)
{
void* item = nullptr;
std::string pathStr = path.string();
std::string extension = path.extension().string();
std::string payloadName = "asset";
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_::ImGuiDragDropFlags_None))
{
if (extension == ".obj")
{
payloadName = core::reflection::GetTypeName<render::Mesh*>();
item = world.meshes.GetResource(pathStr);
if (item == nullptr)
{
game::ModelLoader loader{ world.renderer };
item = world.meshes.AddResource(pathStr, loader.Load(pathStr));
}
}
else if (extension == ".mat")
{
payloadName = core::reflection::GetTypeName<render::Material*>();
item = world.materials.GetResource(pathStr);
if (item == nullptr)
AssetDatabase::ImportAsset(world, path);
}
else if (extension == ".png" || extension == ".jpg")
{
payloadName = core::reflection::GetTypeName<render::Texture*>();
item = world.textures.GetResource(pathStr);
if (item == nullptr)
AssetDatabase::ImportAsset(world, path);
}
ImGui::SetDragDropPayload(payloadName.c_str(), &item, sizeof(render::Mesh*));
ImGui::Text("%s", path.filename().c_str());
ImGui::EndDragDropSource();
}
}
inline auto Project::GetIcon(const std::filesystem::path& path) const -> const game::GUITexture*
{
std::string extension = path.extension().string();
const game::GUITexture* icon = folderIcon;
if (std::filesystem::is_regular_file(path))
{
icon = fileIcon;
if (extension == ".obj")
icon = meshIcon;
else if (extension == ".mat")
icon = EditorResource::GetInstance()->GetIcon(EditorResource::Icon::Material);
}
return icon;
}
inline bool Project::RenderFile(const std::filesystem::path& path, float& cursorX, float spacing, float width)
{
cursorX = ImGui::GetCursorPosX();
if (cursorX + iconSize > width)
{
ImGui::NewLine();
cursorX = ImGui::GetCursorPosX();
}
ImGui::BeginGroup();
const game::GUITexture* icon = GetIcon(path);
ImGui::PushStyleColor(ImGuiCol_::ImGuiCol_Button, iconBackgroundColor);
if (ImGui::ImageButton(path.string().c_str(), *icon, ImVec2{ iconSize, iconSize }))
{
auto uuidStr = AssetDatabase::GetAssetUUID(path);
if (uuidStr)
{
auto objPtr = core::SObjectManager::GetInstance()->GetSObject(uuidStr.value().ToString());
if (core::IsValid(objPtr))
world.SetSelectedObject(objPtr);
}
}
SetDragItem(path);
ImGui::PopStyleColor();
// 폴더면 더블 클릭 시 경로 변경
if (std::filesystem::is_directory(path))
{
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_::ImGuiMouseButton_Left))
{
ImGui::EndGroup();
currentPath = path;
GetAllFiles(currentPath);
return false;
}
}
std::string name = GetElideFileName(path, iconSize);
float textWidth = ImGui::CalcTextSize(name.c_str()).x;
float textOffset = (iconSize - textWidth) / 2.f;
if (textOffset >= 0.0f)
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + textOffset);
ImGui::Text("%s", name.c_str());
ImGui::EndGroup();
ImGui::SameLine(0.0f, spacing);
cursorX += iconSize + spacing;
return true;
}
inline void Project::ShowRightClickPopup()
{
if (ImGui::BeginPopupContextWindow("ProjectRightClickPopup"))
{
if (ImGui::BeginMenu("Create"))
{
if (ImGui::MenuItem("Folder"))
{
core::FileSystem::CreateFolder(currentPath, "NewFolder");
GetAllFiles(currentPath);
}
if (ImGui::MenuItem("Material"))
{
static render::Shader* defaultShader = world.shaders.GetResource("Default");
assert(defaultShader);
std::string name{ core::FileSystem::CreateUniqueFileName(currentPath, "NewMaterial.mat") };
auto mat = world.materials.AddResource(name, render::Material{ defaultShader });
mat->SetName(name);
mat->Build(world.renderer);
AssetDatabase::CreateAsset(world, currentPath / name, *mat);
GetAllFiles(currentPath);
}
ImGui::EndMenu();
}
ImGui::EndPopup();
}
}
SH_EDITOR_API void Project::Update()
{
}
SH_EDITOR_API void Project::Render()
{
static ImGuiWindowFlags style =
ImGuiWindowFlags_::ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_NoBringToFrontOnFocus;
ImGui::Begin("Project", nullptr, style);
float availableWidth = ImGui::GetContentRegionAvail().x;
float cursorX = ImGui::GetCursorPosX();
float spacing = ImGui::GetStyle().ItemSpacing.x;
if (currentPath != rootPath)
{
RenderParentFolder();
ImGui::SameLine();
}
for (auto& path : foldersPath)
{
if (!RenderFile(path, cursorX, spacing, availableWidth))
break;
}
for (auto& path : filesPath)
{
if (std::find(invisibleExtensions.begin(), invisibleExtensions.end(), path.extension().string()) != invisibleExtensions.end())
continue;
if (!RenderFile(path, cursorX, spacing, availableWidth))
break;
}
ShowRightClickPopup();
ImGui::End();
}
SH_EDITOR_API void Project::CreateNewProject(const std::filesystem::path& dir)
{
if (!std::filesystem::create_directory(dir))
{
SH_INFO_FORMAT("Can't create directory: {}", dir.u8string());
return;
}
rootPath = dir;
currentPath = dir;
GetAllFiles(currentPath);
}
SH_EDITOR_API void Project::OpenProject(const std::filesystem::path& dir)
{
rootPath = dir;
currentPath = dir;
GetAllFiles(currentPath);
AssetDatabase::LoadAllAssets(world, dir, true);
}
SH_EDITOR_API void Project::SaveWorld()
{
std::ofstream os{ rootPath / "test.world" };
os << std::setw(4) << world.Serialize();
os.close();
}
SH_EDITOR_API void Project::LoadWorld()
{
core::FileLoader loader{};
auto file = loader.LoadText((rootPath / "test.world").string());
if (file)
{
world.Deserialize(core::Json::parse(file.value()));
}
}
}