WIP: new anim graph editor can show loaded graph.

RefactorUnifiedBlendTreeStateMachineHandling
Martin Felis 2024-03-03 20:20:24 +01:00
parent e61180c4a7
commit a79ffaa420
1 changed files with 49 additions and 40 deletions

View File

@ -14,6 +14,7 @@
#include "misc/cpp/imgui_stdlib.h" #include "misc/cpp/imgui_stdlib.h"
static AnimGraphResource sGraphGresource = AnimGraphResource(); static AnimGraphResource sGraphGresource = AnimGraphResource();
static bool sGraphLoadedThisFrame = false;
ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) { ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
switch (socket_type) { switch (socket_type) {
@ -36,6 +37,14 @@ ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
return ImNodesPinShape_Quad; return ImNodesPinShape_Quad;
} }
int GetNodeInputSocketId(int node_index, int input_socket_index) {
return node_index * 1000 + input_socket_index;
}
int GetNodeOutputSocketId(int node_index, int output_socket_index) {
return node_index * 1000 + 100 + output_socket_index;
}
void NodeSocketEditor(Socket& socket) { void NodeSocketEditor(Socket& socket) {
int mode_current = static_cast<int>(socket.m_type); int mode_current = static_cast<int>(socket.m_type);
ImGui::InputText("Name", &socket.m_name); ImGui::InputText("Name", &socket.m_name);
@ -181,9 +190,7 @@ void AnimGraphEditorRenderSidebar(
100.f); 100.f);
} else if (property.m_type == SocketType::SocketTypeBool) { } else if (property.m_type == SocketType::SocketTypeBool) {
bool flag_value = property.GetValue<bool>(); bool flag_value = property.GetValue<bool>();
if (ImGui::Checkbox( if (ImGui::Checkbox(property.m_name.c_str(), &flag_value)) {
property.m_name.c_str(),
&flag_value)) {
property.SetValue(flag_value); property.SetValue(flag_value);
} }
} else if (property.m_type == SocketType::SocketTypeString) { } else if (property.m_type == SocketType::SocketTypeString) {
@ -254,6 +261,7 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
} }
if (ImGui::Button("Load")) { if (ImGui::Button("Load")) {
sGraphGresource.loadFromFile("editor_graph.json"); sGraphGresource.loadFromFile("editor_graph.json");
sGraphLoadedThisFrame = true;
// for (size_t i = 0, n = sGraphGresource.m_nodes.size(); i < n; i++) { // for (size_t i = 0, n = sGraphGresource.m_nodes.size(); i < n; i++) {
// const AnimNodeResource& node_resource = sGraphGresource.m_nodes[i]; // const AnimNodeResource& node_resource = sGraphGresource.m_nodes[i];
@ -283,6 +291,7 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
ax::NodeEditor::SetCurrentEditor(context); ax::NodeEditor::SetCurrentEditor(context);
ax::NodeEditor::Begin("Graph Editor"); ax::NodeEditor::Begin("Graph Editor");
#if 1
for (size_t node_id = 0, n = sGraphGresource.m_nodes.size(); node_id < n; for (size_t node_id = 0, n = sGraphGresource.m_nodes.size(); node_id < n;
node_id++) { node_id++) {
AnimNodeResource& node_resource = sGraphGresource.m_nodes[node_id]; AnimNodeResource& node_resource = sGraphGresource.m_nodes[node_id];
@ -291,7 +300,11 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
// continue; // continue;
} }
if (sGraphLoadedThisFrame) {
ax::NodeEditor::SetNodePosition(
node_id,
ImVec2(node_resource.m_position[0], node_resource.m_position[1]));
}
ax::NodeEditor::BeginNode(node_id); ax::NodeEditor::BeginNode(node_id);
ImGui::Text("%s", node_resource.m_type_name.c_str()); ImGui::Text("%s", node_resource.m_type_name.c_str());
@ -300,19 +313,21 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
node_resource.m_socket_accessor->m_inputs; node_resource.m_socket_accessor->m_inputs;
for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) { for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) {
Socket& socket = node_inputs[j]; Socket& socket = node_inputs[j];
int pin_id = static_cast<int>(node_id) + j * 1000; ax::NodeEditor::BeginPin(
ax::NodeEditor::BeginPin(pin_id, ax::NodeEditor::PinKind::Input); GetNodeInputSocketId(static_cast<int>(node_id), static_cast<int>(j)),
ax::NodeEditor::PinKind::Input);
ImGui::Text("%s", socket.m_name.c_str()); ImGui::Text("%s", socket.m_name.c_str());
ax::NodeEditor::EndPin(); ax::NodeEditor::EndPin();
} }
// Outputs // Outputs
const std::vector<Socket>& node_outputs = std::vector<Socket>& node_outputs =
node_resource.m_socket_accessor->m_outputs; node_resource.m_socket_accessor->m_outputs;
for (size_t j = 0, ni = node_outputs.size(); j < ni; j++) { for (size_t j = 0, ni = node_outputs.size(); j < ni; j++) {
const Socket& socket = node_outputs[j]; Socket& socket = node_outputs[j];
int pin_id = static_cast<int>(node_id) + j * 1000 * 1000; ax::NodeEditor::BeginPin(
ax::NodeEditor::BeginPin(pin_id, ax::NodeEditor::PinKind::Output); GetNodeOutputSocketId(static_cast<int>(node_id), static_cast<int>(j)),
ax::NodeEditor::PinKind::Output);
ImGui::Text("%s", socket.m_name.c_str()); ImGui::Text("%s", socket.m_name.c_str());
ax::NodeEditor::EndPin(); ax::NodeEditor::EndPin();
} }
@ -320,37 +335,28 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
ax::NodeEditor::EndNode(); ax::NodeEditor::EndNode();
} }
#if 0 int link_id = 0;
int unique_id = 1; for (size_t connection_id = 0, n = sGraphGresource.m_connections.size(); connection_id < n;
ax::NodeEditor::BeginNode(unique_id++); connection_id++) {
const AnimGraphConnectionResource& connection_resource = sGraphGresource.m_connections[connection_id];
// Node A const AnimNodeResource& source_node_resource = sGraphGresource.m_nodes[connection_resource.source_node_index];
ImGui::Text("Node A"); int source_socket_index = source_node_resource.m_socket_accessor->GetOutputIndex(connection_resource.source_socket_name.c_str());
ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Input);
ImGui::Text("In");
ax::NodeEditor::EndPin();
ImGui::SameLine();
ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Output);
ImGui::Text("Out");
ax::NodeEditor::EndPin();
ax::NodeEditor::EndNode(); const AnimNodeResource& target_node_resource = sGraphGresource.m_nodes[connection_resource.target_node_index];
int target_socket_index = target_node_resource.m_socket_accessor->GetInputIndex(connection_resource.target_socket_name.c_str());
// Node B int source_socket_id = GetNodeOutputSocketId(static_cast<int>(connection_resource.source_node_index), source_socket_index);
ax::NodeEditor::BeginNode(unique_id++); int target_socket_id = GetNodeInputSocketId(static_cast<int>(connection_resource.target_node_index), target_socket_index);
ax::NodeEditor::Link(link_id++, source_socket_id, target_socket_id);
}
ImGui::Text("Node B");
ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Input);
ImGui::Text("In");
ax::NodeEditor::EndPin();
ImGui::SameLine();
ax::NodeEditor::BeginPin(unique_id++, ax::NodeEditor::PinKind::Output);
ImGui::Text("Out");
ax::NodeEditor::EndPin();
ax::NodeEditor::EndNode();
#endif #endif
#if 1
// Create Connections // Create Connections
if (ax::NodeEditor::BeginCreate()) { if (ax::NodeEditor::BeginCreate()) {
ax::NodeEditor::PinId input_pin_id, output_pin_id; ax::NodeEditor::PinId input_pin_id, output_pin_id;
@ -362,9 +368,12 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
} }
} }
ax::NodeEditor::EndCreate(); ax::NodeEditor::EndCreate();
#endif
ax::NodeEditor::End(); ax::NodeEditor::End();
sGraphLoadedThisFrame = false;
ax::NodeEditor::SetCurrentEditor(nullptr); ax::NodeEditor::SetCurrentEditor(nullptr);
} }