diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index 3f934fd..2e20130 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -117,11 +117,11 @@ namespace Navigator { { if(ImGui::MenuItem(ICON_FA_FOLDER_OPEN "\tOpen")) { - m_fileDialog.SetOpenFileDialog(true); + m_fileDialog.OpenDialog(FileDialog::Type::OpenFile); } if(ImGui::MenuItem(ICON_FA_SAVE "\tSave")) { - m_fileDialog.SetSaveFileDialog(true); + m_fileDialog.OpenDialog(FileDialog::Type::SaveFile); } if (ImGui::MenuItem(ICON_FA_TIMES_CIRCLE "\tExit")) { @@ -174,22 +174,30 @@ namespace Navigator { } //Render all of our sub-windows, dialogs, panels, etc - std::string open_file_result = m_fileDialog.ImGuiRenderOpenFile(".nav"); - std::string save_file_result = m_fileDialog.ImGuiRenderSaveFile(".nav"); - if (!open_file_result.empty()) + auto fd_result = m_fileDialog.RenderFileDialog(".nav"); + if (!fd_result.first.empty()) { - SpectrumSerializer serializer(open_file_result); - serializer.DeserializeData(); - UpdateHistogramList(); - UpdateCutList(); - } - else if (!save_file_result.empty()) - { - NAV_INFO("Found a Save File! {0}", save_file_result); - SpectrumSerializer serializer(save_file_result); - serializer.SerializeData(m_histoList, m_cutList); + switch (fd_result.second) + { + case FileDialog::Type::OpenFile: + { + SpectrumSerializer serializer(fd_result.first); + serializer.DeserializeData(); + UpdateHistogramList(); + UpdateCutList(); + break; + } + case FileDialog::Type::SaveFile: + { + NAV_INFO("Found a Save File! {0}", fd_result.first); + SpectrumSerializer serializer(fd_result.first); + serializer.SerializeData(m_histoList, m_cutList); + break; + } + } } + if(m_spectrumDialog.ImGuiRenderSpectrumDialog(m_histoList, m_cutList, m_paramList)) UpdateHistogramList(); @@ -355,11 +363,11 @@ namespace Navigator { ImGui::SameLine(); if(ImGui::Button("Open")) { - m_fileDialog.SetSaveFileDialog(true); + m_fileDialog.OpenDialog(FileDialog::Type::SaveFile); } - std::string result = m_fileDialog.ImGuiRenderSaveFile(".csv"); - if(!result.empty()) - filename = result; + auto result = m_fileDialog.RenderFileDialog(".csv"); + if(!result.first.empty() && result.second == FileDialog::Type::SaveFile) + filename = result.first; if(ImGui::Button("Ok")) { ExportHistogram(selectedGram, filename); diff --git a/Navigator/src/Navigator/Editor/FileDialog.cpp b/Navigator/src/Navigator/Editor/FileDialog.cpp index 27152e1..47f99e5 100644 --- a/Navigator/src/Navigator/Editor/FileDialog.cpp +++ b/Navigator/src/Navigator/Editor/FileDialog.cpp @@ -29,8 +29,7 @@ namespace Navigator { } FileDialog::FileDialog() : - m_currentPath(std::filesystem::current_path()), m_openFileName(ICON_FA_FILE " Open File"), m_saveFileName(ICON_FA_SAVE " Save File"), m_openDirName(ICON_FA_FOLDER " Open Directory"), - m_selectedItem(""), m_openFileFlag(false), m_saveFileFlag(false), m_openDirFlag(false) + m_currentPath(std::filesystem::current_path()), m_type(Type::None), m_selectedItem(""), m_openDialogFlag(false) { table_flags = ImGuiTableFlags_BordersH | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_RowBg; select_flags = ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_DontClosePopups; @@ -40,218 +39,227 @@ namespace Navigator { //Each type of action has its own render function - std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext) + std::pair FileDialog::RenderFileDialog(const std::string& ext) { - if (m_openFileFlag) + if (m_openDialogFlag) { - m_openFileFlag = false; m_selectedItem = ""; + m_openDialogFlag = false; m_currentPath = std::filesystem::current_path(); - ImGui::OpenPopup(m_openFileName.c_str()); + ImGui::OpenPopup("File Dialog"); } + std::string result = ""; - std::string text = ""; - if (ImGui::BeginPopupModal(m_openFileName.c_str())) + if (ImGui::BeginPopupModal("File Dialog")) { - ImGui::Text("%s", ("Current Directory: " + m_currentPath.lexically_normal().string()).c_str()); - ImGui::SameLine(); - ImGui::Text("%s", ("Extension Filter: "+ext).c_str()); - ImGui::InputText("Selected", &m_selectedItem); - if (ImGui::Button("Ok")) + switch (m_type) { - std::filesystem::path filepath = m_currentPath / m_selectedItem; - result = filepath.string(); - ImGui::CloseCurrentPopup(); - } - ImGui::SameLine(); - if (ImGui::Button("Cancel")) - ImGui::CloseCurrentPopup(); - if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1,-1))) - { - ImGui::TableSetupColumn("Name"); - ImGui::TableSetupColumn("Size"); - ImGui::TableHeadersRow(); - ImGui::TableNextRow(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags)) + case Type::OpenFile: { - m_selectedItem.clear(); - m_currentPath.append(".."); + result = ImGuiRenderOpenFile(ext); + break; } - ImGui::TableNextColumn(); - ImGui::Text("N/A"); - for (auto& entry : std::filesystem::directory_iterator(m_currentPath)) + case Type::SaveFile: { - if (entry.is_directory()) - { - ImGui::TableNextRow(); - text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(text.c_str(), false, select_flags)) - { - m_selectedItem.clear(); - m_currentPath /= entry.path(); - } - ImGui::TableNextColumn(); - ImGui::Text("N/A"); - } - else if(entry.path().filename().extension() == ext) - { - ImGui::TableNextRow(); - text = ICON_FA_FILE " " + entry.path().filename().string(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(text.c_str(), false, select_flags)) - m_selectedItem = entry.path().filename().string(); - ImGui::TableNextColumn(); - ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str()); - } + result = ImGuiRenderSaveFile(ext); + break; } - ImGui::EndTable(); + case Type::OpenDir: + { + result = ImGuiRenderOpenDir(); + break; + } + case Type::None: break; } - ImGui::EndPopup(); } + return std::make_pair(result, m_type); + } + + std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext) + { + std::string result = ""; + std::string text = ""; + + ImGui::Text("%s", ("Current Directory: " + m_currentPath.lexically_normal().string()).c_str()); + ImGui::SameLine(); + ImGui::Text("%s", ("Extension Filter: "+ext).c_str()); + ImGui::InputText("Selected", &m_selectedItem); + if (ImGui::Button("Ok")) + { + std::filesystem::path filepath = m_currentPath / m_selectedItem; + result = filepath.string(); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel")) + ImGui::CloseCurrentPopup(); + + if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1))) + { + ImGui::TableSetupColumn("Name"); + ImGui::TableSetupColumn("Size"); + ImGui::TableHeadersRow(); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags)) + { + m_selectedItem.clear(); + m_currentPath.append(".."); + } + ImGui::TableNextColumn(); + ImGui::Text("N/A"); + for (auto& entry : std::filesystem::directory_iterator(m_currentPath)) + { + if (entry.is_directory()) + { + ImGui::TableNextRow(); + text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(text.c_str(), false, select_flags)) + { + m_selectedItem.clear(); + m_currentPath /= entry.path(); + } + ImGui::TableNextColumn(); + ImGui::Text("N/A"); + } + else if (entry.path().filename().extension() == ext) + { + ImGui::TableNextRow(); + text = ICON_FA_FILE " " + entry.path().filename().string(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(text.c_str(), false, select_flags)) + m_selectedItem = entry.path().filename().string(); + ImGui::TableNextColumn(); + ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str()); + } + } + ImGui::EndTable(); + } return result; } std::string FileDialog::ImGuiRenderSaveFile(const std::string& ext) { - if (m_saveFileFlag) - { - m_saveFileFlag = false; - m_selectedItem = ""; - m_currentPath = std::filesystem::current_path(); - ImGui::OpenPopup(m_saveFileName.c_str()); - } std::string result = ""; std::string text = ""; - if (ImGui::BeginPopupModal(m_saveFileName.c_str())) + + ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str()); + ImGui::SameLine(); + ImGui::Text("%s", ("Extension Filter: "+ext).c_str()); + ImGui::InputText("Selected", &m_selectedItem); + if (ImGui::Button("Ok")) { - ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str()); - ImGui::SameLine(); - ImGui::Text("%s", ("Extension Filter: "+ext).c_str()); - ImGui::InputText("Selected", &m_selectedItem); - if (ImGui::Button("Ok")) + std::filesystem::path filepath = m_currentPath / m_selectedItem; + result = filepath.string(); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel")) + ImGui::CloseCurrentPopup(); + + if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1))) + { + ImGui::TableSetupColumn("Name"); + ImGui::TableSetupColumn("Size"); + ImGui::TableHeadersRow(); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags)) { - std::filesystem::path filepath = m_currentPath / m_selectedItem; - result = filepath.string(); - ImGui::CloseCurrentPopup(); + m_selectedItem.clear(); + m_currentPath.append(".."); } - ImGui::SameLine(); - if (ImGui::Button("Cancel")) - ImGui::CloseCurrentPopup(); - if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1))) + ImGui::TableNextColumn(); + ImGui::Text("N/A"); + for (auto& entry : std::filesystem::directory_iterator(m_currentPath)) { - ImGui::TableSetupColumn("Name"); - ImGui::TableSetupColumn("Size"); - ImGui::TableHeadersRow(); - ImGui::TableNextRow(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags)) + if (entry.is_directory()) { - m_selectedItem.clear(); - m_currentPath.append(".."); + ImGui::TableNextRow(); + text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(text.c_str(), false, select_flags)) + { + m_selectedItem.clear(); + m_currentPath /= entry.path(); + } + ImGui::TableNextColumn(); + ImGui::Text("N/A"); } - ImGui::TableNextColumn(); - ImGui::Text("N/A"); - for (auto& entry : std::filesystem::directory_iterator(m_currentPath)) + else if (entry.path().filename().extension() == ext) { - if (entry.is_directory()) - { - ImGui::TableNextRow(); - text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(text.c_str(), false, select_flags)) - { - m_selectedItem.clear(); - m_currentPath /= entry.path(); - } - ImGui::TableNextColumn(); - ImGui::Text("N/A"); - } - else if (entry.path().filename().extension() == ext) - { - ImGui::TableNextRow(); - text = ICON_FA_FILE " " + entry.path().filename().string(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(text.c_str(), false, select_flags)) - m_selectedItem = entry.path().filename().string(); - ImGui::TableNextColumn(); - ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str()); - } + ImGui::TableNextRow(); + text = ICON_FA_FILE " " + entry.path().filename().string(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(text.c_str(), false, select_flags)) + m_selectedItem = entry.path().filename().string(); + ImGui::TableNextColumn(); + ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str()); } - ImGui::EndTable(); } - ImGui::EndPopup(); + ImGui::EndTable(); } return result; } std::string FileDialog::ImGuiRenderOpenDir() { - if (m_openDirFlag) - { - m_openDirFlag = false; - m_currentPath = std::filesystem::current_path(); - m_selectedItem = m_currentPath.string(); - ImGui::OpenPopup(m_openDirName.c_str()); - } std::string result = ""; std::string text = ""; - if (ImGui::BeginPopupModal(m_openDirName.c_str())) + + ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str()); + ImGui::InputText("Selected", &m_selectedItem); + if (ImGui::Button("Ok")) { - ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str()); - ImGui::InputText("Selected", &m_selectedItem); - if (ImGui::Button("Ok")) + result = m_selectedItem; + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel")) + ImGui::CloseCurrentPopup(); + + if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1))) + { + ImGui::TableSetupColumn("Name"); + ImGui::TableSetupColumn("Size"); + ImGui::TableHeadersRow(); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags)) { - result = m_selectedItem; - ImGui::CloseCurrentPopup(); + m_currentPath.append(".."); + m_selectedItem = m_currentPath.string(); } - ImGui::SameLine(); - if (ImGui::Button("Cancel")) - ImGui::CloseCurrentPopup(); - if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1))) + ImGui::TableNextColumn(); + ImGui::Text("N/A"); + for (auto& entry : std::filesystem::directory_iterator(m_currentPath)) { - ImGui::TableSetupColumn("Name"); - ImGui::TableSetupColumn("Size"); - ImGui::TableHeadersRow(); ImGui::TableNextRow(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags)) + if (entry.is_directory()) { - m_currentPath.append(".."); - m_selectedItem = m_currentPath.string(); + text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); + ImGui::TableNextColumn(); + if (ImGui::Selectable(text.c_str(), false, select_flags)) + { + m_currentPath /= entry.path(); + m_selectedItem = m_currentPath.string(); + } + ImGui::TableNextColumn(); + ImGui::Text("N/A"); } - ImGui::TableNextColumn(); - ImGui::Text("N/A"); - for (auto& entry : std::filesystem::directory_iterator(m_currentPath)) + else { - ImGui::TableNextRow(); - if (entry.is_directory()) - { - text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); - ImGui::TableNextColumn(); - if (ImGui::Selectable(text.c_str(), false, select_flags)) - { - m_currentPath /= entry.path(); - m_selectedItem = m_currentPath.string(); - } - ImGui::TableNextColumn(); - ImGui::Text("N/A"); - } - else - { - text = ICON_FA_FILE " " + entry.path().filename().string(); - ImGui::TableNextColumn(); - ImGui::Text("%s", text.c_str()); - ImGui::TableNextColumn(); - ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str()); - } + text = ICON_FA_FILE " " + entry.path().filename().string(); + ImGui::TableNextColumn(); + ImGui::Text("%s", text.c_str()); + ImGui::TableNextColumn(); + ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str()); } - ImGui::EndTable(); } - ImGui::EndPopup(); + ImGui::EndTable(); } return result; } diff --git a/Navigator/src/Navigator/Editor/FileDialog.h b/Navigator/src/Navigator/Editor/FileDialog.h index 474f9c7..3aaa49e 100644 --- a/Navigator/src/Navigator/Editor/FileDialog.h +++ b/Navigator/src/Navigator/Editor/FileDialog.h @@ -19,41 +19,37 @@ namespace Navigator { + + class NAV_API FileDialog { public: + + enum class Type + { + OpenFile, + SaveFile, + OpenDir, + None + }; + FileDialog(); ~FileDialog(); + inline void OpenDialog(Type type) { m_type = type; m_openDialogFlag = true; } + std::pair RenderFileDialog(const std::string& ext = ""); + + private: std::string ImGuiRenderOpenFile(const std::string& ext); std::string ImGuiRenderSaveFile(const std::string& ext); std::string ImGuiRenderOpenDir(); - - inline void ResetCurrentDirectory() { m_currentPath = std::filesystem::current_path(); } - inline void SetOpenFileDialog(bool value) { m_openFileFlag = value; } - inline void SetSaveFileDialog(bool value) { m_saveFileFlag = value; } - inline void SetOpenDirDialog(bool value) { m_openDirFlag = value; } - - inline bool IsOpenFileOpen() { return m_openFileFlag; } - inline bool IsOpenDirOpen() { return m_openDirFlag; } - inline bool IsSaveFileOpen() { return m_saveFileFlag; } - - inline const std::string& GetOpenFileWindowName() { return m_openFileName; } - inline const std::string& GetSaveFileWindowName() { return m_saveFileName; } - inline const std::string& GetOpenDirWindowName() { return m_openDirName; } - - private: std::filesystem::path m_currentPath; - std::string m_openFileName; - std::string m_saveFileName; - std::string m_openDirName; + Type m_type; std::string m_selectedItem; - bool m_openFileFlag; - bool m_saveFileFlag; - bool m_openDirFlag; + bool m_openDialogFlag; ImGuiTableFlags table_flags; ImGuiSelectableFlags select_flags; diff --git a/Navigator/src/Navigator/Editor/SourceDialog.cpp b/Navigator/src/Navigator/Editor/SourceDialog.cpp index f60326c..2b1d5bd 100644 --- a/Navigator/src/Navigator/Editor/SourceDialog.cpp +++ b/Navigator/src/Navigator/Editor/SourceDialog.cpp @@ -66,11 +66,11 @@ namespace Navigator { ImGui::InputText("Run Directory", &m_chosenLocation); if (ImGui::Button("Choose Location")) { - m_fileDialog.SetOpenDirDialog(true); + m_fileDialog.OpenDialog(FileDialog::Type::OpenDir); } - auto temp = m_fileDialog.ImGuiRenderOpenDir(); - if (temp != "") - m_chosenLocation = temp; + auto temp = m_fileDialog.RenderFileDialog(); + if (!temp.first.empty() && temp.second == FileDialog::Type::OpenDir) + m_chosenLocation = temp.first; } ImGui::InputInt("Coinc. Window (ps)", &m_chosenWindow);