Links can be removed in graph editor.

AnimGraphEditor
Martin Felis 2022-02-15 20:57:59 +01:00
parent 3e02f28b18
commit 9b743fd081
1 changed files with 31 additions and 23 deletions

View File

@ -7,8 +7,6 @@
#include "AnimGraphResource.h" #include "AnimGraphResource.h"
#include "imnodes.h" #include "imnodes.h"
static AnimGraphResource gGraphResource;
ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
switch (socket_type) { switch (socket_type) {
case SocketType::SocketTypeAnimation: case SocketType::SocketTypeAnimation:
@ -29,31 +27,33 @@ ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
} }
void AnimGraphEditorUpdate() { void AnimGraphEditorUpdate() {
static AnimGraphResource graph_resource = AnimGraphResource();
ImGui::BeginMenuBar(); ImGui::BeginMenuBar();
if (ImGui::Button("Save")) { if (ImGui::Button("Save")) {
gGraphResource.saveToFile("editor_graph.json"); graph_resource.saveToFile("editor_graph.json");
} }
if (ImGui::Button("Load")) { 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++) { for (size_t i = 0, n = graph_resource.m_nodes.size(); i < n; i++) {
const AnimNodeResource& node_resource = gGraphResource.m_nodes[i]; const AnimNodeResource& node_resource = graph_resource.m_nodes[i];
ImNodes::SetNodeGridSpacePos( ImNodes::SetNodeGridSpacePos(
i, i,
ImVec2(node_resource.m_position[0], node_resource.m_position[1])); ImVec2(node_resource.m_position[0], node_resource.m_position[1]));
} }
} }
if (ImGui::Button("Clear")) { if (ImGui::Button("Clear")) {
gGraphResource.clear(); graph_resource.clear();
} }
char graph_name_buffer[256]; char graph_name_buffer[256];
memset(graph_name_buffer, 0, sizeof(graph_name_buffer)); memset(graph_name_buffer, 0, sizeof(graph_name_buffer));
strncpy( strncpy(
graph_name_buffer, graph_name_buffer,
gGraphResource.m_name.c_str(), graph_resource.m_name.c_str(),
sizeof(graph_name_buffer)); sizeof(graph_name_buffer));
if (ImGui::InputText("Name", graph_name_buffer, 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(); ImGui::EndMenuBar();
@ -90,9 +90,9 @@ void AnimGraphEditorUpdate() {
if (node_type_name != "") { if (node_type_name != "") {
AnimNodeResource node_resource = AnimNodeResource node_resource =
AnimNodeResourceFactory(node_type_name); 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()); ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
gGraphResource.m_nodes.push_back(node_resource); graph_resource.m_nodes.push_back(node_resource);
} }
ImGui::EndPopup(); ImGui::EndPopup();
@ -102,9 +102,9 @@ void AnimGraphEditorUpdate() {
} }
// Graph Output and Inputs // Graph Output and Inputs
if (gGraphResource.m_nodes.size() > 0) { if (graph_resource.m_nodes.size() > 0) {
// Graph Output // Graph Output
AnimNodeResource& graph_output_node = gGraphResource.m_nodes[0]; AnimNodeResource& graph_output_node = graph_resource.getGraphOutputNode();
ImNodes::BeginNode(0); ImNodes::BeginNode(0);
// Header // Header
@ -122,6 +122,7 @@ void AnimGraphEditorUpdate() {
sGetSocketShapeFromSocketType(socket.m_type), sGetSocketShapeFromSocketType(socket.m_type),
ImColor(255, 255, 255, 255)); ImColor(255, 255, 255, 255));
ImGui::Text(socket.m_name.c_str()); ImGui::Text(socket.m_name.c_str());
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::EndInputAttribute(); ImNodes::EndInputAttribute();
} }
@ -141,7 +142,7 @@ void AnimGraphEditorUpdate() {
ImNodes::EndNode(); ImNodes::EndNode();
// Graph Input // Graph Input
AnimNodeResource& graph_input_node = gGraphResource.m_nodes[1]; AnimNodeResource& graph_input_node = graph_resource.getGraphInputNode();
ImNodes::BeginNode(1); ImNodes::BeginNode(1);
// Header // Header
@ -159,6 +160,7 @@ void AnimGraphEditorUpdate() {
sGetSocketShapeFromSocketType(socket.m_type), sGetSocketShapeFromSocketType(socket.m_type),
ImColor(255, 255, 255, 255)); ImColor(255, 255, 255, 255));
ImGui::Text(socket.m_name.c_str()); ImGui::Text(socket.m_name.c_str());
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::EndInputAttribute(); ImNodes::EndInputAttribute();
} }
@ -178,8 +180,8 @@ void AnimGraphEditorUpdate() {
ImNodes::EndNode(); ImNodes::EndNode();
} }
for (size_t i = 0, n = gGraphResource.m_nodes.size(); i < n; i++) { for (size_t i = 2, n = graph_resource.m_nodes.size(); i < n; i++) {
AnimNodeResource& node_resource = gGraphResource.m_nodes[i]; AnimNodeResource& node_resource = graph_resource.m_nodes[i];
ImNodes::BeginNode(i); ImNodes::BeginNode(i);
@ -198,6 +200,8 @@ void AnimGraphEditorUpdate() {
sGetSocketShapeFromSocketType(socket.m_type), sGetSocketShapeFromSocketType(socket.m_type),
ImColor(255, 255, 255, 255)); ImColor(255, 255, 255, 255));
ImGui::Text(socket.m_name.c_str()); ImGui::Text(socket.m_name.c_str());
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::EndInputAttribute(); ImNodes::EndInputAttribute();
} }
@ -211,6 +215,7 @@ void AnimGraphEditorUpdate() {
sGetSocketShapeFromSocketType(socket.m_type), sGetSocketShapeFromSocketType(socket.m_type),
ImColor(255, 255, 255, 255)); ImColor(255, 255, 255, 255));
ImGui::Text(socket.m_name.c_str()); ImGui::Text(socket.m_name.c_str());
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::EndInputAttribute(); ImNodes::EndInputAttribute();
} }
@ -221,8 +226,8 @@ void AnimGraphEditorUpdate() {
ImNodes::EndNode(); ImNodes::EndNode();
} }
for (size_t i = 0, n = gGraphResource.m_connections.size(); i < n; i++) { for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) {
const AnimGraphConnection& connection = gGraphResource.m_connections[i]; const AnimGraphConnection& connection = graph_resource.m_connections[i];
int start_attr, end_attr; int start_attr, end_attr;
start_attr = GenerateOutputAttributeId( start_attr = GenerateOutputAttributeId(
connection.m_source_node_index, connection.m_source_node_index,
@ -235,6 +240,7 @@ void AnimGraphEditorUpdate() {
ImNodes::EndNodeEditor(); ImNodes::EndNodeEditor();
// Handle newly created links.
int start_attr, end_attr; int start_attr, end_attr;
if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) { if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) {
int node_start_id; int node_start_id;
@ -248,16 +254,18 @@ void AnimGraphEditorUpdate() {
int node_end_input_index; int node_end_input_index;
SplitInputAttributeId(end_attr, &node_end_id, &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; AnimGraphConnection connection;
connection.m_source_node_index = node_start_id; connection.m_source_node_index = node_start_id;
connection.m_source_socket_index = node_start_output_index; connection.m_source_socket_index = node_start_output_index;
connection.m_target_node_index = node_end_id; connection.m_target_node_index = node_end_id;
connection.m_target_socket_index = node_end_input_index; 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);
} }
} }