7#include <rapidjson/filereadstream.h>
8#include <rapidjson/document.h>
9#include <rapidjson/error/en.h>
17namespace fs = std::filesystem;
23 EE_CORE_INFO(
"Editor Assets Browser Initiated.");
31 m_shouldUpdate =
false;
37 ImGui::Begin(
"Asset Browser");
40 for (
auto it = m_relatedPaths.rbegin(); it != m_relatedPaths.rend(); ++it)
42 ImGui::BeginDisabled(it->Path == m_CurrentPath);
43 if (ImGui::Button(it->DisplayName.c_str()))
45 m_CurrentPath = it->Path;
46 m_shouldUpdate =
true;
57 ImVec2 buttonSize(72, 72);
58 float spacing = ImGui::GetStyle().ItemSpacing.x * 2;
59 float panelWidth = ImGui::GetWindowSize().x;
60 int colNb = (int) std::floor(panelWidth / (buttonSize.x + spacing));
61 colNb = std::max(1, colNb);
65 if (m_CurrentPath !=
".") {
68 ImGui::ImageButton(
"back", (ImTextureID) m_folderTexture->GetNativeHandle(), buttonSize);
70 if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
71 m_CurrentPath = m_CurrentPath.parent_path();
72 m_shouldUpdate =
true;
75 ImGui::TextWrapped(
"../");
89 if (ImGui::ImageButton(
"file_item", (ImTextureID) m_currentTextures[item.iconPath]->GetNativeHandle(), buttonSize)) {}
91 if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
93 m_CurrentPath +=
"/" + item.name;
94 m_shouldUpdate =
true;
101 ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + buttonSize.x);
102 ImGui::TextWrapped(
"%s", item.name.c_str());
103 ImGui::PopTextWrapPos();
108 if ((index + 1) % colNb != 0)
117void Elevate::Editor::AssetBrowserPanel::UpdateRelatedPaths()
119 m_relatedPaths.clear();
121 std::string displayName = m_CurrentPath ==
"." ?
"Game Content" : m_CurrentPath.filename().string();
122 m_relatedPaths.push_back({ m_CurrentPath, displayName });
123 AddParentPaths(m_CurrentPath);
126void Elevate::Editor::AssetBrowserPanel::AddParentPaths(std::filesystem::path path)
128 if (path.has_parent_path())
130 std::filesystem::path parent = path.parent_path();
131 std::string displayName = parent ==
"." ?
"Game Content" : parent.filename().string();
132 m_relatedPaths.push_back({ parent, displayName });
133 AddParentPaths(parent);
137void Elevate::Editor::AssetBrowserPanel::LoadFileItemsList()
140 m_currentTextures.clear();
142 for (
const auto& entry : fs::directory_iterator(m_CurrentPath)) {
144 std::string ext =
"";
146 if (entry.is_directory()) {
147 if (fs::is_empty(entry.path())) {
148 meta = m_FileMetadata[
"EMPTY_DIRECTORY"];
151 meta = m_FileMetadata[
"DIRECTORY"];
155 ext = entry.path().extension().string();
156 if (!ext.empty() && ext[0] ==
'.') {
161 if (std::find(m_ignoredExtensions.begin(), m_ignoredExtensions.end(), ext) != m_ignoredExtensions.end())
167 if (m_FileMetadata.find(ext) != m_FileMetadata.end()) {
168 meta = m_FileMetadata[ext];
171 meta = m_FileMetadata[
"ANY"];
176 if (meta.type ==
Image) {
177 fileItem = FileItem(entry.path().string(), entry.path().filename().string(), ext, entry.path().string(), meta.type);
180 fileItem = FileItem(entry.path().string(), entry.path().filename().string(), ext, meta.iconPath, meta.type);
184 m_FileItems.push_back(fileItem);
187 std::sort(m_FileItems.begin(), m_FileItems.end(), [](
const FileItem& a,
const FileItem& b) {
188 if (a.type == b.type)
190 return a.name < b.name;
194 return a.type < b.type;
199void Elevate::Editor::AssetBrowserPanel::LoadExtensionsMeta(std::string filepath)
202 FILE* fp = fopen(resolvedPath.c_str(),
"r");
204 EE_CORE_ERROR(
"Cannot open JSON file : {}", resolvedPath);
208 char readBuffer[65536];
209 rapidjson::FileReadStream is(fp, readBuffer,
sizeof(readBuffer));
211 rapidjson::Document doc;
215 if (doc.HasParseError()) {
216 EE_CORE_ERROR(
"Erreur parsing JSON : %s", rapidjson::GetParseError_En(doc.GetParseError()));
220 if (doc.HasMember(
"ignore") && doc[
"ignore"].IsArray()) {
221 const rapidjson::Value& ignore = doc[
"ignore"];
222 for (rapidjson::SizeType i = 0; i < ignore.Size(); i++)
224 const rapidjson::Value& filetype = ignore[i];
225 if (filetype.IsString())
227 m_ignoredExtensions.push_back(filetype.GetString());
232 if (!doc.HasMember(
"assets") || !doc[
"assets"].IsArray()) {
233 EE_CORE_ERROR(
"Could not find valid assets key in JSON file.");
237 const rapidjson::Value& assets = doc[
"assets"];
238 for (rapidjson::SizeType i = 0; i < assets.Size(); i++) {
239 const rapidjson::Value& asset = assets[i];
241 if (!asset.HasMember(
"extension") || !asset[
"extension"].IsString() ||
242 !asset.HasMember(
"iconPath") || !asset[
"iconPath"].IsString() ||
243 !asset.HasMember(
"type") || !asset[
"type"].IsString()) {
244 EE_CORE_ERROR(
"The asset %s is invalid (missing data or incorrect type)", i + 1);
248 std::string extension = asset[
"extension"].GetString();
249 std::string iconPath = asset[
"iconPath"].GetString();
250 std::string typeStr = asset[
"type"].GetString();
253 FileMetadata meta(type, iconPath);
254 m_FileMetadata[extension] = meta;
void OnImGuiRender() override
static std::string Resolve(const std::string &virtualPath)
static TexturePtr CreateFromFile(const std::string &path, TextureType usage=TextureType::Diffuse)
void OpenWithDefaultApp(const std::string &filePath)