diff --git a/SofaImGui/CMakeLists.txt b/SofaImGui/CMakeLists.txt index cf93dbc154..b49d44c7c3 100644 --- a/SofaImGui/CMakeLists.txt +++ b/SofaImGui/CMakeLists.txt @@ -121,6 +121,7 @@ set(HEADER_FILES ${SOFAIMGUI_SOURCE_DIR}/windows/DisplayFlags.h ${SOFAIMGUI_SOURCE_DIR}/windows/Plugins.h ${SOFAIMGUI_SOURCE_DIR}/windows/Components.h + ${SOFAIMGUI_SOURCE_DIR}/windows/Snapshot.h ${SOFAIMGUI_SOURCE_DIR}/AppIniFile.h ${SOFAIMGUI_SOURCE_DIR}/windows/WindowState.h ${SOFAIMGUI_SOURCE_DIR}/guis/BaseAdditionalGUI.h @@ -146,6 +147,7 @@ set(SOURCE_FILES ${SOFAIMGUI_SOURCE_DIR}/windows/DisplayFlags.cpp ${SOFAIMGUI_SOURCE_DIR}/windows/Plugins.cpp ${SOFAIMGUI_SOURCE_DIR}/windows/Components.cpp + ${SOFAIMGUI_SOURCE_DIR}/windows/Snapshot.cpp ${SOFAIMGUI_SOURCE_DIR}/windows/Settings.cpp ${SOFAIMGUI_SOURCE_DIR}/windows/ViewPort.cpp ${SOFAIMGUI_SOURCE_DIR}/AppIniFile.cpp diff --git a/SofaImGui/src/SofaImGui/ImGuiGUIEngine.cpp b/SofaImGui/src/SofaImGui/ImGuiGUIEngine.cpp index 8137710b79..9a19728e3a 100644 --- a/SofaImGui/src/SofaImGui/ImGuiGUIEngine.cpp +++ b/SofaImGui/src/SofaImGui/ImGuiGUIEngine.cpp @@ -46,6 +46,7 @@ #include "windows/DisplayFlags.h" #include "windows/Log.h" #include "windows/MouseManager.h" +#include "windows/Snapshot.h" #include "windows/Performances.h" #include "windows/Plugins.h" #include "windows/Profiler.h" @@ -68,6 +69,7 @@ #include #include //imgui_internal.h is included in order to use the DockspaceBuilder API (which is still in development) #include +#include #include #include #include @@ -83,7 +85,6 @@ #include - using namespace sofa; namespace sofaimgui @@ -104,6 +105,7 @@ ImGuiGUIEngine::ImGuiGUIEngine() , winManagerComponents(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("components.txt"))) , winManagerLog(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("log.txt"))) , winManagerMouse(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("mouse.txt"))) + , winManagerSnapshot(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("snapshot.txt"))) , winManagerSettings(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("settings.txt"))) , winManagerViewPort(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("viewport.txt"))) , firstRunState(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string("firstrun.txt"))) @@ -215,7 +217,7 @@ void ImGuiGUIEngine::loadFile(sofaglfw::SofaGLFWBaseGUI* baseGUI, sofa::core::sp sofa::simulation::node::unload(groot); groot = sofa::simulation::node::load(filePathName.c_str()); - + if( !groot ) groot = sofa::simulation::getSimulation()->createNewGraph(""); @@ -406,6 +408,7 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI) static constexpr auto windowNameComponents = ICON_FA_LIST " Components"; static constexpr auto windowNameLog = ICON_FA_TERMINAL " Log"; static constexpr auto windowNameMouse = ICON_FA_COMPUTER_MOUSE " Mouse Manager"; + static constexpr auto windowNameSnapshot = ICON_FA_FLOPPY_DISK " Snapshot"; static constexpr auto windowNameSettings = ICON_FA_SLIDERS " Settings"; if (!*firstRunState.getStatePtr()) @@ -550,6 +553,8 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI) ImGui::Checkbox(windowNameMouse, winManagerMouse.getStatePtr()); + ImGui::Checkbox(windowNameSnapshot, winManagerSnapshot.getStatePtr()); + if (guis::MainAdditionGUIRegistry::getAllGUIs().empty() == false) { ImGui::Separator(); @@ -742,6 +747,11 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI) **************************************/ windows::showManagerMouseWindow(windowNameMouse, winManagerMouse, baseGUI); + /*************************************** + * Snapshot window + **************************************/ + windows::showSnapshot(windowNameSnapshot, winManagerSnapshot, groot); + /*************************************** * Additional GUIs **************************************/ @@ -1036,4 +1046,6 @@ type::Vec2i ImGuiGUIEngine::getFrameBufferPixels(std::vector& pixels) return {viewport[2], viewport[3]}; } + + } //namespace sofaimgui diff --git a/SofaImGui/src/SofaImGui/ImGuiGUIEngine.h b/SofaImGui/src/SofaImGui/ImGuiGUIEngine.h index dec54be2c0..5a6aec29cb 100644 --- a/SofaImGui/src/SofaImGui/ImGuiGUIEngine.h +++ b/SofaImGui/src/SofaImGui/ImGuiGUIEngine.h @@ -102,6 +102,7 @@ class ImGuiGUIEngine : public sofaglfw::BaseGUIEngine windows::WindowState winManagerComponents; windows::WindowState winManagerLog; windows::WindowState winManagerMouse; + windows::WindowState winManagerSnapshot; windows::WindowState winManagerSettings; windows::WindowState winManagerViewPort; std::map winManagerAdditionalGUIs; diff --git a/SofaImGui/src/SofaImGui/windows/Snapshot.cpp b/SofaImGui/src/SofaImGui/windows/Snapshot.cpp new file mode 100644 index 0000000000..1ff8701f1c --- /dev/null +++ b/SofaImGui/src/SofaImGui/windows/Snapshot.cpp @@ -0,0 +1,193 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU General Public License as published by the Free * +* Software Foundation; either version 2 of the License, or (at your option) * +* any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * +* more details. * +* * +* You should have received a copy of the GNU General Public License along * +* with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Snapshot.h" + +namespace windows +{ + SnapshotManager snapshot_manager; + + void showSnapshot(const char* const& windowNameSnapshot, + WindowState& winManagerSnapshot, sofa::core::sptr& groot) + { + + if (*winManagerSnapshot.getStatePtr()) + { + if (ImGui::Begin(windowNameSnapshot, winManagerSnapshot.getStatePtr())) + { + static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders| ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg; + if(ImGui::BeginTable("MainTable", 2, flags, ImVec2(0.f, 400.f))) { + ImGui::TableSetupColumn("Save/Load"); + ImGui::TableSetupColumn("Snapshot in memory"); + ImGui::TableHeadersRow(); + ImGui::TableNextRow(); + + ImGui::TableSetColumnIndex(0); + float totalHeight = ImGui::GetContentRegionAvail().y; + float halfHeight = totalHeight * 0.5f - 4.0f; + + ImGui::BeginChild("Save Snapshot", ImVec2(0, halfHeight), ImGuiChildFlags_Borders); + ImGui::Text("Save Snapshot"); + ImGui::Separator(); + if (ImGui::Button("Memory")) + { + snapshot_manager.doMemorySave(groot); + } + if (ImGui::Button("File")) + { + doFileSave(groot, false); + } + if (ImGui::Button("Set of Snapshots")) + { + if (!snapshot_manager.m_snapshotsFromMemory.empty()) + { + doFileSave(groot, true); + } + else + std::cout << "No snapshot" << std::endl; + + } + ImGui::EndChild(); + + ImGui::BeginChild("Load Snapshot", ImVec2(0, halfHeight), ImGuiChildFlags_Borders); + ImGui::Text("Load Snapshot"); + ImGui::Separator(); + if (ImGui::Button("Memory (recent)")) + { + snapshot_manager.doMemoryLoad(groot); + } + if (ImGui::Button("File")) + { + doFileLoad(groot, false); + } + + if (ImGui::Button("Set of Snapshots")) + { + doFileLoad(groot, true); + } + ImGui::EndChild(); + + ImGui::TableSetColumnIndex(1); + + float listHeight = ImGui::GetContentRegionAvail().y - 8.0f; + + ImGui::BeginChild("Recents", ImVec2(0, listHeight), true); + + if (snapshot_manager.m_snapshotsFromFiles.empty() && snapshot_manager.m_snapshotsFromMemory.empty()) + { + ImGui::TextDisabled("(No recent files)"); + } + else + { + for (auto file : snapshot_manager.m_snapshotsFromFiles) + { + if (ImGui::Selectable(file.c_str())) + { + if(file.ends_with(".json")) + { + auto m_snapshot = std::make_shared(); + importFrom(*m_snapshot,file); + auto visitor = LoadSnapshotVisitor(nullptr,*m_snapshot); + groot->execute(visitor); + } + } + } + for (auto [name, file] : snapshot_manager.m_snapshotsFromMemory) + { + if(ImGui::Selectable(name.c_str())) + { + auto m_snapshot = std::make_shared(); + m_snapshot = file; + auto visitor = LoadSnapshotVisitor(nullptr,*m_snapshot); + groot->execute(visitor); + } + } + } + ImGui::EndChild(); + ImGui::EndTable(); + } + } + ImGui::End(); + } + } + + void doFileSave(sofa::core::sptr& groot, bool isSet) + { + NFD_Init(); + nfdchar_t* savePath; + nfdfilteritem_t filterItem[1] = {{"Snapshot code", "json,txt"}}; + std::string filepath = "null"; + nfdresult_t result = NFD_SaveDialog(&savePath, filterItem, 1, NULL, "Untitled.json"); + if (result == NFD_OKAY) + { + std::string path(savePath); + snapshot_manager.doSaveTo(groot,path,isSet); + } + else + { + msg_error("SaveSnapshot") << "Error of saving snapshot" ; + } + NFD_Quit(); + } + + void doFileLoad(sofa::core::sptr& groot, bool isSet) + { + nfdchar_t *outPath = NULL; + nfdfilteritem_t filterItem[1] = {{"Snapshot code", "json"}}; + nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, NULL); + std::string filepath = "null"; + if ( result == NFD_OKAY && !isSet) + { + std::string path(outPath); + snapshot_manager.doLoadTo(groot,path); + } + else if (result == NFD_OKAY && isSet) + { + std::string path(outPath); + snapshot_manager.doLoadToSet(path); + } + else + { + msg_error("LoadSnapshot") << "Error of loading snapshot" ; + } + NFD_Quit(); + } +} diff --git a/SofaImGui/src/SofaImGui/windows/Snapshot.h b/SofaImGui/src/SofaImGui/windows/Snapshot.h new file mode 100644 index 0000000000..7e0f1baeb1 --- /dev/null +++ b/SofaImGui/src/SofaImGui/windows/Snapshot.h @@ -0,0 +1,50 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU General Public License as published by the Free * +* Software Foundation; either version 2 of the License, or (at your option) * +* any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * +* more details. * +* * +* You should have received a copy of the GNU General Public License along * +* with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include +#include "WindowState.h" + +#include +using sofa::helper::system::FileSystem; +#include +#include +using sofa::simulation::SnapshotManager; +#include +using sofa::simulation::LoadSnapshotVisitor; + + +namespace windows +{ + /** + * @brief Shows the Snapshot window. + * + * This function displays a window which user can use Snapshot's features. + * + */ + void showSnapshot(const char* const& windowNameSnapshot, + WindowState& winManagerSnapshot,sofa::core::sptr& groot); + + void doFileSave(sofa::core::sptr& groot, bool isSet); + void doFileLoad(sofa::core::sptr& groot, bool isSet); + +} // namespace sofaimgui