Skip to content

Commit

Permalink
demo docking: use responsive fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Apr 12, 2024
1 parent d3882aa commit aeedc1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/hello_imgui/impl/hello_imgui_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand All @@ -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
}

Expand Down Expand Up @@ -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"))
{
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand All @@ -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));
Expand All @@ -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");
Expand All @@ -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();
Expand All @@ -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())
Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand All @@ -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");

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit aeedc1e

Please sign in to comment.