Edit(Mirror): AssetBrowserPanel

- Renamed ContentBrowserPanel -> AssetBrowserPanel
- AssetBrowserPanel now shows directory/txt/image files
      with image buttons
This commit is contained in:
Light 2022-03-07 20:01:44 +03:30
parent 8c0bdbf849
commit b1e60d08b7
13 changed files with 165 additions and 145 deletions

View file

@ -4,28 +4,30 @@
namespace Light {
class SharedContext;
class SharedContext;
// #todo: improve textures
class Texture
{
public:
static Ref<Texture> Create(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, Ref<SharedContext> sharedContext, const std::string& filePath);
// #todo: improve textures
class Texture
{
public:
static Ref<Texture> Create(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, Ref<SharedContext> sharedContext, const std::string& filePath);
Texture(const Texture&) = delete;
Texture& operator=(const Texture&) = delete;
Texture(const Texture&) = delete;
Texture& operator=(const Texture&) = delete;
virtual ~Texture() = default;
virtual ~Texture() = default;
virtual void Bind(unsigned int slot = 0) = 0;
virtual void Bind(unsigned int slot = 0) = 0;
inline const std::string& GetFilePath() const { return m_FilePath; }
virtual void* GetTexture() = 0;
protected:
Texture(const std::string& filePath);
inline const std::string& GetFilePath() const { return m_FilePath; }
protected:
std::string m_FilePath;
};
protected:
Texture(const std::string& filePath);
}
protected:
std::string m_FilePath;
};
} // namespace Light

View file

@ -17,9 +17,10 @@
#include "Events/WindowEvents.h"
// graphics
#include "Graphics/Framebuffer.h"
#include "Graphics/GraphicsContext.h"
#include "Graphics/Renderer.h"
#include "Graphics/Framebuffer.h"
#include "Graphics/Texture.h"
// input
#include "Input/Input.h"
@ -49,9 +50,9 @@
#include "Math/Random.h"
// scene
#include "Scene/Scene.h"
#include "Scene/Entity.h"
#include "Scene/Components.h"
#include "Scene/Entity.h"
#include "Scene/Scene.h"
// entry point
#ifdef LIGHT_ENTRY_POINT

View file

