180 lines
4.8 KiB
C
180 lines
4.8 KiB
C
![]() |
//
|
||
|
// Created by martin on 04.02.22.
|
||
|
//
|
||
|
|
||
|
#ifndef ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
||
|
#define ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
||
|
|
||
|
#include <cstring>
|
||
|
#include <iostream>
|
||
|
#include <map>
|
||
|
#include <string>
|
||
|
#include <type_traits>
|
||
|
#include <vector>
|
||
|
|
||
|
#include "SyncTrack.h"
|
||
|
#include "AnimGraphData.h"
|
||
|
#include "AnimGraphNodes.h"
|
||
|
|
||
|
struct AnimNode;
|
||
|
struct NodeSocketAccessorBase;
|
||
|
|
||
|
struct AnimNodeResource {
|
||
|
std::string m_name;
|
||
|
std::string m_type_name;
|
||
|
AnimNode* m_anim_node = nullptr;
|
||
|
NodeSocketAccessorBase* m_socket_accessor = nullptr;
|
||
|
float m_position[2] = {0.f, 0.f};
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
//
|
||
|
// AnimGraphResource
|
||
|
//
|
||
|
struct AnimGraphConnection {
|
||
|
const AnimNodeResource* m_source_node = nullptr;
|
||
|
const Socket* m_source_socket = nullptr;
|
||
|
const AnimNodeResource* m_target_node = nullptr;
|
||
|
const Socket* m_target_socket = nullptr;
|
||
|
};
|
||
|
|
||
|
struct AnimGraphResource {
|
||
|
std::string m_name;
|
||
|
std::vector<AnimNodeResource> m_nodes;
|
||
|
std::vector<AnimGraphConnection> m_connections;
|
||
|
|
||
|
~AnimGraphResource() {
|
||
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||
|
delete m_nodes[i].m_anim_node;
|
||
|
delete m_nodes[i].m_socket_accessor;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
AnimGraphResource() { clear(); }
|
||
|
|
||
|
void clear();
|
||
|
void clearNodes();
|
||
|
void initGraphConnectors();
|
||
|
bool saveToFile(const char* filename) const;
|
||
|
bool loadFromFile(const char* filename);
|
||
|
|
||
|
AnimNodeResource& getGraphOutputNode() { return m_nodes[0]; }
|
||
|
AnimNodeResource& getGraphInputNode() { return m_nodes[1]; }
|
||
|
|
||
|
const AnimNodeResource& getGraphOutputNode() const { return m_nodes[0]; }
|
||
|
const AnimNodeResource& getGraphInputNode() const { return m_nodes[1]; }
|
||
|
|
||
|
size_t getNodeIndex(const AnimNodeResource& node_resource) const {
|
||
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||
|
if (&m_nodes[i] == &node_resource) {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
size_t addNode(AnimNodeResource node_resource) {
|
||
|
m_nodes.push_back(node_resource);
|
||
|
return m_nodes.size() - 1;
|
||
|
}
|
||
|
|
||
|
bool connectSockets(
|
||
|
const AnimNodeResource& source_node,
|
||
|
const std::string& source_socket_name,
|
||
|
const AnimNodeResource& target_node,
|
||
|
const std::string& target_socket_name) {
|
||
|
size_t source_index = -1;
|
||
|
size_t target_index = -1;
|
||
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||
|
if (&source_node == &m_nodes[i]) {
|
||
|
source_index = i;
|
||
|
}
|
||
|
|
||
|
if (&target_node == &m_nodes[i]) {
|
||
|
target_index = i;
|
||
|
}
|
||
|
|
||
|
if (source_index < m_nodes.size() && target_index < m_nodes.size()) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (source_index >= m_nodes.size() || target_index >= m_nodes.size()) {
|
||
|
std::cerr << "Cannot connect nodes: could not find nodes." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
Socket* source_socket =
|
||
|
source_node.m_socket_accessor->FindOutputSocket(source_socket_name);
|
||
|
Socket* target_socket =
|
||
|
target_node.m_socket_accessor->FindInputSocket(target_socket_name);
|
||
|
|
||
|
if (source_socket == nullptr || target_socket == nullptr) {
|
||
|
std::cerr << "Cannot connect nodes: could not find sockets." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
AnimGraphConnection connection;
|
||
|
connection.m_source_node = &source_node;
|
||
|
connection.m_source_socket = source_socket;
|
||
|
connection.m_target_node = &target_node;
|
||
|
connection.m_target_socket = target_socket;
|
||
|
m_connections.push_back(connection);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
static inline AnimNode* AnimNodeFactory(const std::string& name) {
|
||
|
AnimNode* result;
|
||
|
if (name == "Blend2") {
|
||
|
result = new Blend2Node;
|
||
|
} else if (name == "SpeedScale") {
|
||
|
result = new SpeedScaleNode;
|
||
|
} else if (name == "AnimSampler") {
|
||
|
result = new AnimSamplerNode;
|
||
|
} else if (name == "BlendTree") {
|
||
|
result = new BlendTreeNode;
|
||
|
}
|
||
|
|
||
|
if (result != nullptr) {
|
||
|
result->m_node_type_name = name;
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
std::cerr << "Invalid node type: " << name << std::endl;
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
static inline NodeSocketAccessorBase* AnimNodeAccessorFactory(
|
||
|
const std::string& node_type_name,
|
||
|
AnimNode* node) {
|
||
|
if (node_type_name == "Blend2") {
|
||
|
return new NodeSocketAccessor<Blend2Node>(node);
|
||
|
} else if (node_type_name == "SpeedScale") {
|
||
|
return new NodeSocketAccessor<SpeedScaleNode>(node);
|
||
|
} else if (node_type_name == "AnimSampler") {
|
||
|
return new NodeSocketAccessor<AnimSamplerNode>(node);
|
||
|
} else if (node_type_name == "BlendTree") {
|
||
|
return new NodeSocketAccessor<BlendTreeNode>(node);
|
||
|
} else {
|
||
|
std::cerr << "Invalid node type name " << node_type_name << "."
|
||
|
<< std::endl;
|
||
|
}
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
static inline AnimNodeResource AnimNodeResourceFactory(
|
||
|
const std::string& node_type_name) {
|
||
|
AnimNodeResource result;
|
||
|
result.m_type_name = node_type_name;
|
||
|
result.m_anim_node = AnimNodeFactory(node_type_name);
|
||
|
result.m_socket_accessor =
|
||
|
AnimNodeAccessorFactory(node_type_name, result.m_anim_node);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H
|