Skip to content

Commit

Permalink
WIP. Crude dynamic font texture update. (fixup)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Apr 30, 2021
1 parent 6c460b0 commit cfa79ac
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 33 deletions.
1 change: 0 additions & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ namespace ImGui
IMGUI_API void ShowUserGuide(); // add basic help/info block (not a window): how to manipulate ImGui as a end-user (mouse/keyboard controls).
IMGUI_API const char* GetVersion(); // get the compiled version string e.g. "1.80 WIP" (essentially the value for IMGUI_VERSION from the compiled version of imgui.cpp)

IMGUI_API void UpdateFontDemo();
IMGUI_API void ShowFontDemoWindow();

// Styles
Expand Down
102 changes: 70 additions & 32 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7702,14 +7702,25 @@ static const char* fonts[] =
"ProggyTiny.ttf",
};

static int selected_font_index = -1;
static float selected_font_size = -1.0f;
static int font_index = 0;
static float font_size = 16.0f;
struct FontDemoState
{
int selected_font_index = -1;
float selected_font_size = -1.0f;
int font_index = 0;
float font_size = 16.0f;
};

static FontDemoState font_demo_state;
static FontDemoState font_demo_state2;

void ImGui::UpdateFontDemo()
static void UpdateFontDemo(FontDemoState& state)
{
if (selected_font_index >= 0 || selected_font_size > 0.0f)
int& selected_font_index = state.selected_font_index;
float& selected_font_size = state.selected_font_size;
int& font_index = state.font_index;
float& font_size = state.font_size;

//if (selected_font_index >= 0 || selected_font_size > 0.0f)
{
ImGuiIO& io = ImGui::GetIO();

Expand Down Expand Up @@ -7742,6 +7753,53 @@ void ImGui::UpdateFontDemo()
io.Fonts->Build();
io.Fonts->Locked = true; // #thedmd: remove this
ImGui::SetCurrentFont(ImGui::GetDefaultFont());
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->_CmdHeader.Texture = ImTexture(io.Fonts);
draw_list->_OnChangedTextureID();

draw_list = ImGui::GetOverlayDrawList();
draw_list->_CmdHeader.Texture = ImTexture(io.Fonts);
draw_list->_OnChangedTextureID();

draw_list = ImGui::GetBackgroundDrawList();
draw_list->_CmdHeader.Texture = ImTexture(io.Fonts);
draw_list->_OnChangedTextureID();
}
}

static void ShowFontDemo(FontDemoState& state)
{
int& selected_font_index = state.selected_font_index;
float& selected_font_size = state.selected_font_size;
int& font_index = state.font_index;
float& font_size = state.font_size;

UpdateFontDemo(state);

if (ImGui::BeginCombo("Font", fonts[font_index]))
{
for (int i = 0; i < IM_ARRAYSIZE(fonts); ++i)
{
if (ImGui::Selectable(fonts[i]))
selected_font_index = i;
}

ImGui::EndCombo();
}

float size = font_size;
if (ImGui::SliderFloat("Size", &size, 6.0f, 48.0f))
selected_font_size = size;

ImGuiIO& io = ImGui::GetIO();
ImFontAtlas* atlas = io.Fonts;
//if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight))
ImGui::Text("Atlas texture (%dx%d pixels)", atlas->TexData.TexWidth, atlas->TexData.TexHeight);
{
ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f);
ImGui::Image(ImTexture(atlas), ImVec2((float)atlas->TexData.TexWidth, (float)atlas->TexData.TexHeight), ImVec2(0, 0), ImVec2(1, 1), tint_col, border_col);
//ImGui::TreePop();
}
}

Expand All @@ -7758,33 +7816,13 @@ void ImGui::ShowFontDemoWindow()
if (io.BackendRendererName)
ImGui::TextUnformatted(io.BackendRendererName);

if (ImGui::BeginCombo("Font", fonts[font_index]))
{
for (int i = 0; i < IM_ARRAYSIZE(fonts); ++i)
{
if (ImGui::Selectable(fonts[i]))
selected_font_index = i;
}

ImGui::EndCombo();
}

float size = font_size;
if (ImGui::SliderFloat("Size", &size, 6.0f, 48.0f))
selected_font_size = size;

//ImGuiIO& io = ImGui::GetIO();
ImFontAtlas* atlas = io.Fonts;
//if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight))
ImGui::Text("Atlas texture (%dx%d pixels)", atlas->TexData.TexWidth, atlas->TexData.TexHeight);
{
ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f);
ImGui::Image(ImTexture(atlas), ImVec2((float)atlas->TexData.TexWidth, (float)atlas->TexData.TexHeight), ImVec2(0, 0), ImVec2(1, 1), tint_col, border_col);
//ImGui::TreePop();
}
ImGui::PushID(0);
ShowFontDemo(font_demo_state);
ImGui::PopID();

UpdateFontDemo();
ImGui::PushID(1);
ShowFontDemo(font_demo_state2);
ImGui::PopID();

ImGui::End();
}
Expand Down

0 comments on commit cfa79ac

Please sign in to comment.