New Popup panel system working

gui_api_redesign
Joey Pollack 4 years ago
parent 578bd98b40
commit 2f27fcf8bc

@ -116,6 +116,18 @@ namespace lunarium
return OpRes::OK(); return OpRes::OK();
} }
OpRes Panel::OpenPopup(int id)
{
auto iter = mPopups.find(id);
if (iter == mPopups.end())
{
return OpRes::Fail("Attempt to open A popup with id: %d but it does not exist.", id);
}
iter->second->IsOpen = true;
return OpRes::OK();
}
void Panel::RunPopups() void Panel::RunPopups()
{ {
for (auto iter = mPopups.begin(); iter != mPopups.end(); iter++) for (auto iter = mPopups.begin(); iter != mPopups.end(); iter++)
@ -123,7 +135,11 @@ namespace lunarium
Popup* ppu = iter->second; Popup* ppu = iter->second;
if (ppu->IsOpen) if (ppu->IsOpen)
{ {
ppu->IsOpen = ppu->PopupFunc(this); if (ImGui::BeginPopupContextItem(ppu->Name.c_str()))
{
ppu->IsOpen = ppu->PopupFunc(this);
ImGui::EndPopup();
}
ImGui::OpenPopup(ppu->Name.c_str()); ImGui::OpenPopup(ppu->Name.c_str());
} }
} }

@ -46,7 +46,7 @@ namespace lunarium
void GetSize(int& w, int& h) const; void GetSize(int& w, int& h) const;
PanelDockZone GetDockZone() const; PanelDockZone GetDockZone() const;
OpRes AddPopup(int id, std::string name, std::function<bool(Panel*)>); [[nodiscard]] OpRes AddPopup(int id, std::string name, std::function<bool(Panel*)>);
private: private:
std::string mPanelName; std::string mPanelName;
@ -70,11 +70,14 @@ namespace lunarium
void RunPopups(); void RunPopups();
protected: protected:
/// Called right before ImGui::Begin for this panel
/// Override to implement any NextWindow or style pushing code
virtual void PreBegin(); virtual void PreBegin();
virtual void DoFrame() = 0; virtual void DoFrame() = 0;
void SetNumPushedStyles(int num); void SetNumPushedStyles(int num);
void OpenPopup(int id); [[nodiscard]] OpRes OpenPopup(int id);
private: private:
friend class PanelManager; friend class PanelManager;

