Minor refactor.

RefactorUnifiedBlendTreeStateMachineHandling
Martin Felis 2024-05-07 18:50:38 +02:00
parent 698abbce4b
commit 99d5a5eb0f
2 changed files with 56 additions and 17 deletions

View File

@ -46,16 +46,23 @@ ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
return ImNodesPinShape_Quad; return ImNodesPinShape_Quad;
} }
void NodeSocketEditor(Socket& socket) { bool NodeSocketEditor(Socket& socket) {
bool modified = false;
int mode_current = static_cast<int>(socket.m_type); int mode_current = static_cast<int>(socket.m_type);
ImGui::InputText("Name", &socket.m_name); if (ImGui::InputText("Name", &socket.m_name)) {
modified = true;
}
if (ImGui::Combo( if (ImGui::Combo(
"Type", "Type",
&mode_current, &mode_current,
SocketTypeNames, SocketTypeNames,
sizeof(SocketTypeNames) / sizeof(char*))) { sizeof(SocketTypeNames) / sizeof(char*))) {
socket.m_type = static_cast<SocketType>(mode_current); socket.m_type = static_cast<SocketType>(mode_current);
modified = true;
} }
return modified;
} }
void RemoveBlendTreeConnectionsForSocket( void RemoveBlendTreeConnectionsForSocket(
@ -227,7 +234,12 @@ void AnimGraphEditorRenderSidebar(
while (iter != outputs.end()) { while (iter != outputs.end()) {
Socket& output = *iter; Socket& output = *iter;
ImGui::PushID(&output); ImGui::PushID(&output);
NodeSocketEditor(output); if (NodeSocketEditor(output)) {
AnimGraphResource* current_graph_resource =
sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex];
current_graph_resource->m_socket_accessor->m_inputs = outputs;
}
if (ImGui::Button("X")) { if (ImGui::Button("X")) {
RemoveBlendTreeConnectionsForSocket( RemoveBlendTreeConnectionsForSocket(
blend_tree_resource, blend_tree_resource,
@ -239,6 +251,13 @@ void AnimGraphEditorRenderSidebar(
} }
ImGui::PopID(); ImGui::PopID();
} }
if (ImGui::Button("+")) {
AnimGraphResource* current_graph_resource =
sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex];
current_graph_resource->RegisterBlendTreeOutputSocket<float>(
"GraphFloatOutput");
}
} }
if (node_resource == blend_tree_resource.GetGraphInputNode()) { if (node_resource == blend_tree_resource.GetGraphInputNode()) {
@ -251,7 +270,11 @@ void AnimGraphEditorRenderSidebar(
while (iter != inputs.end()) { while (iter != inputs.end()) {
Socket& input = *iter; Socket& input = *iter;
ImGui::PushID(&input); ImGui::PushID(&input);
NodeSocketEditor(input); if (NodeSocketEditor(input)) {
AnimGraphResource* current_graph_resource =
sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex];
current_graph_resource->m_socket_accessor->m_inputs = inputs;
}
if (ImGui::Button("X")) { if (ImGui::Button("X")) {
RemoveBlendTreeConnectionsForSocket( RemoveBlendTreeConnectionsForSocket(
blend_tree_resource, blend_tree_resource,
@ -263,6 +286,13 @@ void AnimGraphEditorRenderSidebar(
} }
ImGui::PopID(); ImGui::PopID();
} }
if (ImGui::Button("+")) {
AnimGraphResource* current_graph_resource =
sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex];
current_graph_resource->RegisterBlendTreeInputSocket<float>(
"GraphFloatInput");
}
} }
} }
@ -271,12 +301,16 @@ void AnimGraphEditorClear() {
ax::NodeEditor::ClearSelection(); ax::NodeEditor::ClearSelection();
} }
if (sEditorState.rootGraphResource) {
delete sEditorState.rootGraphResource->m_socket_accessor;
}
delete sEditorState.rootGraphResource; delete sEditorState.rootGraphResource;
sEditorState.rootGraphResource = new AnimGraphResource(); sEditorState.rootGraphResource = new AnimGraphResource();
sEditorState.rootGraphResource->m_name = "Root"; sEditorState.rootGraphResource->m_name = "Root";
sEditorState.rootGraphResource->m_graph_type_name = "BlendTree"; sEditorState.rootGraphResource->m_graph_type_name = "BlendTree";
sEditorState.rootGraphResource->m_blend_tree_resource.InitGraphConnectors(); sEditorState.rootGraphResource->m_blend_tree_resource.InitGraphConnectors();
sEditorState.rootGraphResource->m_socket_accessor = new NodeDescriptorBase;
sEditorState.hierarchyStack.clear(); sEditorState.hierarchyStack.clear();
sEditorState.hierarchyStack.push_back(sEditorState.rootGraphResource); sEditorState.hierarchyStack.push_back(sEditorState.rootGraphResource);
@ -456,17 +490,18 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
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;
if (ax::NodeEditor::QueryNewLink(&input_pin_id, &output_pin_id)) { if (ax::NodeEditor::QueryNewLink(&input_pin_id, &output_pin_id)) {
if (input_pin_id && output_pin_id) { int source_node_index;
int source_node_index; int source_node_socket_index;
int source_node_socket_index; const AnimNodeResource* source_node = nullptr;
const Socket* source_socket = nullptr; const Socket* source_socket = nullptr;
if (input_pin_id) {
OutputPinIdToNodeIndexAndSocketIndex( OutputPinIdToNodeIndexAndSocketIndex(
input_pin_id.Get(), input_pin_id.Get(),
&source_node_index, &source_node_index,
&source_node_socket_index); &source_node_socket_index);
const AnimNodeResource* source_node = source_node =
sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex] sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex]
->m_blend_tree_resource.GetNode(source_node_index); ->m_blend_tree_resource.GetNode(source_node_index);
if (source_node->m_socket_accessor->m_outputs.size() if (source_node->m_socket_accessor->m_outputs.size()
@ -479,17 +514,20 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
source_node, source_node,
source_node_socket_index); source_node_socket_index);
} }
}
int target_node_index; int target_node_index;
int target_node_socket_index; int target_node_socket_index;
const Socket* target_socket = nullptr; const AnimNodeResource* target_node = nullptr;
const Socket* target_socket = nullptr;
if (output_pin_id) {
InputPinIdToNodeIndexAndSocketIndex( InputPinIdToNodeIndexAndSocketIndex(
output_pin_id.Get(), output_pin_id.Get(),
&target_node_index, &target_node_index,
&target_node_socket_index); &target_node_socket_index);
const AnimNodeResource* target_node = target_node =
sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex] sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex]
->m_blend_tree_resource.GetNode(target_node_index); ->m_blend_tree_resource.GetNode(target_node_index);
if (target_node->m_socket_accessor->m_inputs.size() if (target_node->m_socket_accessor->m_inputs.size()
@ -502,7 +540,9 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) {
target_node, target_node,
target_node_socket_index); target_node_socket_index);
} }
}
if (input_pin_id && output_pin_id) {
if (source_socket == nullptr || target_socket == nullptr if (source_socket == nullptr || target_socket == nullptr
|| !sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex] || !sEditorState.hierarchyStack[sEditorState.hierarchyStackIndex]
->m_blend_tree_resource.IsConnectionValid( ->m_blend_tree_resource.IsConnectionValid(

View File

@ -182,6 +182,7 @@ AnimNodeResource* sAnimGraphNodeFromJson(
if (node_type == "BlendTree") { if (node_type == "BlendTree") {
AnimGraphResource* result = new AnimGraphResource(); AnimGraphResource* result = new AnimGraphResource();
result->m_socket_accessor = new NodeDescriptorBase;
sAnimGraphResourceBlendTreeFromJson(json_node, result); sAnimGraphResourceBlendTreeFromJson(json_node, result);
return result; return result;
} }
@ -330,8 +331,7 @@ static bool sAnimGraphResourceBlendTreeFromJson(
if (json_data["nodes"][0].contains("inputs")) { if (json_data["nodes"][0].contains("inputs")) {
const json& graph_outputs = json_data["nodes"][0]["inputs"]; const json& graph_outputs = json_data["nodes"][0]["inputs"];
for (const auto& graph_output : graph_outputs) { for (const auto& graph_output : graph_outputs) {
AnimNodeResource* graph_node = blend_tree_resource.GetNode(0); result_graph_resource->RegisterBlendTreeOutputSocket(
graph_node->m_socket_accessor->m_inputs.push_back(
sJsonToSocket(graph_output)); sJsonToSocket(graph_output));
} }
} }
@ -340,8 +340,7 @@ static bool sAnimGraphResourceBlendTreeFromJson(
if (json_data["nodes"][1].contains("outputs")) { if (json_data["nodes"][1].contains("outputs")) {
const json& graph_inputs = json_data["nodes"][1]["outputs"]; const json& graph_inputs = json_data["nodes"][1]["outputs"];
for (const auto& graph_input : graph_inputs) { for (const auto& graph_input : graph_inputs) {
AnimNodeResource* graph_node = blend_tree_resource.GetNode(1); result_graph_resource->RegisterBlendTreeInputSocket(
graph_node->m_socket_accessor->m_outputs.push_back(
sJsonToSocket(graph_input)); sJsonToSocket(graph_input));
} }
} }