From 44087d7a7c53f04e81eb9389500e3146e8afc5b5 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Tue, 30 Apr 2024 18:40:54 +0200 Subject: [PATCH] Removed legacy editor, started working on editing embedded graphs. --- src/AnimGraph/AnimGraphEditor.cc | 464 ++++++----------------------- src/AnimGraph/AnimGraphEditor.h | 2 - src/AnimGraph/AnimGraphResource.cc | 120 +++++++- src/AnimGraph/AnimGraphResource.h | 11 + src/main.cc | 73 ----- 5 files changed, 202 insertions(+), 468 deletions(-) diff --git a/src/AnimGraph/AnimGraphEditor.cc b/src/AnimGraph/AnimGraphEditor.cc index 36837ad..da02a6f 100644 --- a/src/AnimGraph/AnimGraphEditor.cc +++ b/src/AnimGraph/AnimGraphEditor.cc @@ -13,8 +13,13 @@ #include "imnodes.h" #include "misc/cpp/imgui_stdlib.h" -static AnimGraphResource sGraphGresource = AnimGraphResource(); -static bool sGraphLoadedThisFrame = false; +struct EditorState { + AnimGraphResource* rootGraphResource = nullptr; + AnimGraphResource* currentGraphResource = nullptr; + bool isGraphLoadedThisFrame = false; +}; + +static EditorState sEditorState; ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { switch (socket_type) { @@ -164,7 +169,10 @@ void AnimGraphEditorRenderSidebar( node_resource->m_name = node_name_buffer; } - int num_properties = node_resource->m_socket_accessor->m_properties.size(); + int num_properties = 0; + if (node_resource->m_socket_accessor != nullptr) { + num_properties = node_resource->m_socket_accessor->m_properties.size(); + } for (int i = 0; i < num_properties; i++) { Socket& property = node_resource->m_socket_accessor->m_properties[i]; if (property.m_type == SocketType::SocketTypeInt) { @@ -251,9 +259,13 @@ void AnimGraphEditorRenderSidebar( } void AnimGraphEditorClear() { - sGraphGresource.Clear(); - sGraphGresource.m_blend_tree_resource.InitGraphConnectors(); - sGraphGresource.m_graph_type_name = "BlendTree"; + sEditorState.currentGraphResource = nullptr; + delete sEditorState.rootGraphResource; + + sEditorState.rootGraphResource = new AnimGraphResource(); + sEditorState.rootGraphResource->m_graph_type_name = "BlendTree"; + sEditorState.rootGraphResource->m_blend_tree_resource.InitGraphConnectors(); + sEditorState.currentGraphResource = sEditorState.rootGraphResource; } void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { @@ -261,11 +273,11 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { ImGui::BeginMenuBar(); if (ImGui::Button("Save")) { - sGraphGresource.SaveToFile("editor_graph.json"); + sEditorState.rootGraphResource->SaveToFile("editor_graph.json"); } if (ImGui::Button("Load")) { - sGraphGresource.LoadFromFile("editor_graph.json"); - sGraphLoadedThisFrame = true; + sEditorState.rootGraphResource->LoadFromFile("editor_graph.json"); + sEditorState.isGraphLoadedThisFrame = true; } if (ImGui::Button("Clear")) { AnimGraphEditorClear(); @@ -277,10 +289,10 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { memset(graph_name_buffer, 0, sizeof(graph_name_buffer)); strncpy( graph_name_buffer, - sGraphGresource.m_name.c_str(), + sEditorState.currentGraphResource->m_name.c_str(), sizeof(graph_name_buffer)); if (ImGui::InputText("Name", graph_name_buffer, sizeof(graph_name_buffer))) { - sGraphGresource.m_name = graph_name_buffer; + sEditorState.currentGraphResource->m_name = graph_name_buffer; } ImGui::EndMenuBar(); @@ -292,19 +304,20 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { // ax::NodeEditor::Begin("Graph Editor"); -#if 1 for (size_t node_id = 0, - n = sGraphGresource.m_blend_tree_resource.GetNumNodes(); + n = sEditorState.currentGraphResource->m_blend_tree_resource + .GetNumNodes(); node_id < n; node_id++) { AnimNodeResource* node_resource = - sGraphGresource.m_blend_tree_resource.GetNode(node_id); + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( + node_id); if (node_id == 0 || node_id == 1) { // continue; } - if (sGraphLoadedThisFrame) { + if (sEditorState.isGraphLoadedThisFrame) { ax::NodeEditor::SetNodePosition( node_id, ImVec2(node_resource->m_position[0], node_resource->m_position[1])); @@ -313,8 +326,9 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { ImGui::Text("%s", node_resource->m_node_type_name.c_str()); // Inputs - std::vector& node_inputs = - node_resource->m_socket_accessor->m_inputs; + std::vector node_inputs = + sEditorState.currentGraphResource->m_blend_tree_resource + .GetNodeInputSockets(node_resource); for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) { Socket& socket = node_inputs[j]; ax::NodeEditor::BeginPin( @@ -327,8 +341,9 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { } // Outputs - std::vector& node_outputs = - node_resource->m_socket_accessor->m_outputs; + std::vector node_outputs = + sEditorState.currentGraphResource->m_blend_tree_resource + .GetNodeOutputSockets(node_resource); for (size_t j = 0, ni = node_outputs.size(); j < ni; j++) { Socket& socket = node_outputs[j]; ax::NodeEditor::BeginPin( @@ -349,21 +364,23 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { int link_id = 0; for (size_t connection_id = 0, - n = sGraphGresource.m_blend_tree_resource.GetNumConnections(); + n = sEditorState.currentGraphResource->m_blend_tree_resource + .GetNumConnections(); connection_id < n; connection_id++) { const BlendTreeConnectionResource* connection_resource = - sGraphGresource.m_blend_tree_resource.GetConnection(connection_id); + sEditorState.currentGraphResource->m_blend_tree_resource.GetConnection( + connection_id); const AnimNodeResource* source_node_resource = - sGraphGresource.m_blend_tree_resource.GetNode( + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( connection_resource->source_node_index); int source_socket_index = source_node_resource->m_socket_accessor->GetOutputIndex( connection_resource->source_socket_name.c_str()); const AnimNodeResource* target_node_resource = - sGraphGresource.m_blend_tree_resource.GetNode( + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( connection_resource->target_node_index); int target_socket_index = target_node_resource->m_socket_accessor->GetInputIndex( @@ -379,9 +396,6 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { ax::NodeEditor::Link(link_id++, source_socket_pin_id, target_socket_pin_id); } -#endif - -#if 1 // Create Connections if (ax::NodeEditor::BeginCreate()) { ax::NodeEditor::PinId input_pin_id, output_pin_id; @@ -397,7 +411,8 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { &source_node_socket_index); const AnimNodeResource* source_node = - sGraphGresource.m_blend_tree_resource.GetNode(source_node_index); + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( + source_node_index); if (source_node->m_socket_accessor->m_outputs.size() < source_node_socket_index) { source_node_socket_index = -1; @@ -412,14 +427,25 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { &target_node_socket_index); const AnimNodeResource* target_node = - sGraphGresource.m_blend_tree_resource.GetNode(target_node_index); + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( + target_node_index); if (target_node->m_socket_accessor->m_inputs.size() < target_node_socket_index) { target_node_socket_index = -1; } - if (source_node_socket_index == -1 - || target_node_socket_index == -1) { + const Socket* source_socket = + sEditorState.currentGraphResource->m_blend_tree_resource + .GetNodeOutputSocketByIndex( + source_node, + source_node_socket_index); + const Socket* target_socket = + sEditorState.currentGraphResource->m_blend_tree_resource + .GetNodeInputSocketByIndex( + target_node, + target_node_socket_index); + + if (source_socket == nullptr || target_socket == nullptr) { ax::NodeEditor::RejectNewItem(); } else { const std::string& source_socket_name = @@ -432,18 +458,18 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { ->m_inputs[target_node_socket_index] .m_name; - sGraphGresource.m_blend_tree_resource.ConnectSockets( - source_node, - source_socket_name, - target_node, - target_socket_name); + sEditorState.currentGraphResource->m_blend_tree_resource + .ConnectSockets( + source_node, + source_socket_name, + target_node, + target_socket_name); } } } } } ax::NodeEditor::EndCreate(); -#endif // Popup menu { @@ -489,12 +515,18 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { node_type_name = "ConstScalarNode"; } + if (ImGui::MenuItem("BlendTree")) { + node_type_name = "BlendTree"; + } + if (!node_type_name.empty()) { AnimNodeResource* node_resource = AnimNodeResourceFactory(node_type_name); - size_t node_id = sGraphGresource.m_blend_tree_resource.GetNumNodes(); + size_t node_id = sEditorState.currentGraphResource + ->m_blend_tree_resource.GetNumNodes(); ax::NodeEditor::SetNodePosition(node_id, popup_mouse_position); - sGraphGresource.m_blend_tree_resource.AddNode(node_resource); + sEditorState.currentGraphResource->m_blend_tree_resource.AddNode( + node_resource); } ImGui::EndPopup(); @@ -517,353 +549,31 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { if (selected_node_id.Get() != 0) { AnimGraphEditorRenderSidebar( - sGraphGresource.m_blend_tree_resource, - sGraphGresource.m_blend_tree_resource.GetNode( + sEditorState.currentGraphResource->m_blend_tree_resource, + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( selected_node_id.Get())); } } ImGui::Columns(1); + ax::NodeEditor::NodeId double_clicked_node_id = + ax::NodeEditor::GetDoubleClickedNode(); + if (!double_clicked_node_id.Invalid) { + AnimNodeResource* clicked_node_resource = + sEditorState.currentGraphResource->m_blend_tree_resource.GetNode( + double_clicked_node_id.Get()); + if (clicked_node_resource->m_node_type_name == "BlendTree") { + sEditorState.currentGraphResource = + dynamic_cast(clicked_node_resource); + ax::NodeEditor::ClearSelection(); + } + } + ax::NodeEditor::SetCurrentEditor(nullptr); - sGraphLoadedThisFrame = false; -} - -void LegacyAnimGraphEditorUpdate() { - ImGui::BeginMenuBar(); - if (ImGui::Button("Save")) { - sGraphGresource.SaveToFile("editor_graph.json"); - } - if (ImGui::Button("Load")) { - sGraphGresource.LoadFromFile("editor_graph.json"); - - for (size_t i = 0, n = sGraphGresource.m_blend_tree_resource.GetNumNodes(); - i < n; - i++) { - const AnimNodeResource* node_resource = - sGraphGresource.m_blend_tree_resource.GetNode(i); - ImNodes::SetNodeGridSpacePos( - i, - ImVec2(node_resource->m_position[0], node_resource->m_position[1])); - } - } - if (ImGui::Button("Clear")) { - sGraphGresource.Clear(); - } - char graph_name_buffer[256]; - memset(graph_name_buffer, 0, sizeof(graph_name_buffer)); - strncpy( - graph_name_buffer, - sGraphGresource.m_name.c_str(), - sizeof(graph_name_buffer)); - if (ImGui::InputText("Name", graph_name_buffer, sizeof(graph_name_buffer))) { - sGraphGresource.m_name = graph_name_buffer; - } - - ImGui::EndMenuBar(); - - ImGui::Columns(2); - - // - // Node editor canvas - // - ImNodes::BeginNodeEditor(); - - // Popup menu - { - const bool open_popup = - ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) - && ImNodes::IsEditorHovered() - && ImGui::IsMouseReleased(ImGuiMouseButton_Right); - - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.f, 8.f)); - if (!ImGui::IsAnyItemHovered() && open_popup) { - ImGui::OpenPopup("add node"); - } - - if (ImGui::BeginPopup("add node")) { - const ImVec2 click_pos = ImGui::GetMousePosOnOpeningCurrentPopup(); - std::string node_type_name = ""; - if (ImGui::MenuItem("AnimSampler")) { - node_type_name = "AnimSampler"; - } - - if (ImGui::MenuItem("Blend2")) { - node_type_name = "Blend2"; - } - - if (ImGui::MenuItem("SpeedScale")) { - node_type_name = "SpeedScale"; - } - - if (ImGui::MenuItem("LockTranslationNode")) { - node_type_name = "LockTranslationNode"; - } - - if (ImGui::MenuItem("MathAddNode")) { - node_type_name = "MathAddNode"; - } - - if (ImGui::MenuItem("MathFloatToVec3Node")) { - node_type_name = "MathFloatToVec3Node"; - } - - if (ImGui::MenuItem("ConstScalarNode")) { - node_type_name = "ConstScalarNode"; - } - - if (node_type_name != "") { - AnimNodeResource* node_resource = - AnimNodeResourceFactory(node_type_name); - size_t node_id = sGraphGresource.m_blend_tree_resource.GetNumNodes(); - ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos()); - sGraphGresource.m_blend_tree_resource.AddNode(node_resource); - } - - ImGui::EndPopup(); - } - - ImGui::PopStyleVar(ImGuiStyleVar_WindowPadding); - } - - for (size_t i = 0, n = sGraphGresource.m_blend_tree_resource.GetNumNodes(); - i < n; - i++) { - AnimNodeResource* node_resource = - sGraphGresource.m_blend_tree_resource.GetNode(i); - - ImNodes::BeginNode(i); - ImGui::PushItemWidth(110.0f); - - // Header - ImNodes::BeginNodeTitleBar(); - if (node_resource - == sGraphGresource.m_blend_tree_resource.GetGraphOutputNode()) { - ImGui::TextUnformatted("Graph Outputs"); - } else if ( - node_resource - == sGraphGresource.m_blend_tree_resource.GetGraphInputNode()) { - ImGui::TextUnformatted("Graph Inputs"); - } else { - ImGui::TextUnformatted(node_resource->m_node_type_name.c_str()); - } - ImNodes::EndNodeTitleBar(); - - // Inputs - std::vector& node_inputs = - node_resource->m_socket_accessor->m_inputs; - for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) { - Socket& socket = node_inputs[j]; - - ImColor socket_color = ImColor(255, 255, 255, 255); - if (socket.m_flags & SocketFlagAffectsTime) { - socket_color = ImColor(255, 128, 128, 255); - } - - ImNodes::BeginInputAttribute( - GenerateInputAttributeId(i, j), - sGetSocketShapeFromSocketType(socket.m_type), - socket_color); - ImGui::TextUnformatted(socket.m_name.c_str()); - - bool socket_connected = - sGraphGresource.m_blend_tree_resource.IsSocketConnected( - node_resource, - socket.m_name); - if (!socket_connected && (socket.m_type == SocketType::SocketTypeFloat)) { - ImGui::SameLine(); - float socket_value = socket.m_value.float_value; - ImGui::PushItemWidth( - 130.0f - ImGui::CalcTextSize(socket.m_name.c_str()).x); - if (ImGui::DragFloat("##hidelabel", &socket_value, 0.01f)) { - socket.SetValue(socket_value); - } - ImGui::PopItemWidth(); - } - - if (!socket_connected && (socket.m_type == SocketType::SocketTypeInt)) { - ImGui::SameLine(); - int socket_value = socket.m_value.int_value; - ImGui::PushItemWidth( - 130.0f - ImGui::CalcTextSize(socket.m_name.c_str()).x); - if (ImGui::InputInt("##hidelabel", &socket_value, 1)) { - socket.SetValue(socket_value); - } - ImGui::PopItemWidth(); - } - - ImNodes::PushAttributeFlag( - ImNodesAttributeFlags_EnableLinkDetachWithDragClick); - ImNodes::EndInputAttribute(); - } - - // Outputs - const 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]; - ImNodes::BeginOutputAttribute( - GenerateOutputAttributeId(i, j), - sGetSocketShapeFromSocketType(socket.m_type), - ImColor(255, 255, 255, 255)); - ImGui::TextUnformatted(socket.m_name.c_str()); - ImNodes::PushAttributeFlag( - ImNodesAttributeFlags_EnableLinkDetachWithDragClick); - ImNodes::EndInputAttribute(); - } - - // Graph output node - if (i == 0) { - if (ImGui::Button("+Output")) { - AnimNodeResource* graph_output_node = - sGraphGresource.m_blend_tree_resource.GetGraphOutputNode(); - - static float bla = 0.f; - std::string socket_name = "Output"; - socket_name += std::to_string( - graph_output_node->m_socket_accessor->m_inputs.size()); - graph_output_node->m_socket_accessor->RegisterInput( - socket_name.c_str(), - nullptr); - } - } else if (i == 1) { - if (ImGui::Button("+Input")) { - AnimNodeResource* graph_input_node = - sGraphGresource.m_blend_tree_resource.GetGraphInputNode(); - - static float bla = 0.f; - std::string socket_name = "Input"; - socket_name += std::to_string( - graph_input_node->m_socket_accessor->m_outputs.size()); - graph_input_node->m_socket_accessor->RegisterOutput( - socket_name.c_str(), - nullptr); - } - } - - // Save state in node resource - ImVec2 node_pos = ImNodes::GetNodeGridSpacePos(i); - node_resource->m_position[0] = node_pos[0]; - node_resource->m_position[1] = node_pos[1]; - ImGui::PopItemWidth(); - - ImNodes::EndNode(); - - // Ensure flags such as SocketFlagAffectsTime are properly set. - node_resource->m_socket_accessor->UpdateFlags(); - } - - for (size_t i = 0, - n = sGraphGresource.m_blend_tree_resource.GetNumConnections(); - i < n; - i++) { - const BlendTreeConnectionResource* connection = - sGraphGresource.m_blend_tree_resource.GetConnection(i); - int start_attr, end_attr; - - const AnimNodeResource* source_node = - sGraphGresource.m_blend_tree_resource.GetNode( - connection->source_node_index); - int source_socket_index = source_node->m_socket_accessor->GetOutputIndex( - connection->source_socket_name.c_str()); - - const AnimNodeResource* target_node = - sGraphGresource.m_blend_tree_resource.GetNode( - connection->target_node_index); - int target_socket_index = target_node->m_socket_accessor->GetInputIndex( - connection->target_socket_name.c_str()); - - start_attr = GenerateOutputAttributeId( - connection->source_node_index, - source_socket_index); - end_attr = GenerateInputAttributeId( - connection->target_node_index, - target_socket_index); - - ImNodes::Link(i, start_attr, end_attr); - } - - ImNodes::EndNodeEditor(); - - // Handle newly created links. - int start_attr, end_attr; - if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) { - int node_start_id; - int node_start_output_index; - SplitOutputAttributeId( - start_attr, - &node_start_id, - &node_start_output_index); - - int node_end_id; - int node_end_input_index; - SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index); - - BlendTreeConnectionResource connection; - connection.source_node_index = node_start_id; - const AnimNodeResource* source_node = - sGraphGresource.m_blend_tree_resource.GetNode(node_start_id); - connection.source_socket_name = - source_node->m_socket_accessor->m_outputs[node_start_output_index] - .m_name; - - connection.target_node_index = node_end_id; - const AnimNodeResource* target_node = - sGraphGresource.m_blend_tree_resource.GetNode(node_end_id); - connection.target_socket_name = - target_node->m_socket_accessor->m_inputs[node_end_input_index].m_name; - - sGraphGresource.m_blend_tree_resource.ConnectSockets( - source_node, - connection.source_socket_name, - target_node, - connection.target_socket_name); - } - - if (ImGui::IsKeyPressed(ImGuiKey_Delete, false)) { - std::cerr << "Delete key!" << std::endl; - } - - // Handle link detachements. - int link_id = 0; - if (ImNodes::IsLinkDestroyed(&link_id)) { - BlendTreeConnectionResource* connection = - sGraphGresource.m_blend_tree_resource.GetConnection(link_id); - AnimNodeResource* source_node = - sGraphGresource.m_blend_tree_resource.GetNode( - connection->source_node_index); - AnimNodeResource* target_node = - sGraphGresource.m_blend_tree_resource.GetNode( - connection->target_node_index); - sGraphGresource.m_blend_tree_resource.DisconnectSockets( - source_node, - connection->source_socket_name, - target_node, - connection->target_socket_name); - } - - int selected_nodes[ImNodes::NumSelectedNodes()]; - ImNodes::GetSelectedNodes(selected_nodes); - - // - // Sidebar - // - ImGui::NextColumn(); - - if (ImNodes::NumSelectedNodes() == 1) { - if (selected_nodes[0] - < sGraphGresource.m_blend_tree_resource.GetNumNodes()) { - AnimNodeResource* selected_node = - sGraphGresource.m_blend_tree_resource.GetNode(selected_nodes[0]); - AnimGraphEditorRenderSidebar( - sGraphGresource.m_blend_tree_resource, - selected_node); - } - } - - ImGui::Columns(1); + sEditorState.isGraphLoadedThisFrame = false; } void AnimGraphEditorGetRuntimeGraph(AnimGraphBlendTree& blend_tree) { - sGraphGresource.CreateBlendTreeInstance(blend_tree); + sEditorState.rootGraphResource->CreateBlendTreeInstance(blend_tree); } \ No newline at end of file diff --git a/src/AnimGraph/AnimGraphEditor.h b/src/AnimGraph/AnimGraphEditor.h index 1d4e1df..bb6921e 100644 --- a/src/AnimGraph/AnimGraphEditor.h +++ b/src/AnimGraph/AnimGraphEditor.h @@ -69,8 +69,6 @@ void AnimGraphEditorClear(); void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context); -void LegacyAnimGraphEditorUpdate(); - void AnimGraphEditorGetRuntimeGraph(AnimGraphBlendTree& anim_graph); #endif //ANIMTESTBED_ANIMGRAPHEDITOR_H diff --git a/src/AnimGraph/AnimGraphResource.cc b/src/AnimGraph/AnimGraphResource.cc index b6794f6..991ddb9 100644 --- a/src/AnimGraph/AnimGraphResource.cc +++ b/src/AnimGraph/AnimGraphResource.cc @@ -343,23 +343,25 @@ static bool sAnimGraphResourceBlendTreeFromJson( } // Load connections - for (const auto& json_connection : json_data["connections"]) { - if (json_connection["type"] != "AnimGraphConnectionResource") { - std::cerr << "Invalid json object. Expected type " - "'AnimGraphConnectionResource' " - "but got '" - << json_connection["type"] << "'." << std::endl; - return false; + if (json_data.contains("connections")) { + for (const auto& json_connection : json_data["connections"]) { + if (json_connection["type"] != "AnimGraphConnectionResource") { + std::cerr << "Invalid json object. Expected type " + "'AnimGraphConnectionResource' " + "but got '" + << json_connection["type"] << "'." << std::endl; + return false; + } + + BlendTreeConnectionResource connection = + sAnimGraphConnectionFromJson(json_connection); + + blend_tree_resource.ConnectSockets( + blend_tree_resource.GetNode(connection.source_node_index), + connection.source_socket_name, + blend_tree_resource.GetNode(connection.target_node_index), + connection.target_socket_name); } - - BlendTreeConnectionResource connection = - sAnimGraphConnectionFromJson(json_connection); - - blend_tree_resource.ConnectSockets( - blend_tree_resource.GetNode(connection.source_node_index), - connection.source_socket_name, - blend_tree_resource.GetNode(connection.target_node_index), - connection.target_socket_name); } return true; @@ -503,6 +505,31 @@ Socket* BlendTreeResource::GetNodeOutputSocket( return output_socket; } +const Socket* BlendTreeResource::GetNodeOutputSocketByIndex( + const AnimNodeResource* node, + const size_t socket_output_index) const { + const std::vector* output_sockets = nullptr; + + if (node->m_socket_accessor) { + output_sockets = &node->m_socket_accessor->m_outputs; + } else if (node->m_node_type_name == "BlendTree") { + const AnimGraphResource* graph_resource = + dynamic_cast(node); + const BlendTreeResource& blend_tree_resource = + graph_resource->m_blend_tree_resource; + + output_sockets = + &blend_tree_resource.GetGraphOutputNode()->m_socket_accessor->m_outputs; + } + + if (output_sockets != nullptr + && output_sockets->size() > socket_output_index) { + return &output_sockets->operator[](socket_output_index); + } + + return nullptr; +} + Socket* BlendTreeResource::GetNodeInputSocket( const AnimNodeResource* node, const std::string& input_socket_name) const { @@ -526,6 +553,67 @@ Socket* BlendTreeResource::GetNodeInputSocket( return input_socket; } +const Socket* BlendTreeResource::GetNodeInputSocketByIndex( + const AnimNodeResource* node, + const size_t socket_input_index) const { + const std::vector* output_sockets = nullptr; + + if (node->m_socket_accessor) { + output_sockets = &node->m_socket_accessor->m_inputs; + } else if (node->m_node_type_name == "BlendTree") { + const AnimGraphResource* graph_resource = + dynamic_cast(node); + const BlendTreeResource& blend_tree_resource = + graph_resource->m_blend_tree_resource; + + output_sockets = + &blend_tree_resource.GetGraphOutputNode()->m_socket_accessor->m_outputs; + } + + if (output_sockets != nullptr + && output_sockets->size() > socket_input_index) { + return &output_sockets->operator[](socket_input_index); + } + + return nullptr; +} + +std::vector BlendTreeResource::GetNodeOutputSockets( + const AnimNodeResource* node) const { + if (node->m_socket_accessor) { + return node->m_socket_accessor->m_outputs; + } + + if (node->m_node_type_name == "BlendTree") { + const AnimGraphResource* graph_resource = + dynamic_cast(node); + const BlendTreeResource& blend_tree_resource = + graph_resource->m_blend_tree_resource; + return blend_tree_resource.GetGraphOutputNode() + ->m_socket_accessor->m_inputs; + } + + return std::vector(); +} + +std::vector BlendTreeResource::GetNodeInputSockets( + const AnimNodeResource* node) const { + if (node->m_socket_accessor) { + return node->m_socket_accessor->m_inputs; + } + + if (node->m_node_type_name == "BlendTree") { + const AnimGraphResource* graph_resource = + dynamic_cast(node); + const BlendTreeResource& blend_tree_resource = + graph_resource->m_blend_tree_resource; + return blend_tree_resource.GetGraphInputNode() + ->m_socket_accessor->m_outputs; + } + + return std::vector(); +} + bool BlendTreeResource::IsConnectionValid( const AnimNodeResource* source_node, const std::string& source_socket_name, diff --git a/src/AnimGraph/AnimGraphResource.h b/src/AnimGraph/AnimGraphResource.h index 2b93b99..3b73561 100644 --- a/src/AnimGraph/AnimGraphResource.h +++ b/src/AnimGraph/AnimGraphResource.h @@ -132,10 +132,21 @@ struct BlendTreeResource { const AnimNodeResource* node, const std::string& output_socket_name) const; + const Socket* GetNodeOutputSocketByIndex( + const AnimNodeResource* node, + const size_t socket_output_index) const; + Socket* GetNodeInputSocket( const AnimNodeResource* node, const std::string& input_socket_name) const; + const Socket* GetNodeInputSocketByIndex( + const AnimNodeResource* node, + const size_t socket_input_index) const; + + std::vector GetNodeOutputSockets(const AnimNodeResource* node) const; + std::vector GetNodeInputSockets(const AnimNodeResource* node) const; + bool ConnectSockets( const AnimNodeResource* source_node, const std::string& source_socket_name, diff --git a/src/main.cc b/src/main.cc index 02ec934..bfd9f60 100644 --- a/src/main.cc +++ b/src/main.cc @@ -170,13 +170,6 @@ struct ApplicationConfig { int window_position[2] = {100, 30}; int window_size[2] = {1000, 600}; - struct LegacyGraphEditor { - bool visible = false; - int position[2] = {20, 20}; - int size[2] = {800, 500}; - }; - LegacyGraphEditor legacy_graph_editor; - struct GraphEditor { bool visible = false; int position[2] = {20, 20}; @@ -222,14 +215,6 @@ void to_json(nlohmann::json& j, const ApplicationConfig& config) { j["main_window"]["size"][0] = config.window_size[0]; j["main_window"]["size"][1] = config.window_size[1]; - j["legacy_graph_editor"]["visible"] = config.legacy_graph_editor.visible; - j["legacy_graph_editor"]["position"][0] = - config.legacy_graph_editor.position[0]; - j["legacy_graph_editor"]["position"][1] = - config.legacy_graph_editor.position[1]; - j["legacy_graph_editor"]["size"][0] = config.legacy_graph_editor.size[0]; - j["legacy_graph_editor"]["size"][1] = config.legacy_graph_editor.size[1]; - j["graph_editor"]["visible"] = config.graph_editor.visible; j["graph_editor"]["position"][0] = config.graph_editor.position[0]; j["graph_editor"]["position"][1] = config.graph_editor.position[1]; @@ -281,28 +266,6 @@ void from_json(const nlohmann::json& j, ApplicationConfig& config) { } } - if (j.contains("legacy_graph_editor")) { - if (j["legacy_graph_editor"].contains("visible")) { - config.legacy_graph_editor.visible = j["legacy_graph_editor"]["visible"]; - } - - if (j["legacy_graph_editor"].contains("position") - and j["legacy_graph_editor"]["position"].size() == 2) { - config.legacy_graph_editor.position[0] = - j["legacy_graph_editor"]["position"].at(0); - config.legacy_graph_editor.position[1] = - j["legacy_graph_editor"]["position"].at(1); - } - - if (j["legacy_graph_editor"].contains("size") - and j["legacy_graph_editor"]["size"].size() == 2) { - config.legacy_graph_editor.size[0] = - j["legacy_graph_editor"]["size"].at(0); - config.legacy_graph_editor.size[1] = - j["legacy_graph_editor"]["size"].at(1); - } - } - if (j.contains("graph_editor")) { if (j["graph_editor"].contains("visible")) { config.graph_editor.visible = j["graph_editor"]["visible"]; @@ -817,9 +780,6 @@ int main() { &gApplicationConfig.animation_player_widget.visible); ImGui::Separator(); - ImGui::Checkbox( - "Legacy Graph Editor", - &gApplicationConfig.legacy_graph_editor.visible); ImGui::Checkbox( "ImGui Demo", @@ -1118,39 +1078,6 @@ int main() { ImGui::End(); } - // Legacy Animation Graph Editor - if (gApplicationConfig.legacy_graph_editor.visible) { - ImGui::SetNextWindowPos( - ImVec2( - gApplicationConfig.legacy_graph_editor.position[0], - gApplicationConfig.legacy_graph_editor.position[1]), - ImGuiCond_FirstUseEver); - ImGui::SetNextWindowSize( - ImVec2( - gApplicationConfig.legacy_graph_editor.size[0], - gApplicationConfig.legacy_graph_editor.size[1]), - ImGuiCond_FirstUseEver); - - ImGui::Begin( - "Legacy Graph Editor", - &gApplicationConfig.legacy_graph_editor.visible, - ImGuiWindowFlags_MenuBar); - - ImVec2 graph_editor_position = ImGui::GetWindowPos(); - gApplicationConfig.legacy_graph_editor.position[0] = - graph_editor_position.x; - gApplicationConfig.legacy_graph_editor.position[1] = - graph_editor_position.y; - - ImVec2 graph_editor_size = ImGui::GetWindowSize(); - gApplicationConfig.legacy_graph_editor.size[0] = graph_editor_size.x; - gApplicationConfig.legacy_graph_editor.size[1] = graph_editor_size.y; - - LegacyAnimGraphEditorUpdate(); - - ImGui::End(); - } - // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowDemoWindow() if (gApplicationConfig.show_imgui_demo_window) { ImGui::SetNextWindowPos(ImVec2(460, 20), ImGuiCond_FirstUseEver);