2022-02-11 16:51:18 +01:00
|
|
|
//
|
|
|
|
// Created by martin on 11.02.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AnimGraphEditor.h"
|
|
|
|
|
|
|
|
#include "AnimGraphResource.h"
|
2022-03-25 11:46:44 +01:00
|
|
|
#include "imgui.h"
|
2022-02-12 12:06:25 +01:00
|
|
|
#include "imnodes.h"
|
2022-03-25 11:46:44 +01:00
|
|
|
#include "misc/cpp/imgui_stdlib.h"
|
2022-02-18 23:33:30 +01:00
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
|
|
|
|
switch (socket_type) {
|
|
|
|
case SocketType::SocketTypeAnimation:
|
|
|
|
return ImNodesPinShape_QuadFilled;
|
|
|
|
case SocketType::SocketTypeFloat:
|
|
|
|
return ImNodesPinShape_CircleFilled;
|
|
|
|
case SocketType::SocketTypeVec3:
|
|
|
|
return ImNodesPinShape_TriangleFilled;
|
|
|
|
case SocketType::SocketTypeQuat:
|
|
|
|
return ImNodesPinShape_Triangle;
|
|
|
|
case SocketType::SocketTypeBool:
|
|
|
|
return ImNodesPinShape_Circle;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ImNodesPinShape_Quad;
|
|
|
|
}
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-03-25 11:46:44 +01:00
|
|
|
void NodeSocketEditor(Socket& socket) {
|
|
|
|
int mode_current = static_cast<int>(socket.m_type);
|
|
|
|
ImGui::InputText("Name", &socket.m_name);
|
|
|
|
if (ImGui::Combo(
|
|
|
|
"Type",
|
|
|
|
&mode_current,
|
|
|
|
SocketTypeNames,
|
|
|
|
sizeof(SocketTypeNames) / sizeof(char*))) {
|
|
|
|
socket.m_type = static_cast<SocketType>(mode_current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveConnectionsForSocket(
|
|
|
|
AnimGraphResource& graph_resource,
|
|
|
|
AnimNodeResource& node_resource,
|
|
|
|
Socket& socket) {
|
2022-04-01 13:19:54 +02:00
|
|
|
std::vector<AnimGraphConnectionResource>::iterator iter =
|
2022-03-25 11:46:44 +01:00
|
|
|
graph_resource.m_connections.begin();
|
|
|
|
|
|
|
|
while (iter != graph_resource.m_connections.end()) {
|
2022-04-01 13:19:54 +02:00
|
|
|
// TODO adjust for refactor
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
// AnimGraphConnectionResource& connection = *iter;
|
|
|
|
// if (connection.m_source_node == &node_resource
|
|
|
|
// && connection.m_source_socket == &socket) {
|
|
|
|
// iter = graph_resource.m_connections.erase(iter);
|
|
|
|
// } else {
|
|
|
|
// iter++;
|
|
|
|
// }
|
2022-03-25 11:46:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimGraphEditorRenderSidebar(
|
|
|
|
AnimGraphResource& graph_resource,
|
|
|
|
AnimNodeResource& node_resource) {
|
2022-02-18 22:24:19 +01:00
|
|
|
ImGui::Text("[%s]", node_resource.m_type_name.c_str());
|
|
|
|
|
|
|
|
char node_name_buffer[256];
|
|
|
|
memset(node_name_buffer, 0, sizeof(node_name_buffer));
|
|
|
|
strncpy(
|
|
|
|
node_name_buffer,
|
|
|
|
node_resource.m_name.c_str(),
|
|
|
|
std::min(node_resource.m_name.size(), sizeof(node_name_buffer)));
|
|
|
|
|
|
|
|
if (ImGui::InputText("Name", node_name_buffer, sizeof(node_name_buffer))) {
|
|
|
|
node_resource.m_name = node_name_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_properties = node_resource.m_socket_accessor->m_properties.size();
|
|
|
|
for (int i = 0; i < num_properties; i++) {
|
|
|
|
Socket& property = node_resource.m_socket_accessor->m_properties[i];
|
|
|
|
if (property.m_type == SocketType::SocketTypeFloat) {
|
|
|
|
ImGui::SliderFloat(
|
|
|
|
property.m_name.c_str(),
|
|
|
|
reinterpret_cast<float*>(property.m_value.ptr),
|
|
|
|
-100.f,
|
|
|
|
100.f);
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeBool) {
|
|
|
|
ImGui::Checkbox(
|
|
|
|
property.m_name.c_str(),
|
|
|
|
reinterpret_cast<bool*>(property.m_value.ptr));
|
|
|
|
} else if (property.m_type == SocketType::SocketTypeString) {
|
|
|
|
std::string* property_string =
|
|
|
|
reinterpret_cast<std::string*>(property.m_value.ptr);
|
|
|
|
char string_buf[256];
|
|
|
|
memset(string_buf, 0, sizeof(string_buf));
|
|
|
|
strncpy(
|
|
|
|
string_buf,
|
|
|
|
property_string->c_str(),
|
|
|
|
std::min(property_string->size(), sizeof(string_buf)));
|
|
|
|
if (ImGui::InputText(
|
|
|
|
property.m_name.c_str(),
|
|
|
|
string_buf,
|
|
|
|
sizeof(string_buf))) {
|
|
|
|
(*property_string) = string_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
if (&node_resource == &graph_resource.getGraphOutputNode()) {
|
|
|
|
ImGui::Text("Outputs");
|
|
|
|
|
|
|
|
// Graph outputs are the inputs of the output node!
|
|
|
|
std::vector<Socket>& outputs = node_resource.m_socket_accessor->m_inputs;
|
|
|
|
|
|
|
|
std::vector<Socket>::iterator iter = outputs.begin();
|
|
|
|
while (iter != outputs.end()) {
|
|
|
|
Socket& output = *iter;
|
|
|
|
ImGui::PushID(&output);
|
|
|
|
NodeSocketEditor(output);
|
|
|
|
if (ImGui::Button("X")) {
|
|
|
|
RemoveConnectionsForSocket(graph_resource, node_resource, output);
|
|
|
|
iter = outputs.erase(iter);
|
|
|
|
} else {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
ImGui::PopID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&node_resource == &graph_resource.getGraphInputNode()) {
|
|
|
|
ImGui::Text("Inputs");
|
|
|
|
|
|
|
|
// Graph inputs are the outputs of the input node!
|
|
|
|
std::vector<Socket>& inputs = node_resource.m_socket_accessor->m_outputs;
|
|
|
|
|
|
|
|
std::vector<Socket>::iterator iter = inputs.begin();
|
|
|
|
while (iter != inputs.end()) {
|
|
|
|
Socket& input = *iter;
|
|
|
|
ImGui::PushID(&input);
|
|
|
|
NodeSocketEditor(input);
|
|
|
|
if (ImGui::Button("X")) {
|
|
|
|
RemoveConnectionsForSocket(graph_resource, node_resource, input);
|
|
|
|
iter = inputs.erase(iter);
|
|
|
|
} else {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
ImGui::PopID();
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 22:24:19 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
void AnimGraphEditorUpdate() {
|
2022-02-15 20:57:59 +01:00
|
|
|
static AnimGraphResource graph_resource = AnimGraphResource();
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
ImGui::BeginMenuBar();
|
|
|
|
if (ImGui::Button("Save")) {
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.saveToFile("editor_graph.json");
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
if (ImGui::Button("Load")) {
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.loadFromFile("editor_graph.json");
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-02-15 20:57:59 +01:00
|
|
|
for (size_t i = 0, n = graph_resource.m_nodes.size(); i < n; i++) {
|
|
|
|
const AnimNodeResource& node_resource = graph_resource.m_nodes[i];
|
2022-02-12 12:06:25 +01:00
|
|
|
ImNodes::SetNodeGridSpacePos(
|
|
|
|
i,
|
|
|
|
ImVec2(node_resource.m_position[0], node_resource.m_position[1]));
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ImGui::Button("Clear")) {
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.clear();
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
char graph_name_buffer[256];
|
2022-02-12 12:06:25 +01:00
|
|
|
memset(graph_name_buffer, 0, sizeof(graph_name_buffer));
|
|
|
|
strncpy(
|
|
|
|
graph_name_buffer,
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.m_name.c_str(),
|
2022-02-12 12:06:25 +01:00
|
|
|
sizeof(graph_name_buffer));
|
2022-02-11 16:51:18 +01:00
|
|
|
if (ImGui::InputText("Name", graph_name_buffer, sizeof(graph_name_buffer))) {
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.m_name = graph_name_buffer;
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenuBar();
|
|
|
|
|
2022-02-18 22:24:19 +01:00
|
|
|
ImGui::Columns(2);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Node editor canvas
|
|
|
|
//
|
2022-02-11 16:51:18 +01:00
|
|
|
ImNodes::BeginNodeEditor();
|
|
|
|
|
2022-02-12 12:06:25 +01:00
|
|
|
// Popup menu
|
|
|
|
{
|
|
|
|
const bool open_popup =
|
|
|
|
ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)
|
|
|
|
&& ImNodes::IsEditorHovered()
|
|
|
|
&& ImGui::IsMouseReleased(ImGuiMouseButton_Right);
|
|
|
|
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.f, 8.f));
|
|
|
|
if (!ImGui::IsAnyItemHovered() && open_popup) {
|
|
|
|
ImGui::OpenPopup("add node");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("add node")) {
|
|
|
|
const ImVec2 click_pos = ImGui::GetMousePosOnOpeningCurrentPopup();
|
|
|
|
std::string node_type_name = "";
|
|
|
|
if (ImGui::MenuItem("AnimSampler")) {
|
|
|
|
node_type_name = "AnimSampler";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("Blend2")) {
|
|
|
|
node_type_name = "Blend2";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("SpeedScale")) {
|
|
|
|
node_type_name = "SpeedScale";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node_type_name != "") {
|
|
|
|
AnimNodeResource node_resource =
|
|
|
|
AnimNodeResourceFactory(node_type_name);
|
2022-02-15 20:57:59 +01:00
|
|
|
size_t node_id = graph_resource.m_nodes.size();
|
2022-02-12 12:06:25 +01:00
|
|
|
ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.m_nodes.push_back(node_resource);
|
2022-02-12 12:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PopStyleVar(ImGuiStyleVar_WindowPadding);
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 21:06:12 +01:00
|
|
|
for (size_t i = 0, n = graph_resource.m_nodes.size(); i < n; i++) {
|
2022-02-15 20:57:59 +01:00
|
|
|
AnimNodeResource& node_resource = graph_resource.m_nodes[i];
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
ImNodes::BeginNode(i);
|
|
|
|
|
|
|
|
// Header
|
|
|
|
ImNodes::BeginNodeTitleBar();
|
2022-03-25 11:02:22 +01:00
|
|
|
if (&node_resource == &graph_resource.getGraphOutputNode()) {
|
2022-02-15 21:06:12 +01:00
|
|
|
ImGui::TextUnformatted("Graph Outputs");
|
2022-03-25 11:02:22 +01:00
|
|
|
} else if (&node_resource == &graph_resource.getGraphInputNode()) {
|
2022-02-15 21:06:12 +01:00
|
|
|
ImGui::TextUnformatted("Graph Inputs");
|
|
|
|
} else {
|
|
|
|
ImGui::TextUnformatted(node_resource.m_type_name.c_str());
|
|
|
|
}
|
2022-02-11 16:51:18 +01:00
|
|
|
ImNodes::EndNodeTitleBar();
|
|
|
|
|
|
|
|
// Inputs
|
2022-02-12 12:06:25 +01:00
|
|
|
const std::vector<Socket>& node_inputs =
|
|
|
|
node_resource.m_socket_accessor->m_inputs;
|
2022-02-11 16:51:18 +01:00
|
|
|
for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) {
|
|
|
|
const Socket& socket = node_inputs[j];
|
2022-02-18 23:33:30 +01:00
|
|
|
|
|
|
|
ImColor socket_color = ImColor(255, 255, 255, 255);
|
|
|
|
if (socket.m_flags & SocketFlagAffectsTime) {
|
|
|
|
socket_color = ImColor(255, 128, 128, 255);
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
ImNodes::BeginInputAttribute(
|
|
|
|
GenerateInputAttributeId(i, j),
|
|
|
|
sGetSocketShapeFromSocketType(socket.m_type),
|
2022-02-18 23:33:30 +01:00
|
|
|
socket_color);
|
2022-02-19 12:16:57 +01:00
|
|
|
ImGui::TextUnformatted(socket.m_name.c_str());
|
2022-02-15 20:57:59 +01:00
|
|
|
|
2022-02-18 22:24:19 +01:00
|
|
|
ImNodes::PushAttributeFlag(
|
|
|
|
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
|
2022-02-11 16:51:18 +01:00
|
|
|
ImNodes::EndInputAttribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs
|
2022-02-12 12:06:25 +01:00
|
|
|
const std::vector<Socket>& node_outputs =
|
|
|
|
node_resource.m_socket_accessor->m_outputs;
|
2022-02-11 16:51:18 +01:00
|
|
|
for (size_t j = 0, ni = node_outputs.size(); j < ni; j++) {
|
|
|
|
const Socket& socket = node_outputs[j];
|
2022-02-14 22:37:19 +01:00
|
|
|
ImNodes::BeginOutputAttribute(
|
|
|
|
GenerateOutputAttributeId(i, j),
|
|
|
|
sGetSocketShapeFromSocketType(socket.m_type),
|
|
|
|
ImColor(255, 255, 255, 255));
|
2022-02-19 12:16:57 +01:00
|
|
|
ImGui::TextUnformatted(socket.m_name.c_str());
|
2022-02-18 22:24:19 +01:00
|
|
|
ImNodes::PushAttributeFlag(
|
|
|
|
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
|
2022-02-11 16:51:18 +01:00
|
|
|
ImNodes::EndInputAttribute();
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:06:12 +01:00
|
|
|
// Graph output node
|
|
|
|
if (i == 0) {
|
|
|
|
if (ImGui::Button("+Output")) {
|
2022-02-18 22:24:19 +01:00
|
|
|
AnimNodeResource& graph_output_node =
|
|
|
|
graph_resource.getGraphOutputNode();
|
2022-02-15 21:06:12 +01:00
|
|
|
|
|
|
|
static float bla = 0.f;
|
|
|
|
std::string socket_name = "Output";
|
2022-02-18 22:24:19 +01:00
|
|
|
socket_name += std::to_string(
|
|
|
|
graph_output_node.m_socket_accessor->m_inputs.size());
|
2022-02-15 21:06:12 +01:00
|
|
|
graph_output_node.m_socket_accessor->RegisterInput<float>(
|
|
|
|
socket_name,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
} else if (i == 1) {
|
|
|
|
if (ImGui::Button("+Input")) {
|
|
|
|
AnimNodeResource& graph_input_node = graph_resource.getGraphInputNode();
|
|
|
|
|
|
|
|
static float bla = 0.f;
|
|
|
|
std::string socket_name = "Input";
|
2022-02-18 22:24:19 +01:00
|
|
|
socket_name += std::to_string(
|
|
|
|
graph_input_node.m_socket_accessor->m_outputs.size());
|
2022-02-15 21:06:12 +01:00
|
|
|
graph_input_node.m_socket_accessor->RegisterOutput<float>(
|
|
|
|
socket_name,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
// Save state in node resource
|
|
|
|
ImVec2 node_pos = ImNodes::GetNodeGridSpacePos(i);
|
|
|
|
node_resource.m_position[0] = node_pos[0];
|
|
|
|
node_resource.m_position[1] = node_pos[1];
|
|
|
|
ImNodes::EndNode();
|
2022-02-18 23:33:30 +01:00
|
|
|
|
|
|
|
// Ensure flags such as SocketFlagAffectsTime are properly set.
|
|
|
|
node_resource.m_socket_accessor->UpdateFlags();
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:57:59 +01:00
|
|
|
for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) {
|
2022-04-01 13:19:54 +02:00
|
|
|
const AnimGraphConnectionResource& connection =
|
|
|
|
graph_resource.m_connections[i];
|
2022-02-11 16:51:18 +01:00
|
|
|
int start_attr, end_attr;
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
const AnimNodeResource& source_node =
|
|
|
|
graph_resource.m_nodes[connection.source_node_index];
|
|
|
|
int source_socket_index = source_node.m_socket_accessor->GetOutputIndex(
|
|
|
|
connection.source_socket_name);
|
|
|
|
|
|
|
|
const AnimNodeResource& target_node =
|
|
|
|
graph_resource.m_nodes[connection.target_node_index];
|
|
|
|
int target_socket_index = target_node.m_socket_accessor->GetInputIndex(
|
|
|
|
connection.target_socket_name);
|
|
|
|
|
|
|
|
start_attr = GenerateOutputAttributeId(
|
|
|
|
connection.source_node_index,
|
|
|
|
source_socket_index);
|
|
|
|
end_attr = GenerateInputAttributeId(
|
|
|
|
connection.target_node_index,
|
|
|
|
target_socket_index);
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
ImNodes::Link(i, start_attr, end_attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImNodes::EndNodeEditor();
|
|
|
|
|
2022-02-15 20:57:59 +01:00
|
|
|
// Handle newly created links.
|
2022-02-11 16:51:18 +01:00
|
|
|
int start_attr, end_attr;
|
|
|
|
if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) {
|
|
|
|
int node_start_id;
|
|
|
|
int node_start_output_index;
|
2022-02-12 12:06:25 +01:00
|
|
|
SplitOutputAttributeId(
|
|
|
|
start_attr,
|
|
|
|
&node_start_id,
|
|
|
|
&node_start_output_index);
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
int node_end_id;
|
|
|
|
int node_end_input_index;
|
|
|
|
SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index);
|
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
AnimGraphConnectionResource connection;
|
|
|
|
connection.source_node_index = node_start_id;
|
|
|
|
const AnimNodeResource& source_node = graph_resource.m_nodes[node_start_id];
|
|
|
|
connection.source_socket_name =
|
|
|
|
source_node.m_socket_accessor->m_outputs[node_start_output_index]
|
|
|
|
.m_name;
|
|
|
|
|
|
|
|
connection.target_node_index = node_end_id;
|
|
|
|
const AnimNodeResource& target_node = graph_resource.m_nodes[node_end_id];
|
|
|
|
connection.target_socket_name =
|
|
|
|
target_node.m_socket_accessor->m_inputs[node_end_input_index].m_name;
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-02-15 20:57:59 +01:00
|
|
|
graph_resource.m_connections.push_back(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle link detachements.
|
|
|
|
int link_id = 0;
|
|
|
|
if (ImNodes::IsLinkDestroyed(&link_id)) {
|
2022-02-18 22:24:19 +01:00
|
|
|
graph_resource.m_connections.erase(
|
|
|
|
graph_resource.m_connections.begin() + link_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int selected_nodes[ImNodes::NumSelectedNodes()];
|
|
|
|
ImNodes::GetSelectedNodes(selected_nodes);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sidebar
|
|
|
|
//
|
|
|
|
ImGui::NextColumn();
|
|
|
|
|
|
|
|
if (ImNodes::NumSelectedNodes() == 1) {
|
|
|
|
if (selected_nodes[0] < graph_resource.m_nodes.size()) {
|
|
|
|
AnimNodeResource& selected_node =
|
|
|
|
graph_resource.m_nodes[selected_nodes[0]];
|
2022-03-25 11:46:44 +01:00
|
|
|
AnimGraphEditorRenderSidebar(graph_resource, selected_node);
|
2022-02-18 22:24:19 +01:00
|
|
|
}
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
2022-02-18 22:24:19 +01:00
|
|
|
|
|
|
|
ImGui::Columns(1);
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|