Files
IAEngine/Src/Editor/imp/cpp/UI/View/AssetBrowser.cpp
Isuru Samarathunga 9d6f525b81 Fixes
2025-11-11 09:24:14 +05:30

152 lines
5.4 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <Editor.hpp>
#include <UI/View/AssetBrowser.hpp>
namespace ia::iae
{
VOID View_AssetBrowser::Initialize()
{
SetName("Asset Browser");
SetIcon(ICON_FA_BOX_OPEN);
if (!std::filesystem::exists("Assets"))
THROW_INVALID_DATA("Not a valid IAEngine project directory");
m_assetDirectoryPath = std::filesystem::current_path() / "Assets/";
ChangeCurrentOpenDirectory(m_assetDirectoryPath);
FillFileEntries(m_assetDirectoryPath, m_assetDirectoryFiles);
}
VOID View_AssetBrowser::Terminate()
{
}
VOID View_AssetBrowser::Render()
{
PreRender();
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{7.5f, 7.5f});
if (ImGui::BeginTable("root", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp))
{
ImGui::TableSetupColumn("Navigation", ImGuiTableColumnFlags_WidthStretch, 0.15f);
ImGui::TableSetupColumn("Asset Browser", ImGuiTableColumnFlags_WidthStretch, 0.85f);
ImGui::TableNextColumn();
if (ImGui::BeginChild("Navigation", ImVec2(0, -FLT_MIN), ImGuiChildFlags_AlwaysUseWindowPadding))
{
for (const auto &t : m_assetDirectoryFiles)
{
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 1.75f);
if (ImGui::Selectable(t.IconAndName.c_str()))
{
if (t.IsDirectory)
{
ChangeCurrentOpenDirectory(t.Path);
break;
}
OpenAsset(t.Path);
}
}
}
ImGui::EndChild();
ImGui::TableNextColumn();
if (ImGui::BeginChild("Asset Browser", ImVec2(0, -FLT_MIN), ImGuiChildFlags_AlwaysUseWindowPadding))
{
for (const auto &t : m_currentOpenDirectoryFiles)
{
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 1.75f);
if (ImGui::Selectable(t.IconAndName.c_str()))
{
if (t.IsDirectory)
{
ChangeCurrentOpenDirectory(t.Path);
break;
}
OpenAsset(t.Path);
}
}
}
ImGui::EndChild();
ImGui::EndTable();
}
ImGui::PopStyleVar();
PostRender();
}
VOID View_AssetBrowser::Update()
{
}
VOID View_AssetBrowser::OnEvent(IN SDL_Event *event)
{
}
VOID View_AssetBrowser::ChangeCurrentOpenDirectory(IN CONST std::filesystem::path &path)
{
m_currentOpenDirectoryPath = path;
std::filesystem::current_path(m_currentOpenDirectoryPath);
if (path == m_assetDirectoryPath)
m_currentOpenDirectoryFiles.clear();
else
FillFileEntries(m_currentOpenDirectoryPath, m_currentOpenDirectoryFiles);
}
VOID View_AssetBrowser::FillFileEntries(IN CONST std::filesystem::path &path, IN Vector<FileEntry> &fileEntries)
{
fileEntries.clear();
for (const auto &t : std::filesystem::directory_iterator(path))
{
if (!t.is_directory())
continue;
fileEntries.pushBack({.Icon = ICON_FA_FOLDER,
.Name = t.path().filename().string().c_str(),
.IsDirectory = true,
.Path = t.path()});
fileEntries.back().IconAndName = BuildString(fileEntries.back().Icon, " ", fileEntries.back().Name);
}
for (const auto &t : std::filesystem::directory_iterator(path))
{
if (t.is_directory())
continue;
fileEntries.pushBack({.Icon = GetFileEntryIcon(t.path().extension().string().c_str()),
.Name = t.path().filename().string().c_str(),
.IsDirectory = false,
.Path = t.path()});
fileEntries.back().IconAndName = BuildString(fileEntries.back().Icon, " ", fileEntries.back().Name);
}
}
PCCHAR View_AssetBrowser::GetFileEntryIcon(IN PCCHAR extension)
{
if (!strcmp(extension, ".xml"))
return ICON_FA_CODE;
if (!strcmp(extension, ".png") || !strcmp(extension, ".jpg"))
return ICON_FA_IMAGE;
return ICON_FA_FILE;
}
VOID View_AssetBrowser::OpenAsset(IN CONST std::filesystem::path &path)
{
Editor::Instance().OpenAsset(path);
}
} // namespace ia::iae