@ -1,10 +1,9 @@
#include "glShader.h"
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/matrix.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/matrix.hpp>
namespace Light {
@ -17,7 +16,6 @@ glShader::glShader(BasicFileHandle vertexFile, BasicFileHandle pixelFile)
std::string vertexSource(vertexFile.GetData(), vertexFile.GetData() + vertexFile.GetSize());
std::string pixelSource(pixelFile.GetData(), pixelFile.GetData() + pixelFile.GetSize());
LT_ENGINE_WARN(pixelSource);
unsigned int vertexShader = CompileShader(vertexSource, Shader::Stage::VERTEX);
unsigned int pixelShader = CompileShader(pixelSource, Shader::Stage::PIXEL);
@ -74,7 +72,6 @@ unsigned int glShader::CompileShader(std::string source, Shader::Stage stage)
{
// &(address of) needs an lvalue
const char* lvalue_source = source.c_str();
LT_ENGINE_WARN("Source______________________________________\n{}", lvalue_source);
unsigned int shader = glCreateShader(stage == Shader::Stage::VERTEX ? GL_VERTEX_SHADER :
stage == Shader::Stage::PIXEL ? GL_FRAGMENT_SHADER :
stage == Shader::Stage::GEOMETRY ? GL_GEOMETRY_SHADER :
@ -110,7 +107,7 @@ unsigned int glShader::CompileShader(std::string source, Shader::Stage stage)
char* infoLog = (char*)alloca(logLength);
glGetShaderInfoLog(shader, logLength, &logLength, &infoLog[0]);
LT_ENGINE_TRACE(infoLog);
LT_ENGINE_WARN(infoLog);
}
}
#endif

View file

@ -4,52 +4,56 @@
namespace Light {
glTexture::glTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, const std::string& filePath):
Texture(filePath),
m_TextureID(NULL)
{
glTexture::glTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, const std::string& filePath)
: Texture(filePath), m_TextureID(NULL)
{
// create texture
glCreateTextures(GL_TEXTURE_2D, 1, &m_TextureID);
// create texture
glCreateTextures(GL_TEXTURE_2D, 1, &m_TextureID);
// set texture parameters
glTextureParameteri(m_TextureID, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTextureParameteri(m_TextureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(m_TextureID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(m_TextureID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// set texture parameters
glTextureParameteri(m_TextureID, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTextureParameteri(m_TextureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(m_TextureID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(m_TextureID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// determine formats
unsigned int format = components == 4u ? GL_RGBA :
components == 3u ? GL_RGB :
components == 2u ? GL_RG :
components == 1u ? GL_RED : NULL;
// determine formats
unsigned int format = components == 4u ? GL_RGBA :
components == 3u ? GL_RGB :
components == 2u ? GL_RG :
components == 1u ? GL_RED :
NULL;
unsigned int internalFormat = format == GL_RGBA ? GL_RGBA8 :
format == GL_RGB ? GL_RGB8 :
format == GL_RG ? GL_RG8 :
format == GL_RED ? GL_R8 : NULL;
unsigned int internalFormat = format == GL_RGBA ? GL_RGBA8 :
format == GL_RGB ? GL_RGB8 :
format == GL_RG ? GL_RG8 :
format == GL_RED ? GL_R8 :
NULL;
// check
LT_ENGINE_ASSERT(format, "glTexture::glTexture: invalid number of components: {}", components);
// check
LT_ENGINE_ASSERT(format, "glTexture::glTexture: invalid number of components: {}", components);
// #todo: isn't there something like glTextureImage2D ???
// create texture and mipsmaps
Bind();
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
glGenerateMipmap(GL_TEXTURE_2D);
}
// #todo: isn't there something like glTextureImage2D ???
// create texture and mipsmaps
Bind();
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
glGenerateMipmap(GL_TEXTURE_2D);
}
glTexture::~glTexture()
{
glDeleteTextures(1, &m_TextureID);
}
glTexture::~glTexture()
{
glDeleteTextures(1, &m_TextureID);
}
void glTexture::Bind(unsigned int slot /* = 0u */)
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_TextureID);
}
void glTexture::Bind(unsigned int slot /* = 0u */)
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_TextureID);
}
}
void* glTexture::GetTexture()
{
return (void*)(intptr_t)m_TextureID;
}
} // namespace Light

View file

@ -1,21 +1,22 @@
#pragma once
#include "Graphics/Texture.h"
#include "Base/Base.h"
#include "Graphics/Texture.h"
namespace Light {
class glTexture : public Texture
{
private:
unsigned int m_TextureID;
class glTexture: public Texture
{
private:
unsigned int m_TextureID;
public:
glTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, const std::string& filePath);
~glTexture();
public:
glTexture(unsigned int width, unsigned int height, unsigned int components, unsigned char* pixels, const std::string& filePath);
~glTexture();
void Bind(unsigned int slot = 0u) override;
};
void Bind(unsigned int slot = 0u) override;
}
void* GetTexture() override;
};
} // namespace Light

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

View file

