-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFolderTree.cpp
More file actions
277 lines (235 loc) · 10.9 KB
/
FolderTree.cpp
File metadata and controls
277 lines (235 loc) · 10.9 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
//// FolderTree.cpp ///////////////////////////////////////////////////////////////
//
// zzzzz zzz zzzzzzzzzzzzz zzzz zzzz zzzzzz zzzzz
// zzzzzzz zzz zzzz zzzz zzzz zzzz
// zzz zzz zzz zzzzzzzzzzzzz zzzz zzzz zzz
// zzz zzz zzz z zzzz zzzz zzzz zzzz
// zzz zzz zzzzzzzzzzzzz zzzz zzz zzzzzzz zzzzz
//
// Author: Mehdy MORVAN
// Date: 06/05/2025
// Description: Source file for the folder tree rendering
//
///////////////////////////////////////////////////////////////////////////////
#include "AssetManagerWindow.hpp"
#include "IconsFontAwesome.h"
#include "assets/Asset.hpp"
#include "assets/AssetCatalog.hpp"
#include <filesystem>
#include <set>
namespace nexo::editor {
void AssetManagerWindow::drawFolderTreeItem(const std::string& name, const std::string& path)
{
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
// Check if this is the selected folder
if (path == m_currentFolder)
flags |= ImGuiTreeNodeFlags_Selected;
if (!m_folderChildren.contains(path))
flags |= ImGuiTreeNodeFlags_Leaf;
// Folder icon
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(230, 180, 80, 255));
ImGui::Text(ICON_FA_FOLDER);
ImGui::PopStyleColor();
ImGui::SameLine();
bool opened = ImGui::TreeNodeEx(name.c_str(), flags);
if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
m_currentFolder = path;
if (ImGui::BeginPopupContextItem()) {
if (ImGui::MenuItem("New Folder")) {
m_folderCreationState.parentPath = path;
m_folderCreationState.isCreatingFolder = true;
ImGui::OpenPopup("Create New Folder");
std::format_to_n(
m_folderCreationState.folderName,
sizeof(m_folderCreationState.folderName) - 1, // Ensure null termination
"New Folder"
);
}
ImGui::EndPopup();
}
if (opened) {
// Use the precomputed children list
if (const auto it = m_folderChildren.find(path); it != m_folderChildren.end()) {
for (const auto& childPath : it->second) {
// Find the name of the child from m_folderStructure
std::string childName;
for (const auto& [p, n] : m_folderStructure) {
if (p == childPath) {
childName = n;
break;
}
}
drawFolderTreeItem(childName, childPath);
}
}
ImGui::TreePop();
}
}
void AssetManagerWindow::handleNewFolderCreation()
{
if (m_folderCreationState.isCreatingFolder) {
ImGui::OpenPopup("Create New Folder");
// Center the popup
const ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Create New Folder", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::Text("Enter name for new folder:");
ImGui::InputText("##FolderName", m_folderCreationState.folderName, sizeof(m_folderCreationState.folderName));
ImGui::Separator();
if (ImGui::Button("Create", ImVec2(120, 0))) {
if (strnlen(m_folderCreationState.folderName, sizeof(m_folderCreationState.folderName)) > 0) {
std::string newFolderPath;
if (m_folderCreationState.parentPath.empty())
newFolderPath = m_folderCreationState.folderName;
else
newFolderPath = m_folderCreationState.parentPath + "/" + m_folderCreationState.folderName;
// Check if folder already exists
bool folderExists = false;
for (const auto &path: m_folderStructure | std::views::keys) {
if (path == newFolderPath) {
folderExists = true;
break;
}
}
if (!folderExists) {
m_folderStructure.emplace_back(newFolderPath, m_folderCreationState.folderName);
LOG(NEXO_INFO, "Created new folder: {}", newFolderPath);
m_folderCreationState.isCreatingFolder = false;
ImGui::CloseCurrentPopup();
} else {
m_folderCreationState.showError = true;
m_folderCreationState.errorMessage = "Folder already exists";
}
} else {
m_folderCreationState.showError = true;
m_folderCreationState.errorMessage = "Folder name cannot be empty";
}
}
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(120, 0))) {
m_folderCreationState.isCreatingFolder = false;
ImGui::CloseCurrentPopup();
}
// Display error message if needed
if (m_folderCreationState.showError) {
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 0, 0, 255));
ImGui::Text("%s", m_folderCreationState.errorMessage.c_str());
ImGui::PopStyleColor();
// Clear error after a few seconds
if (m_folderCreationState.errorTimer <= 0.0f) {
m_folderCreationState.showError = false;
m_folderCreationState.errorTimer = 3.0f; // Reset timer
} else {
m_folderCreationState.errorTimer -= ImGui::GetIO().DeltaTime;
}
}
ImGui::EndPopup();
}
}
}
void AssetManagerWindow::buildFolderStructure()
{
m_folderStructure.clear();
// Root entry
m_folderStructure.emplace_back("", "Assets");
m_folderChildren.clear(); // Clear the folder children map
// First pass: build the folder structure
std::set<std::string, std::less<>> uniqueFolderPaths;
std::unordered_set<std::string> seen{""};
for (const auto assets = assets::AssetCatalog::getInstance().getAssets(); auto& ref : assets) {
if (const auto assetData = ref.lock()) {
// normalized path: e.g. "Random/Sub"
std::filesystem::path p{ assetData->getMetadata().location.getPath() };
std::filesystem::path curr;
for (auto const& part : p) {
// skip empty or “_internal” style parts
if (auto s = part.string(); s.empty() || s.front() == '_')
continue;
curr /= part;
if (auto folderPath = curr.string(); seen.emplace(folderPath).second) {
m_folderStructure.emplace_back(
folderPath,
curr.filename().string()
);
}
}
}
}
std::sort(
m_folderStructure.begin() + 1,
m_folderStructure.end(),
[](auto const& a, auto const& b){
return a.first < b.first;
}
);
}
void AssetManagerWindow::drawFolderTree()
{
handleNewFolderCreation();
ImGui::PushItemWidth(-1);
ImGui::InputTextWithHint("##search", "Search...", m_searchBuffer, sizeof(m_searchBuffer));
ImGui::PopItemWidth();
ImGui::Separator();
// favorites section
{
if (ImGui::TreeNodeEx(ICON_FA_STAR " Favorites", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_OpenOnDoubleClick)) {
struct FavoriteItem {
std::string label;
assets::AssetType type;
};
static const FavoriteItem favorites[] = {
{ICON_FA_ADJUST " Materials", assets::AssetType::MATERIAL},
{ICON_FA_CUBE " Models", assets::AssetType::MODEL},
{ICON_FA_SQUARE " Textures", assets::AssetType::TEXTURE}
};
for (const auto& fav : favorites) {
const bool isSelected = (fav.type == m_selectedType);
ImGuiTreeNodeFlags itemFlags = ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen;
if (isSelected)
itemFlags |= ImGuiTreeNodeFlags_Selected;
const std::string labelName = fav.label + (isSelected ? " " ICON_FA_CHECK : "");
ImGui::TreeNodeEx(labelName.c_str(), itemFlags);
if (ImGui::IsItemClicked()) {
if (isSelected)
m_selectedType = assets::AssetType::UNKNOWN;
else
m_selectedType = fav.type;
}
}
ImGui::TreePop();
}
}
// folder structure
{
ImGuiTreeNodeFlags headerFlags = ImGuiTreeNodeFlags_OpenOnDoubleClick;
if (m_currentFolder.empty()) {
headerFlags |= ImGuiTreeNodeFlags_Selected;
}
bool assetsOpen = ImGui::TreeNodeEx(ICON_FA_FOLDER " Assets", headerFlags);
// Handle right-click on Assets root
if (ImGui::BeginPopupContextItem()) {
if (ImGui::MenuItem("New Folder")) {
m_folderCreationState.parentPath = "";
m_folderCreationState.isCreatingFolder = true;
std::format_to_n(
m_folderCreationState.folderName,
sizeof(m_folderCreationState.folderName) - 1, // Ensure null termination
"New Folder"
);
}
ImGui::EndPopup();
}
if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
m_currentFolder = "";
if (assetsOpen) {
for (const auto& [path, name] : m_folderStructure) {
if (!path.empty() && path.find('/') == std::string::npos) {
drawFolderTreeItem(name, path);
}
}
ImGui::TreePop();
}
}
}
}