Nelarius / imnodes

A small, dependency-free node editor for dear imgui
MIT License
1.97k stars 241 forks source link

Node positions reset when using tabs #110

Closed hoffstadt closed 3 years ago

hoffstadt commented 3 years ago

System Information

ImNodes 0.4
Dear ImGui 1.83
Operating System Windows 10
Graphics API Directx 11

Issue

Node positions are resetting after switching tabs that contain node editors. With multiple nodes, they all "pile up". See here.

Minimal Example

ImGui::Begin("Testing");

if (ImGui::BeginTabBar("Tab bar"))
{

    if (ImGui::BeginTabItem("Tab 1"))
    {

        imnodes::BeginNodeEditor();

        imnodes::BeginNode(53);
        ImGui::Dummy(ImVec2(80.0f, 45.0f));
        imnodes::EndNode();

        imnodes::EndNodeEditor();

        ImGui::EndTabItem();
    }

    if (ImGui::BeginTabItem("Tab 2"))
    {

        imnodes::BeginNodeEditor();

        imnodes::BeginNode(54);
        ImGui::Dummy(ImVec2(80.0f, 45.0f));
        imnodes::EndNode();

        imnodes::EndNodeEditor();

        ImGui::EndTabItem();
    }

    ImGui::EndTabBar();
}

ImGui::End();

Position Before Switching Tabs (moved manually)

image

Position after switching between tabs

image

Notes

yubeizhu commented 3 years ago

I also found this issue when using node editors in more than one window. The position of all nodes in their node editor are reset and nodes can't be moved.

Nelarius commented 3 years ago

Have you tried creating a separate ImNodesEditorContext instance for the tabs? This is unfortunately something which isn't documented particularly well, but editing node graphs in separate canvases requires the creation of a separate editor context instance. See examples/multi_editor.cpp for a an example on rendering two node graphs in separate ImGui windows. I think tabs should be supported as well.

hoffstadt commented 3 years ago

Awesome, this fixed it. Here is the solution for those that come across this:

        static auto context1 = imnodes::EditorContextCreate();
        static auto context2 = imnodes::EditorContextCreate();

        ImGui::Begin("Testing");

        if (ImGui::BeginTabBar("Tab bar"))
        {

            if (ImGui::BeginTabItem("Tab 1"))
            {

                imnodes::EditorContextSet(context1);
                imnodes::BeginNodeEditor();

                imnodes::BeginNode(53);
                ImGui::Dummy(ImVec2(80.0f, 45.0f));
                imnodes::EndNode();

                imnodes::EndNodeEditor();

                ImGui::EndTabItem();
            }

            if (ImGui::BeginTabItem("Tab 2"))
            {

                imnodes::EditorContextSet(context2);
                imnodes::BeginNodeEditor();

                imnodes::BeginNode(54);
                ImGui::Dummy(ImVec2(80.0f, 45.0f));
                imnodes::EndNode();

                imnodes::EndNodeEditor();

                ImGui::EndTabItem();
            }

            ImGui::EndTabBar();
        }

        ImGui::End();