From 9b743fd08110b4545638d03b7c99dd7d7a34d70a Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Tue, 15 Feb 2022 20:57:59 +0100 Subject: [PATCH] Links can be removed in graph editor. --- src/AnimGraphEditor.cc | 54 ++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/AnimGraphEditor.cc b/src/AnimGraphEditor.cc index 5ccc5e6..ccb6fce 100644 --- a/src/AnimGraphEditor.cc +++ b/src/AnimGraphEditor.cc @@ -7,8 +7,6 @@ #include "AnimGraphResource.h" #include "imnodes.h" -static AnimGraphResource gGraphResource; - ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { switch (socket_type) { case SocketType::SocketTypeAnimation: @@ -29,31 +27,33 @@ ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { } void AnimGraphEditorUpdate() { + static AnimGraphResource graph_resource = AnimGraphResource(); + ImGui::BeginMenuBar(); if (ImGui::Button("Save")) { - gGraphResource.saveToFile("editor_graph.json"); + graph_resource.saveToFile("editor_graph.json"); } if (ImGui::Button("Load")) { - gGraphResource.loadFromFile("editor_graph.json"); + graph_resource.loadFromFile("editor_graph.json"); - for (size_t i = 0, n = gGraphResource.m_nodes.size(); i < n; i++) { - const AnimNodeResource& node_resource = gGraphResource.m_nodes[i]; + for (size_t i = 0, n = graph_resource.m_nodes.size(); i < n; i++) { + const AnimNodeResource& node_resource = graph_resource.m_nodes[i]; ImNodes::SetNodeGridSpacePos( i, ImVec2(node_resource.m_position[0], node_resource.m_position[1])); } } if (ImGui::Button("Clear")) { - gGraphResource.clear(); + graph_resource.clear(); } char graph_name_buffer[256]; memset(graph_name_buffer, 0, sizeof(graph_name_buffer)); strncpy( graph_name_buffer, - gGraphResource.m_name.c_str(), + graph_resource.m_name.c_str(), sizeof(graph_name_buffer)); if (ImGui::InputText("Name", graph_name_buffer, sizeof(graph_name_buffer))) { - gGraphResource.m_name = graph_name_buffer; + graph_resource.m_name = graph_name_buffer; } ImGui::EndMenuBar(); @@ -90,9 +90,9 @@ void AnimGraphEditorUpdate() { if (node_type_name != "") { AnimNodeResource node_resource = AnimNodeResourceFactory(node_type_name); - size_t node_id = gGraphResource.m_nodes.size(); + size_t node_id = graph_resource.m_nodes.size(); ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos()); - gGraphResource.m_nodes.push_back(node_resource); + graph_resource.m_nodes.push_back(node_resource); } ImGui::EndPopup(); @@ -102,9 +102,9 @@ void AnimGraphEditorUpdate() { } // Graph Output and Inputs - if (gGraphResource.m_nodes.size() > 0) { + if (graph_resource.m_nodes.size() > 0) { // Graph Output - AnimNodeResource& graph_output_node = gGraphResource.m_nodes[0]; + AnimNodeResource& graph_output_node = graph_resource.getGraphOutputNode(); ImNodes::BeginNode(0); // Header @@ -122,6 +122,7 @@ void AnimGraphEditorUpdate() { sGetSocketShapeFromSocketType(socket.m_type), ImColor(255, 255, 255, 255)); ImGui::Text(socket.m_name.c_str()); + ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick); ImNodes::EndInputAttribute(); } @@ -141,7 +142,7 @@ void AnimGraphEditorUpdate() { ImNodes::EndNode(); // Graph Input - AnimNodeResource& graph_input_node = gGraphResource.m_nodes[1]; + AnimNodeResource& graph_input_node = graph_resource.getGraphInputNode(); ImNodes::BeginNode(1); // Header @@ -159,6 +160,7 @@ void AnimGraphEditorUpdate() { sGetSocketShapeFromSocketType(socket.m_type), ImColor(255, 255, 255, 255)); ImGui::Text(socket.m_name.c_str()); + ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick); ImNodes::EndInputAttribute(); } @@ -178,8 +180,8 @@ void AnimGraphEditorUpdate() { ImNodes::EndNode(); } - for (size_t i = 0, n = gGraphResource.m_nodes.size(); i < n; i++) { - AnimNodeResource& node_resource = gGraphResource.m_nodes[i]; + for (size_t i = 2, n = graph_resource.m_nodes.size(); i < n; i++) { + AnimNodeResource& node_resource = graph_resource.m_nodes[i]; ImNodes::BeginNode(i); @@ -198,6 +200,8 @@ void AnimGraphEditorUpdate() { sGetSocketShapeFromSocketType(socket.m_type), ImColor(255, 255, 255, 255)); ImGui::Text(socket.m_name.c_str()); + + ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick); ImNodes::EndInputAttribute(); } @@ -211,6 +215,7 @@ void AnimGraphEditorUpdate() { sGetSocketShapeFromSocketType(socket.m_type), ImColor(255, 255, 255, 255)); ImGui::Text(socket.m_name.c_str()); + ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick); ImNodes::EndInputAttribute(); } @@ -221,8 +226,8 @@ void AnimGraphEditorUpdate() { ImNodes::EndNode(); } - for (size_t i = 0, n = gGraphResource.m_connections.size(); i < n; i++) { - const AnimGraphConnection& connection = gGraphResource.m_connections[i]; + for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) { + const AnimGraphConnection& connection = graph_resource.m_connections[i]; int start_attr, end_attr; start_attr = GenerateOutputAttributeId( connection.m_source_node_index, @@ -235,6 +240,7 @@ void AnimGraphEditorUpdate() { ImNodes::EndNodeEditor(); + // Handle newly created links. int start_attr, end_attr; if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) { int node_start_id; @@ -248,16 +254,18 @@ void AnimGraphEditorUpdate() { int node_end_input_index; SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index); - std::cout << "Link created: " << node_start_id << ", " - << node_start_output_index << " -> " << node_end_id << ", " - << node_end_input_index << std::endl; - AnimGraphConnection connection; connection.m_source_node_index = node_start_id; connection.m_source_socket_index = node_start_output_index; connection.m_target_node_index = node_end_id; connection.m_target_socket_index = node_end_input_index; - gGraphResource.m_connections.push_back(connection); + graph_resource.m_connections.push_back(connection); + } + + // Handle link detachements. + int link_id = 0; + if (ImNodes::IsLinkDestroyed(&link_id)) { + graph_resource.m_connections.erase(graph_resource.m_connections.begin() + link_id); } } \ No newline at end of file