Nelarius / imnodes

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

Using a CollapsingHeader/treenode inside a node, it's ui goes out of bounds of the node; #167

Open bigbigzxl opened 1 year ago

bigbigzxl commented 1 year ago
  1. Using a CollapsingHeader/treenode inside a node, it's ui goes out of bounds of the node;
    ImNodes::BeginNode(node.id);
    {
    ImNodes::BeginStaticAttribute(node.ui.add.rhs);
    if (ImGui::CollapsingHeader("Filtering"))
    {
        ImGui::BulletText(
            "Sections below are demonstrating many aspects of the library.");
        ImGui::TextUnformatted("result");
    }
    ImNodes::EndStaticAttribute();
    }
    ImNodes::EndNode();

    image

hiroMTB commented 9 months ago

This can be solved by a quick dirty hack like below.

imnodes.cpp

int EndNode()
{
   ...

    ImNodeData& node = editor.Nodes.Pool[GImNodes->CurrentNodeIdx];
    node.Rect = GetItemRect();

    int width = node.Rect.GetWidth();

   ...    
    return width;
}

// your node implementation

#include <imgui_internal.h>

void drawMyNode(){
  // start your node
  ImNodes::BeginNode(id);

  ImGuiWindow* window = ImGui::GetCurrentWindow();
  ImRect backup = window->WorkRect;

  static nodeWidth = 100;  // default 100px

  // Here we overwrite window value
  window->WorkRect.Min.x = window->DC.CursorPos.x;
  window->WorkRect.Max.x = window->WorkRect.Min.x + nodeWidth;

  if(ImGui::CollapsingHeader("My Header", ImGuiTreeNodeFlags_DefaultOpen))
  {
       ImGui::Text("ABC");
  }
  window->WorkRect = backup;  // recover correct value
  ImNodes::EndNode();

  nodeWidth = w;
}

What we do with above code is ...

  1. first render node with 100px (or whatever) default width as we don't know the actual width
  2. modified version of EndNode() returns node width
  3. Overwrite window.WorkRect and put it back after CollapsingHeader.

Negative points are ...