Refactored construction of AnimNodeResources and AnimGraphResources by using factory methods more consistently.

This commit is contained in:
Martin Felis 2025-03-02 12:45:55 +01:00
parent 5a6ac92a48
commit feb5f57a86
4 changed files with 39 additions and 15 deletions

View File

@ -91,6 +91,13 @@ struct AnimDataAllocator {
size_t size() { return m_anim_data_list.size(); } size_t size() { return m_anim_data_list.size(); }
}; };
enum class AnimGraphType {
GraphTypeUndefined = 0,
GraphTypeBlendTree,
GraphTypeStateMachine,
GraphTypeLast
};
struct AnimGraphContext { struct AnimGraphContext {
AnimGraph* m_graph = nullptr; AnimGraph* m_graph = nullptr;
ozz::animation::Skeleton* m_skeleton = nullptr; ozz::animation::Skeleton* m_skeleton = nullptr;

View File

@ -181,15 +181,14 @@ AnimNodeResource* sAnimGraphNodeFromJson(
std::string node_type = json_node["node_type"]; std::string node_type = json_node["node_type"];
if (node_type == "BlendTree") { if (node_type == "BlendTree") {
AnimGraphResource* result = new AnimGraphResource(); AnimGraphResource* result =
dynamic_cast<AnimGraphResource*>(AnimNodeResourceFactory("BlendTree"));
sAnimGraphResourceBlendTreeFromJson(json_node, result); sAnimGraphResourceBlendTreeFromJson(json_node, result);
return result; return result;
} }
AnimNodeResource* result; AnimNodeResource* result = AnimNodeResourceFactory(node_type);
result = new AnimNodeResource();
result->m_name = json_node["name"]; result->m_name = json_node["name"];
result->m_node_type_name = node_type;
result->m_position[0] = json_node["position"][0]; result->m_position[0] = json_node["position"][0];
result->m_position[1] = json_node["position"][1]; 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<AnimData>("Output");
} else {
std::cerr
<< "Warning: construction of state machine graphs not yet implemented!"
<< std::endl;
}
}
bool AnimGraphResource::LoadFromFile(const char* filename) { bool AnimGraphResource::LoadFromFile(const char* filename) {
Clear(); Clear();

View File

@ -66,10 +66,12 @@ struct BlendTreeResource {
AddNode(AnimNodeResourceFactory("BlendTreeSockets")); AddNode(AnimNodeResourceFactory("BlendTreeSockets"));
AnimNodeResource* output_node = GetGraphOutputNode(); AnimNodeResource* output_node = GetGraphOutputNode();
output_node->m_name = "Outputs"; output_node->m_name = "Outputs";
output_node->m_position[0] = 200;
AddNode(AnimNodeResourceFactory("BlendTreeSockets")); AddNode(AnimNodeResourceFactory("BlendTreeSockets"));
AnimNodeResource* input_node = GetGraphInputNode(); AnimNodeResource* input_node = GetGraphInputNode();
input_node->m_name = "Inputs"; input_node->m_name = "Inputs";
input_node->m_position[0] = -200;
} }
[[nodiscard]] AnimNodeResource* GetGraphOutputNode() const { [[nodiscard]] AnimNodeResource* GetGraphOutputNode() const {
@ -268,6 +270,7 @@ struct StateMachineResource {
}; };
struct AnimGraphResource : AnimNodeResource { struct AnimGraphResource : AnimNodeResource {
explicit AnimGraphResource(AnimGraphType graph_type);
virtual ~AnimGraphResource() { Clear(); }; virtual ~AnimGraphResource() { Clear(); };
std::string m_graph_type_name; std::string m_graph_type_name;
@ -365,17 +368,16 @@ static inline AnimNodeResource* AnimNodeResourceFactory(
AnimNodeResource* result; AnimNodeResource* result;
if (node_type_name == "BlendTree") { if (node_type_name == "BlendTree") {
AnimGraphResource* blend_tree_resource = new AnimGraphResource(); AnimGraphResource* blend_tree_resource =
blend_tree_resource->m_graph_type_name = "BlendTree"; new AnimGraphResource(AnimGraphType::GraphTypeBlendTree);
blend_tree_resource->m_blend_tree_resource.InitGraphConnectors();
result = blend_tree_resource; result = blend_tree_resource;
} else { } else {
result = new AnimNodeResource(); result = new AnimNodeResource();
result->m_virtual_socket_accessor =
VirtualAnimNodeDescriptorFactory(node_type_name);
} }
result->m_node_type_name = node_type_name; result->m_node_type_name = node_type_name;
result->m_virtual_socket_accessor =
VirtualAnimNodeDescriptorFactory(node_type_name);
return result; return result;
} }

View File

@ -369,12 +369,10 @@ void AnimGraphEditorClear() {
delete sEditorState.rootGraphResource; delete sEditorState.rootGraphResource;
} }
sEditorState.rootGraphResource = new AnimGraphResource(); sEditorState.rootGraphResource =
dynamic_cast<AnimGraphResource*>(AnimNodeResourceFactory("BlendTree"));
sEditorState.rootGraphResource->m_name = "Root"; sEditorState.rootGraphResource->m_name = "Root";
sEditorState.rootGraphResource->m_graph_type_name = "BlendTree"; sEditorState.isGraphLoadedThisFrame = true;
sEditorState.rootGraphResource->m_blend_tree_resource.InitGraphConnectors();
sEditorState.rootGraphResource->m_virtual_socket_accessor =
new NodeDescriptorBase;
sEditorState.hierarchyStack.clear(); sEditorState.hierarchyStack.clear();
sEditorState.hierarchyStack.push_back(sEditorState.rootGraphResource); sEditorState.hierarchyStack.push_back(sEditorState.rootGraphResource);
@ -594,7 +592,11 @@ void BlendTreeRenderNodes(
} }
builder.Header(); 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); ImGui::Spring(0);
builder.EndHeader(); builder.EndHeader();