Asset browser is wonky now

Refresh asset browser
Compile asset
This commit is contained in:
Sri Sham Haran 2022-11-10 23:01:09 +08:00
parent c95a6a2492
commit 2fdff77420
2 changed files with 61 additions and 16 deletions

View File

@ -36,13 +36,27 @@ namespace SHADE
DrawCurrentFolder(); DrawCurrentFolder();
} }
ImGui::End(); ImGui::End();
if (queueReset)
{
SHAssetManager::RefreshDirectory();
rootFolder = SHAssetManager::GetRootFolder();
queueReset = false;
}
}
void SHAssetBrowser::Refresh()
{
queueReset = true;
} }
void SHAssetBrowser::DrawMenuBar() void SHAssetBrowser::DrawMenuBar()
{ {
if (ImGui::BeginMenuBar()) if (ImGui::BeginMenuBar())
{ {
if(ImGui::SmallButton("Refresh"))
{
Refresh();
}
ImGui::EndMenuBar(); ImGui::EndMenuBar();
} }
} }
@ -102,16 +116,14 @@ namespace SHADE
} }
for (auto const& file : files) for (auto const& file : files)
{ {
if(file.assetMeta == nullptr)
continue;
const float horizontalLineSize = 25.0f; const float horizontalLineSize = 25.0f;
const ImRect childRect = DrawFile(file.assetMeta); const ImRect childRect = DrawFile(file);
const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f; const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f;
drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1); drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1);
vertLineEnd.y = midPoint; vertLineEnd.y = midPoint;
} }
drawList->AddLine(vertLineStart, vertLineEnd, treeLineColor, 1); drawList->AddLine(vertLineStart, vertLineEnd, treeLineColor, 1);
if(assetBeingCreated.has_value() && std::get<0>(assetBeingCreated.value()) == folder) if (assetBeingCreated.has_value() && std::get<0>(assetBeingCreated.value()) == folder)
DrawAssetBeingCreated(); DrawAssetBeingCreated();
ImGui::TreePop(); ImGui::TreePop();
@ -148,12 +160,40 @@ namespace SHADE
//} //}
} }
ImRect SHAssetBrowser::DrawFile(SHAsset const* const asset) noexcept ImRect SHAssetBrowser::DrawFile(SHFile file) noexcept
{ {
if (asset == nullptr)
return ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); SHAsset const* const asset = file.assetMeta;
const bool isSelected = std::ranges::find(selectedAssets, asset->id) != selectedAssets.end(); if (file.compilable)
ImGuiTreeNodeFlags flags = (!asset->subAssets.empty()) ? ImGuiTreeNodeFlags_OpenOnArrow : ImGuiTreeNodeFlags_Leaf; {
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf;
std::string icon{ ICON_MD_FILE_PRESENT };
bool const isOpen = ImGui::TreeNodeEx(file.name.data(), flags, "%s %s%s", icon.data(), file.name.data(), file.ext.data());
const ImRect nodeRect = ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax());
if (ImGui::BeginPopupContextItem())
{
if (ImGui::Selectable("Compile"))
{
SHAssetManager::CompileAsset(file.path, true);
Refresh();
}
ImGui::EndPopup();
}
ImGui::TreePop();
return nodeRect;
}
if (asset)
DrawAsset(asset);
}
ImRect SHAssetBrowser::DrawAsset(SHAsset const* const asset)
{
const bool isSelected = asset ? std::ranges::find(selectedAssets, asset->id) != selectedAssets.end() : false;
ImGuiTreeNodeFlags flags = (asset && !asset->subAssets.empty()) ? ImGuiTreeNodeFlags_OpenOnArrow : ImGuiTreeNodeFlags_Leaf;
if (isSelected) if (isSelected)
flags |= ImGuiTreeNodeFlags_Selected; flags |= ImGuiTreeNodeFlags_Selected;
std::string icon{}; std::string icon{};
@ -172,9 +212,11 @@ namespace SHADE
case AssetType::MAX_COUNT: break; case AssetType::MAX_COUNT: break;
default:; default:;
} }
bool const isOpen = ImGui::TreeNodeEx(asset, flags, "%s %s", icon.data(), asset->name.data()); bool const isOpen = ImGui::TreeNodeEx(asset, flags, "%s %s", icon.data(), asset->name.data());
const ImRect nodeRect = ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); const ImRect nodeRect = ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax());
if (SHDragDrop::BeginSource()) if (SHDragDrop::BeginSource())
{ {
auto id = asset->id; auto id = asset->id;
@ -197,7 +239,7 @@ namespace SHADE
case AssetType::TEXTURE: break; case AssetType::TEXTURE: break;
case AssetType::MESH: break; case AssetType::MESH: break;
case AssetType::SCENE: case AssetType::SCENE:
if(auto editor = SHSystemManager::GetSystem<SHEditor>()) if (auto editor = SHSystemManager::GetSystem<SHEditor>())
{ {
editor->LoadScene(asset->id); editor->LoadScene(asset->id);
} }
@ -222,12 +264,12 @@ namespace SHADE
ImVec2 vertLineStart = ImGui::GetCursorScreenPos(); ImVec2 vertLineStart = ImGui::GetCursorScreenPos();
vertLineStart.x += horizontalOffset; vertLineStart.x += horizontalOffset;
ImVec2 vertLineEnd = vertLineStart; ImVec2 vertLineEnd = vertLineStart;
if(isOpen) if (isOpen)
{ {
for(auto const& subAsset : asset->subAssets) for (auto const& subAsset : asset->subAssets)
{ {
const float horizontalLineSize = 25.0f; const float horizontalLineSize = 25.0f;
const ImRect childRect = DrawFile(subAsset); const ImRect childRect = DrawAsset(subAsset);
const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f; const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f;
drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1); drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1);
vertLineEnd.y = midPoint; vertLineEnd.y = midPoint;
@ -253,6 +295,7 @@ namespace SHADE
matInspector->OpenMaterial(assetId, true); matInspector->OpenMaterial(assetId, true);
} }
assetBeingCreated.reset(); assetBeingCreated.reset();
Refresh();
} }
} }
} }

View File

@ -21,7 +21,8 @@ namespace SHADE
void DrawMenuBar(); void DrawMenuBar();
ImRect RecursivelyDrawTree(FolderPointer folder); ImRect RecursivelyDrawTree(FolderPointer folder);
void DrawCurrentFolder(); void DrawCurrentFolder();
ImRect DrawFile(SHAsset const* const asset) noexcept; ImRect DrawFile(SHFile file) noexcept;
ImRect DrawAsset(SHAsset const* const asset);
void DrawAssetBeingCreated() noexcept; void DrawAssetBeingCreated() noexcept;
FolderPointer rootFolder, prevFolder, currentFolder; FolderPointer rootFolder, prevFolder, currentFolder;
@ -29,5 +30,6 @@ namespace SHADE
std::vector<FolderPointer> selectedFolders; std::vector<FolderPointer> selectedFolders;
std::vector<AssetID> selectedAssets; std::vector<AssetID> selectedAssets;
static constexpr float tileWidth = 50.0f; static constexpr float tileWidth = 50.0f;
bool queueReset = false;
}; };
} }