@ -26,6 +26,7 @@ namespace editor
: Panel("Asset Browser", PanelDockZone::DDZ_BOTTOM, true, (ImGuiWindowFlags)ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), : Panel("Asset Browser", PanelDockZone::DDZ_BOTTOM, true, (ImGuiWindowFlags)ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar),
mAssetDirectory(dir), mpEditor(pEditor), mNewFolder(false), mSyncTree(false) mAssetDirectory(dir), mpEditor(pEditor), mNewFolder(false), mSyncTree(false)
{ {
DefinePopups();
} }
void AssetBrowser::SetAssetDirectory(std::filesystem::path dir) void AssetBrowser::SetAssetDirectory(std::filesystem::path dir)
@ -39,6 +40,10 @@ namespace editor
return mSelectedDir; return mSelectedDir;
} }
///////////////////////////////////////////////////////////////////
// DO FRAME DO FRAME DO FRAME
///////////////////////////////////////////////////////////////////
void AssetBrowser::DoFrame() void AssetBrowser::DoFrame()
{ {
@ -63,9 +68,15 @@ namespace editor
// Contents // Contents
DoContentArea(wind_size); DoContentArea(wind_size);
HandlePopupActions(); //HandlePopupActions();
} }
///////////////////////////////////////////////////////////////////
// FRAME HELPERS FRAME HELPERS
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// DIRECTORY TREE
void AssetBrowser::DoDirTree(std::filesystem::path dir) void AssetBrowser::DoDirTree(std::filesystem::path dir)
{ {
if ("" == dir) if ("" == dir)
@ -115,6 +126,8 @@ namespace editor
} }
} }
//////////////////////////////////////////////////////////////
// CONTENT AREA
void AssetBrowser::DoContentArea(ImVec2 wind_size) 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))
@ -140,8 +153,8 @@ namespace editor
if (ImGui::ImageButton((ImTextureID)DataManager::mNewFolderIcon->GetGLTextureID64(), if (ImGui::ImageButton((ImTextureID)DataManager::mNewFolderIcon->GetGLTextureID64(),
ImVec2(DataManager::mNewFolderIcon->GetWidth(), DataManager::mNewFolderIcon->GetHeight()), ImVec2(DataManager::mNewFolderIcon->GetWidth(), DataManager::mNewFolderIcon->GetHeight()),
ImVec2(0, 0), ImVec2(1, 1), 4)) ImVec2(0, 0), ImVec2(1, 1), 4))
{ {
mNewFolder = true; OpenPopup(PopUp::NEW_FOLDER).LogIfFailed(Editor::LogCat);
} }
ImGui::EndChild(); ImGui::EndChild();
@ -197,7 +210,7 @@ namespace editor
} }
} }
DoContextMenu(); DoContentContextMenu();
ImGui::EndChild(); ImGui::EndChild();
} }
@ -205,13 +218,16 @@ namespace editor
ImGui::EndChild(); ImGui::EndChild();
} }
void AssetBrowser::DoContextMenu() //////////////////////////////////////////////////////////////
// CONTEXT MENUS
void AssetBrowser::DoContentContextMenu()
{ {
if (ImGui::BeginPopupContextWindow(0, 1, false)) if (ImGui::BeginPopupContextWindow(0, 1, false))
{ {
if (ImGui::Button("New Folder")) if (ImGui::Button("New Folder"))
{ {
mNewFolder = true; //mNewFolder = true;
OpenPopup(PopUp::NEW_FOLDER).LogIfFailed(Editor::LogCat);
} }
// TODO: Buttons for creating/importing new assets // TODO: Buttons for creating/importing new assets
@ -219,45 +235,8 @@ namespace editor
} }
} }
//////////////////////////////////////////////////////////////
void AssetBrowser::DefinePopups() // ASSET DROP HELPER
{
}
void AssetBrowser::DoNewFolder()
{
if (ImGui::BeginPopupContextItem("New Folder Name"))
{
char name_buf[64] = "New Folder";
ImGui::TextUnformatted("Folder Name: ");
ImGui::SameLine();
if (ImGui::InputText("##Folder Name", name_buf, 64, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue))
{
std::filesystem::create_directory(mSelectedDir / name_buf);
mNewFolder = false;
ImGui::CloseCurrentPopup();
}
if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true))
{
mNewFolder = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
void AssetBrowser::HandlePopupActions()
{
if (mNewFolder)
{
DoNewFolder();
ImGui::OpenPopup("New Folder Name");
}
}
void AssetBrowser::HandleAssetDrop(std::filesystem::path dir) void AssetBrowser::HandleAssetDrop(std::filesystem::path dir)
{ {
if (ImGui::BeginDragDropTarget()) if (ImGui::BeginDragDropTarget())
@ -274,5 +253,36 @@ namespace editor
ImGui::EndDragDropTarget(); ImGui::EndDragDropTarget();
} }
} }
///////////////////////////////////////////////////////////////////
// POPUP PANELS POPUP PANELS
///////////////////////////////////////////////////////////////////
void AssetBrowser::DefinePopups()
{
AddPopup(PopUp::NEW_FOLDER, "New Folder Name", [](Panel *p)
{
bool stay_open = true;
AssetBrowser* ab = (AssetBrowser*)p;
char name_buf[64] = "New Folder";
ImGui::TextUnformatted("Folder Name: ");
ImGui::SameLine();
if (ImGui::InputText("##Folder Name", name_buf, 64, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue))
{
std::filesystem::create_directory(ab->mSelectedDir / name_buf);
stay_open = false;
ImGui::CloseCurrentPopup();
}
if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true))
{
stay_open = false;
ImGui::CloseCurrentPopup();
}
return stay_open;
}).LogIfFailed(Editor::LogCat);
}
} }
} }

@ -23,21 +23,12 @@ namespace editor
{ {
public: public:
AssetBrowser(std::filesystem::path dir, Editor* pEditor); AssetBrowser(std::filesystem::path dir, Editor* pEditor);
void SetAssetDirectory(std::filesystem::path dir); void SetAssetDirectory(std::filesystem::path dir);
void DoFrame(); void DoFrame();
std::filesystem::path GetSelectedDirectory(); std::filesystem::path GetSelectedDirectory();
private:
struct Node
{
std::filesystem::path Myself;
std::vector<Node*> Children;
std::vector<std::filesystem::path> Files;
};
private: private:
std::filesystem::path mAssetDirectory; std::filesystem::path mAssetDirectory;
std::filesystem::path mSelectedDir; std::filesystem::path mSelectedDir;
@ -48,14 +39,24 @@ namespace editor
private: private:
void DefinePopups(); void DefinePopups();
void HandlePopupActions(); // Frame helpers
void HandleAssetDrop(std::filesystem::path dir);
void DoDirTree(std::filesystem::path dir); void DoDirTree(std::filesystem::path dir);
void DoContentArea(ImVec2 wind_size); void DoContentArea(ImVec2 wind_size);
void DoContextMenu();
void DoNewFolder(); // Context menus
void DoContentContextMenu();
// General Helpers
void HandleAssetDrop(std::filesystem::path dir);
private: // POPUP ENUMS
enum PopUp
{
NEW_FOLDER,
};
}; };
} }
} }

Loading…
Cancel
Save