Fattorino / ImNodeFlow

Node based editor/blueprints for ImGui
MIT License
157 stars 20 forks source link

Removing Nodes #10

Closed avlec closed 7 months ago

avlec commented 7 months ago

Ability to add/remove nodes from the Node editor via a removeNode interface.

I was playing around with how easy it would be. I've got it just searching, finding, and erasing the node from the ImNodeFlow context. Works fine if there is no connections but runs into some UB when the next update loop comes through and uses the deleted Pin in the Link. Haven't gotten further than a quick debugger to take a quick peek at what is going wrong. I'll put up a branch and continue off it when my cold goes away.

Fattorino commented 7 months ago

Sorry I forgot to make it an issue but earlier today I added it to the todo list https://github.com/users/Fattorino/projects/3?pane=issue&itemId=53394186 And probably I'll be done by tonight. The idea is to have methods like findNode(uid) and dropNode(uid) to get a reference or delete a node.

avlec commented 7 months ago

This is what I was working on https://github.com/avlec/ImNodeFlow/tree/remove-node the idea being you remove via name OR by the pointer you got via addNode

Fattorino commented 7 months ago

The problem is that two Nodes can have the same name, so I implemented a NodeUID system similar to the PinUID one, and a simple std::unordered_map. Now I am working on how I can provide helpers to quickly create new unique UIDs.

Fattorino commented 7 months ago

This is what I have done so far, it works with connected nodes too, but I'm not happy that it's hard to create unique uids for each pin and then use it to find a node without a lot of user-side logic. I need to find a way to create a few helpers to make the process smoother and faster on the user side. 2b4c526 If you have any ideas let me know.

Fattorino commented 7 months ago

While scratching my head trying to figure out a UID system for the nodes, I modified how nodes are defined. Now the creation of a node looks something like this:

class CoolNode : public BaseNode
{
public:
    CoolNode() // No more long and ugly constructor
    {
        setTitle("I'm cool!");
        getStyle() = NodeStyle::brown();
        getStyle()->padding.w = 16.f;

        auto big = PinStyle::green();
        big->socket_radius = 10.f;
        addIN<int>("Cool", 0, 0, PinStyle::red());
        addOUT<int>("Node", 0, big)->behaviour([](){return 0;});
    }
};

image

The style and title can be overridden when adding the node to the grid addNode(pos, title, style); And of course, after that, you can forward all other optional parameters.

avlec commented 7 months ago

If you can allocate the memory for the nodes then use the pointer to the node as the UID would that work?