2022-02-11 16:51:18 +01:00
|
|
|
//
|
|
|
|
// Created by martin on 04.02.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AnimGraphResource.h"
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
#include "3rdparty/json/json.hpp"
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
|
|
|
// Socket <-> json
|
|
|
|
//
|
|
|
|
std::string sSocketTypeToStr(SocketType pin_type) {
|
2022-03-25 10:59:12 +01:00
|
|
|
if (pin_type < SocketType::SocketTypeUndefined
|
|
|
|
|| pin_type >= SocketType::SocketTypeLast) {
|
|
|
|
return "Unknown";
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 10:59:12 +01:00
|
|
|
return SocketTypeNames[static_cast<int>(pin_type)];
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
json sSocketToJson(const Socket& socket) {
|
2022-02-11 16:51:18 +01:00
|
|
|
json result;
|
2022-02-14 22:37:19 +01:00
|
|
|
result["name"] = socket.m_name;
|
|
|
|
result["type"] = sSocketTypeToStr(socket.m_type);
|
2022-02-18 23:33:30 +01:00
|
|
|
|
|
|
|
if (socket.m_value.ptr != nullptr) {
|
|
|
|
if (socket.m_type == SocketType::SocketTypeBool) {
|
|
|
|
result["value"] = *reinterpret_cast<bool*>(socket.m_value.ptr);
|
|
|
|
} else if (socket.m_type == SocketType::SocketTypeAnimation) {
|
|
|
|
} else if (socket.m_type == SocketType::SocketTypeFloat) {
|
|
|
|
result["value"] = *reinterpret_cast<float*>(socket.m_value.ptr);
|
|
|
|
} else if (socket.m_type == SocketType::SocketTypeVec3) {
|
|
|
|
Vec3& vec3 = *reinterpret_cast<Vec3*>(socket.m_value.ptr);
|
|
|
|
result["value"][0] = vec3[0];
|
|
|
|
result["value"][1] = vec3[1];
|
|
|
|
result["value"][2] = vec3[2];
|
|
|
|
} else if (socket.m_type == SocketType::SocketTypeQuat) {
|
|
|
|
Quat& quat = *reinterpret_cast<Quat*>(socket.m_value.ptr);
|
|
|
|
result["value"][0] = quat[0];
|
|
|
|
result["value"][1] = quat[1];
|
|
|
|
result["value"][2] = quat[2];
|
|
|
|
result["value"][3] = quat[3];
|
|
|
|
} else if (socket.m_type == SocketType::SocketTypeString) {
|
|
|
|
result["value"] = *reinterpret_cast<std::string*>(socket.m_value.ptr);
|
|
|
|
} else {
|
|
|
|
std::cerr << "Invalid socket type '" << static_cast<int>(socket.m_type)
|
|
|
|
<< "'." << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket sJsonToSocket(const json& json_data) {
|
|
|
|
Socket result;
|
|
|
|
result.m_type = SocketType::SocketTypeUndefined;
|
|
|
|
result.m_value.ptr = nullptr;
|
|
|
|
result.m_name = json_data["name"];
|
|
|
|
|
|
|
|
std::string type_string = json_data["type"];
|
|
|
|
|
|
|
|
if (type_string == "Bool") {
|
|
|
|
result.m_type = SocketType::SocketTypeBool;
|
|
|
|
result.m_type_size = sizeof(bool);
|
|
|
|
} else if (type_string == "Animation") {
|
|
|
|
result.m_type = SocketType::SocketTypeAnimation;
|
|
|
|
result.m_type_size = sizeof(AnimData);
|
|
|
|
} else if (type_string == "Float") {
|
|
|
|
result.m_type = SocketType::SocketTypeFloat;
|
|
|
|
result.m_type_size = sizeof(float);
|
|
|
|
} else if (type_string == "Vec3") {
|
|
|
|
result.m_type = SocketType::SocketTypeVec3;
|
|
|
|
result.m_type_size = sizeof(Vec3);
|
|
|
|
} else if (type_string == "Quat") {
|
|
|
|
result.m_type = SocketType::SocketTypeQuat;
|
|
|
|
result.m_type_size = sizeof(Quat);
|
|
|
|
} else if (type_string == "String") {
|
|
|
|
result.m_type = SocketType::SocketTypeString;
|
|
|
|
result.m_type_size = sizeof(std::string);
|
|
|
|
} else {
|
|
|
|
std::cerr << "Invalid socket type '" << type_string << "'." << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
|
|
|
// AnimGraphNode <-> json
|
|
|
|
//
|
2022-02-11 16:51:18 +01:00
|
|
|
json sAnimGraphNodeToJson(const AnimNodeResource& node) {
|
|
|
|
json result;
|
|
|
|
|
|
|
|
result["name"] = node.m_name;
|
|
|
|
result["type"] = "AnimNodeResource";
|
|
|
|
result["node_type"] = node.m_type_name;
|
|
|
|
|
|
|
|
for (size_t j = 0; j < 2; j++) {
|
|
|
|
result["position"][j] = node.m_position[j];
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:33:30 +01:00
|
|
|
for (size_t j = 0, n = node.m_socket_accessor->m_properties.size(); j < n;
|
|
|
|
j++) {
|
|
|
|
Socket& property = node.m_socket_accessor->m_properties[j];
|
|
|
|
result["properties"][property.m_name] = sSocketToJson(property);
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimNodeResource sAnimGraphNodeFromJson(const json& json_node) {
|
|
|
|
AnimNodeResource result;
|
|
|
|
|
|
|
|
result.m_name = json_node["name"];
|
|
|
|
result.m_type_name = json_node["node_type"];
|
|
|
|
result.m_position[0] = json_node["position"][0];
|
|
|
|
result.m_position[1] = json_node["position"][1];
|
|
|
|
|
|
|
|
result.m_anim_node = AnimNodeFactory(result.m_type_name);
|
2022-02-14 22:37:19 +01:00
|
|
|
result.m_socket_accessor =
|
|
|
|
AnimNodeAccessorFactory(result.m_type_name, result.m_anim_node);
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-02-18 23:33:30 +01:00
|
|
|
for (size_t j = 0, n = result.m_socket_accessor->m_properties.size(); j < n;
|
|
|
|
j++) {
|
|
|
|
Socket& property = result.m_socket_accessor->m_properties[j];
|
|
|
|
json json_property = json_node["properties"][property.m_name];
|
|
|
|
|
|
|
|
if (sSocketTypeToStr(property.m_type) == json_property["type"]) {
|
|
|
|
if (property.m_type == SocketType::SocketTypeBool) {
|
|
|
|
result.m_socket_accessor->SetProperty<bool>(
|
|
|
|
property.m_name,
|
|
|
|
json_property["value"]);
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeAnimation) {
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeFloat) {
|
|
|
|
result.m_socket_accessor->SetProperty<float>(
|
|
|
|
property.m_name,
|
|
|
|
json_property["value"]);
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeVec3) {
|
|
|
|
Vec3* property_vec3 = reinterpret_cast<Vec3*>(property.m_value.ptr);
|
|
|
|
(*property_vec3)[0] = json_property["value"][0];
|
|
|
|
(*property_vec3)[1] = json_property["value"][1];
|
|
|
|
(*property_vec3)[2] = json_property["value"][2];
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeQuat) {
|
|
|
|
Quat* property_quat = reinterpret_cast<Quat*>(property.m_value.ptr);
|
|
|
|
(*property_quat)[0] = json_property["value"][0];
|
|
|
|
(*property_quat)[1] = json_property["value"][1];
|
|
|
|
(*property_quat)[2] = json_property["value"][2];
|
|
|
|
(*property_quat)[3] = json_property["value"][3];
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeString) {
|
|
|
|
result.m_socket_accessor->SetProperty<std::string>(
|
|
|
|
property.m_name,
|
|
|
|
json_property["value"]);
|
|
|
|
} else {
|
|
|
|
std::cerr << "Invalid type for property '" << property.m_name
|
|
|
|
<< "'. Cannot parse json to type '"
|
|
|
|
<< static_cast<int>(property.m_type) << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << "Invalid type for property '" << property.m_name
|
|
|
|
<< "': expected " << sSocketTypeToStr(property.m_type)
|
|
|
|
<< " but got " << json_property["type"] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
|
|
|
// AnimGraphConnection <-> Json
|
|
|
|
//
|
2022-03-25 11:23:03 +01:00
|
|
|
json sAnimGraphConnectionToJson(const AnimGraphResource& graph_resource, const AnimGraphConnection& connection) {
|
2022-02-11 16:51:18 +01:00
|
|
|
json result;
|
|
|
|
|
|
|
|
result["type"] = "AnimGraphConnection";
|
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
const AnimNodeResource* source_node = connection.m_source_node;
|
|
|
|
result["source_node_index"] = graph_resource.getNodeIndex(*source_node);
|
|
|
|
result["source_socket_index"] = source_node->m_socket_accessor->GetOutputIndex(connection.m_source_socket->m_name);
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
const AnimNodeResource* target_node = connection.m_source_node;
|
|
|
|
result["source_node_index"] = graph_resource.getNodeIndex(*target_node);
|
|
|
|
result["source_socket_index"] = target_node->m_socket_accessor->GetInputIndex(connection.m_target_node->m_name);
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
AnimGraphConnection sAnimGraphConnectionFromJson(const AnimGraphResource& graph_resource, const json& json_node) {
|
2022-02-11 16:51:18 +01:00
|
|
|
AnimGraphConnection connection;
|
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
int source_node_index = json_node["source_node_index"];
|
|
|
|
connection.m_source_node = &graph_resource.m_nodes[source_node_index];
|
|
|
|
int source_socket_index = json_node["source_socket_index"];
|
|
|
|
connection.m_source_socket = &connection.m_source_node->m_socket_accessor->m_outputs[source_socket_index];
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
|
|
|
|
int target_node_index = json_node["target_node_index"];
|
|
|
|
connection.m_target_node = &graph_resource.m_nodes[target_node_index];
|
|
|
|
|
|
|
|
int target_socket_index = json_node["target_socket_index"];
|
|
|
|
connection.m_target_socket = &connection.m_target_node->m_socket_accessor->m_outputs[target_socket_index];
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimGraphResource::clear() {
|
|
|
|
m_name = "";
|
2022-02-18 22:24:19 +01:00
|
|
|
|
|
|
|
clearNodes();
|
|
|
|
m_connections.clear();
|
|
|
|
|
|
|
|
initGraphConnectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimGraphResource::clearNodes() {
|
2022-02-14 22:37:19 +01:00
|
|
|
for (size_t i = 0; i < m_nodes.size(); i++) {
|
|
|
|
delete m_nodes[i].m_socket_accessor;
|
|
|
|
m_nodes[i].m_socket_accessor = nullptr;
|
|
|
|
delete m_nodes[i].m_anim_node;
|
|
|
|
m_nodes[i].m_anim_node = nullptr;
|
|
|
|
}
|
2022-02-11 16:51:18 +01:00
|
|
|
m_nodes.clear();
|
2022-02-18 22:24:19 +01:00
|
|
|
}
|
2022-02-12 10:14:26 +01:00
|
|
|
|
2022-02-18 22:24:19 +01:00
|
|
|
void AnimGraphResource::initGraphConnectors() {
|
2022-02-12 10:14:26 +01:00
|
|
|
m_nodes.push_back(AnimNodeResourceFactory("BlendTree"));
|
2022-02-14 22:37:19 +01:00
|
|
|
m_nodes[0].m_name = "Outputs";
|
|
|
|
m_nodes.push_back(AnimNodeResourceFactory("BlendTree"));
|
|
|
|
m_nodes[1].m_name = "Inputs";
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
bool AnimGraphResource::saveToFile(const char* filename) const {
|
2022-02-11 16:51:18 +01:00
|
|
|
json result;
|
|
|
|
|
|
|
|
result["name"] = m_name;
|
|
|
|
result["type"] = "AnimGraphResource";
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_nodes.size(); i++) {
|
|
|
|
const AnimNodeResource& node = m_nodes[i];
|
|
|
|
result["nodes"][i] = sAnimGraphNodeToJson(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_connections.size(); i++) {
|
|
|
|
const AnimGraphConnection& connection = m_connections[i];
|
2022-03-25 11:23:03 +01:00
|
|
|
result["connections"][i] = sAnimGraphConnectionToJson(*this, connection);
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
// Graph inputs and outputs
|
|
|
|
{
|
|
|
|
const AnimNodeResource& graph_output_node = m_nodes[0];
|
|
|
|
const std::vector<Socket> graph_inputs =
|
|
|
|
graph_output_node.m_socket_accessor->m_inputs;
|
|
|
|
for (size_t i = 0; i < graph_inputs.size(); i++) {
|
|
|
|
result["nodes"][0]["inputs"][i] = sSocketToJson(graph_inputs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const AnimNodeResource& graph_input_node = m_nodes[1];
|
|
|
|
const std::vector<Socket> graph_outputs =
|
|
|
|
graph_input_node.m_socket_accessor->m_outputs;
|
|
|
|
for (size_t i = 0; i < graph_outputs.size(); i++) {
|
|
|
|
result["nodes"][1]["outputs"][i] = sSocketToJson(graph_outputs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
std::ofstream output_file;
|
2022-02-14 22:37:19 +01:00
|
|
|
output_file.open(filename);
|
2022-02-11 16:51:18 +01:00
|
|
|
output_file << to_string(result) << std::endl;
|
|
|
|
output_file.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
bool AnimGraphResource::loadFromFile(const char* filename) {
|
2022-02-11 16:51:18 +01:00
|
|
|
std::ifstream input_file;
|
|
|
|
input_file.open(filename);
|
|
|
|
std::stringstream buffer;
|
|
|
|
buffer << input_file.rdbuf();
|
|
|
|
|
|
|
|
json json_data = json::parse(buffer.str(), nullptr, false);
|
|
|
|
if (json_data.is_discarded()) {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr << "Error parsing json of file '" << filename << "'."
|
|
|
|
<< std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (json_data["type"] != "AnimGraphResource") {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr
|
|
|
|
<< "Invalid json object. Expected type 'AnimGraphResource' but got '"
|
|
|
|
<< json_data["type"] << "'." << std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
clear();
|
2022-02-18 22:24:19 +01:00
|
|
|
clearNodes();
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
m_name = json_data["name"];
|
2022-03-25 11:23:03 +01:00
|
|
|
|
|
|
|
// Load nodes
|
2022-02-11 16:51:18 +01:00
|
|
|
for (size_t i = 0; i < json_data["nodes"].size(); i++) {
|
|
|
|
const json& json_node = json_data["nodes"][i];
|
|
|
|
if (json_node["type"] != "AnimNodeResource") {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr
|
|
|
|
<< "Invalid json object. Expected type 'AnimNodeResource' but got '"
|
|
|
|
<< json_node["type"] << "'." << std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimNodeResource node = sAnimGraphNodeFromJson(json_node);
|
|
|
|
m_nodes.push_back(node);
|
|
|
|
}
|
2022-03-25 11:23:03 +01:00
|
|
|
|
|
|
|
// Setup graph inputs and outputs
|
|
|
|
const json& graph_outputs = json_data["nodes"][0]["inputs"];
|
|
|
|
for (size_t i = 0; i < graph_outputs.size(); i++) {
|
|
|
|
AnimNodeResource& graph_node = m_nodes[0];
|
|
|
|
graph_node.m_socket_accessor->m_inputs.push_back(
|
|
|
|
sJsonToSocket(graph_outputs[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
const json& graph_inputs = json_data["nodes"][1]["outputs"];
|
|
|
|
for (size_t i = 0; i < graph_inputs.size(); i++) {
|
|
|
|
AnimNodeResource& graph_node = m_nodes[1];
|
|
|
|
graph_node.m_socket_accessor->m_outputs.push_back(
|
|
|
|
sJsonToSocket(graph_inputs[i]));
|
|
|
|
}
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
// Load connections
|
2022-02-11 16:51:18 +01:00
|
|
|
for (size_t i = 0; i < json_data["connections"].size(); i++) {
|
|
|
|
const json& json_connection = json_data["connections"][i];
|
|
|
|
if (json_connection["type"] != "AnimGraphConnection") {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr << "Invalid json object. Expected type 'AnimGraphConnection' "
|
|
|
|
"but got '"
|
|
|
|
<< json_connection["type"] << "'." << std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
AnimGraphConnection connection =
|
2022-03-25 11:23:03 +01:00
|
|
|
sAnimGraphConnectionFromJson(*this, json_connection);
|
2022-02-11 16:51:18 +01:00
|
|
|
m_connections.push_back(connection);
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:33:30 +01:00
|
|
|
return true;
|
2022-03-25 11:46:44 +01:00
|
|
|
}
|