thedmd / imgui-node-editor

Node Editor built using Dear ImGui
MIT License
3.67k stars 539 forks source link

How to use index 0 as a node ID? #188

Closed dontweaks closed 1 year ago

dontweaks commented 1 year ago

Is there a way to use index 0 as a node ID? Right now, if one of my nodes has an index of 0 it breaks the usability. Same thing is for links and pins.

Example: The "Multiplier" node moves when I hold LMB even if it is not selected. Instead, a selection rectangle should be drawn:

https://user-images.githubusercontent.com/83558889/204521694-5b72d4ee-a245-4cee-bcf1-10694f246b39.mp4

The "Multiplier" node id is 0, and the following nodes' ids are 1 and 2. When the "Multiplier" node id is 1 and the following nodes' ids are 2 and 3 it behaves as expected.

https://user-images.githubusercontent.com/83558889/204522834-d4777d0a-b188-40fa-ae06-601855e5baa9.mp4

paxcut commented 1 year ago

Id zero is reserved for the background. Turn on the metrics to verify this. Since everything is ultimately an object the node with id 0 is treated as the background. Paxcut

thedmd commented 1 year ago

Paxcut is correct. 0 is reserved and act like invalid id. Internally it is used in handling input for background.

Any ID you encounter in node editor will follow this rule. Perhaps I should define explicit constants in header to avoid confusion in the future.

dontweaks commented 1 year ago

:/ I thought so too. Wanted to make sure, though. I wanted to use 0 as an index in an array of nodes. I could've used index + 1 for an id and index - 1 to get out the index into the array, but I didn't like that approach.

Anyways, is there a variable like static uint32_t BACKGROUND_INPUT_ID = 0 and I can change it or it's like if(id == 0) then shit happens?

thedmd commented 1 year ago

Using 0 as na ID may break things in unexpected way. Code was not written with that possibility in mind. It might, but it might not work.

If you decide to follow the route of changing the background ID is as simple as changing this line (please don't mind the typo, will in Button, will fix later): https://github.com/thedmd/imgui-node-editor/blob/2f99b2d613a400f6579762bd7e7c343a0d844158/imgui_node_editor.cpp#L2497

Default value for Pin/Node/Link ID's need to change to. You can for example use (uintptr)-1 instead of 0: https://github.com/thedmd/imgui-node-editor/blob/2f99b2d613a400f6579762bd7e7c343a0d844158/imgui_node_editor.h#L471-L472

Does this works for you?


Zero is generally choosen as sentinel value in favor of (uintptr)-1 also for performance reasons. CPU's have plenty of instructions that checks for zero. Using anything else will yield compiler generating less efficient code.


Other ways to tackle this problem:

dontweaks commented 1 year ago

Great! That's what I was looking for. Thanks