Implement texture previews for file browser

This commit is contained in:
2024-06-25 16:14:48 -04:00
parent 73636d8b98
commit 0374ac47ce
5 changed files with 80 additions and 22 deletions

View File

@@ -16,7 +16,7 @@ namespace fgl::engine::filesystem
{
//! Textures for files (pre-rendered image, images, ect)
inline static std::unordered_map< std::filesystem::path, Texture > file_textures {};
inline static std::unordered_map< std::filesystem::path, std::shared_ptr< Texture > > file_textures {};
inline static std::unique_ptr< DirInfo > current { nullptr };
@@ -120,15 +120,6 @@ namespace fgl::engine::filesystem
ImGui::Columns( 1 );
}
void drawTexture()
{}
void drawModel()
{}
void drawBinary()
{}
std::string toHumanByteSize( size_t size )
{
if ( size < 1000 )
@@ -147,19 +138,78 @@ namespace fgl::engine::filesystem
return format_ns::format( "{:0.2f} GB", static_cast< float >( size ) / 1000.0f / 1000.0f / 1000.0f );
}
void drawTexture( const FileInfo& info )
{
if ( auto itter = file_textures.find( info.path ); itter != file_textures.end() )
{
auto& [ path, texture ] = *itter;
texture->drawImGuiButton( { desired_size, desired_size } );
}
else
{
file_texture->drawImGuiButton( { desired_size, desired_size } );
auto tex { getTextureStore().load( info.path ) };
// Add the texture
file_textures.insert( std::make_pair( info.path, std::move( tex ) ) );
}
if ( ImGui::BeginDragDropSource() )
{
ImGui::SetDragDropPayload(
DRAG_TYPE_FILE_TEXTURE_INFO, &info, sizeof( info ), ImGuiCond_Once /* Only copy the info once */ );
ImGui::SetTooltip( info.filename.c_str() );
ImGui::EndDragDropSource();
}
}
void drawBinary( const FileInfo& info )
{
// file_texture->drawImGui( { 128, 128 } );
file_texture->drawImGuiButton( { desired_size, desired_size } );
//Unable to drag/drop because we have no idea what this is supposed to be for.
}
void drawModel( const FileInfo& info )
{
//TODO: Pre-render preview image for models
drawBinary( info );
if ( ImGui::BeginDragDropSource() )
{
ImGui::SetDragDropPayload(
DRAG_TYPE_FILE_MODEL_INFO, &info, sizeof( info ), ImGuiCond_Once /* Only copy the info once */ );
ImGui::SetTooltip( info.filename.c_str() );
ImGui::EndDragDropSource();
}
}
void FileBrowser::drawFile( const FileInfo& data )
{
ImGui::PushID( data.path.c_str() );
// file_texture->drawImGui( { 128, 128 } );
file_texture->drawImGuiButton( { desired_size, desired_size } );
if ( ImGui::BeginDragDropSource() )
switch ( data.engine_type )
{
ImGui::SetDragDropPayload(
DRAG_TYPE_FILE_INFO, &data, sizeof( data ), ImGuiCond_Once /* Only copy the data once */ );
ImGui::SetTooltip( data.filename.c_str() );
ImGui::EndDragDropSource();
case TEXTURE:
drawTexture( data );
break;
case MODEL:
[[fallthrough]];
case SCENE:
drawModel( data );
break;
default:
[[fallthrough]];
case BINARY:
[[fallthrough]];
case UNKNOWN:
drawBinary( data );
break;
}
ImGui::Text( data.filename.c_str() );
@@ -194,6 +244,7 @@ namespace fgl::engine::filesystem
void FileBrowser::openFolder( DirInfo dir )
{
file_textures.clear();
current = std::make_unique< DirInfo >( dir.path );
}

View File

@@ -4,5 +4,5 @@
#pragma once
//constexpr inline static std::string_view DRAG_TYPE_FILE_INFO { "_FILE_INFO" };
#define DRAG_TYPE_FILE_INFO "_FILE_INFO"
#define DRAG_TYPE_FILE_MODEL_INFO "_MODEL_FILE_INFO"
#define DRAG_TYPE_FILE_TEXTURE_INFO "_TEXTURE_FILE_INFO"

View File

@@ -27,7 +27,7 @@ namespace fgl::engine::gui
{
if ( ImGui::BeginDragDropTarget() )
{
if ( const ImGuiPayload* payload = ImGui::AcceptDragDropPayload( DRAG_TYPE_FILE_INFO ); payload )
if ( const ImGuiPayload* payload = ImGui::AcceptDragDropPayload( DRAG_TYPE_FILE_MODEL_INFO ); payload )
{
if ( payload->IsDelivery() )
{

View File

@@ -32,11 +32,14 @@ namespace fgl::engine
}
std::tuple< std::vector< std::byte >, int, int, vk::Format >
loadTexture( const std::filesystem::path& path, const vk::Format format )
loadTexture( const std::filesystem::path& path, vk::Format format = vk::Format::eUndefined )
{
ZoneScoped;
if ( !std::filesystem::exists( path ) ) throw std::runtime_error( "Failed to open file: " + path.string() );
//TODO: More robust image loading. I should be checking what channels images have and what they are using for their bits per channel.
if ( format == vk::Format::eUndefined ) format = vk::Format::eR8G8B8A8Unorm;
int x { 0 };
int y { 0 };
int channels { 0 };
@@ -130,6 +133,9 @@ namespace fgl::engine
Texture( loadTexture( path, format ) )
{}
Texture::Texture( const std::filesystem::path& path ) : Texture( loadTexture( path ) )
{}
Texture::~Texture()
{
if ( m_imgui_set != VK_NULL_HANDLE ) ImGui_ImplVulkan_RemoveTexture( m_imgui_set );

View File

@@ -52,6 +52,7 @@ namespace fgl::engine
Texture( const std::vector< std::byte >& data, const vk::Extent2D extent, const vk::Format texture_format );
[[nodiscard]] Texture( const std::filesystem::path& path, const vk::Format format );
[[nodiscard]] Texture( const std::filesystem::path& path );
void stage( vk::raii::CommandBuffer& cmd ) override;
void dropStaging();