Skip to content

Commit f6a6aad

Browse files
committed
Better deselection handling in AnimationTree editors
1 parent 825ef23 commit f6a6aad

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

editor/animation/animation_blend_space_1d_editor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEven
143143
return;
144144
}
145145
}
146+
147+
// If no point was selected, select host BlendSpace1D node.
148+
if (selected_point == -1) {
149+
EditorNode::get_singleton()->push_item(blend_space.ptr(), "", true);
150+
}
146151
}
147152

148153
if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {
@@ -529,7 +534,11 @@ void AnimationNodeBlendSpace1DEditor::_erase_selected() {
529534
undo_redo->add_undo_method(this, "_update_space");
530535
undo_redo->commit_action();
531536

537+
// Return selection to host BlendSpace1D node.
538+
EditorNode::get_singleton()->push_item(blend_space.ptr(), "", true);
539+
532540
updating = false;
541+
_update_tool_erase();
533542

534543
blend_space_draw->queue_redraw();
535544
}

editor/animation/animation_blend_space_2d_editor.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEven
203203
}
204204
}
205205
}
206+
207+
// If no point or triangle was selected, select host BlendSpace2D node.
208+
if (selected_point == -1 && selected_triangle == -1) {
209+
EditorNode::get_singleton()->push_item(blend_space.ptr(), "", true);
210+
}
206211
}
207212

208213
if (mb.is_valid() && mb->is_pressed() && tool_triangle->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
@@ -727,7 +732,12 @@ void AnimationNodeBlendSpace2DEditor::_erase_selected() {
727732
undo_redo->add_do_method(this, "_update_space");
728733
undo_redo->add_undo_method(this, "_update_space");
729734
undo_redo->commit_action();
735+
736+
// Return selection to host BlendSpace2D node.
737+
EditorNode::get_singleton()->push_item(blend_space.ptr(), "", true);
738+
730739
updating = false;
740+
_update_tool_erase();
731741

732742
blend_space_draw->queue_redraw();
733743
} else if (selected_triangle != -1) {
@@ -739,7 +749,11 @@ void AnimationNodeBlendSpace2DEditor::_erase_selected() {
739749
undo_redo->add_do_method(this, "_update_space");
740750
undo_redo->add_undo_method(this, "_update_space");
741751
undo_redo->commit_action();
752+
753+
selected_triangle = -1;
754+
742755
updating = false;
756+
_update_tool_erase();
743757

744758
blend_space_draw->queue_redraw();
745759
}

editor/animation/animation_blend_tree_editor_plugin.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ void AnimationNodeBlendTreeEditor::update_graph() {
127127

128128
visible_properties.clear();
129129

130+
// Store selected nodes before clearing the graph.
131+
List<StringName> selected_nodes;
132+
for (int i = 0; i < graph->get_child_count(); i++) {
133+
GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
134+
if (gn && gn->is_selected()) {
135+
selected_nodes.push_back(gn->get_name());
136+
}
137+
}
138+
130139
graph->set_scroll_offset(blend_tree->get_graph_offset() * EDSCALE);
131140

132141
graph->clear_connections();
@@ -304,6 +313,17 @@ void AnimationNodeBlendTreeEditor::update_graph() {
304313
graph->set_minimap_opacity(graph_minimap_opacity);
305314
float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
306315
graph->set_connection_lines_curvature(graph_lines_curvature);
316+
317+
// Restore selected nodes after graph reconstruction.
318+
for (const StringName &name : selected_nodes) {
319+
for (int i = 0; i < graph->get_child_count(); i++) {
320+
GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
321+
if (gn && gn->get_name() == name) {
322+
gn->set_selected(true);
323+
break;
324+
}
325+
}
326+
}
307327
}
308328

309329
void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
@@ -539,6 +559,9 @@ void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {
539559
undo_redo->add_do_method(this, "update_graph");
540560
undo_redo->add_undo_method(this, "update_graph");
541561
undo_redo->commit_action();
562+
563+
// Return selection to host BlendTree node.
564+
EditorNode::get_singleton()->push_item(blend_tree.ptr(), "", true);
542565
}
543566

544567
void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
@@ -597,6 +620,22 @@ void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
597620
EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
598621
}
599622

623+
void AnimationNodeBlendTreeEditor::_node_deselected(Object *p_node) {
624+
// Check if no nodes are selected, return selection to host BlendTree node.
625+
bool any_selected = false;
626+
for (int i = 0; i < graph->get_child_count(); i++) {
627+
GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
628+
if (gn && gn->is_selected()) {
629+
any_selected = true;
630+
break;
631+
}
632+
}
633+
634+
if (!any_selected) {
635+
EditorNode::get_singleton()->push_item(blend_tree.ptr(), "", true);
636+
}
637+
}
638+
600639
void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
601640
Ref<AnimationNode> an = blend_tree->get_node(p_which);
602641
ERR_FAIL_COND(an.is_null());
@@ -1192,6 +1231,7 @@ AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
11921231
graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);
11931232
graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);
11941233
graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
1234+
graph->connect("node_deselected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_deselected));
11951235
graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
11961236
graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
11971237
graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));

editor/animation/animation_blend_tree_editor_plugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class AnimationNodeBlendTreeEditor : public AnimationTreeNodeEditorPlugin {
111111

112112
void _scroll_changed(const Vector2 &p_scroll);
113113
void _node_selected(Object *p_node);
114+
void _node_deselected(Object *p_node);
114115
void _open_in_editor(const String &p_which);
115116
void _anim_selected(int p_index, const Array &p_options, const String &p_node);
116117
void _delete_node_request(const String &p_which);

editor/animation/animation_state_machine_editor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEv
284284
}
285285
}
286286

287+
// If no state or transition was selected, select host StateMachine node.
288+
if (selected_node.is_empty() && selected_transition_index == -1) {
289+
EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true);
290+
}
291+
287292
state_machine_draw->queue_redraw();
288293
_update_mode();
289294
}
@@ -1648,6 +1653,10 @@ void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action
16481653

16491654
connected_nodes.clear();
16501655
selected_nodes.clear();
1656+
selected_node = StringName();
1657+
1658+
// Return selection to host StateMachine node.
1659+
EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true);
16511660
}
16521661

16531662
if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {
@@ -1668,8 +1677,12 @@ void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action
16681677
selected_transition_from = StringName();
16691678
selected_transition_to = StringName();
16701679
selected_transition_index = -1;
1680+
1681+
// Return selection to host StateMachine node.
1682+
EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true);
16711683
}
16721684

1685+
_update_mode();
16731686
state_machine_draw->queue_redraw();
16741687
}
16751688

0 commit comments

Comments
 (0)