From aeedc1e82e836a8c3a7fed019c6dc3e4e06dc5b9 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Fri, 12 Apr 2024 21:35:08 +0200 Subject: [PATCH] demo docking: use responsive fonts --- src/hello_imgui/impl/hello_imgui_font.cpp | 2 +- .../hello_imgui_demodocking.main.cpp | 53 ++++++++++--------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/hello_imgui/impl/hello_imgui_font.cpp b/src/hello_imgui/impl/hello_imgui_font.cpp index f2326b3d..05cf90f8 100644 --- a/src/hello_imgui/impl/hello_imgui_font.cpp +++ b/src/hello_imgui/impl/hello_imgui_font.cpp @@ -231,7 +231,7 @@ namespace HelloImGui FontDpiResponsive* LoadFontDpiResponsive(const std::string & fontFilename, float fontSize, const FontLoadingParams & fontLoadingParams) { - IM_ASSERT((!gWasLoadFontBareCalled) && "If using LoadFontDpiResponsive(), only use it, and do not use LoadFont()!"); + IM_ASSERT((!gWasLoadFontBareCalled) && "If using LoadFontDpiResponsive(), set runnerParams.dpiAwareParams.onlyUseFontDpiResponsive=true and do not not use LoadFont()!"); gWasLoadFontDpiResponsiveCalled = true; // Ensure that we have enough capacity, so that pointers remain valid diff --git a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp index 31d5e4a2..9acdfdc6 100644 --- a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp +++ b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp @@ -62,10 +62,10 @@ struct AppState MyAppSettings myAppSettings; // This values will be stored in the application settings - ImFont* TitleFont = nullptr; - ImFont* ColorFont = nullptr; - ImFont* EmojiFont = nullptr; - ImFont* LargeIconFont = nullptr; + HelloImGui::FontDpiResponsive *TitleFont; + HelloImGui::FontDpiResponsive *ColorFont; + HelloImGui::FontDpiResponsive *EmojiFont; + HelloImGui::FontDpiResponsive *LargeIconFont; }; @@ -74,24 +74,27 @@ struct AppState ////////////////////////////////////////////////////////////////////////// void LoadFonts(AppState& appState) // This is called by runnerParams.callbacks.LoadAdditionalFonts { - HelloImGui::GetRunnerParams()->callbacks.defaultIconFont = HelloImGui::DefaultIconFont::FontAwesome6; + auto runnerParams = HelloImGui::GetRunnerParams(); + runnerParams->dpiAwareParams.onlyUseFontDpiResponsive=true; + + runnerParams->callbacks.defaultIconFont = HelloImGui::DefaultIconFont::FontAwesome6; // First, load the default font (the default font should be loaded first) HelloImGui::ImGuiDefaultSettings::LoadDefaultFont_WithFontAwesomeIcons(); // Then load the other fonts - appState.TitleFont = HelloImGui::LoadFont("fonts/DroidSans.ttf", 18.f); + appState.TitleFont = HelloImGui::LoadFontDpiResponsive("fonts/DroidSans.ttf", 18.f); HelloImGui::FontLoadingParams fontLoadingParamsEmoji; fontLoadingParamsEmoji.useFullGlyphRange = true; - appState.EmojiFont = HelloImGui::LoadFont("fonts/NotoEmoji-Regular.ttf", 24.f, fontLoadingParamsEmoji); + appState.EmojiFont = HelloImGui::LoadFontDpiResponsive("fonts/NotoEmoji-Regular.ttf", 24.f, fontLoadingParamsEmoji); HelloImGui::FontLoadingParams fontLoadingParamsLargeIcon; fontLoadingParamsLargeIcon.useFullGlyphRange = true; - appState.LargeIconFont = HelloImGui::LoadFont("fonts/fontawesome-webfont.ttf", 24.f, fontLoadingParamsLargeIcon); + appState.LargeIconFont = HelloImGui::LoadFontDpiResponsive("fonts/fontawesome-webfont.ttf", 24.f, fontLoadingParamsLargeIcon); #ifdef IMGUI_ENABLE_FREETYPE // Found at https://www.colorfonts.wtf/ HelloImGui::FontLoadingParams fontLoadingParamsColor; fontLoadingParamsColor.loadColor = true; - appState.ColorFont = HelloImGui::LoadFont("fonts/Playbox/Playbox-FREE.otf", 24.f, fontLoadingParamsColor); + appState.ColorFont = HelloImGui::LoadFontDpiResponsive("fonts/Playbox/Playbox-FREE.otf", 24.f, fontLoadingParamsColor); #endif } @@ -139,7 +142,7 @@ void SaveMyAppSettings(const AppState& appState) // Display a button that will hide the application window void DemoHideWindow(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Hide app window"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Hide app window"); ImGui::PopFont(); static double lastHideTime = -1.; if (ImGui::Button("Hide")) { @@ -169,7 +172,7 @@ void DemoShowAdditionalWindow(AppState& appState) // * either make them initially invisible, and exclude them from the view menu (such as shown here) // * or modify runnerParams.dockingParams.dockableWindows inside the callback RunnerCallbacks.PreNewFrame const char* windowName = "Additional Window"; - ImGui::PushFont(appState.TitleFont); ImGui::Text("Dynamically add window"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Dynamically add window"); ImGui::PopFont(); if (ImGui::Button("Show additional window")) { auto additionalWindowPtr = HelloImGui::GetRunnerParams()->dockingParams.dockableWindowOfName(windowName); @@ -185,7 +188,7 @@ void DemoShowAdditionalWindow(AppState& appState) void DemoLogs(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Log Demo"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Log Demo"); ImGui::PopFont(); ImGui::BeginGroup(); // Edit a float using a slider from 0.0f to 1.0f @@ -209,7 +212,7 @@ void DemoLogs(AppState& appState) void DemoUserSettings(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("User settings"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("User settings"); ImGui::PopFont(); ImGui::BeginGroup(); ImGui::SetNextItemWidth(HelloImGui::EmSize(7.f)); ImGui::InputText("Name", &appState.myAppSettings.name); @@ -223,7 +226,7 @@ void DemoUserSettings(AppState& appState) void DemoRocket(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Status Bar Demo"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Status Bar Demo"); ImGui::PopFont(); ImGui::BeginGroup(); if (appState.rocket_state == AppState::RocketState::Init) { @@ -260,7 +263,7 @@ void DemoRocket(AppState& appState) void DemoDockingFlags(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Main dock space node flags"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Main dock space node flags"); ImGui::PopFont(); ImGui::TextWrapped(R"( This will edit the ImGuiDockNodeFlags for "MainDockSpace". Most flags are inherited by children dock spaces. @@ -295,13 +298,13 @@ Most flags are inherited by children dock spaces. void GuiWindowLayoutCustomization(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Switch between layouts"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Switch between layouts"); ImGui::PopFont(); ImGui::Text("with the menu \"View/Layouts\""); if (ImGui::IsItemHovered()) ImGui::SetTooltip("Each layout remembers separately the modifications applied by the user, \nand the selected layout is restored at startup"); ImGui::Separator(); - ImGui::PushFont(appState.TitleFont); ImGui::Text("Change the theme"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Change the theme"); ImGui::PopFont(); ImGui::Text("with the menu \"View/Theme\""); if (ImGui::IsItemHovered()) ImGui::SetTooltip("The selected theme is remembered and restored at startup"); @@ -314,7 +317,7 @@ void GuiWindowLayoutCustomization(AppState& appState) void DemoAssets(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Image From Asset"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Image From Asset"); ImGui::PopFont(); HelloImGui::BeginGroupColumn(); ImGui::Dummy(HelloImGui::EmToVec2(0.f, 0.45f)); @@ -325,7 +328,7 @@ void DemoAssets(AppState& appState) void DemoFonts(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Fonts"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Fonts"); ImGui::PopFont(); ImGui::TextWrapped("Mix icons " ICON_FA_FACE_SMILE " and text " ICON_FA_ROCKET ""); if (ImGui::IsItemHovered()) ImGui::SetTooltip("Example with Font Awesome Icons"); @@ -334,7 +337,7 @@ void DemoFonts(AppState& appState) ImGui::BeginGroup(); { - ImGui::PushFont(appState.EmojiFont); + ImGui::PushFont(appState.EmojiFont->font); // ✌️ (Victory Hand Emoji) ImGui::Text(U8_TO_CHAR(u8"\U0000270C\U0000FE0F")); ImGui::SameLine(); @@ -361,7 +364,7 @@ void DemoFonts(AppState& appState) #ifdef IMGUI_ENABLE_FREETYPE ImGui::Text("Colored Fonts"); - ImGui::PushFont(appState.ColorFont); + ImGui::PushFont(appState.ColorFont->font); ImGui::Text("C O L O R !"); ImGui::PopFont(); if (ImGui::IsItemHovered()) @@ -371,7 +374,7 @@ void DemoFonts(AppState& appState) void DemoThemes(AppState& appState) { - ImGui::PushFont(appState.TitleFont); ImGui::Text("Themes"); ImGui::PopFont(); + ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Themes"); ImGui::PopFont(); auto& tweakedTheme = HelloImGui::GetRunnerParams()->imGuiWindowParams.tweakedTheme; ImGui::BeginGroup(); @@ -453,7 +456,7 @@ void ShowAppMenuItems() void ShowTopToolbar(AppState& appState) { - ImGui::PushFont(appState.LargeIconFont); + ImGui::PushFont(appState.LargeIconFont->font); if (ImGui::Button(ICON_FA_POWER_OFF)) HelloImGui::GetRunnerParams()->appShallExit = true; @@ -474,7 +477,7 @@ void ShowTopToolbar(AppState& appState) void ShowRightToolbar(AppState& appState) { - ImGui::PushFont(appState.LargeIconFont); + ImGui::PushFont(appState.LargeIconFont->font); if (ImGui::Button(ICON_FA_CIRCLE_ARROW_LEFT)) HelloImGui::Log(HelloImGui::LogLevel::Info, "Clicked on Circle left in the right toolbar"); @@ -794,6 +797,8 @@ int main(int, char**) //runnerParams.dpiAwareParams.dpiWindowSizeFactor = 1.0f; //runnerParams.dpiAwareParams.fontRenderingScale = 1.0f; + runnerParams.platformBackendType = HelloImGui::PlatformBackendType::Sdl; + HelloImGui::Run(runnerParams); // Note: with ImGuiBundle, it is also possible to use ImmApp::Run(...) return 0;