Imgui: How to specify window content region?

Created on 10 Dec 2017  路  3Comments  路  Source: ocornut/imgui

Hi,

First of all, I would like to thank you for a great library. It is truly awesome.

Now the question: consider I want to make a minimap. So I'm preparing the texture with rendered minimap (say, 200x200 px). Now I create a window and draw the minimap. The problem is that I can't just create a window of 200x200 size, since available content region will be smaller. I can, of course, ballpark the parameters to get the desired size, but window is not of the fixed size, that's the problem. Simple example to illustrate the problem

ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_FirstUseEver);

ImDrawList* draw_list = ImGui::GetWindowDrawList();
{
    ImVec2 canvas_pos  = ImGui::GetCursorScreenPos();
    ImVec2 canvas_size = ImGui::GetContentRegionAvail();

    ...
    draw_lsit->AddImage(...);
    ...
}

Here, canvas_size will be of size 184x165 (or something like that), where I want it to be 200x200. I've tried ImGui::SetNextWindowContentSize with 200x200, but with no luck. Is there a way to make a window with specific size of the content region? Thanks.

bug layout

Most helpful comment

@BlackWatersInc I also pushed a fix to SetNextWindowContentSize() (which until now I mostly used with child window).

A few things to know:

  • When auto-sizing we add WindowPadding. If you want a 200x200 window exactly without padding, you'll set window padding to zero.
  • Until now I always made borders _not_ affect layout, aka they overlap whatever they can but don't affect positioning. As a result currently if you want a window with visible border but with zero window padding you've got a problem where the border with be drawn over your content. You can either request a bigger size to take account of the borders (which are style.WindowBorderSize thick), or make sure that style.WindowPadding >= style.WindowBorderSize.
    I may have to revisit those things again as the whole "contents size" thing is a little of a mess at the moment.
ImGui::SetNextWindowContentSize(ImVec2(200,200));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
static bool menu = true;
ImGui::Begin("Test #1490, v2", NULL, ImGuiWindowFlags_AlwaysAutoResize | (menu ? ImGuiWindowFlags_MenuBar : 0));
ImVec2 p1 = ImGui::GetCursorScreenPos();
ImVec2 p2 = ImVec2(p1.x + 200, p1.y + 200);
ImGui::Checkbox("Menu", &menu);
ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255,0,255,255));
ImGui::GetWindowDrawList()->AddCircleFilled(p1, 6.0f, IM_COL32(255, 0, 255, 255));
ImGui::GetWindowDrawList()->AddCircleFilled(p2, 6.0f, IM_COL32(255, 0, 255, 255));
ImGui::End();
ImGui::PopStyleVar();

image

All 3 comments

It looks like a bug with SetNextWindowContentSize(), will investigate.

You can also use that in the meanwhile:
(edited)

        ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
        ImGui::Begin("Test #1490", NULL, ImGuiWindowFlags_AlwaysAutoResize);
        ImVec2 p = ImGui::GetCursorScreenPos();
        ImGui::SetCursorScreenPos(ImVec2(p.x + 200, p.y + 200));
        ImGui::End();
        ImGui::PopStyleVar();

@BlackWatersInc I also pushed a fix to SetNextWindowContentSize() (which until now I mostly used with child window).

A few things to know:

  • When auto-sizing we add WindowPadding. If you want a 200x200 window exactly without padding, you'll set window padding to zero.
  • Until now I always made borders _not_ affect layout, aka they overlap whatever they can but don't affect positioning. As a result currently if you want a window with visible border but with zero window padding you've got a problem where the border with be drawn over your content. You can either request a bigger size to take account of the borders (which are style.WindowBorderSize thick), or make sure that style.WindowPadding >= style.WindowBorderSize.
    I may have to revisit those things again as the whole "contents size" thing is a little of a mess at the moment.
ImGui::SetNextWindowContentSize(ImVec2(200,200));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
static bool menu = true;
ImGui::Begin("Test #1490, v2", NULL, ImGuiWindowFlags_AlwaysAutoResize | (menu ? ImGuiWindowFlags_MenuBar : 0));
ImVec2 p1 = ImGui::GetCursorScreenPos();
ImVec2 p2 = ImVec2(p1.x + 200, p1.y + 200);
ImGui::Checkbox("Menu", &menu);
ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255,0,255,255));
ImGui::GetWindowDrawList()->AddCircleFilled(p1, 6.0f, IM_COL32(255, 0, 255, 255));
ImGui::GetWindowDrawList()->AddCircleFilled(p2, 6.0f, IM_COL32(255, 0, 255, 255));
ImGui::End();
ImGui::PopStyleVar();

image

Wow, the fix was delivered really _fast_. Impressive. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

the-lay picture the-lay  路  3Comments

dowit picture dowit  路  3Comments

Folling picture Folling  路  3Comments

ILoveImgui picture ILoveImgui  路  3Comments

mkanakis picture mkanakis  路  3Comments