Skip to content

Commit 0a40f37

Browse files
Hide non-public interface (#256)
* remove imgui.h from ImgGuiGUIEngine header * change RigidMass to template explicit instanciation to hide impl which is using imgui.h * remove imgui internal headers from distributed public headers * hide impl of ScalarWidget * remove useless headers * fix missing headers simpleini.h * use SimpleIni as a dependency * add SimpleIni dep in SofaImGUi cmake config * do not install implot and IconFontCppHeaders headers * typo * hide SimpleIni to impl and do not expose private headers ViewPort.h and Settings.h * do not expose Performances.h header as it includes imgui * fix SimpleIni target as private * fix imgui headers are not part of SofaImGui anymore, must be treated as dependency * fix SofaGLFW cmake config in with missing gflw --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent 98206ba commit 0a40f37

File tree

15 files changed

+209
-114
lines changed

15 files changed

+209
-114
lines changed

SofaGLFW/SofaGLFWConfig.cmake.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ find_package(Sofa.Simulation.Graph QUIET REQUIRED)
88
find_package(Sofa.GL QUIET REQUIRED)
99
find_package(Sofa.Component.Visual QUIET REQUIRED)
1010

11+
if(NOT TARGET glfw)
12+
sofa_find_package(glfw3 QUIET REQUIRED)
13+
endif()
14+
1115
set(SOFAGLFW_HAVE_SOFA_GUI_COMMON @SOFAGLFW_HAVE_SOFA_GUI_COMMON@)
1216
if(SOFAGLFW_HAVE_SOFA_GUI_COMMON)
1317
find_package(Sofa.GUI.Common QUIET REQUIRED)

SofaImGui/CMakeLists.txt

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ sofa_fetch_dependency(imgui
2020
DONT_BUILD
2121
)
2222

23+
# nfd (nativefiledialog-extended)
2324
find_package(nfd CONFIG QUIET)
2425

2526
if(TARGET nfd::nfd)
@@ -42,43 +43,34 @@ elseif( (DEFINED SOFA_ALLOW_FETCH_DEPENDENCIES AND SOFA_ALLOW_FETCH_DEPENDENCIES
4243
add_library(nfd::nfd ALIAS nfd) # introduced in nfd >= v1.2.1
4344
endif()
4445

45-
46+
# ImPlot
4647
sofa_fetch_dependency(ImPlot
4748
GIT_REPOSITORY https://github.com/epezent/implot
4849
GIT_TAG 18c72431f8265e2b0b5378a3a73d8a883b2175ff # v0.16
4950
DONT_BUILD
5051
)
5152

53+
# IconFontCppHeaders
5254
sofa_fetch_dependency(IconFontCppHeaders
5355
GIT_REPOSITORY https://github.com/juliettef/IconFontCppHeaders
5456
GIT_TAG bf610efb2e5d4361df799d0dfb5ae3977d7bba2e
5557
DONT_BUILD
5658
)
5759

58-
sofa_fetch_dependency(simpleini
59-
GIT_REPOSITORY https://github.com/brofield/simpleini
60-
GIT_TAG 09c21bda1dc1b578fa55f4a005d79b0afd481296 # v4.22
60+
# SimpleIni
61+
find_package(SimpleIni QUIET)
6162

62-
)
63+
if(NOT TARGET SimpleIni::SimpleIni AND ((DEFINED SOFA_ALLOW_FETCH_DEPENDENCIES AND SOFA_ALLOW_FETCH_DEPENDENCIES) OR (NOT DEFINED SOFA_ALLOW_FETCH_DEPENDENCIES)))
64+
message("${PROJECT_NAME}: SimpleIni not found and SOFA_ALLOW_FETCH_DEPENDENCIES is ON, fetching source code...")
65+
66+
sofa_fetch_dependency(simpleini
67+
GIT_REPOSITORY https://github.com/brofield/simpleini
68+
GIT_TAG 09c21bda1dc1b578fa55f4a005d79b0afd481296 # v4.22
69+
)
70+
elseif(NOT TARGET SimpleIni::SimpleIni)
71+
message(FATAL_ERROR "{PROJECT_NAME}: SimpleIni not found and SOFA_ALLOW_FETCH_DEPENDENCIES is OFF and thus cannot be fetched. Install SimpleIni, or enable SOFA_ALLOW_FETCH_DEPENDENCIES to fix this issue.")
72+
endif()
6373

64-
set(IMGUI_HEADER_FILES
65-
${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.h
66-
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.h
67-
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl2.h
68-
${imgui_SOURCE_DIR}/imconfig.h
69-
${imgui_SOURCE_DIR}/imgui.h
70-
${imgui_SOURCE_DIR}/imgui_internal.h
71-
${imgui_SOURCE_DIR}/misc/cpp/imgui_stdlib.h
72-
${ImPlot_SOURCE_DIR}/implot.h
73-
${ImPlot_SOURCE_DIR}/implot_internal.h
74-
${IconFontCppHeaders_SOURCE_DIR}/IconsFontAwesome4.h
75-
${IconFontCppHeaders_SOURCE_DIR}/IconsFontAwesome6.h
76-
${simpleini_SOURCE_DIR}/SimpleIni.h
77-
resources/fa-regular-400.h
78-
resources/fa-solid-900.h
79-
resources/Roboto-Medium.h
80-
resources/Style.h
81-
)
8274
set(IMGUI_SOURCE_FILES
8375
${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
8476
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
@@ -102,24 +94,20 @@ set(HEADER_FILES
10294
${SOFAIMGUI_SOURCE_DIR}/ImGuiDataWidget.h
10395
${SOFAIMGUI_SOURCE_DIR}/ImGuiGUI.h
10496
${SOFAIMGUI_SOURCE_DIR}/ImGuiGUIEngine.h
105-
${SOFAIMGUI_SOURCE_DIR}/ObjectColor.h
10697
${SOFAIMGUI_SOURCE_DIR}/UIStrings.h
10798
${SOFAIMGUI_SOURCE_DIR}/widgets/DisplayFlagsWidget.h
10899
${SOFAIMGUI_SOURCE_DIR}/widgets/LinearSpringWidget.h
109100
${SOFAIMGUI_SOURCE_DIR}/widgets/MaterialWidget.h
110101
${SOFAIMGUI_SOURCE_DIR}/widgets/RigidMass.h
111102
${SOFAIMGUI_SOURCE_DIR}/widgets/ScalarWidget.h
112103
${SOFAIMGUI_SOURCE_DIR}/widgets/VecVectorWidget.h
113-
${SOFAIMGUI_SOURCE_DIR}/windows/Performances.h
114104
${SOFAIMGUI_SOURCE_DIR}/windows/Log.h
115105
${SOFAIMGUI_SOURCE_DIR}/windows/MouseManager.h
116106
${SOFAIMGUI_SOURCE_DIR}/windows/Profiler.h
117107
${SOFAIMGUI_SOURCE_DIR}/windows/SceneGraph.h
118108
${SOFAIMGUI_SOURCE_DIR}/windows/DisplayFlags.h
119109
${SOFAIMGUI_SOURCE_DIR}/windows/Plugins.h
120110
${SOFAIMGUI_SOURCE_DIR}/windows/Components.h
121-
${SOFAIMGUI_SOURCE_DIR}/windows/Settings.h
122-
${SOFAIMGUI_SOURCE_DIR}/windows/ViewPort.h
123111
${SOFAIMGUI_SOURCE_DIR}/AppIniFile.h
124112
${SOFAIMGUI_SOURCE_DIR}/windows/WindowState.h
125113
${SOFAIMGUI_SOURCE_DIR}/guis/BaseAdditionalGUI.h
@@ -134,6 +122,8 @@ set(SOURCE_FILES
134122
${SOFAIMGUI_SOURCE_DIR}/initSofaImGui.cpp
135123
${SOFAIMGUI_SOURCE_DIR}/widgets/DisplayFlagsWidget.cpp
136124
${SOFAIMGUI_SOURCE_DIR}/widgets/MaterialWidget.cpp
125+
${SOFAIMGUI_SOURCE_DIR}/widgets/RigidMass.cpp
126+
${SOFAIMGUI_SOURCE_DIR}/widgets/ScalarWidget.cpp
137127
${SOFAIMGUI_SOURCE_DIR}/windows/Performances.cpp
138128
${SOFAIMGUI_SOURCE_DIR}/windows/Log.cpp
139129
${SOFAIMGUI_SOURCE_DIR}/windows/MouseManager.cpp
@@ -154,10 +144,11 @@ set(SOURCE_FILES
154144

155145
set(IMGUI_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources ${imgui_SOURCE_DIR} ${ImPlot_SOURCE_DIR} ${IconFontCppHeaders_SOURCE_DIR} ${simpleini_SOURCE_DIR})
156146

157-
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${IMGUI_HEADER_FILES} ${IMGUI_SOURCE_FILES})
147+
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${IMGUI_SOURCE_FILES})
158148
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${IMGUI_SOURCE_DIR}>")
159149
target_link_libraries(${PROJECT_NAME} PUBLIC SofaGLFW Sofa.GL.Component.Rendering3D ${CMAKE_DL_LIBS})
160150
target_link_libraries(${PROJECT_NAME} PRIVATE nfd)
151+
target_link_libraries(${PROJECT_NAME} PRIVATE SimpleIni::SimpleIni)
161152

162153
# setup the same API exports for imgui
163154
target_compile_definitions(${PROJECT_NAME} PUBLIC IMGUI_API=SOFAIMGUI_API)

SofaImGui/extensions/SofaImGui.Camera/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ project(SofaImGui.Camera VERSION 1.0 LANGUAGES CXX)
55
find_package(Sofa.Config REQUIRED)
66
sofa_find_package(SofaImGui REQUIRED)
77

8+
sofa_fetch_dependency(imgui
9+
GIT_REPOSITORY https://github.com/ocornut/imgui
10+
GIT_TAG 11b3a7c8ca23201294464c7f368614a9106af2a1 # v1.91.8-docking
11+
DONT_BUILD
12+
)
13+
814
set(HEADER_FILES
915
src/SofaImGui.Camera/config.h.in
1016
src/SofaImGui.Camera/init.h
@@ -18,7 +24,10 @@ set(SOURCE_FILES
1824
src/SofaImGui.Camera/CameraGUI.cpp
1925
)
2026

27+
set(IMGUI_SOURCE_DIR ${imgui_SOURCE_DIR})
28+
2129
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
30+
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${IMGUI_SOURCE_DIR}>")
2231
target_link_libraries(${PROJECT_NAME} PUBLIC SofaImGui)
2332

2433
sofa_create_package_with_targets(

SofaImGui/src/SofaImGui/AppIniFile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <sofa/helper/Utils.h>
2424
#include <sofa/helper/system/FileSystem.h>
2525
#include <sofa/gui/common/BaseGUI.h>
26-
#include "windows/Performances.h"
2726
#include "AppIniFile.h"
2827

2928

SofaImGui/src/SofaImGui/ImGuiGUIEngine.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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>
@@ -68,6 +69,7 @@
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;
8688
namespace sofaimgui
8789
{
8890

91+
struct ImGuiGUIEngine::Settings
92+
{
93+
CSimpleIniA ini;
94+
};
95+
8996
ImGuiGUIEngine::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+
106116
void 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

SofaImGui/src/SofaImGui/ImGuiGUIEngine.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
#include "guis/AdditionalGUIRegistry.h"
3030
#include "windows/WindowState.h"
31-
#include <SimpleIni.h>
32-
#include <imgui.h>
3331

3432
using windows::WindowState;
3533

@@ -49,7 +47,7 @@ class ImGuiGUIEngine : public sofaglfw::BaseGUIEngine
4947
public:
5048

5149
ImGuiGUIEngine() ;
52-
~ImGuiGUIEngine() = default;
50+
~ImGuiGUIEngine();
5351

5452
void init() override;
5553
void initBackend(GLFWwindow*) override;
@@ -83,8 +81,12 @@ class ImGuiGUIEngine : public sofaglfw::BaseGUIEngine
8381
std::pair<unsigned int, unsigned int> m_currentFBOSize;
8482
std::pair<float, float> m_viewportWindowSize;
8583
bool isMouseOnViewport { false };
86-
CSimpleIniA ini;
87-
void resetView(ImGuiID dockspace_id, const char *windowNameSceneGraph, const char *winNameSelectionDescription, const char *windowNameLog, const char *windowNameViewport) ;
84+
85+
struct Settings;
86+
std::unique_ptr<Settings> settings;
87+
88+
using _ImGuiID = unsigned int;
89+
void resetView(_ImGuiID dockspace_id, const char *windowNameSceneGraph, const char *winNameSelectionDescription, const char *windowNameLog, const char *windowNameViewport) ;
8890

8991
// WindowState members
9092
windows::WindowState winManagerProfiler;

0 commit comments

Comments
 (0)