Initial version of NodeDescriptor which aims to replace NodeSocketAccessor.

AnimGraphEditor
Martin Felis 2023-03-29 22:25:09 +02:00
parent 08283d9bcf
commit 9dd10e8f27
4 changed files with 183 additions and 0 deletions

View File

@ -108,6 +108,7 @@ set(ozz_offline_test_objs
target_sources(runtests PRIVATE target_sources(runtests PRIVATE
tests/AnimGraphResourceTests.cc tests/AnimGraphResourceTests.cc
tests/AnimGraphEvalTests.cc tests/AnimGraphEvalTests.cc
tests/NodeDescriptorTests.cc
tests/SyncTrackTests.cc tests/SyncTrackTests.cc
tests/main.cc tests/main.cc
${ozz_offline_test_objs} ${ozz_offline_test_objs}

View File

@ -145,6 +145,137 @@ struct Socket {
size_t m_type_size = 0; size_t m_type_size = 0;
}; };
template <typename T>
SocketType GetSocketType() {
if constexpr (std::is_same<T, bool>::value) {
return SocketType::SocketTypeBool;
}
if constexpr (std::is_same<T, AnimData>::value) {
return SocketType::SocketTypeAnimation;
}
if constexpr (std::is_same<T, float>::value) {
return SocketType::SocketTypeFloat;
}
if constexpr (std::is_same<T, Vec3>::value) {
return SocketType::SocketTypeVec3;
}
if constexpr (std::is_same<T, Quat>::value) {
return SocketType::SocketTypeQuat;
}
if constexpr (std::is_same<T, std::string>::value) {
return SocketType::SocketTypeString;
}
assert(false && "This should not be reachable");
abort();
return SocketType::SocketTypeUndefined;
}
struct NodeDescriptorBase {
std::vector<Socket> m_properties;
std::vector<Socket> m_inputs;
std::vector<Socket> m_outputs;
template <typename T>
bool RegisterInput(const char* name, T** value_ptr_ptr) {
return RegisterSocket(name, value_ptr_ptr, m_inputs);
}
template <typename T>
bool RegisterOutput(const char* name, T** value_ptr_ptr) {
return RegisterSocket(name, value_ptr_ptr, m_outputs);
}
template <typename T>
bool RegisterProperty(const char* name, T* value_ptr, int flags = 0) {
for (int i = 0; i < m_properties.size(); i++) {
if (m_properties[i].m_name == name) {
return false;
}
}
Socket socket;
socket.m_name = name;
socket.m_type = GetSocketType<T>();
socket.m_reference.ptr = static_cast<void*>(value_ptr);
socket.m_flags = flags;
socket.m_type_size = sizeof(T);
m_properties.push_back(socket);
return true;
}
template <typename T>
T* GetInput(const char* name) {
Socket* socket = FindSocket(name, m_inputs);
assert(GetSocketType<T>() == socket->m_type);
return *socket->m_reference.ptr_ptr;
}
template <typename T>
void SetInput(const char* name, T* value_ptr) {
Socket* socket = FindSocket(name, m_inputs);
assert(GetSocketType<T>() == socket->m_type);
*socket->m_reference.ptr_ptr = value_ptr;
}
template <typename T>
void SetProperty(const char* name, const T& value) {
Socket* socket = FindSocket(name, m_properties);
assert(GetSocketType<T>() == socket->m_type);
*static_cast<T*>(socket->m_reference.ptr) = value;
}
template <typename T>
const T& GetProperty(const char* name) {
Socket* socket = FindSocket(name, m_properties);
assert(GetSocketType<T>() == socket->m_type);
return *static_cast<T*>(socket->m_reference.ptr);
}
private:
Socket* FindSocket(const char* name, std::vector<Socket>& sockets) {
for (int i = 0, n = sockets.size(); i < n; i++) {
if (sockets[i].m_name == name) {
return &sockets[i];
}
}
return nullptr;
}
template <typename T>
bool RegisterSocket(const char* name, T** value_ptr_ptr, std::vector<Socket>& sockets) {
for (int i = 0; i < sockets.size(); i++) {
if (sockets[i].m_name == name) {
return false;
}
}
Socket socket;
socket.m_name = name;
socket.m_type = GetSocketType<T>();
socket.m_reference.ptr_ptr = (void**)(value_ptr_ptr);
socket.m_type_size = sizeof(T);
sockets.push_back(socket);
return true;
}
};
template <typename T>
struct NodeDescriptor : public NodeDescriptorBase {
virtual ~NodeDescriptor() {}
};
struct NodeSocketAccessorBase { struct NodeSocketAccessorBase {
std::vector<Socket> m_properties; std::vector<Socket> m_properties;
std::vector<Socket> m_inputs; std::vector<Socket> m_inputs;

View File

@ -150,6 +150,20 @@ struct NodeSocketAccessor<Blend2Node> : public NodeSocketAccessorBase {
} }
}; };
template <>
struct NodeDescriptor<Blend2Node> : public NodeDescriptorBase {
NodeDescriptor(Blend2Node* node) {
RegisterInput("Input0", &node->i_input0);
RegisterInput("Input1", &node->i_input1);
RegisterInput("Weight", &node->i_blend_weight);
RegisterOutput("Output", &node->o_output);
RegisterProperty("Sync", &node->m_sync_blend);
}
};
// //
// SpeedScaleNode // SpeedScaleNode
// //

View File

@ -0,0 +1,37 @@
//
// Created by martin on 04.02.22.
//
#include "AnimGraph/AnimGraphData.h"
#include "AnimGraph/AnimGraphNodes.h"
#include "catch.hpp"
TEST_CASE("Descriptor Access", "[NodeDescriptorTests]") {
Blend2Node blend2Node;
NodeDescriptor<Blend2Node> blend2Descriptor (&blend2Node);
CHECK(blend2Descriptor.m_inputs.size() == 3);
CHECK(*blend2Descriptor.m_inputs[0].m_reference.ptr_ptr == blend2Node.i_input0);
CHECK(*blend2Descriptor.m_inputs[1].m_reference.ptr_ptr == blend2Node.i_input1);
CHECK(*blend2Descriptor.m_inputs[2].m_reference.ptr_ptr == blend2Node.i_blend_weight);
CHECK(blend2Descriptor.m_inputs[0].m_type_size == sizeof(AnimData));
CHECK(blend2Descriptor.m_inputs[2].m_type_size == 4);
CHECK(blend2Descriptor.m_outputs.size() == 1);
CHECK(*blend2Descriptor.m_outputs[0].m_reference.ptr_ptr == blend2Node.o_output);
CHECK(blend2Descriptor.m_properties.size() == 1);
CHECK(blend2Descriptor.m_properties[0].m_reference.ptr == &blend2Node.m_sync_blend);
CHECK(blend2Node.i_input0 == nullptr);
AnimData some_anim_data;
blend2Descriptor.SetInput("Input0", &some_anim_data);
CHECK(blend2Node.i_input0 == &some_anim_data);
CHECK(blend2Node.m_sync_blend == false);
CHECK(blend2Descriptor.GetProperty<bool>("Sync") == false);
blend2Descriptor.SetProperty<bool>("Sync", true);
CHECK(blend2Node.m_sync_blend == true);
CHECK(blend2Descriptor.GetProperty<bool>("Sync") == true);
}