NoJobEngine 2.0.7
Loading...
Searching...
No Matches
ProjectPanel.cpp
Go to the documentation of this file.
4
11#include "Engine/Scene/Scene.h"
12
13#include <imgui.h>
14#include <algorithm>
15#include <cctype>
16#include <cstdint>
17
18namespace NoJob
19{
21 {
22 m_CurrentDirectory = AssetManager::GetProjectRoot();
23 }
24
26 Entity& selectedEntity,
27 const std::shared_ptr<Mesh>& defaultCubeMesh,
28 const std::shared_ptr<Material>& defaultCubeMaterial,
29 const CreateModelFunction& createModel,
30 const LogFunction& log)
31 {
32 ImGui::Begin("Project");
33
34 if (m_CurrentDirectory.empty())
35 m_CurrentDirectory = AssetManager::GetProjectRoot();
36
37 const auto projectRoot = AssetManager::GetProjectRoot();
38 const auto assetsRoot = AssetManager::GetAssetsDirectory();
39
40 if (m_CurrentDirectory != projectRoot)
41 {
42 if (ImGui::Button("< Back"))
43 {
44 const auto parent = m_CurrentDirectory.parent_path();
45 m_CurrentDirectory = parent.string().size() < projectRoot.string().size()
46 ? projectRoot : parent;
47 }
48 ImGui::SameLine();
49 }
50
51 ImGui::TextDisabled("%s",
52 m_CurrentDirectory.lexically_relative(projectRoot).generic_string().c_str());
53 ImGui::Separator();
54 ImGui::SetNextItemWidth(-1.0f);
55 ImGui::InputTextWithHint("##ProjectSearch", "Search current folder...",
56 m_Search, sizeof(m_Search));
57 ImGui::Separator();
58
59 const bool folderInsideAssets =
60 m_CurrentDirectory == assetsRoot ||
61 m_CurrentDirectory.string().rfind(assetsRoot.string(), 0) == 0;
62
63 if (folderInsideAssets)
64 {
65 ImGui::InvisibleButton("##ProjectPrefabDropTarget",
66 ImVec2(ImGui::GetContentRegionAvail().x, 28.0f));
67 if (ImGui::BeginDragDropTarget())
68 {
69 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("NOJOB_ENTITY"))
70 {
71 const auto handle = *static_cast<const std::uint32_t*>(payload->Data);
72 if (scene && scene->IsValid(handle))
74 Entity(handle, scene), m_CurrentDirectory, log);
75 }
76 ImGui::EndDragDropTarget();
77 }
78 ImGui::SetCursorPosY(ImGui::GetCursorPosY() - 28.0f);
79 ImGui::TextDisabled("Drop a Hierarchy object here to create a Prefab");
80 ImGui::Separator();
81 }
82
83 if (ImGui::BeginPopupContextWindow("ProjectCreateContext",
84 ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems))
85 {
86 if (ImGui::BeginMenu("Create"))
87 {
88 const bool canCreatePrefab = folderInsideAssets && static_cast<bool>(selectedEntity);
89 if (ImGui::MenuItem("C++ Script", nullptr, false, folderInsideAssets))
90 m_RequestCreateCppScript = true;
91 if (ImGui::MenuItem("Prefab From Selected", nullptr, false, canCreatePrefab))
92 ProjectAssetOperations::CreatePrefab(selectedEntity, m_CurrentDirectory, log);
93 ImGui::EndMenu();
94 }
95 ImGui::EndPopup();
96 }
97
98 if (m_RequestCreateCppScript)
99 {
100 ImGui::OpenPopup("Create C++ Script");
101 m_RequestCreateCppScript = false;
102 }
103
104 static char newScriptName[128] = "NewScript";
105 if (ImGui::BeginPopupModal("Create C++ Script", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
106 {
107 ImGui::TextUnformatted("Script name");
108 ImGui::SetNextItemWidth(280.0f);
109 const bool enter = ImGui::InputText("##CppScriptName", newScriptName,
110 sizeof(newScriptName), ImGuiInputTextFlags_EnterReturnsTrue);
111
112 const auto scriptsRoot = assetsRoot / "Scripts";
113 const std::string sanitized = ProjectAssetOperations::SanitizeCppIdentifier(newScriptName);
114 const bool exists = std::filesystem::exists(scriptsRoot / (sanitized + ".h")) ||
115 std::filesystem::exists(scriptsRoot / (sanitized + ".cpp"));
116 if (exists) ImGui::TextDisabled("A script with this name already exists.");
117 else ImGui::TextDisabled("Creates Assets/Scripts/%s.h and %s.cpp",
118 sanitized.c_str(), sanitized.c_str());
119
120 const bool create = (ImGui::Button("Create") || enter) && !exists;
121 ImGui::SameLine();
122 const bool cancel = ImGui::Button("Cancel");
123 if (create && ProjectAssetOperations::CreateCppScript(scriptsRoot, sanitized, log))
124 {
125 m_CurrentDirectory = scriptsRoot;
126 newScriptName[0] = '\0';
127 ImGui::CloseCurrentPopup();
128 }
129 if (cancel) ImGui::CloseCurrentPopup();
130 ImGui::EndPopup();
131 }
132
133 std::error_code error;
134 if (std::filesystem::exists(m_CurrentDirectory, error))
135 {
136 for (const auto& entry : std::filesystem::directory_iterator(
137 m_CurrentDirectory, std::filesystem::directory_options::skip_permission_denied, error))
138 {
139 const auto path = entry.path();
140 const std::string name = path.filename().string();
141
142 if (m_Search[0] != '\0')
143 {
144 std::string filter = m_Search;
145 std::string lowerName = name;
146 std::transform(filter.begin(), filter.end(), filter.begin(),
147 [](unsigned char c){ return static_cast<char>(std::tolower(c)); });
148 std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
149 [](unsigned char c){ return static_cast<char>(std::tolower(c)); });
150 if (lowerName.find(filter) == std::string::npos) continue;
151 }
152
153 if (m_CurrentDirectory == projectRoot &&
154 path.lexically_normal() != assetsRoot.lexically_normal())
155 continue;
156
157 ImGui::PushID(path.string().c_str());
158 if (entry.is_directory())
159 {
160 if (ImGui::Selectable(("[Folder] " + name).c_str(), false,
161 ImGuiSelectableFlags_AllowDoubleClick) &&
162 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
163 m_CurrentDirectory = path;
164
165 if (ImGui::BeginDragDropTarget())
166 {
167 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("NOJOB_ENTITY"))
168 {
169 const auto handle = *static_cast<const std::uint32_t*>(payload->Data);
170 if (scene && scene->IsValid(handle))
171 ProjectAssetOperations::CreatePrefab(Entity(handle, scene), path, log);
172 }
173 ImGui::EndDragDropTarget();
174 }
175 }
176 else
177 {
178 const std::string extension = path.extension().string();
179 const bool isTexture = extension == ".png" || extension == ".jpg" ||
180 extension == ".jpeg" || extension == ".bmp" || extension == ".tga";
181 const bool isModel = extension == ".obj" || extension == ".fbx" ||
182 extension == ".gltf" || extension == ".glb" || extension == ".dae" ||
183 extension == ".stl" || extension == ".ply" || extension == ".3ds" ||
184 extension == ".blend";
185 const bool isMaterial = extension == ".nojobmat";
186 const bool isPrefab = extension == ".nojobprefab";
187 const bool isScript = extension == ".cpp" || extension == ".h" || extension == ".hpp";
188 const bool isAudio = extension == ".wav" || extension == ".mp3" || extension == ".flac";
189
190 if (isAudio)
191 {
192 ImGui::Selectable(("[Audio] " + name).c_str());
193 const std::string relative = AssetManager::ToProjectRelative(path).generic_string();
194 if (ImGui::BeginDragDropSource())
195 {
196 ImGui::SetDragDropPayload("NOJOB_AUDIO_ASSET", relative.c_str(), relative.size() + 1);
197 ImGui::Text("Audio: %s", name.c_str());
198 ImGui::EndDragDropSource();
199 }
200 }
201 else if (isScript)
202 {
203 const bool clicked = ImGui::Selectable(("[C++] " + name).c_str(), false,
204 ImGuiSelectableFlags_AllowDoubleClick);
205#ifdef _WIN32
206 if (clicked && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
207 VisualStudioIntegration::OpenScript(path, projectRoot, log);
208 if (ImGui::BeginPopupContextItem())
209 {
210 if (ImGui::MenuItem("Open in Visual Studio"))
211 VisualStudioIntegration::OpenScript(path, projectRoot, log);
212 ImGui::Separator();
213 ImGui::TextDisabled("%s", path.lexically_relative(projectRoot).generic_string().c_str());
214 ImGui::EndPopup();
215 }
216#endif
217 }
218 else if (isModel)
219 {
220 if (ImGui::Selectable(name.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick) &&
221 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && createModel)
222 createModel(AssetManager::ToProjectRelative(path));
223 }
224 else if (isPrefab)
225 {
226 auto instantiate = [&]()
227 {
228 if (!scene) return;
229 auto entity = PrefabSerializer::Instantiate(*scene, path, defaultCubeMesh, defaultCubeMaterial);
230 if (entity) selectedEntity = entity;
231 };
232 if (ImGui::Selectable(name.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick) &&
233 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) instantiate();
234 if (ImGui::BeginPopupContextItem())
235 {
236 if (ImGui::MenuItem("Instantiate Prefab")) instantiate();
237 ImGui::EndPopup();
238 }
239 }
240 else if (isMaterial)
241 {
242 const bool clicked = ImGui::Selectable(name.c_str(), false,
243 ImGuiSelectableFlags_AllowDoubleClick);
244 const std::string relative = AssetManager::ToProjectRelative(path).generic_string();
245 if (clicked && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && selectedEntity &&
246 selectedEntity.HasComponent<MeshRendererComponent>())
247 {
248 auto& renderer = selectedEntity.GetComponent<MeshRendererComponent>();
249 if (renderer.MaterialAsset)
250 {
251 AssetRegistry registry(assetsRoot);
252 registry.Load();
253 auto loaded = MaterialSerializer::Load(path, renderer.MaterialAsset->GetShader(), registry);
254 if (loaded) renderer.MaterialAsset = loaded;
255 }
256 }
257 if (ImGui::BeginDragDropSource())
258 {
259 ImGui::SetDragDropPayload("NOJOB_MATERIAL_ASSET", relative.c_str(), relative.size() + 1);
260 ImGui::Text("Material: %s", name.c_str());
261 ImGui::EndDragDropSource();
262 }
263 }
264 else if (isTexture)
265 {
266 ImGui::Selectable(name.c_str());
267 if (ImGui::BeginDragDropSource())
268 {
269 const std::string relative = AssetManager::ToProjectRelative(path).generic_string();
270 ImGui::SetDragDropPayload("NOJOB_TEXTURE_ASSET", relative.c_str(), relative.size() + 1);
271 ImGui::Text("Texture: %s", name.c_str());
272 ImGui::EndDragDropSource();
273 }
274 }
275 else ImGui::TextDisabled("%s", name.c_str());
276 }
277 ImGui::PopID();
278 }
279 }
280 ImGui::End();
281 }
282}
AssetManager declarations for the Asset subsystem.
AssetRegistry declarations for the Asset Management subsystem.
Components declarations for the Scenes and Native Scripting subsystem.
Material declarations for the Rendering subsystem.
MaterialSerializer declarations for the Asset Management subsystem.
PrefabSerializer declarations for the Asset Management subsystem.
ProjectAssetOperations declarations for the Asset Management subsystem.
ProjectPanel declarations for the Asset Management subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
VisualStudioIntegration declarations for the Project Scripting subsystem.
static const std::filesystem::path & GetProjectRoot()
static std::filesystem::path ToProjectRelative(const std::filesystem::path &path)
static const std::filesystem::path & GetAssetsDirectory()
Entity()=default
T & GetComponent()
Definition Scene.h:851
bool HasComponent() const
Definition Scene.h:865
static std::shared_ptr< Material > Load(const std::filesystem::path &, const std::shared_ptr< Shader > &, AssetRegistry &)
static Entity Instantiate(Scene &, const std::filesystem::path &, const std::shared_ptr< Mesh > &, const std::shared_ptr< Material > &)
static std::filesystem::path CreatePrefab(Entity source, const std::filesystem::path &directory, const LogFunction &log={})
static std::string SanitizeCppIdentifier(std::string name)
static bool CreateCppScript(const std::filesystem::path &scriptsDirectory, const std::string &requestedName, const LogFunction &log={})
std::function< void(const std::filesystem::path &)> CreateModelFunction
void Draw(Scene *scene, Entity &selectedEntity, const std::shared_ptr< Mesh > &defaultCubeMesh, const std::shared_ptr< Material > &defaultCubeMaterial, const CreateModelFunction &createModel, const LogFunction &log)
std::function< void(std::string)> LogFunction
bool IsValid(std::uint32_t handle) const
Definition Scene.cpp:221
static bool OpenScript(const std::filesystem::path &scriptPath, const std::filesystem::path &projectRoot, const LogFunction &log)