Added node ordering code and tests.

This commit is contained in:
Martin Felis
2022-02-19 12:16:57 +01:00
parent bd304bde4e
commit da916a7346
4 changed files with 189 additions and 25 deletions
+2 -2
View File
@@ -185,7 +185,7 @@ void AnimGraphEditorUpdate() {
GenerateInputAttributeId(i, j),
sGetSocketShapeFromSocketType(socket.m_type),
socket_color);
ImGui::Text(socket.m_name.c_str());
ImGui::TextUnformatted(socket.m_name.c_str());
ImNodes::PushAttributeFlag(
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
@@ -201,7 +201,7 @@ void AnimGraphEditorUpdate() {
GenerateOutputAttributeId(i, j),
sGetSocketShapeFromSocketType(socket.m_type),
ImColor(255, 255, 255, 255));
ImGui::Text(socket.m_name.c_str());
ImGui::TextUnformatted(socket.m_name.c_str());
ImNodes::PushAttributeFlag(
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::EndInputAttribute();
+35
View File
@@ -351,6 +351,41 @@ bool AnimGraphResource::loadFromFile(const char* filename) {
return true;
}
void AnimGraph::UpdateOrderedNodes() {
std::vector<int> node_index_stack;
node_index_stack.push_back(0);
m_ordered_nodes.clear();
while (node_index_stack.size() > 0) {
std::vector<AnimNodeInput>& node_inputs = m_node_inputs[node_index_stack.back()];
node_index_stack.pop_back();
for (size_t i = 0, n = node_inputs.size(); i < n; i++) {
AnimNode* input_node = node_inputs[i].m_node;
if (input_node == nullptr) {
continue;
}
int input_node_index = getAnimNodeIndex(input_node);
bool is_node_processed = false;
for (size_t j = 0, m = m_ordered_nodes.size(); j < m; j++) {
if (m_ordered_nodes[j] == input_node) {
is_node_processed = true;
break;
}
}
if (is_node_processed) {
continue;
}
m_ordered_nodes.push_back(input_node);
node_index_stack.push_back(input_node_index);
}
}
}
void AnimGraph::MarkActiveNodes() {
m_frame_counter++;
+27 -4
View File
@@ -81,9 +81,23 @@ struct AnimNodeResource {
float m_position[2] = {0.f, 0.f};
};
struct AnimNodeInput {
AnimNode* m_node;
std::string m_input_name;
};
struct AnimNode {
virtual ~AnimNode(){};
virtual void UpdateActiveInputFrameCounters (const std::vector<AnimNodeInput>& inputs) {
for (size_t i = 0, n = inputs.size(); i < n; i++) {
if (inputs[i].m_node != nullptr) {
inputs[i].m_node->m_frame_counter = m_frame_counter;
}
}
}
std::string m_name;
std::string m_node_type_name;
bool m_is_time_synced;
@@ -93,10 +107,6 @@ struct AnimNode {
SyncTrack m_sync_track;
};
struct AnimNodeInput {
AnimNode* m_node;
std::string m_input_name;
};
struct NodeSocketAccessorBase {
NodeSocketAccessorBase() {}
@@ -521,6 +531,7 @@ struct AnimGraph {
delete m_socket_accessor;
}
void UpdateOrderedNodes();
void MarkActiveNodes();
bool CheckNodeActive(int node_index) {
assert(node_index < m_nodes.size());
@@ -533,6 +544,7 @@ struct AnimGraph {
AnimData m_local_transforms;
std::vector<AnimNode*> m_nodes;
std::vector<AnimNode*> m_ordered_nodes;
std::vector<std::vector<AnimNodeInput> > m_node_inputs;
NodeSocketAccessorBase* m_socket_accessor;
char* m_input_buffer = nullptr;
@@ -544,6 +556,15 @@ struct AnimGraph {
void* GetOutput(const std::string& name) const;
void* GetInput(const std::string& name) const;
int getAnimNodeOrderIndex(const AnimNode* node) {
for (size_t i = 0, n = m_ordered_nodes.size(); i < n; i++) {
if (m_ordered_nodes[i] == node) {
return i;
}
}
return -1;
}
AnimNode* getAnimNodeForInput(
size_t node_index,
const std::string& input_name) {
@@ -749,6 +770,8 @@ struct AnimGraph {
}
}
result.UpdateOrderedNodes();
return result;
}
};