Asset Browser tool bar improved

Asset Browser directory tree behavior improved
gui_api_redesign
Joey Pollack 4 years ago
parent ce1ce8ef49
commit 72894c35fa

@ -59,7 +59,8 @@ namespace lunarium
// NOTE: Generating a font file for now. Late might look into loading directly from memory // NOTE: Generating a font file for now. Late might look into loading directly from memory
DataManager::GenerateFontFileAt(Font::F_ROBOTO, "robo.ttf"); DataManager::GenerateFontFileAt(Font::F_ROBOTO, "robo.ttf");
//io.Fonts->AddFontFromFileTTF("open.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("open.ttf", 16.0f);
io.Fonts->AddFontFromFileTTF("robo.ttf", 18.0f); mFonts[GuiFont::FONT_ROBO] = io.Fonts->AddFontFromFileTTF("robo.ttf", 18.0f);
mFonts[GuiFont::FONT_ROBO_SMALL] = io.Fonts->AddFontFromFileTTF("robo.ttf", 12.0f);
//io.Fonts->AddFontFromFileTTF("Karla-Regular.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("Karla-Regular.ttf", 16.0f);
// Setup Dear ImGui style // Setup Dear ImGui style
@ -139,6 +140,12 @@ namespace lunarium
} }
ImFont* GUI::GetFont(GuiFont font)
{
return mFonts[font];
}
void GUI::SetStyle(GuiStyle style) void GUI::SetStyle(GuiStyle style)
{ {
// Reset the style // Reset the style

@ -10,9 +10,11 @@
#define GUI_H_ #define GUI_H_
#include <utils/op_res.h> #include <utils/op_res.h>
#include <map>
struct GLFWwindow; struct GLFWwindow;
struct ImGuiStyle; struct ImGuiStyle;
struct ImFont;
namespace lunarium namespace lunarium
{ {
@ -30,6 +32,12 @@ namespace lunarium
STYLE_CORPORATE_GRAY_3D, STYLE_CORPORATE_GRAY_3D,
}; };
enum GuiFont
{
FONT_ROBO,
FONT_ROBO_SMALL,
};
class GUI class GUI
{ {
public: public:
@ -45,6 +53,7 @@ namespace lunarium
bool WantCaptureKeyboard() const; bool WantCaptureKeyboard() const;
void SetStyle(GuiStyle style); void SetStyle(GuiStyle style);
ImFont* GetFont(GuiFont font);
private: private:
GUI(); GUI();
@ -55,6 +64,7 @@ namespace lunarium
static GUI* mpInstance; static GUI* mpInstance;
bool mbIsInit; bool mbIsInit;
ImGuiStyle* mpBaseStyle; ImGuiStyle* mpBaseStyle;
std::map<GuiFont, ImFont*> mFonts;
//bool mbShowDemo; //bool mbShowDemo;
private: // STYLES private: // STYLES

@ -7,11 +7,14 @@
******************************************************************************/ ******************************************************************************/
#include "asset_browser.h" #include "asset_browser.h"
#include <gui/gui.h>
#include <dearimgui/imgui.h> #include <dearimgui/imgui.h>
#include <editor/editor.h> #include <editor/editor.h>
#include <editor/contents/content_manager.h> #include <editor/contents/content_manager.h>
#include <editor/contents/editor_asset.h> #include <editor/contents/editor_asset.h>
#include <utils/logger.h> #include <utils/logger.h>
#include <assets/types/image.h>
#include <internal_data/data_manager.h>
namespace lunarium namespace lunarium
{ {
@ -19,10 +22,8 @@ namespace editor
{ {
AssetBrowser::AssetBrowser(std::filesystem::path dir, Editor* pEditor) AssetBrowser::AssetBrowser(std::filesystem::path dir, Editor* pEditor)
: Panel("Asset Browser", gui::PanelDockZone::DDZ_BOTTOM, true), : Panel("Asset Browser", gui::PanelDockZone::DDZ_BOTTOM, true),
mAssetDirectory(dir), mTreeRoot(nullptr), mSelectedNode(nullptr), mpEditor(pEditor), mNewFolder(false), mSyncTree(false) mAssetDirectory(dir), mpEditor(pEditor), mNewFolder(false), mSyncTree(false)
{ {
mTreeRoot = ReloadAssets(mAssetDirectory);
mSelectedNode = mTreeRoot;
} }
void AssetBrowser::SetAssetDirectory(std::filesystem::path dir) void AssetBrowser::SetAssetDirectory(std::filesystem::path dir)
@ -66,6 +67,63 @@ namespace editor
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + wind_size.x * 0.15f); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + wind_size.x * 0.15f);
// Contents // Contents
DoContentArea(wind_size);
ImGui::End();
return true;
}
void AssetBrowser::DoDirTree(std::filesystem::path dir)
{
if ("" == dir)
return;
if (!std::filesystem::is_directory(dir))
return;
std::string next_node_name = dir.filename().string();
if (mSyncTree)
{
if (mSelectedDir.string().find(next_node_name) != std::string::npos)
{
ImGui::SetNextItemOpen(true);
}
else
{
ImGui::SetNextItemOpen(false);
}
}
if (ImGui::TreeNode(next_node_name.c_str()))
{
if (!mSyncTree)
{
if (ImGui::IsItemClicked())
{
mSelectedDir = dir;
ImGui::SetNextItemOpen(false);
}
HandleAssetDrop(mSelectedDir);
}
for(auto const& dir_entry: std::filesystem::directory_iterator{dir})
{
DoDirTree(dir_entry);
}
ImGui::TreePop();
}
else
{
if (ImGui::IsItemClicked())
{
mSelectedDir = dir;
ImGui::SetNextItemOpen(false);
}
}
}
void AssetBrowser::DoContentArea(ImVec2 wind_size)
{
if (ImGui::BeginChild("Contents", ImVec2(wind_size.x * 0.85f, wind_size.y), true, ImGuiWindowFlags_NoCollapse)) if (ImGui::BeginChild("Contents", ImVec2(wind_size.x * 0.85f, wind_size.y), true, ImGuiWindowFlags_NoCollapse))
{ {
if (std::filesystem::exists(mSelectedDir)) if (std::filesystem::exists(mSelectedDir))
@ -74,7 +132,11 @@ namespace editor
float xPos = ImGui::GetCursorPosX(); float xPos = ImGui::GetCursorPosX();
float left = xPos; float left = xPos;
ImGui::BeginChild("ToolBar", ImVec2(0, ImGui::GetFrameHeightWithSpacing()));
ImGui::BeginChild("ToolBar", ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 1.5f));
ImGui::PushFont(GUI::GetInstance().GetFont(GuiFont::FONT_ROBO_SMALL));
ImGui::TextUnformatted(mpEditor->GetProject()->MakeRelativeToAssets(mSelectedDir, true).string().c_str());
ImGui::PopFont();
if (ImGui::Button("Back")) if (ImGui::Button("Back"))
{ {
mSelectedDir = mSelectedDir.parent_path(); mSelectedDir = mSelectedDir.parent_path();
@ -83,14 +145,18 @@ namespace editor
ImGui::EndChild(); ImGui::EndChild();
ImGui::Separator();
ImGui::BeginChild("Files"); ImGui::BeginChild("Files");
// List directories // List directories
for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) for (auto const &dir_entry : std::filesystem::directory_iterator{mSelectedDir})
{ {
if (dir_entry.is_directory()) if (dir_entry.is_directory())
{ {
ImGui::Text("DIRECTORY: %s", dir_entry.path().filename().string().c_str()); ImGui::Image((ImTextureID)DataManager::mFolderIcon->GetGLTextureID64(),
ImVec2(DataManager::mFolderIcon->GetWidth(), DataManager::mFolderIcon->GetHeight()));
ImGui::SameLine();
ImGui::Selectable(dir_entry.path().filename().string().c_str());
HandleAssetDrop(dir_entry.path()); HandleAssetDrop(dir_entry.path());
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
{ {
@ -105,7 +171,7 @@ namespace editor
} }
std::filesystem::path selected_final = mpEditor->GetProject()->MakeRelativeToAssets(mSelectedDir); std::filesystem::path selected_final = mpEditor->GetProject()->MakeRelativeToAssets(mSelectedDir);
std::vector<EditorAsset*> assets; std::vector<EditorAsset *> assets;
ContentManager::GetInstance().GetAllAssetsInDirectory(assets, selected_final); ContentManager::GetInstance().GetAllAssetsInDirectory(assets, selected_final);
for (auto iter = assets.begin(); iter != assets.end(); iter++) for (auto iter = assets.begin(); iter != assets.end(); iter++)
@ -119,7 +185,7 @@ namespace editor
{ {
// Need to pass a pointer to the payload data. This means we need to pass a // Need to pass a pointer to the payload data. This means we need to pass a
// pointer to the EditorAsset pointer (EditorAsset**) which &(*iter) becomes // pointer to the EditorAsset pointer (EditorAsset**) which &(*iter) becomes
ImGui::SetDragDropPayload("EditorAsset", (void*)&(*iter), sizeof(EditorAsset*)); ImGui::SetDragDropPayload("EditorAsset", (void *)&(*iter), sizeof(EditorAsset *));
ImGui::Text("%s", (*iter)->GetFileLocation().stem().string().c_str()); ImGui::Text("%s", (*iter)->GetFileLocation().stem().string().c_str());
ImGui::EndDragDropSource(); ImGui::EndDragDropSource();
} }
@ -130,52 +196,12 @@ namespace editor
} }
} }
DoContextMenu(); DoContextMenu();
ImGui::EndChild(); ImGui::EndChild();
} }
} }
ImGui::EndChild(); ImGui::EndChild();
ImGui::End();
return true;
}
void AssetBrowser::DoDirTree(std::filesystem::path dir)
{
if ("" == dir)
return;
if (!std::filesystem::is_directory(dir))
return;
std::string next_node_name = dir.filename().string();
if (mSyncTree)
{
if (mSelectedDir.string().find(next_node_name) != std::string::npos)
{
ImGui::SetNextItemOpen(true);
}
else
{
ImGui::SetNextItemOpen(false);
}
}
if (ImGui::TreeNode(next_node_name.c_str()))
{
if (!mSyncTree)
{
mSelectedDir = dir;
HandleAssetDrop(mSelectedDir);
}
for(auto const& dir_entry: std::filesystem::directory_iterator{dir})
{
DoDirTree(dir_entry);
}
ImGui::TreePop();
}
} }
void AssetBrowser::DoContextMenu() void AssetBrowser::DoContextMenu()
@ -209,29 +235,6 @@ namespace editor
ImGui::OpenPopup("New Folder Name"); ImGui::OpenPopup("New Folder Name");
} }
AssetBrowser::Node* AssetBrowser::ReloadAssets(std::filesystem::path dir)
{
if ("" == dir)
return nullptr;
Node* pNode = new Node;
pNode->Myself = dir;
for(auto const& dir_entry: std::filesystem::directory_iterator{dir})
{
if (std::filesystem::is_directory(dir_entry))
{
pNode->Children.push_back(ReloadAssets(dir_entry));
}
else
{
pNode->Files.push_back(dir_entry);
}
}
return pNode;
}
void AssetBrowser::HandleAssetDrop(std::filesystem::path dir) void AssetBrowser::HandleAssetDrop(std::filesystem::path dir)
{ {
if (ImGui::BeginDragDropTarget()) if (ImGui::BeginDragDropTarget())
@ -252,25 +255,3 @@ namespace editor
} }
} }
} }
// void treeChildren(ImGuiTreeNodeFlags node_flags, bool isOpen, int index)
// {
// if (isOpen)
// {
// for (int p = 0; p < meshList[index].children.size(); p++)
// {
// if (meshList[index].children[p].children.empty())
// {
// node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen; // ImGuiTreeNodeFlags_Bullet
// ImGui::TreeNodeEx((void *)(intptr_t)p, node_flags, meshList[index].children[p].name.c_str());
// }
// else
// {
// bool o = ImGui::TreeNodeEx((void *)(intptr_t)p, node_flags, meshList[index].children[p].name.c_str());
// ImGui::TreePop();
// treeChildren(node_flags, o, p);
// }
// }
// ImGui::TreePop();
// }
// }

@ -41,19 +41,16 @@ namespace editor
private: private:
std::filesystem::path mAssetDirectory; std::filesystem::path mAssetDirectory;
std::filesystem::path mSelectedDir; std::filesystem::path mSelectedDir;
Node* mTreeRoot;
Node* mSelectedNode;
Editor* mpEditor; Editor* mpEditor;
bool mNewFolder; bool mNewFolder;
bool mSyncTree; bool mSyncTree;
private: private:
Node* ReloadAssets(std::filesystem::path dir);
Node* FindNodeAt(std::filesystem::path dir);
void HandleAssetDrop(std::filesystem::path dir); void HandleAssetDrop(std::filesystem::path dir);
void DoDirTree(std::filesystem::path dir); void DoDirTree(std::filesystem::path dir);
void DoContentArea(ImVec2 wind_size);
void DoContextMenu(); void DoContextMenu();
}; };
} }

Loading…
Cancel
Save