rokups / ImNodes

Node graph implementation for Dear ImGui. Used in https://github.com/rokups/rbfx
MIT License
668 stars 57 forks source link

Position of loaded nodes from file is not consistent/correct #32

Closed flamendless closed 3 years ago

flamendless commented 3 years ago

I'm saving my nodes to an .ini file. Here's a screenshot before saving:

2021-03-25_22-10

And here's the content of the .ini saved file:

[node_0]
name = a
kind = VARIABLE
x = 1291.000000
y = 205.000000
value_slot = INTEGER
value = 3
input_1 = INTEGER
output_1 = INTEGER

[node_1]
name = b
kind = VARIABLE
x = 1740.000000
y = 203.000000
value_slot = INTEGER
value = 8
input_1 = INTEGER
output_1 = INTEGER

But everytime I load the node from the ini file. I get inconsistent results. Here are sample of loading them: 2021-03-25_22-11

2021-03-25_22-11_1

For more information, here's a snippet how I save/load the node to the file:

//saving
for (Node* node : Nodes::v_nodes) //std::vector<Node*>
{
                //struct Node { ImVec2 m_pos }; //just like in the sample.cpp
        std::string str_x = std::to_string(node->m_pos.x);
        std::string str_y = std::to_string(node->m_pos.y);
        ini.SetValue(section, "x", str_x.c_str());
        ini.SetValue(section, "y", str_y.c_str());
}
//loading
const float x = std::stof(ini.GetValue(section, "x"));
const float y = std::stof(ini.GetValue(section, "y"));

Node* node = new Node();
node->m_pos = ImVec2(x, y);
Nodes::v_nodes.push_back(node);
//ImNodes::AutoPositionNode(node); //cant do this as there is no context yet
Nodes::first_load = true;

//in render
ImNodes::BeginCanvas(canvas);
if (Nodes::first_load)
{
        for (Node* node : Nodes::v_nodes)
            ImNodes::AutoPositionNode(node);

        Nodes::first_load = false;
}
ImNodes::EndCanvas();
flamendless commented 3 years ago

After poking around, I've found out that it is unnecessary to call AutoPositionNode on loaded nodes since the position saved in the config is enough. So no need for:

if (Nodes::first_load)
{
        for (Node* node : Nodes::v_nodes)
            ImNodes::AutoPositionNode(node);

        Nodes::first_load = false;
}
rokups commented 3 years ago

Good to know its not a bug after all!