2424#include < iomanip>
2525#include < ostream>
2626#include < unordered_set>
27+ #include < type_traits>
2728#include < SofaGLFW/SofaGLFWBaseGUI.h>
2829
2930#include < sofa/core/CategoryLibrary.h>
6869#include < imgui_internal.h> // imgui_internal.h is included in order to use the DockspaceBuilder API (which is still in development)
6970#include < implot.h>
7071#include < nfd.h>
72+ #include < SimpleIni.h>
7173#include < sofa/component/visual/VisualStyle.h>
7274#include < sofa/core/ObjectFactory.h>
7375#include < sofa/core/visual/VisualParams.h>
@@ -86,6 +88,11 @@ using namespace sofa;
8688namespace sofaimgui
8789{
8890
91+ struct ImGuiGUIEngine ::Settings
92+ {
93+ CSimpleIniA ini;
94+ };
95+
8996ImGuiGUIEngine::ImGuiGUIEngine ()
9097 : winManagerProfiler(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string(" profiler.txt" )))
9198 , winManagerSceneGraph(helper::system::FileSystem::append(sofaimgui::getConfigurationFolderPath(), std::string(" scenegraph.txt" )))
@@ -103,6 +110,9 @@ ImGuiGUIEngine::ImGuiGUIEngine()
103110{
104111}
105112
113+ ImGuiGUIEngine::~ImGuiGUIEngine ()
114+ {}
115+
106116void ImGuiGUIEngine::init ()
107117{
108118 IMGUI_CHECKVERSION ();
@@ -121,22 +131,21 @@ void ImGuiGUIEngine::init()
121131 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
122132 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
123133
124-
125-
126- ini.SetUnicode ();
134+ settings = std::make_unique<Settings>();
135+ settings->ini .SetUnicode ();
127136 if (sofa::helper::system::FileSystem::exists (sofaimgui::AppIniFile::getAppIniFile ()))
128137 {
129- [[maybe_unused]] SI_Error rc = ini.LoadFile (sofaimgui::AppIniFile::getAppIniFile ().c_str ());
138+ [[maybe_unused]] SI_Error rc = settings-> ini .LoadFile (sofaimgui::AppIniFile::getAppIniFile ().c_str ());
130139 assert (rc == SI_OK);
131140 msg_info (" ImGuiGUIEngine" ) << " Fetching settings from " << sofaimgui::AppIniFile::getAppIniFile ();
132141 }
133142
134143 const char * pv;
135- pv = ini.GetValue (" Style" , " theme" );
144+ pv = settings-> ini .GetValue (" Style" , " theme" );
136145 if (!pv)
137146 {
138- ini.SetValue (" Style" , " theme" , sofaimgui::defaultStyle.c_str (), ini::styleDescription);
139- SI_Error rc = ini.SaveFile (sofaimgui::AppIniFile::getAppIniFile ().c_str ());
147+ settings-> ini .SetValue (" Style" , " theme" , sofaimgui::defaultStyle.c_str (), ini::styleDescription);
148+ SI_Error rc = settings-> ini .SaveFile (sofaimgui::AppIniFile::getAppIniFile ().c_str ());
140149 if (rc != SI_OK)
141150 {
142151 msg_error (" ImGuiGUIEngine" ) << " Saving file '" << sofaimgui::AppIniFile::getAppIniFile () << " ' failed. " << std::strerror (errno) << " . Error code " << rc;
@@ -188,18 +197,18 @@ void ImGuiGUIEngine::initBackend(GLFWwindow* glfwWindow)
188197 io.Fonts ->AddFontFromMemoryCompressedTTF (FA_SOLID_900_compressed_data, FA_SOLID_900_compressed_size, 16 * yscale, &config, icon_ranges);
189198
190199 // restore the global scale stored in the Settings ini file
191- const float globalScale = static_cast <float >(ini.GetDoubleValue (" Visualization" , " globalScale" , 1.0 ));
200+ const float globalScale = static_cast <float >(settings-> ini .GetDoubleValue (" Visualization" , " globalScale" , 1.0 ));
192201 this ->setScale (globalScale, windowMonitor);
193202 }
194203
195204 // restore window settings if set
196- const bool rememberWindowPosition = ini.GetBoolValue (" Window" , " rememberWindowPosition" , true );
205+ const bool rememberWindowPosition = settings-> ini .GetBoolValue (" Window" , " rememberWindowPosition" , true );
197206 if (rememberWindowPosition)
198207 {
199- if (ini.KeyExists (" Window" , " windowPosX" ) && ini.KeyExists (" Window" , " windowPosY" ))
208+ if (settings-> ini .KeyExists (" Window" , " windowPosX" ) && settings-> ini .KeyExists (" Window" , " windowPosY" ))
200209 {
201- const long windowPosX = ini.GetLongValue (" Window" , " windowPosX" );
202- const long windowPosY = ini.GetLongValue (" Window" , " windowPosY" );
210+ const long windowPosX = settings-> ini .GetLongValue (" Window" , " windowPosX" );
211+ const long windowPosY = settings-> ini .GetLongValue (" Window" , " windowPosY" );
203212
204213 int monitorCount;
205214 GLFWmonitor** monitors = glfwGetMonitors (&monitorCount);
@@ -246,13 +255,13 @@ void ImGuiGUIEngine::initBackend(GLFWwindow* glfwWindow)
246255
247256 }
248257
249- const bool rememberWindowSize = ini.GetBoolValue (" Window" , " rememberWindowSize" , true );
258+ const bool rememberWindowSize = settings-> ini .GetBoolValue (" Window" , " rememberWindowSize" , true );
250259 if (rememberWindowSize)
251260 {
252- if (ini.KeyExists (" Window" , " windowSizeX" ) && ini.KeyExists (" Window" , " windowSizeY" ))
261+ if (settings-> ini .KeyExists (" Window" , " windowSizeX" ) && settings-> ini .KeyExists (" Window" , " windowSizeY" ))
253262 {
254- const long windowSizeX = ini.GetLongValue (" Window" , " windowSizeX" );
255- const long windowSizeY = ini.GetLongValue (" Window" , " windowSizeY" );
263+ const long windowSizeX = settings-> ini .GetLongValue (" Window" , " windowSizeX" );
264+ const long windowSizeY = settings-> ini .GetLongValue (" Window" , " windowSizeY" );
256265 if (windowSizeX > 0 && windowSizeY > 0 )
257266 {
258267 glfwSetWindowSize (glfwWindow, static_cast <int >(windowSizeX), static_cast <int >(windowSizeY));
@@ -398,7 +407,7 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI)
398407
399408 auto groot = baseGUI->getRootNode ();
400409
401- bool alwaysShowFrame = ini.GetBoolValue (" Visualization" , " alwaysShowFrame" , true );
410+ bool alwaysShowFrame = settings-> ini .GetBoolValue (" Visualization" , " alwaysShowFrame" , true );
402411 if (alwaysShowFrame)
403412 {
404413 auto sceneFrame = groot->get <sofa::gl::component::rendering3d::OglSceneFrame>();
@@ -682,7 +691,7 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI)
682691 /* **************************************
683692 * Viewport window
684693 **************************************/
685- windows::showViewPort (groot, windowNameViewport, ini, m_fbo, m_viewportWindowSize,
694+ windows::showViewPort (groot, windowNameViewport, settings-> ini , m_fbo, m_viewportWindowSize,
686695 isMouseOnViewport, winManagerViewPort, baseGUI,
687696 isViewportDisplayedForTheFirstTime, lastViewPortPos);
688697
@@ -756,7 +765,7 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI)
756765 /* **************************************
757766 * Settings window
758767 **************************************/
759- windows::showSettings (windowNameSettings,ini, winManagerSettings, this );
768+ windows::showSettings (windowNameSettings, settings-> ini , winManagerSettings, this );
760769
761770 ImGui::Render ();
762771#if SOFAIMGUI_FORCE_OPENGL2 == 1
@@ -782,8 +791,9 @@ void ImGuiGUIEngine::endFrame()
782791 std::setlocale (LC_NUMERIC, m_localeBackup.c_str ());
783792}
784793
785- void ImGuiGUIEngine::resetView (ImGuiID dockspace_id, const char * windowNameSceneGraph, const char * winNameSelectionDescription, const char *windowNameLog, const char *windowNameViewport)
794+ void ImGuiGUIEngine::resetView (_ImGuiID dockspace_id, const char * windowNameSceneGraph, const char * winNameSelectionDescription, const char *windowNameLog, const char *windowNameViewport)
786795{
796+ static_assert (std::is_same<_ImGuiID, ImGuiID>::value, " _ImGuiID and ImGuiID types must be identical. _ImGuiID must be adjusted." );
787797 ImGuiViewport* viewport = ImGui::GetMainViewport ();
788798
789799 ImGui::DockBuilderRemoveNode (dockspace_id); // clear any previous layout
@@ -854,11 +864,11 @@ void ImGuiGUIEngine::terminate()
854864 const auto lastWindowSize = ImGui::GetMainViewport ()->Size ;
855865
856866 // save latest window state
857- ini.SetLongValue (" Window" , " windowPosX" , static_cast <long >(lastWindowPos.x ));
858- ini.SetLongValue (" Window" , " windowPosY" , static_cast <long >(lastWindowPos.y ));
859- ini.SetLongValue (" Window" , " windowSizeX" , static_cast <long >(lastWindowSize.x ));
860- ini.SetLongValue (" Window" , " windowSizeY" , static_cast <long >(lastWindowSize.y ));
861- [[maybe_unused]] SI_Error rc = ini.SaveFile (sofaimgui::AppIniFile::getAppIniFile ().c_str ());
867+ settings-> ini .SetLongValue (" Window" , " windowPosX" , static_cast <long >(lastWindowPos.x ));
868+ settings-> ini .SetLongValue (" Window" , " windowPosY" , static_cast <long >(lastWindowPos.y ));
869+ settings-> ini .SetLongValue (" Window" , " windowSizeX" , static_cast <long >(lastWindowSize.x ));
870+ settings-> ini .SetLongValue (" Window" , " windowSizeY" , static_cast <long >(lastWindowSize.y ));
871+ [[maybe_unused]] SI_Error rc = settings-> ini .SaveFile (sofaimgui::AppIniFile::getAppIniFile ().c_str ());
862872
863873 NFD_Quit ();
864874
0 commit comments