AnimTestbed/src/AnimGraph/AnimGraphEditor.cc

426 lines
13 KiB
C++
Raw Normal View History

//
// Created by martin on 11.02.22.
//
#include "AnimGraphEditor.h"
#include "AnimGraphResource.h"
2022-03-25 11:46:44 +01:00
#include "imgui.h"
#include "imnodes.h"
2022-03-25 11:46:44 +01:00
#include "misc/cpp/imgui_stdlib.h"
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-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) {
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()) {
// 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_reference.ptr),
2022-02-18 22:24:19 +01:00
-100.f,
100.f);
} else if (property.m_type == SocketType::SocketTypeBool) {
ImGui::Checkbox(
property.m_name.c_str(),
reinterpret_cast<bool*>(property.m_reference.ptr));
2022-02-18 22:24:19 +01:00
} else if (property.m_type == SocketType::SocketTypeString) {
std::string* property_string =
reinterpret_cast<std::string*>(property.m_reference.ptr);
2022-02-18 22:24:19 +01:00
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
}
void AnimGraphEditorUpdate() {
2022-02-15 20:57:59 +01:00
static AnimGraphResource graph_resource = AnimGraphResource();
ImGui::BeginMenuBar();
if (ImGui::Button("Save")) {
2022-02-15 20:57:59 +01:00
graph_resource.saveToFile("editor_graph.json");
}
if (ImGui::Button("Load")) {
2022-02-15 20:57:59 +01:00
graph_resource.loadFromFile("editor_graph.json");
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];
ImNodes::SetNodeGridSpacePos(
i,
ImVec2(node_resource.m_position[0], node_resource.m_position[1]));
}
}
if (ImGui::Button("Clear")) {
2022-02-15 20:57:59 +01:00
graph_resource.clear();
}
char graph_name_buffer[256];
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(),
sizeof(graph_name_buffer));
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;
}
ImGui::EndMenuBar();
2022-02-18 22:24:19 +01:00
ImGui::Columns(2);
//
// Node editor canvas
//
ImNodes::BeginNodeEditor();
// 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 (ImGui::MenuItem("MathAddNode")) {
node_type_name = "MathAddNode";
}
if (ImGui::MenuItem("MathFloatToVec3Node")) {
node_type_name = "MathFloatToVec3Node";
}
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();
ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
2022-02-15 20:57:59 +01:00
graph_resource.m_nodes.push_back(node_resource);
}
ImGui::EndPopup();
}
ImGui::PopStyleVar(ImGuiStyleVar_WindowPadding);
}
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];
ImNodes::BeginNode(i);
ImGui::PushItemWidth(110.0f);
// Header
ImNodes::BeginNodeTitleBar();
if (&node_resource == &graph_resource.getGraphOutputNode()) {
2022-02-15 21:06:12 +01:00
ImGui::TextUnformatted("Graph Outputs");
} 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());
}
ImNodes::EndNodeTitleBar();
// Inputs
const std::vector<Socket>& node_inputs =
node_resource.m_socket_accessor->m_inputs;
for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) {
const Socket& socket = node_inputs[j];
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),
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
bool socket_connected = graph_resource.isSocketConnected(node_resource, socket.m_name);
if (!socket_connected &&
(socket.m_type == SocketType::SocketTypeFloat)) {
ImGui::SameLine();
float socket_value = 0.f;
ImGui::PushItemWidth(100.0f - ImGui::CalcTextSize(socket.m_name.c_str()).x);
ImGui::DragFloat("##hidelabel", &socket_value, 0.01f);
ImGui::PopItemWidth();
}
2022-02-18 22:24:19 +01:00
ImNodes::PushAttributeFlag(
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::EndInputAttribute();
}
// Outputs
const std::vector<Socket>& node_outputs =
node_resource.m_socket_accessor->m_outputs;
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);
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);
}
}
// 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];
ImGui::PopItemWidth();
ImNodes::EndNode();
// Ensure flags such as SocketFlagAffectsTime are properly set.
node_resource.m_socket_accessor->UpdateFlags();
}
2022-02-15 20:57:59 +01:00
for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) {
const AnimGraphConnectionResource& connection =
graph_resource.m_connections[i];
int start_attr, end_attr;
2022-03-25 11:46:44 +01: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
ImNodes::Link(i, start_attr, end_attr);
}
ImNodes::EndNodeEditor();
2022-02-15 20:57:59 +01:00
// Handle newly created links.
int start_attr, end_attr;
if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) {
int node_start_id;
int node_start_output_index;
SplitOutputAttributeId(
start_attr,
&node_start_id,
&node_start_output_index);
int node_end_id;
int node_end_input_index;
SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index);
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-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-18 22:24:19 +01:00
ImGui::Columns(1);
}