ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.66k stars 10.16k forks source link

GetContentRegionAvail() returns incorrect value at first frame when window is docked by default #4823

Open hls333555 opened 2 years ago

hls333555 commented 2 years ago

Version/Branch of Dear ImGui:

Version: 1.86 Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp Compiler: MSVC2022 Operating System: XXX

My Issue/Question: If I have a window docked initially by calling ImGui::DockBuilderDockWindow, then ImGui::GetContentRegionAvail().x will return an incorrect value when first called, its value will become correct after the first frame call. The above issue only happens if the window is docked at startup, if I disable the initial docking or delete the saved ini file, the value is correct. This issue does not exist in the previous imgui versions.

ImGui::Begin("Test");
// The first frame in my condition, the value is 26, the second frame and later, the value is 758
float value = ImGui::GetContentRegionAvail().x;
ImGui::End();
ocornut commented 2 years ago

Hello, I'll have to check but at the very least it would probably happen if the Dock Node the window is on was just created using DockBuilderXXX api and you didn't specify a size for it. Could you confirm the exact flow?

(It's also possible there's a more general issue that would happen even if the dock node already know its size.)

hls333555 commented 2 years ago

Hello, I'll have to check but at the very least it would probably happen if the Dock Node the window is on was just created using DockBuilderXXX api and you didn't specify a size for it. Could you confirm the exact flow?

(It's also possible there's a more general issue that would happen even if the dock node already know its size.)

Hi, I'm using something like this:

ImGuiID dockspaceID = ImGui::GetID(editorName.c_str());
if (ImGui::DockBuilderGetNode(dockspaceID) == nullptr || m_bShouldRebuildDockLayout)
{
    m_bShouldRebuildDockLayout = false;

    // Clear out existing layout
    ImGui::DockBuilderRemoveNode(dockspaceID);
    // Add empty node
    ImGui::DockBuilderAddNode(dockspaceID, ImGuiDockNodeFlags_DockSpace);
    // Main node should cover entire window
    ImGui::DockBuilderSetNodeSize(dockspaceID, ImGui::GetWindowSize());
    // Build dock layout
    ImGuiID dockLeft;
        ImGuiID dockRight = ImGui::DockBuilderSplitNode(dockspaceID, ImGuiDir_Right, 0.2f, nullptr, &dockLeft);
        ImGuiID dockRightDown;
        ImGuiID dockRightUp = ImGui::DockBuilderSplitNode(dockRight, ImGuiDir_Up, 0.4f, nullptr, &dockRightDown);
        ImGuiID dockLeftUp;
        ImGuiID dockLeftDown = ImGui::DockBuilderSplitNode(dockLeft, ImGuiDir_Down, 0.3f, nullptr, &dockLeftUp);
        ImGuiID dockLeftUpRight;
        ImGuiID dockLeftUpLeft = ImGui::DockBuilderSplitNode(dockLeftUp, ImGuiDir_Left, 0.2f, nullptr, &dockLeftUpRight);
        ImGuiID dockLeftDownRight;
        ImGuiID dockLeftDownLeft = ImGui::DockBuilderSplitNode(dockLeftDown, ImGuiDir_Left, 0.5f, nullptr, &dockLeftDownRight);

        ImGui::DockBuilderDockWindow("###" LEVEL_EDITOR_VIEW, dockLeftUpRight);
        ImGui::DockBuilderDockWindow(SCENE_OUTLINE, dockRightUp);
        ImGui::DockBuilderDockWindow(ENTITY_INSPECTOR, dockRightDown);
        ImGui::DockBuilderDockWindow(CONTENT_BROWSER, dockLeftDownLeft);
        ImGui::DockBuilderDockWindow(CONSOLE, dockLeftDownRight);
    ImGui::DockBuilderFinish(dockspaceID);
}

ImGui::DockSpace(dockspaceID, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_PassthruCentralNode);
hls333555 commented 2 years ago

Hi, this issue seems still exist in ImGui 1.87 docking branch. Is there any update on this? Thanks a lot!

vdweller84 commented 1 year ago

Same for v 1.90. This causes issues when some values are calculated only once the first time a window with docked sub-windows is displayed. While this can be probably circumvented by waiting for one frame, I'd appreciate if there is any update on whether this is expected behavior or a glitch. Splitting a node and then setting each new node's new size doesn't seem to give accurate results.

ocornut commented 1 year ago

Calculating something that has to do with GetContentRegionAvail() "once the first a window with docked sub-windows is displayed" sounds wrong. What would happen if any window or the host platform gets resized?