Font: Added io.FontDefault in ImGuiIO structure to make it easier to change default font from third-party or demo code

This commit is contained in:
ocornut 2016-11-06 21:50:18 +01:00
parent 357167f917
commit b376b68326
3 changed files with 16 additions and 5 deletions

View File

@ -658,6 +658,7 @@ static float GetDraggedColumnOffset(int column_index);
static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true);
static ImFont* GetDefaultFont();
static void SetCurrentFont(ImFont* font); static void SetCurrentFont(ImFont* font);
static void SetCurrentWindow(ImGuiWindow* window); static void SetCurrentWindow(ImGuiWindow* window);
static void SetWindowScrollY(ImGuiWindow* window, float new_scroll_y); static void SetWindowScrollY(ImGuiWindow* window, float new_scroll_y);
@ -809,6 +810,7 @@ ImGuiIO::ImGuiIO()
LogFilename = "imgui_log.txt"; LogFilename = "imgui_log.txt";
Fonts = &GImDefaultFontAtlas; Fonts = &GImDefaultFontAtlas;
FontGlobalScale = 1.0f; FontGlobalScale = 1.0f;
FontDefault = NULL;
DisplayFramebufferScale = ImVec2(1.0f, 1.0f); DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
MousePos = ImVec2(-1,-1); MousePos = ImVec2(-1,-1);
MousePosPrev = ImVec2(-1,-1); MousePosPrev = ImVec2(-1,-1);
@ -2106,7 +2108,8 @@ void ImGui::NewFrame()
g.Initialized = true; g.Initialized = true;
} }
SetCurrentFont(g.IO.Fonts->Fonts[0]); SetCurrentFont(GetDefaultFont());
IM_ASSERT(g.Font->IsLoaded());
g.Time += g.IO.DeltaTime; g.Time += g.IO.DeltaTime;
g.FrameCount += 1; g.FrameCount += 1;
@ -4586,6 +4589,12 @@ float ImGui::CalcItemWidth()
return w; return w;
} }
static ImFont* GetDefaultFont()
{
ImGuiContext& g = *GImGui;
return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0];
}
static void SetCurrentFont(ImFont* font) static void SetCurrentFont(ImFont* font)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -4601,7 +4610,7 @@ void ImGui::PushFont(ImFont* font)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
if (!font) if (!font)
font = g.IO.Fonts->Fonts[0]; font = GetDefaultFont();
SetCurrentFont(font); SetCurrentFont(font);
g.FontStack.push_back(font); g.FontStack.push_back(font);
g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID); g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID);
@ -4612,7 +4621,7 @@ void ImGui::PopFont()
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
g.CurrentWindow->DrawList->PopTextureID(); g.CurrentWindow->DrawList->PopTextureID();
g.FontStack.pop_back(); g.FontStack.pop_back();
SetCurrentFont(g.FontStack.empty() ? g.IO.Fonts->Fonts[0] : g.FontStack.back()); SetCurrentFont(g.FontStack.empty() ? GetDefaultFont() : g.FontStack.back());
} }
void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus) void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus)

View File

@ -739,6 +739,7 @@ struct ImGuiIO
ImFontAtlas* Fonts; // <auto> // Load and assemble one or more fonts into a single tightly packed texture. Output to Fonts array. ImFontAtlas* Fonts; // <auto> // Load and assemble one or more fonts into a single tightly packed texture. Output to Fonts array.
float FontGlobalScale; // = 1.0f // Global scale all fonts float FontGlobalScale; // = 1.0f // Global scale all fonts
bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel. bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel.
ImFont* FontDefault; // = NULL // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0].
ImVec2 DisplayFramebufferScale; // = (1.0f,1.0f) // For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui. ImVec2 DisplayFramebufferScale; // = (1.0f,1.0f) // For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui.
ImVec2 DisplayVisibleMin; // <unset> (0.0f,0.0f) // If you use DisplaySize as a virtual space larger than your screen, set DisplayVisibleMin/Max to the visible area. ImVec2 DisplayVisibleMin; // <unset> (0.0f,0.0f) // If you use DisplaySize as a virtual space larger than your screen, set DisplayVisibleMin/Max to the visible area.
ImVec2 DisplayVisibleMax; // <unset> (0.0f,0.0f) // If the values are the same, we defaults to Min=(0.0f) and Max=DisplaySize ImVec2 DisplayVisibleMax; // <unset> (0.0f,0.0f) // If the values are the same, we defaults to Min=(0.0f) and Max=DisplaySize

View File

@ -1721,7 +1721,8 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
if (ImGui::TreeNode("Fonts", "Fonts (%d)", ImGui::GetIO().Fonts->Fonts.Size)) if (ImGui::TreeNode("Fonts", "Fonts (%d)", ImGui::GetIO().Fonts->Fonts.Size))
{ {
ImGui::SameLine(); ShowHelpMarker("Tip: Load fonts with io.Fonts->AddFontFromFileTTF()\nbefore calling io.Fonts->GetTex* functions."); ImGui::SameLine(); ShowHelpMarker("Tip: Load fonts with io.Fonts->AddFontFromFileTTF()\nbefore calling io.Fonts->GetTex* functions.");
ImFontAtlas* atlas = ImGui::GetIO().Fonts; ImGuiIO& io = ImGui::GetIO();
ImFontAtlas* atlas = io.Fonts;
if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight))
{ {
ImGui::Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255), ImColor(255,255,255,128)); ImGui::Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255), ImColor(255,255,255,128));
@ -1733,7 +1734,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
ImFont* font = atlas->Fonts[i]; ImFont* font = atlas->Fonts[i];
ImGui::BulletText("Font %d: \'%s\', %.2f px, %d glyphs", i, font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size); ImGui::BulletText("Font %d: \'%s\', %.2f px, %d glyphs", i, font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size);
ImGui::TreePush((void*)(intptr_t)i); ImGui::TreePush((void*)(intptr_t)i);
if (i > 0) { ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { atlas->Fonts[i] = atlas->Fonts[0]; atlas->Fonts[0] = font; } } ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) io.FontDefault = font;
ImGui::PushFont(font); ImGui::PushFont(font);
ImGui::Text("The quick brown fox jumps over the lazy dog"); ImGui::Text("The quick brown fox jumps over the lazy dog");
ImGui::PopFont(); ImGui::PopFont();