Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockspace windows sizes with fixed ratios #6095

Closed
Grieverheart opened this issue Jan 18, 2023 · 4 comments
Closed

Dockspace windows sizes with fixed ratios #6095

Grieverheart opened this issue Jan 18, 2023 · 4 comments

Comments

@Grieverheart
Copy link

Grieverheart commented Jan 18, 2023

Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_sdl2.cpp + imgui_impl_opengl3.cpp
Compiler: Apple clang version 12.0.5 (clang-1205.0.22.11)
Operating System: MacOS

My Issue/Question:

I'm trying to make the windows docked in docking space have fixed size ratios when resizing, like in #5209. This is so that I can create a viewport with multiple views of a scene. Now if I understand correctly, this is currently not possible, and dear imgui handles the sizing differently. From #5209 I used the code provided to make a resizable dockspace that keeps the ratios fixed. This is done by rebuilding the dockspace whenever the window is resized:

    static ImVec2 window_size = {};

    static float RATIO_1_5 = 0.8f;
    static float RATIO_5_1 = 0.2f;
    static float RATIO_1_4 = 0.25;

    const ImGuiViewport* viewport = ImGui::GetMainViewport();
    ImGui::SetNextWindowViewport(viewport->ID);
    ImGui::SetNextWindowPos(viewport->WorkPos);
    ImGui::SetNextWindowSize(viewport->WorkSize);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));

    ImGuiWindowFlags window_flags =
        ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse |
        ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
        ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;

    ImGui::Begin("DSTestWindow", NULL, window_flags);
    {
        auto size = ImGui::GetWindowSize();
        auto cr = ImGui::GetContentRegionAvail();
        ImGui::PopStyleVar(3);
        ImGuiID dockspace_id = ImGui::GetID("DSTest");
        ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f));

        ImGui::Begin("Hierarchy");
        //auto s1 = ImGui::GetWindowSize();
        ImGui::End();
        ImGui::Begin("Inspector");
        //auto s2 = ImGui::GetWindowSize();
        ImGui::End();
        ImGui::Begin("View");
        //auto s3 = ImGui::GetWindowSize();
        ImGui::End();
        ImGui::Begin("Asset Browser");
        ImGui::End();
        //printf("%f, %f\n", s1.x+s2.x+s3.x, size.x);

        if(size.x != window_size.x || size.y != window_size.y)
        {
            ImGui::DockBuilderRemoveNode(dockspace_id);
            ImGui::DockBuilderAddNode(dockspace_id, ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_PassthruCentralNode);
            ImGui::DockBuilderSetNodeSize(dockspace_id, size);

            ImGuiID top_area_id = -1;
            auto browser_id = ImGui::DockBuilderSplitNode(
                dockspace_id, ImGuiDir_Down, RATIO_1_4, nullptr, &top_area_id
            );

            ImGuiID top_right_area_id = -1;
            auto hierarchy_id = ImGui::DockBuilderSplitNode(
                top_area_id, ImGuiDir_Left, RATIO_5_1, nullptr, &top_right_area_id
            );

            ImGuiID inspector_id = -1;
            auto view_id = ImGui::DockBuilderSplitNode(
                top_right_area_id, ImGuiDir_Left, RATIO_1_5, nullptr, &inspector_id
            );

            ImGui::DockBuilderDockWindow("Hierarchy", hierarchy_id);
            ImGui::DockBuilderDockWindow("Inspector", inspector_id);
            ImGui::DockBuilderDockWindow("View", view_id);
            ImGui::DockBuilderDockWindow("Asset Browser", browser_id);

            ImGui::DockBuilderFinish(dockspace_id);

            window_size = size;
        }
    }
    ImGui::End();

Like this, I can add two buttons for splitting the window either horizontally or vertically and keep track of the splits (kinda duplicating work unfortunately). Does this make sense to do it this way or is there an easier/more efficient way of achieving what I want?

Furthermore, I want the user to be able to modify the ratios by dragging. For this I need to any changes to the window sizes and rebuild the dockspace with the new ratios. Trying this out, I noticed that ImGui::GetWindowSize() does not give the size I expected, since taking the ratio with the containing window size, does not give the ratio of the split. I figured that this difference comes from the window divider. Is this correct? If so, is there a way to get the divider element size?

@ocornut
Copy link
Owner

ocornut commented Jan 19, 2023

At heart we should be able to enforce constraints to docked windows (#2849). Also see the toolbar hack in #2648.

Does this make sense to do it this way or is there an easier/more efficient way of achieving what I want?

Best to simply not do it ihmo.

Unfortunately there are many similar topics and until we can make meaningful steps forward with Docking, I myself cannot be working on figuring out further elaborate workarounds.

@Grieverheart
Copy link
Author

Thanks, I understand you have too much on your plate. I think I have found a satisfactory solution using the DockBuilder API now that I understand it better.

@WeakKnight
Copy link

Thanks, I understand you have too much on your plate. I think I have found a satisfactory solution using the DockBuilder API now that I understand it better.

Could you share the solution? I come with similar problems。

@Grieverheart
Copy link
Author

@WeakKnight It's basically understanding how that there is a 'central node' and that takes up the space that is left, while other docked windows keep a fixed size. For me this was enough. I created separate dock as my central node, and have windows docked on the sides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants