From a79ffaa420af8581bc6fe0df714bdf58eea0d881 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 3 Mar 2024 20:20:24 +0100 Subject: [PATCH] WIP: new anim graph editor can show loaded graph. --- src/AnimGraph/AnimGraphEditor.cc | 89 ++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/src/AnimGraph/AnimGraphEditor.cc b/src/AnimGraph/AnimGraphEditor.cc index ba6a6bf..f94cb86 100644 --- a/src/AnimGraph/AnimGraphEditor.cc +++ b/src/AnimGraph/AnimGraphEditor.cc @@ -14,6 +14,7 @@ #include "misc/cpp/imgui_stdlib.h" static AnimGraphResource sGraphGresource = AnimGraphResource(); +static bool sGraphLoadedThisFrame = false; ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { switch (socket_type) { @@ -36,6 +37,14 @@ ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { return ImNodesPinShape_Quad; } +int GetNodeInputSocketId(int node_index, int input_socket_index) { + return node_index * 1000 + input_socket_index; +} + +int GetNodeOutputSocketId(int node_index, int output_socket_index) { + return node_index * 1000 + 100 + output_socket_index; +} + void NodeSocketEditor(Socket& socket) { int mode_current = static_cast(socket.m_type); ImGui::InputText("Name", &socket.m_name); @@ -181,9 +190,7 @@ void AnimGraphEditorRenderSidebar( 100.f); } else if (property.m_type == SocketType::SocketTypeBool) { bool flag_value = property.GetValue(); - if (ImGui::Checkbox( - property.m_name.c_str(), - &flag_value)) { + if (ImGui::Checkbox(property.m_name.c_str(), &flag_value)) { property.SetValue(flag_value); } } else if (property.m_type == SocketType::SocketTypeString) { @@ -254,13 +261,14 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { } if (ImGui::Button("Load")) { sGraphGresource.loadFromFile("editor_graph.json"); + sGraphLoadedThisFrame = true; -// for (size_t i = 0, n = sGraphGresource.m_nodes.size(); i < n; i++) { -// const AnimNodeResource& node_resource = sGraphGresource.m_nodes[i]; -// ImNodes::SetNodeGridSpacePos( -// i, -// ImVec2(node_resource.m_position[0], node_resource.m_position[1])); -// } + // for (size_t i = 0, n = sGraphGresource.m_nodes.size(); i < n; i++) { + // const AnimNodeResource& node_resource = sGraphGresource.m_nodes[i]; + // ImNodes::SetNodeGridSpacePos( + // i, + // ImVec2(node_resource.m_position[0], node_resource.m_position[1])); + // } } if (ImGui::Button("Clear")) { sGraphGresource.clear(); @@ -283,6 +291,7 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { ax::NodeEditor::SetCurrentEditor(context); ax::NodeEditor::Begin("Graph Editor"); +#if 1 for (size_t node_id = 0, n = sGraphGresource.m_nodes.size(); node_id < n; node_id++) { AnimNodeResource& node_resource = sGraphGresource.m_nodes[node_id]; @@ -291,7 +300,11 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { // continue; } - + if (sGraphLoadedThisFrame) { + ax::NodeEditor::SetNodePosition( + node_id, + ImVec2(node_resource.m_position[0], node_resource.m_position[1])); + } ax::NodeEditor::BeginNode(node_id); ImGui::Text("%s", node_resource.m_type_name.c_str()); @@ -300,19 +313,21 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { node_resource.m_socket_accessor->m_inputs; for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) { Socket& socket = node_inputs[j]; - int pin_id = static_cast(node_id) + j * 1000; - ax::NodeEditor::BeginPin(pin_id, ax::NodeEditor::PinKind::Input); + ax::NodeEditor::BeginPin( + GetNodeInputSocketId(static_cast(node_id), static_cast(j)), + ax::NodeEditor::PinKind::Input); ImGui::Text("%s", socket.m_name.c_str()); ax::NodeEditor::EndPin(); } // Outputs - const std::vector& node_outputs = + std::vector& node_outputs = node_resource.m_socket_accessor->m_outputs; for (size_t j = 0, ni = node_outputs.size(); j < ni; j++) { - const Socket& socket = node_outputs[j]; - int pin_id = static_cast(node_id) + j * 1000 * 1000; - ax::NodeEditor::BeginPin(pin_id, ax::NodeEditor::PinKind::Output); + Socket& socket = node_outputs[j]; + ax::NodeEditor::BeginPin( + GetNodeOutputSocketId(static_cast(node_id), static_cast(j)), + ax::NodeEditor::PinKind::Output); ImGui::Text("%s", socket.m_name.c_str()); ax::NodeEditor::EndPin(); } @@ -320,37 +335,28 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { ax::NodeEditor::EndNode(); } -#if 0 - int unique_id = 1; - ax::NodeEditor::BeginNode(unique_id++); + int link_id = 0; + for (size_t connection_id = 0, n = sGraphGresource.m_connections.size(); connection_id < n; + connection_id++) { + const AnimGraphConnectionResource& connection_resource = sGraphGresource.m_connections[connection_id]; - // Node A - ImGui::Text("Node A"); - ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Input); - ImGui::Text("In"); - ax::NodeEditor::EndPin(); - ImGui::SameLine(); - ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Output); - ImGui::Text("Out"); - ax::NodeEditor::EndPin(); + const AnimNodeResource& source_node_resource = sGraphGresource.m_nodes[connection_resource.source_node_index]; + int source_socket_index = source_node_resource.m_socket_accessor->GetOutputIndex(connection_resource.source_socket_name.c_str()); - ax::NodeEditor::EndNode(); + const AnimNodeResource& target_node_resource = sGraphGresource.m_nodes[connection_resource.target_node_index]; + int target_socket_index = target_node_resource.m_socket_accessor->GetInputIndex(connection_resource.target_socket_name.c_str()); - // Node B - ax::NodeEditor::BeginNode(unique_id++); + int source_socket_id = GetNodeOutputSocketId(static_cast(connection_resource.source_node_index), source_socket_index); + int target_socket_id = GetNodeInputSocketId(static_cast(connection_resource.target_node_index), target_socket_index); + + ax::NodeEditor::Link(link_id++, source_socket_id, target_socket_id); + } - ImGui::Text("Node B"); - ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Input); - ImGui::Text("In"); - ax::NodeEditor::EndPin(); - ImGui::SameLine(); - ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Output); - ImGui::Text("Out"); - ax::NodeEditor::EndPin(); - ax::NodeEditor::EndNode(); #endif + +#if 1 // Create Connections if (ax::NodeEditor::BeginCreate()) { ax::NodeEditor::PinId input_pin_id, output_pin_id; @@ -362,9 +368,12 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { } } ax::NodeEditor::EndCreate(); +#endif ax::NodeEditor::End(); + sGraphLoadedThisFrame = false; + ax::NodeEditor::SetCurrentEditor(nullptr); }