thedmd / imgui-node-editor

Node Editor built using Dear ImGui
MIT License
3.48k stars 527 forks source link

Can I hook into the creation of Links and Nodes? #249

Closed niclash closed 10 months ago

niclash commented 11 months ago

I have browsed the source code to try and figure out if there is a way to have my own "hook" in the create process.

This is what I want to do;

  1. User tries to create a Link from PinA to PinB,
  2. My application gets notified and sends the CreateLink request to my backend.
  3. The application will "abort" to the UI-driven editor.
  4. Eventually the backend will send a "LinkCreated" event, and my application calls "create link" in the editor.

The same goes for Nodes, as well as for deletions and any other changes (position, name,...).

I can't locate any code that seems to allow for this.

Is this possible in current state of the imgui-node-editor ?

thedmd commented 10 months ago

Yes. The intended mechanism to create new links is creation query.

if (ed::BeginCreate())
{
    ed::PinId startPinId = 0, endPinId = 0;
    if (ed::QueryNewLink(&startPinId, &endPinId))
    {
        if (<test-if-connection-between-startPinId-and-endPinId-is-valid)
        {
            ed::AcceptNewItem(); // tell editor that new link will be created
            // create new link
        }
        else
        {
            ed::RejectNewItem();
        }
    }

    ed::EndCreate();
}

Whole process is better described in basic-interaction-example: https://github.com/thedmd/imgui-node-editor/blob/6e1cadefc9840a5246cbf413fa38a9159cdc0942/examples/basic-interaction-example/basic-interaction-example.cpp#L121-L157