Nelarius / imnodes

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

Callback on node translation #168

Open kiranshila opened 1 year ago

kiranshila commented 1 year ago

Hey! First off, thanks for the great library!

I'm storing node data, including the positions in my own data structure and am a little stuck on how to update my representation of the positions.

When I draw the editor, I'm calling ImNodes::SetNodeScreenSpacePosition from a "realized" graph that contains its positions. Something in the background must be overwriting this behavior as I can drag a node around, but once I release a node - it of course snaps back to the position stored in my data structure.

My question is, how can I intercept the result of node translation in order to update my representation? I tried something the likes of

int num_selected_nodes = ImNodes::NumSelectedNodes();
if (num_selected_nodes > 0 && ImGui::IsMouseReleased(0)) {
  int *mod_ids = (int *)malloc(num_selected_nodes * sizeof(int));
  ImNodes::GetSelectedNodes(mod_ids);
  for (int i = 0; i < num_selected_nodes; i++) {
    int mod_id = mod_ids[i];
    ImVec2 pos = ImNodes::GetNodeScreenSpacePos(mod_id);
    rs::set_module_position(mod_id, pos[0], pos[1]);
  }
  free(mod_ids);
}

But GetNodeScreenSpacePos doesn't seem to be set by the time my if block runs, as it still contains the original position.

Any help would be appreciated.

kiranshila commented 1 year ago

Ah, it seems the positioning of SetNodeScreenSpace matters here. If I set the position before the call to BeginNode I can no longer move nodes around

xahon commented 1 year ago

@kiranshila I just encountered the same problem as well, you can test my implementation of this feature It works in my project, I'm able now to save positions of nodes between save/load sessions If you have any questions on it, feel free to ask