@@ -175,85 +175,14 @@ void ImGuiGUIEngine::initBackend(GLFWwindow* glfwWindow)
175175 ImGui_ImplOpenGL3_Init (nullptr );
176176#endif // SOFAIMGUI_FORCE_OPENGL2 == 1
177177
178- GLFWmonitor* windowMonitor = glfwGetWindowMonitor (glfwWindow) ;
179- if (! windowMonitor)
178+ float yscale { 1 . f } ;
179+ if (GLFWmonitor* windowMonitor = findMyMonitor (glfwWindow) )
180180 {
181- windowMonitor = glfwGetPrimaryMonitor ();
182- }
183- if (windowMonitor)
184- {
185- float xscale{}, yscale{};
181+ float xscale{};
186182 glfwGetMonitorContentScale (windowMonitor, &xscale, &yscale);
187-
188- ImGuiIO& io = ImGui::GetIO ();
189-
190- io.Fonts ->AddFontFromMemoryCompressedTTF (ROBOTO_MEDIUM_compressed_data, ROBOTO_MEDIUM_compressed_size, 16 * yscale);
191-
192- ImFontConfig config;
193- config.MergeMode = true ;
194- config.GlyphMinAdvanceX = 16 .0f ; // Use if you want to make the icon monospaced
195- static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
196- io.Fonts ->AddFontFromMemoryCompressedTTF (FA_REGULAR_400_compressed_data, FA_REGULAR_400_compressed_size, 16 * yscale, &config, icon_ranges);
197- io.Fonts ->AddFontFromMemoryCompressedTTF (FA_SOLID_900_compressed_data, FA_SOLID_900_compressed_size, 16 * yscale, &config, icon_ranges);
198-
199- // restore the global scale stored in the Settings ini file
200- const float globalScale = static_cast <float >(settings->ini .GetDoubleValue (" Visualization" , " globalScale" , 1.0 ));
201- this ->setScale (globalScale, windowMonitor);
202183 }
203-
204- // restore window settings if set
205- const bool rememberWindowPosition = settings->ini .GetBoolValue (" Window" , " rememberWindowPosition" , true );
206- if (rememberWindowPosition)
207- {
208- if (settings->ini .KeyExists (" Window" , " windowPosX" ) && settings->ini .KeyExists (" Window" , " windowPosY" ))
209- {
210- const long windowPosX = settings->ini .GetLongValue (" Window" , " windowPosX" );
211- const long windowPosY = settings->ini .GetLongValue (" Window" , " windowPosY" );
212-
213- int monitorCount;
214- GLFWmonitor** monitors = glfwGetMonitors (&monitorCount);
215-
216- bool foundValidMonitor = false ;
217- for (int i = 0 ; i < monitorCount; ++i)
218- {
219- if (GLFWmonitor* monitor = monitors[i])
220- {
221- // retrieve the work area of the monitor in the whole desktop space, in screen coordinates
222- int monitorXPos{0 }, monitorYPos{0 }, monitorWidth{0 }, monitorHeight{0 };
223- glfwGetMonitorWorkarea (monitor, &monitorXPos, &monitorYPos, &monitorWidth, &monitorHeight);
224- if (!monitorWidth || !monitorHeight)
225- {
226- msg_error (" ImGuiGUIEngine" ) << " Unknown error while trying to fetch the monitor information." ;
227- }
228- else
229- {
230- constexpr long margin = 5 ; // avoid the case where the window is positioned on the border of the monitor (almost invisible/non-selectable)
231-
232- if (windowPosX >= (monitorXPos) &&
233- windowPosX <= (monitorXPos + monitorWidth-margin) &&
234- windowPosY >= (monitorYPos) &&
235- windowPosY <= (monitorYPos + monitorHeight-margin))
236- {
237- glfwSetWindowPos (glfwWindow, static_cast <int >(windowPosX), static_cast <int >(windowPosY));
238- foundValidMonitor = true ;
239- break ;
240- }
241-
242- }
243- }
244- }
245-
246- if (!foundValidMonitor)
247- {
248- msg_error (" ImGuiGUIEngine" ) << " The window position from settings is invalid for any monitor." ;
249- }
250- }
251- else
252- {
253- msg_error (" ImGuiGUIEngine" ) << " Cannot set window position from settings." ;
254- }
255184
256- }
185+ loadFont (yscale);
257186
258187 const bool rememberWindowSize = settings->ini .GetBoolValue (" Window" , " rememberWindowSize" , true );
259188 if (rememberWindowSize)
@@ -815,6 +744,103 @@ void ImGuiGUIEngine::resetView(_ImGuiID dockspace_id, const char* windowNameScen
815744 firstRunState.setState (true );// Mark first run as complete
816745}
817746
747+ GLFWmonitor* ImGuiGUIEngine::findMyMonitor (GLFWwindow* glfwWindow)
748+ {
749+ GLFWmonitor* foundMonitor { nullptr };
750+
751+ const bool rememberWindowPosition = settings->ini .GetBoolValue (" Window" , " rememberWindowPosition" , true );
752+ if (rememberWindowPosition)
753+ {
754+ if (settings->ini .KeyExists (" Window" , " windowPosX" ) && settings->ini .KeyExists (" Window" , " windowPosY" ))
755+ {
756+ const long windowPosX = settings->ini .GetLongValue (" Window" , " windowPosX" );
757+ const long windowPosY = settings->ini .GetLongValue (" Window" , " windowPosY" );
758+
759+ int monitorCount;
760+ GLFWmonitor** monitors = glfwGetMonitors (&monitorCount);
761+
762+ bool foundValidMonitor = false ;
763+ for (int i = 0 ; i < monitorCount; ++i)
764+ {
765+ if (GLFWmonitor* monitor = monitors[i])
766+ {
767+ // retrieve the work area of the monitor in the whole desktop space, in screen coordinates
768+ int monitorXPos{0 }, monitorYPos{0 }, monitorWidth{0 }, monitorHeight{0 };
769+ glfwGetMonitorWorkarea (monitor, &monitorXPos, &monitorYPos, &monitorWidth, &monitorHeight);
770+ if (!monitorWidth || !monitorHeight)
771+ {
772+ msg_error (" ImGuiGUIEngine" ) << " Unknown error while trying to fetch the monitor information." ;
773+ }
774+ else
775+ {
776+ constexpr long margin = 5 ; // avoid the case where the window is positioned on the border of the monitor (almost invisible/non-selectable)
777+
778+ if (windowPosX >= (monitorXPos) &&
779+ windowPosX <= (monitorXPos + monitorWidth-margin) &&
780+ windowPosY >= (monitorYPos) &&
781+ windowPosY <= (monitorYPos + monitorHeight-margin))
782+ {
783+ glfwSetWindowPos (glfwWindow, static_cast <int >(windowPosX), static_cast <int >(windowPosY));
784+ foundValidMonitor = true ;
785+ foundMonitor = monitor;
786+ break ;
787+ }
788+
789+ }
790+ }
791+ }
792+
793+ if (!foundValidMonitor)
794+ {
795+ msg_error (" ImGuiGUIEngine" ) << " The window position from settings is invalid for any monitor." ;
796+ }
797+ }
798+ else
799+ {
800+ msg_error (" ImGuiGUIEngine" ) << " Cannot set window position from settings." ;
801+ }
802+ }
803+
804+ if (!foundMonitor)
805+ {
806+ foundMonitor = glfwGetPrimaryMonitor ();
807+ }
808+
809+ return foundMonitor;
810+ }
811+
812+ void ImGuiGUIEngine::loadFont (float yscale)
813+ {
814+ constexpr float fontSize = 16 .f ;
815+
816+ ImGuiIO& io = ImGui::GetIO ();
817+ io.Fonts ->Clear ();
818+
819+ io.Fonts ->AddFontFromMemoryCompressedTTF (ROBOTO_MEDIUM_compressed_data, ROBOTO_MEDIUM_compressed_size, fontSize * yscale);
820+
821+ ImFontConfig config;
822+ config.MergeMode = true ;
823+ config.GlyphMinAdvanceX = fontSize * yscale;
824+
825+ static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
826+ io.Fonts ->AddFontFromMemoryCompressedTTF (FA_REGULAR_400_compressed_data, FA_REGULAR_400_compressed_size, fontSize * yscale, &config, icon_ranges);
827+ io.Fonts ->AddFontFromMemoryCompressedTTF (FA_SOLID_900_compressed_data, FA_SOLID_900_compressed_size, fontSize * yscale, &config, icon_ranges);
828+
829+ // restore the global scale stored in the Settings ini file
830+ const float globalScale = static_cast <float >(settings->ini .GetDoubleValue (" Visualization" , " globalScale" , 1.0 ));
831+ this ->setScale (globalScale);
832+
833+ io.Fonts ->Build ();
834+
835+ #if SOFAIMGUI_FORCE_OPENGL2 == 1
836+ ImGui_ImplOpenGL2_DestroyFontsTexture ();
837+ ImGui_ImplOpenGL2_CreateFontsTexture ();
838+ #else
839+ ImGui_ImplOpenGL3_DestroyFontsTexture ();
840+ ImGui_ImplOpenGL3_CreateFontsTexture ();
841+ #endif
842+ }
843+
818844void ImGuiGUIEngine::beforeDraw (GLFWwindow*)
819845{
820846
@@ -894,20 +920,15 @@ bool ImGuiGUIEngine::dispatchMouseEvents()
894920 return !ImGui::GetIO ().WantCaptureMouse || isMouseOnViewport;
895921}
896922
923+ void ImGuiGUIEngine::contentScaleChanged (float xscale, float yscale)
924+ {
925+ loadFont (yscale);
926+ }
897927
898- void ImGuiGUIEngine::setScale (double globalScale, GLFWmonitor* monitor )
928+ void ImGuiGUIEngine::setScale (float globalScale)
899929{
900- if (!monitor)
901- {
902- monitor = glfwGetPrimaryMonitor ();
903- }
904-
905930 ImGuiIO& io = ImGui::GetIO ();
906-
907- float xscale{}, yscale{};
908- glfwGetMonitorContentScale (monitor, &xscale, &yscale);
909-
910- io.FontGlobalScale = globalScale / yscale;
931+ io.FontGlobalScale = globalScale;
911932}
912933
913934type::Vec2i ImGuiGUIEngine::getFrameBufferPixels (std::vector<uint8_t >& pixels)
0 commit comments