From feb5f57a860c65850f4bf90223713f5c33fb4142 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 2 Mar 2025 12:45:55 +0100 Subject: [PATCH] Refactored construction of AnimNodeResources and AnimGraphResources by using factory methods more consistently. --- src/AnimGraph/AnimGraphData.h | 7 +++++++ src/AnimGraph/AnimGraphResource.cc | 21 +++++++++++++++++---- src/AnimGraph/AnimGraphResource.h | 12 +++++++----- src/AnimGraphEditor/AnimGraphEditor.cc | 14 ++++++++------ 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/AnimGraph/AnimGraphData.h b/src/AnimGraph/AnimGraphData.h index 3cff8da..35a9ef0 100644 --- a/src/AnimGraph/AnimGraphData.h +++ b/src/AnimGraph/AnimGraphData.h @@ -91,6 +91,13 @@ struct AnimDataAllocator { size_t size() { return m_anim_data_list.size(); } }; +enum class AnimGraphType { + GraphTypeUndefined = 0, + GraphTypeBlendTree, + GraphTypeStateMachine, + GraphTypeLast +}; + struct AnimGraphContext { AnimGraph* m_graph = nullptr; ozz::animation::Skeleton* m_skeleton = nullptr; diff --git a/src/AnimGraph/AnimGraphResource.cc b/src/AnimGraph/AnimGraphResource.cc index 4fe77f5..9b0faed 100644 --- a/src/AnimGraph/AnimGraphResource.cc +++ b/src/AnimGraph/AnimGraphResource.cc @@ -181,15 +181,14 @@ AnimNodeResource* sAnimGraphNodeFromJson( std::string node_type = json_node["node_type"]; if (node_type == "BlendTree") { - AnimGraphResource* result = new AnimGraphResource(); + AnimGraphResource* result = + dynamic_cast(AnimNodeResourceFactory("BlendTree")); sAnimGraphResourceBlendTreeFromJson(json_node, result); return result; } - AnimNodeResource* result; - result = new AnimNodeResource(); + AnimNodeResource* result = AnimNodeResourceFactory(node_type); result->m_name = json_node["name"]; - result->m_node_type_name = node_type; result->m_position[0] = json_node["position"][0]; result->m_position[1] = json_node["position"][1]; @@ -755,6 +754,20 @@ void BlendTreeResource::UpdateNodeSubtrees() { } } +AnimGraphResource::AnimGraphResource(AnimGraphType graph_type) { + if (graph_type == AnimGraphType::GraphTypeBlendTree) { + m_graph_type_name = "BlendTree"; + m_virtual_socket_accessor = VirtualAnimNodeDescriptorFactory("BlendTree"); + + m_blend_tree_resource.InitGraphConnectors(); + RegisterBlendTreeOutputSocket("Output"); + } else { + std::cerr + << "Warning: construction of state machine graphs not yet implemented!" + << std::endl; + } +} + bool AnimGraphResource::LoadFromFile(const char* filename) { Clear(); diff --git a/src/AnimGraph/AnimGraphResource.h b/src/AnimGraph/AnimGraphResource.h index 87e210d..8fbec99 100644 --- a/src/AnimGraph/AnimGraphResource.h +++ b/src/AnimGraph/AnimGraphResource.h @@ -66,10 +66,12 @@ struct BlendTreeResource { AddNode(AnimNodeResourceFactory("BlendTreeSockets")); AnimNodeResource* output_node = GetGraphOutputNode(); output_node->m_name = "Outputs"; + output_node->m_position[0] = 200; AddNode(AnimNodeResourceFactory("BlendTreeSockets")); AnimNodeResource* input_node = GetGraphInputNode(); input_node->m_name = "Inputs"; + input_node->m_position[0] = -200; } [[nodiscard]] AnimNodeResource* GetGraphOutputNode() const { @@ -268,6 +270,7 @@ struct StateMachineResource { }; struct AnimGraphResource : AnimNodeResource { + explicit AnimGraphResource(AnimGraphType graph_type); virtual ~AnimGraphResource() { Clear(); }; std::string m_graph_type_name; @@ -365,17 +368,16 @@ static inline AnimNodeResource* AnimNodeResourceFactory( AnimNodeResource* result; if (node_type_name == "BlendTree") { - AnimGraphResource* blend_tree_resource = new AnimGraphResource(); - blend_tree_resource->m_graph_type_name = "BlendTree"; - blend_tree_resource->m_blend_tree_resource.InitGraphConnectors(); + AnimGraphResource* blend_tree_resource = + new AnimGraphResource(AnimGraphType::GraphTypeBlendTree); result = blend_tree_resource; } else { result = new AnimNodeResource(); + result->m_virtual_socket_accessor = + VirtualAnimNodeDescriptorFactory(node_type_name); } result->m_node_type_name = node_type_name; - result->m_virtual_socket_accessor = - VirtualAnimNodeDescriptorFactory(node_type_name); return result; } diff --git a/src/AnimGraphEditor/AnimGraphEditor.cc b/src/AnimGraphEditor/AnimGraphEditor.cc index a5c954f..8a10468 100644 --- a/src/AnimGraphEditor/AnimGraphEditor.cc +++ b/src/AnimGraphEditor/AnimGraphEditor.cc @@ -369,12 +369,10 @@ void AnimGraphEditorClear() { delete sEditorState.rootGraphResource; } - sEditorState.rootGraphResource = new AnimGraphResource(); + sEditorState.rootGraphResource = + dynamic_cast(AnimNodeResourceFactory("BlendTree")); sEditorState.rootGraphResource->m_name = "Root"; - sEditorState.rootGraphResource->m_graph_type_name = "BlendTree"; - sEditorState.rootGraphResource->m_blend_tree_resource.InitGraphConnectors(); - sEditorState.rootGraphResource->m_virtual_socket_accessor = - new NodeDescriptorBase; + sEditorState.isGraphLoadedThisFrame = true; sEditorState.hierarchyStack.clear(); sEditorState.hierarchyStack.push_back(sEditorState.rootGraphResource); @@ -594,7 +592,11 @@ void BlendTreeRenderNodes( } builder.Header(); - ImGui::Text("%s", node_resource->m_node_type_name.c_str()); + if (node_resource->m_name != "") { + ImGui::Text("%s", node_resource->m_name.c_str()); + } else { + ImGui::Text("%s", node_resource->m_node_type_name.c_str()); + } ImGui::Spring(0); builder.EndHeader();