diff --git a/src/gui/panel.cpp b/src/gui/panel.cpp index 9057729..ea73cf1 100644 --- a/src/gui/panel.cpp +++ b/src/gui/panel.cpp @@ -116,6 +116,18 @@ namespace lunarium 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() { for (auto iter = mPopups.begin(); iter != mPopups.end(); iter++) @@ -123,7 +135,11 @@ namespace lunarium Popup* ppu = iter->second; 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()); } } diff --git a/src/gui/panel.h b/src/gui/panel.h index 0748e2b..894e607 100644 --- a/src/gui/panel.h +++ b/src/gui/panel.h @@ -46,7 +46,7 @@ namespace lunarium void GetSize(int& w, int& h) const; PanelDockZone GetDockZone() const; - OpRes AddPopup(int id, std::string name, std::function); + [[nodiscard]] OpRes AddPopup(int id, std::string name, std::function); private: std::string mPanelName; @@ -70,11 +70,14 @@ namespace lunarium void RunPopups(); protected: + + /// Called right before ImGui::Begin for this panel + /// Override to implement any NextWindow or style pushing code virtual void PreBegin(); virtual void DoFrame() = 0; void SetNumPushedStyles(int num); - void OpenPopup(int id); + [[nodiscard]] OpRes OpenPopup(int id); private: friend class PanelManager; diff --git a/src/run_modes/editor/panels/asset_browser.cpp b/src/run_modes/editor/panels/asset_browser.cpp index e28a27a..9ec3a1d 100644 --- a/src/run_modes/editor/panels/asset_browser.cpp +++ b/src/run_modes/editor/panels/asset_browser.cpp @@ -26,6 +26,7 @@ namespace editor : Panel("Asset Browser", PanelDockZone::DDZ_BOTTOM, true, (ImGuiWindowFlags)ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), mAssetDirectory(dir), mpEditor(pEditor), mNewFolder(false), mSyncTree(false) { + DefinePopups(); } void AssetBrowser::SetAssetDirectory(std::filesystem::path dir) @@ -39,6 +40,10 @@ namespace editor return mSelectedDir; } + /////////////////////////////////////////////////////////////////// + // DO FRAME DO FRAME DO FRAME + /////////////////////////////////////////////////////////////////// + void AssetBrowser::DoFrame() { @@ -63,9 +68,15 @@ namespace editor // Contents DoContentArea(wind_size); - HandlePopupActions(); + //HandlePopupActions(); } + /////////////////////////////////////////////////////////////////// + // FRAME HELPERS FRAME HELPERS + /////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////// + // DIRECTORY TREE void AssetBrowser::DoDirTree(std::filesystem::path dir) { if ("" == dir) @@ -115,6 +126,8 @@ namespace editor } } + ////////////////////////////////////////////////////////////// + // CONTENT AREA void AssetBrowser::DoContentArea(ImVec2 wind_size) { 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(), ImVec2(DataManager::mNewFolderIcon->GetWidth(), DataManager::mNewFolderIcon->GetHeight()), ImVec2(0, 0), ImVec2(1, 1), 4)) - { - mNewFolder = true; + { + OpenPopup(PopUp::NEW_FOLDER).LogIfFailed(Editor::LogCat); } ImGui::EndChild(); @@ -197,7 +210,7 @@ namespace editor } } - DoContextMenu(); + DoContentContextMenu(); ImGui::EndChild(); } @@ -205,13 +218,16 @@ namespace editor ImGui::EndChild(); } - void AssetBrowser::DoContextMenu() + ////////////////////////////////////////////////////////////// + // CONTEXT MENUS + void AssetBrowser::DoContentContextMenu() { if (ImGui::BeginPopupContextWindow(0, 1, false)) { if (ImGui::Button("New Folder")) { - mNewFolder = true; + //mNewFolder = true; + OpenPopup(PopUp::NEW_FOLDER).LogIfFailed(Editor::LogCat); } // TODO: Buttons for creating/importing new assets @@ -219,45 +235,8 @@ namespace editor } } - - void AssetBrowser::DefinePopups() - { - - } - - 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"); - } - } - + ////////////////////////////////////////////////////////////// + // ASSET DROP HELPER void AssetBrowser::HandleAssetDrop(std::filesystem::path dir) { if (ImGui::BeginDragDropTarget()) @@ -274,5 +253,36 @@ namespace editor 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); + } + + } } diff --git a/src/run_modes/editor/panels/asset_browser.h b/src/run_modes/editor/panels/asset_browser.h index 4dfd2b5..01816cc 100644 --- a/src/run_modes/editor/panels/asset_browser.h +++ b/src/run_modes/editor/panels/asset_browser.h @@ -23,21 +23,12 @@ namespace editor { public: AssetBrowser(std::filesystem::path dir, Editor* pEditor); - void SetAssetDirectory(std::filesystem::path dir); void DoFrame(); std::filesystem::path GetSelectedDirectory(); - private: - struct Node - { - std::filesystem::path Myself; - std::vector Children; - std::vector Files; - }; - private: std::filesystem::path mAssetDirectory; std::filesystem::path mSelectedDir; @@ -48,14 +39,24 @@ namespace editor private: void DefinePopups(); - void HandlePopupActions(); - void HandleAssetDrop(std::filesystem::path dir); - + // Frame helpers void DoDirTree(std::filesystem::path dir); 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, + }; }; + } }