@ -11,7 +11,7 @@ EditorLayer::EditorLayer(const std::string& name, const std::vector<std::string>
m_PropertiesPanel = CreateRef<PropertiesPanel>();
m_SceneHierarchyPanel = CreateRef<SceneHierarchyPanel>(m_Scene, m_PropertiesPanel);
m_ContentBrowserPanel = CreateRef<ContentBrowserPanel>();
m_ContentBrowserPanel = CreateRef<AssetBrowserPanel>();
m_Framebuffer = Framebuffer::Create({ 1, 1, 1 }, GraphicsContext::GetSharedContext());

View file

@ -22,7 +22,7 @@ private:
Ref<SceneHierarchyPanel> m_SceneHierarchyPanel;
Ref<PropertiesPanel> m_PropertiesPanel;
Ref<ContentBrowserPanel> m_ContentBrowserPanel;
Ref<AssetBrowserPanel> m_ContentBrowserPanel;
Ref<Framebuffer> m_Framebuffer;

View file

@ -5,14 +5,18 @@
namespace Light {
ContentBrowserPanel::ContentBrowserPanel()
AssetBrowserPanel::AssetBrowserPanel()
: m_CurrentDirectory("Assets"), m_AssetsPath("Assets")
{
// ResourceManager::LoadTexture("_Assets_Directory", "EngineResources/Icons/Asset_Directory");
// m_DirectoryTexture = ResourceManager::GetTexture("_Assets_Directory");
ResourceManager::LoadTexture("_Assets_Directory", "EngineResources/Icons/Asset_Directory.png");
ResourceManager::LoadTexture("_Assets_Image", "EngineResources/Icons/Asset_Image.png");
ResourceManager::LoadTexture("_Assets_Text", "EngineResources/Icons/Asset_Text.png");
m_DirectoryTexture = ResourceManager::GetTexture("_Assets_Directory");
m_ImageTexture = ResourceManager::GetTexture("_Assets_Image");
m_TextTexture = ResourceManager::GetTexture("_Assets_Text");
}
void ContentBrowserPanel::OnUserInterfaceUpdate()
void AssetBrowserPanel::OnUserInterfaceUpdate()
{
ImGui::Begin("Content Browser");
@ -31,58 +35,68 @@ void ContentBrowserPanel::OnUserInterfaceUpdate()
if (ImGui::BeginTable("ContentBrowser", columnCount))
{
m_DirectoryTexture->Bind(0u);
for (auto& dirEntry : std::filesystem::directory_iterator(m_CurrentDirectory))
{
ImGui::TableNextColumn();
LT_ENGINE_TRACE("File: ", dirEntry.path().string());
FileType assetType;
AssetType assetType;
std::string extension = dirEntry.path().extension().string();
// TODO: Tidy up
assetType = extension.empty() ? FileType::Directory :
extension == ".txt" ? FileType::Text :
extension == ".png" ? FileType::Image :
FileType::None;
assetType = extension.empty() ? AssetType::Directory :
if (assetType == FileType::None)
extension == ".txt" ? AssetType::Text :
extension == ".glsl" ? AssetType::Text :
extension == ".png" ? AssetType::Image :
AssetType::None;
// Unsupported asset type
if (assetType == AssetType::None)
{
LT_ENGINE_ERROR("Unsupported file exetnsion: {}", extension);
return;
continue;
}
const auto& path = dirEntry.path();
{
auto relativePath = std::filesystem::relative(path, m_AssetsPath);
std::string relativePathString = relativePath.string();
const auto& path = dirEntry.path();
auto relativePath = std::filesystem::relative(path, m_AssetsPath);
std::string relativePathString = relativePath.string();
switch (assetType)
// Button
ImGui::TableNextColumn();
ImGui::PushID(path.c_str());
switch (assetType)
{
case AssetType::Directory:
if (ImGui::ImageButton(m_DirectoryTexture->GetTexture(), ImVec2(m_FileSize, m_FileSize), ImVec2 { 0.0f, 0.0f }, ImVec2 { 1.0f, 1.0f }, 0, ImVec4 { 0.0f, 0.0f, 0.0f, 0.0f }, ImVec4 { 1.0f, 1.0f, 1.0f, 1.0f }))
{
case FileType::Directory:
if (ImGui::Button(relativePathString.c_str(), ImVec2(m_FileSize, m_FileSize)))
{
m_CurrentDirectory /= path.filename();
}
ImGui::Text("Test");
break;
case FileType::Image:
if (ImGui::Button(relativePathString.c_str(), ImVec2(m_FileSize, m_FileSize)))
{
}
break;
case FileType::Text:
if (ImGui::Button(relativePathString.c_str(), ImVec2(m_FileSize, m_FileSize)))
{
}
default:
break;
m_CurrentDirectory /= path.filename();
LT_ENGINE_INFO(path.filename().string());
}
break;
case AssetType::Image:
if (ImGui::ImageButton(m_ImageTexture->GetTexture(), ImVec2(m_FileSize, m_FileSize), ImVec2 { 0.0f, 0.0f }, ImVec2 { 1.0f, 1.0f }, 0, ImVec4 { 0.0f, 0.0f, 0.0f, 0.0f }, ImVec4 { 1.0f, 1.0f, 1.0f, 1.0f }))
{
}
break;
case AssetType::Text:
if (ImGui::ImageButton(m_TextTexture->GetTexture(), ImVec2(m_FileSize, m_FileSize), ImVec2 { 0.0f, 0.0f }, ImVec2 { 1.0f, 1.0f }, 0, ImVec4 { 0.0f, 0.0f, 0.0f, 0.0f }, ImVec4 { 1.0f, 1.0f, 1.0f, 1.0f }))
{
}
break;
default:
break;
}
// Label
ImGui::Text("%s", path.filename().c_str());
ImGui::PopID();
}
ImGui::EndTable();
}
ImGui::DragInt("Size", (int*)&m_FileSize, 32u, 512u);
ImGui::DragInt("Padding", (int*)&m_FilePadding, 32u, 512u);
ImGui::End();
}

View file

@ -7,10 +7,10 @@
namespace Light {
class ContentBrowserPanel: public Panel
class AssetBrowserPanel: public Panel
{
private:
enum FileType
enum AssetType
{
None = 0,
Directory,
@ -19,7 +19,7 @@ private:
};
public:
ContentBrowserPanel();
AssetBrowserPanel();
void OnUserInterfaceUpdate();
@ -28,10 +28,12 @@ private:
const std::filesystem::path m_AssetsPath;
// TODO: Save configuration
uint32_t m_FileSize = 256u;
uint32_t m_FilePadding = 16u;
uint32_t m_FileSize = 128u;
uint32_t m_FilePadding = 8u;
Ref<Texture> m_DirectoryTexture;
Ref<Texture> m_ImageTexture;
Ref<Texture> m_TextTexture;
};
} // namespace Light

View file

@ -1,53 +1,52 @@
[Window][Dockspace]
Pos=0,0
Size=1270,1404
Size=843,1404
Collapsed=0
[Window][Debug##Default]
ViewportPos=2170,695
ViewportId=0x4DBAC3CB
Size=400,400
ViewportPos=2078,721
ViewportId=0x9F5F46A1
Size=848,1408
Collapsed=0
DockId=0x00000007,0
[Window][Dear ImGui Demo]
Pos=737,24
Size=335,1380
Pos=372,24
Size=295,1380
Collapsed=0
DockId=0x00000004,0
[Window][Hierarchy]
Pos=0,24
Size=363,1380
Size=184,1380
Collapsed=0
DockId=0x00000001,0
[Window][Properties]
Pos=1074,24
Size=182,1380
Pos=669,24
Size=160,1380
Collapsed=0
DockId=0x00000005,0
[Window][Game]
Pos=365,24
Size=370,1380
Pos=186,24
Size=184,1380
Collapsed=0
DockId=0x00000002,0
[Window][Content Browser]
ViewportPos=2170,695
ViewportId=0x4DBAC3CB
Size=400,400
ViewportPos=1359,621
ViewportId=0x371352B7
Size=1274,1296
Collapsed=0
DockId=0x00000007,1
[Docking][Data]
DockNode ID=0x00000007 Pos=2170,695 Size=400,400 Selected=0x371352B7
DockSpace ID=0x1ED03EE2 Window=0x5B816B74 Pos=6,30 Size=1256,1380 Split=X
DockNode ID=0x00000006 Parent=0x1ED03EE2 SizeRef=735,696 Split=X
DockNode ID=0x00000001 Parent=0x00000006 SizeRef=363,696 Selected=0x788BAA0D
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=370,696 CentralNode=1 Selected=0x83199EB2
DockNode ID=0x00000003 Parent=0x1ED03EE2 SizeRef=519,696 Split=X
DockNode ID=0x00000004 Parent=0x00000003 SizeRef=335,680 Selected=0xE927CF2F
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=182,680 Selected=0xC89E3217
DockNode ID=0x00000007 Pos=2078,721 Size=848,1408 Selected=0xBF096F38
DockSpace ID=0x1ED03EE2 Window=0x5B816B74 Pos=1711,30 Size=829,1380 Split=X
DockNode ID=0x00000006 Parent=0x1ED03EE2 SizeRef=370,696 Split=X
DockNode ID=0x00000001 Parent=0x00000006 SizeRef=184,696 Selected=0x29EABFBD
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=184,696 CentralNode=1 Selected=0x26816F31
DockNode ID=0x00000003 Parent=0x1ED03EE2 SizeRef=457,696 Split=X
DockNode ID=0x00000004 Parent=0x00000003 SizeRef=295,680 Selected=0xE87781F4
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=160,680 Selected=0x199AB496