light/modules/mirror/private/panels/asset_browser.cpp

152 lines
3.7 KiB
C++
Raw Normal View History

#include <asset_manager/asset_manager.hpp>
2025-07-11 01:16:52 +03:30
#include <ecs/scene.hpp>
#include <ecs/serializer.hpp>
#include <imgui.h>
2025-07-20 04:46:15 +03:30
#include <mirror/panels/asset_browser.hpp>
#include <renderer/texture.hpp>
2025-07-11 00:05:48 +03:30
namespace lt {
2025-07-06 14:09:58 +03:30
AssetBrowserPanel::AssetBrowserPanel(Ref<Scene> active_scene)
: m_current_directory("./data/assets")
, m_assets_path("./data/assets")
, m_active_scene(std::move(active_scene))
2022-03-06 22:25:23 +03:30
{
AssetManager::load_texture("_Assets_Directory", "data/engine/icons/asset/dir.asset");
AssetManager::load_texture("_Assets_Scene", "data/engine/icons/asset/scene.asset");
AssetManager::load_texture("_Assets_Image", "data/engine/icons/asset/img.asset");
AssetManager::load_texture("_Assets_Text", "data/engine/icons/asset/txt.asset");
m_directory_texture = AssetManager::get_texture("_Assets_Directory");
m_scene_texture = AssetManager::get_texture("_Assets_Scene");
m_image_texture = AssetManager::get_texture("_Assets_Image");
m_text_texture = AssetManager::get_texture("_Assets_Text");
2022-03-06 22:25:23 +03:30
}
void AssetBrowserPanel::on_user_interface_update()
2022-03-04 22:40:20 +03:30
{
ImGui::Begin("Content Browser");
2022-03-12 21:10:23 +03:30
// Parent directory button
2025-07-06 14:09:58 +03:30
if (m_current_directory != std::filesystem::path("data/assets"))
2022-03-04 22:40:20 +03:30
{
if (ImGui::Button(" <-- "))
{
m_current_directory = m_current_directory.parent_path();
}
2022-03-04 22:40:20 +03:30
}
2025-07-06 15:10:34 +03:30
const auto available_region = ImGui::GetContentRegionAvail();
const auto cell_size = m_file_size + m_file_padding;
const auto column_count = std::clamp(
static_cast<uint32_t>(std::floor(available_region.x / cell_size)),
2025-07-05 13:28:41 +03:30
1u,
64u
);
2022-03-06 22:25:23 +03:30
2025-07-06 15:10:34 +03:30
if (ImGui::BeginTable("ContentBrowser", static_cast<int>(column_count)))
2022-03-04 22:40:20 +03:30
{
m_directory_texture->bind(0u);
2025-07-06 15:10:34 +03:30
for (const auto &directory_entry : std::filesystem::directory_iterator(m_current_directory))
{
2025-07-06 15:10:34 +03:30
const auto &path = directory_entry.path();
auto extension = directory_entry.path().extension().string();
2022-03-06 22:25:23 +03:30
2025-07-06 15:10:34 +03:30
auto asset_type = AssetType {};
2025-07-06 15:10:34 +03:30
if (extension.empty())
{
asset_type = AssetType::directory;
}
else if (extension == ".txt" || extension == ".glsl")
{
asset_type = AssetType::text;
}
else if (extension == ".png")
{
asset_type = AssetType::image;
}
else if (extension == ".scene")
{
asset_type = AssetType::scene;
}
else
{
asset_type = AssetType::none;
}
2022-03-12 21:10:23 +03:30
// Extension not supported
2025-07-06 15:10:34 +03:30
if (asset_type == AssetType::none)
{
continue;
}
2022-03-06 22:25:23 +03:30
// Button
const auto path_str = path.string();
ImGui::TableNextColumn();
ImGui::PushID(path_str.c_str());
2025-07-06 15:10:34 +03:30
switch (asset_type)
{
2022-03-12 21:10:23 +03:30
// Directory
2025-07-06 14:09:58 +03:30
case AssetType::directory:
2025-07-05 13:28:41 +03:30
if (ImGui::ImageButton(
path_str.c_str(),
m_directory_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
2025-07-05 13:28:41 +03:30
))
{
m_current_directory /= path.filename();
}
break;
2022-03-12 21:10:23 +03:30
// Scene
2025-07-06 14:09:58 +03:30
case AssetType::scene:
2025-07-05 13:28:41 +03:30
if (ImGui::ImageButton(
path_str.c_str(),
m_scene_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
2025-07-05 13:28:41 +03:30
))
2022-03-12 21:10:23 +03:30
{
2025-07-06 14:02:50 +03:30
auto serializer = SceneSerializer { m_active_scene };
2025-07-06 16:30:38 +03:30
log_inf("Attempting to deserialize: {}", path.string());
serializer.deserialize(path.string());
2022-03-12 21:10:23 +03:30
}
break;
// Image
2025-07-06 14:09:58 +03:30
case AssetType::image:
2025-07-05 13:28:41 +03:30
if (ImGui::ImageButton(
path_str.c_str(),
m_image_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
2025-07-05 13:28:41 +03:30
))
{
}
break;
2022-03-06 22:25:23 +03:30
2022-03-12 21:10:23 +03:30
// Text
2025-07-06 14:09:58 +03:30
case AssetType::text:
2025-07-05 13:28:41 +03:30
if (ImGui::ImageButton(
path_str.c_str(),
m_text_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
2025-07-05 13:28:41 +03:30
))
{
}
break;
2022-03-12 21:10:23 +03:30
2025-07-05 13:28:41 +03:30
default: break;
}
// Label
2025-07-16 13:56:59 +03:30
ImGui::TextUnformatted(std::format("{}", path.filename().string()).c_str());
ImGui::PopID();
}
2022-03-06 22:25:23 +03:30
ImGui::EndTable();
}
2022-03-06 22:25:23 +03:30
ImGui::End();
2022-03-04 22:40:20 +03:30
}
2025-07-11 00:05:48 +03:30
} // namespace lt