| 1 | /**************************************************************************/ |
| 2 | /* animation_state_machine_editor.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "animation_state_machine_editor.h" |
| 32 | |
| 33 | #include "core/io/resource_loader.h" |
| 34 | #include "core/math/geometry_2d.h" |
| 35 | #include "core/os/keyboard.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "editor/editor_undo_redo_manager.h" |
| 38 | #include "editor/gui/editor_file_dialog.h" |
| 39 | #include "editor/settings/editor_settings.h" |
| 40 | #include "editor/themes/editor_scale.h" |
| 41 | #include "scene/animation/animation_blend_tree.h" |
| 42 | #include "scene/gui/line_edit.h" |
| 43 | #include "scene/gui/option_button.h" |
| 44 | #include "scene/gui/panel_container.h" |
| 45 | #include "scene/gui/separator.h" |
| 46 | #include "scene/main/viewport.h" |
| 47 | #include "scene/main/window.h" |
| 48 | #include "scene/resources/style_box_flat.h" |
| 49 | #include "scene/theme/theme_db.h" |
| 50 | |
| 51 | bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) { |
| 52 | Ref<AnimationNodeStateMachine> ansm = p_node; |
| 53 | return ansm.is_valid(); |
| 54 | } |
| 55 | |
| 56 | void AnimationNodeStateMachineEditor::edit(const Ref<AnimationNode> &p_node) { |
| 57 | state_machine = p_node; |
| 58 | |
| 59 | read_only = false; |
| 60 | |
| 61 | if (state_machine.is_valid()) { |
| 62 | read_only = EditorNode::get_singleton()->is_resource_read_only(state_machine); |
| 63 | |
| 64 | selected_transition_from = StringName(); |
| 65 | selected_transition_to = StringName(); |
| 66 | selected_transition_index = -1; |
| 67 | selected_node = StringName(); |
| 68 | selected_nodes.clear(); |
| 69 | connected_nodes.clear(); |
| 70 | _update_mode(); |
| 71 | _update_graph(); |
| 72 | } |
| 73 | |
| 74 | if (read_only) { |
| 75 | tool_create->set_pressed(false); |
| 76 | tool_connect->set_pressed(false); |
| 77 | } |
| 78 | |
| 79 | tool_create->set_disabled(read_only); |
| 80 | tool_connect->set_disabled(read_only); |
| 81 | } |
| 82 | |
| 83 | String AnimationNodeStateMachineEditor::_get_root_playback_path(String &r_node_directory) { |
| 84 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 85 | Vector<String> edited_path = AnimationTreeEditor::get_singleton()->get_edited_path(); |
| 86 | |
| 87 | String base_path; |
| 88 | Vector<String> node_directory_path; |
| 89 | |
| 90 | bool is_playable_anodesm_found = false; |
| 91 | |
| 92 | if (edited_path.size()) { |
| 93 | while (!is_playable_anodesm_found) { |
| 94 | base_path = String("/").join(edited_path); |
| 95 | Ref<AnimationNodeStateMachine> anodesm = !edited_path.size() ? Ref<AnimationNode>(tree->get_root_animation_node().ptr()) : tree->get_root_animation_node()->find_node_by_path(base_path); |
| 96 | if (anodesm.is_null()) { |
| 97 | break; |
| 98 | } else { |
| 99 | if (anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) { |
| 100 | is_playable_anodesm_found = true; |
| 101 | } else { |
| 102 | int idx = edited_path.size() - 1; |
| 103 | node_directory_path.push_back(edited_path[idx]); |
| 104 | edited_path.remove_at(idx); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (is_playable_anodesm_found) { |
| 111 | // Return Root/Nested state machine playback. |
| 112 | node_directory_path.reverse(); |
| 113 | r_node_directory = String("/").join(node_directory_path); |
| 114 | if (node_directory_path.size()) { |
| 115 | r_node_directory += "/"; |
| 116 | } |
| 117 | base_path = !edited_path.size() ? Animation::PARAMETERS_BASE_PATH + "playback" : Animation::PARAMETERS_BASE_PATH + base_path + "/playback"; |
| 118 | } else { |
| 119 | // Hmmm, we have to return Grouped state machine playback... |
| 120 | // It will give the user the error that Root/Nested state machine should be retrieved, that would be kind :-) |
| 121 | r_node_directory = String(); |
| 122 | base_path = AnimationTreeEditor::get_singleton()->get_base_path() + "playback"; |
| 123 | } |
| 124 | |
| 125 | return base_path; |
| 126 | } |
| 127 | |
| 128 | void AnimationNodeStateMachineEditor::_reconnect_transition() { |
| 129 | if (reconnecting_transition_index < 0 || reconnecting_transition_target.is_empty()) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | StringName old_from = state_machine->get_transition_from(reconnecting_transition_index); |
| 134 | StringName old_to = state_machine->get_transition_to(reconnecting_transition_index); |
| 135 | StringName new_from; |
| 136 | StringName new_to; |
| 137 | |
| 138 | if (reconnecting_transition_start) { |
| 139 | new_from = reconnecting_transition_target; |
| 140 | new_to = old_to; |
| 141 | } else { |
| 142 | new_from = old_from; |
| 143 | new_to = reconnecting_transition_target; |
| 144 | } |
| 145 | |
| 146 | // Preserve transition properties. |
| 147 | Ref<AnimationNodeStateMachineTransition> transition = state_machine->get_transition(reconnecting_transition_index); |
| 148 | |
| 149 | updating = true; |
| 150 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 151 | undo_redo->create_action(TTR("Reconnect Transition")); |
| 152 | |
| 153 | // Remove old transition. |
| 154 | undo_redo->add_do_method(state_machine.ptr(), "remove_transition", old_from, old_to); |
| 155 | undo_redo->add_undo_method(state_machine.ptr(), "add_transition", old_from, old_to, transition); |
| 156 | |
| 157 | // Add new transition. |
| 158 | undo_redo->add_do_method(state_machine.ptr(), "add_transition", new_from, new_to, transition); |
| 159 | undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", new_from, new_to); |
| 160 | |
| 161 | undo_redo->add_do_method(this, "_select_transition", new_from, new_to); |
| 162 | undo_redo->add_undo_method(this, "_select_transition", old_from, old_to); |
| 163 | |
| 164 | undo_redo->add_do_method(this, "_update_graph"); |
| 165 | undo_redo->add_undo_method(this, "_update_graph"); |
| 166 | undo_redo->commit_action(); |
| 167 | updating = false; |
| 168 | |
| 169 | selected_transition_from = new_from; |
| 170 | selected_transition_to = new_to; |
| 171 | _update_mode(); |
| 172 | } |
| 173 | |
| 174 | void AnimationNodeStateMachineEditor::_select_transition(const StringName &p_from, const StringName &p_to) { |
| 175 | selected_transition_from = p_from; |
| 176 | selected_transition_to = p_to; |
| 177 | selected_transition_index = -1; |
| 178 | |
| 179 | // Find transition index. |
| 180 | for (int i = 0; i < state_machine->get_transition_count(); i++) { |
| 181 | if (state_machine->get_transition_from(i) == p_from && state_machine->get_transition_to(i) == p_to) { |
| 182 | selected_transition_index = i; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | selected_node = StringName(); |
| 188 | selected_nodes.clear(); |
| 189 | |
| 190 | connected_nodes.clear(); |
| 191 | connected_nodes.insert(selected_transition_from); |
| 192 | connected_nodes.insert(selected_transition_to); |
| 193 | |
| 194 | // Push transition to the inspector. |
| 195 | if (selected_transition_index >= 0) { |
| 196 | Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(selected_transition_index); |
| 197 | if (!state_machine->is_transition_across_group(selected_transition_index)) { |
| 198 | EditorNode::get_singleton()->push_item(tr.ptr(), "", true); |
| 199 | } else { |
| 200 | EditorNode::get_singleton()->push_item(tr.ptr(), "", true); |
| 201 | EditorNode::get_singleton()->push_item(nullptr, "", true); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | _update_mode(); |
| 206 | } |
| 207 | |
| 208 | void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEvent> &p_event) { |
| 209 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 210 | if (!tree) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | String node_directory; |
| 215 | Ref<AnimationNodeStateMachinePlayback> playback = tree->get(_get_root_playback_path(node_directory)); |
| 216 | if (playback.is_null()) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | Ref<InputEventKey> k = p_event; |
| 221 | if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) { |
| 222 | if (selected_node != StringName() || !selected_nodes.is_empty() || selected_transition_to != StringName() || selected_transition_from != StringName()) { |
| 223 | if (!read_only) { |
| 224 | _erase_selected(); |
| 225 | } |
| 226 | accept_event(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | Ref<InputEventMouseButton> mb = p_event; |
| 231 | |
| 232 | // Add new node |
| 233 | if (!read_only) { |
| 234 | if (mb.is_valid() && mb->is_pressed() && !box_selecting && !connecting && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == MouseButton::LEFT))) { |
| 235 | connecting_from = StringName(); |
| 236 | _open_menu(mb->get_position()); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Select node or push a field inside |
| 241 | if (mb.is_valid() && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
| 242 | selected_transition_from = StringName(); |
| 243 | selected_transition_to = StringName(); |
| 244 | selected_transition_index = -1; |
| 245 | selected_node = StringName(); |
| 246 | connected_nodes.clear(); |
| 247 | |
| 248 | for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order |
| 249 | if (node_rects[i].play.has_point(mb->get_position())) { //edit name |
| 250 | if (play_mode->get_selected() == 1 || !playback->is_playing()) { |
| 251 | // Start |
| 252 | playback->start(node_directory + String(node_rects[i].node_name)); |
| 253 | } else { |
| 254 | // Travel |
| 255 | playback->travel(node_directory + String(node_rects[i].node_name)); |
| 256 | } |
| 257 | state_machine_draw->queue_redraw(); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (!read_only) { |
| 262 | if (node_rects[i].name.has_point(mb->get_position()) && state_machine->can_edit_node(node_rects[i].node_name)) { // edit name |
| 263 | // TODO: Avoid using strings, expose a method on LineEdit. |
| 264 | Ref<StyleBox> line_sb = name_edit->get_theme_stylebox(CoreStringName(normal)); |
| 265 | Rect2 edit_rect = node_rects[i].name; |
| 266 | edit_rect.position -= line_sb->get_offset(); |
| 267 | edit_rect.size += line_sb->get_minimum_size(); |
| 268 | |
| 269 | name_edit_popup->set_position(state_machine_draw->get_screen_position() + edit_rect.position); |
| 270 | name_edit_popup->set_size(edit_rect.size); |
| 271 | name_edit->set_text(node_rects[i].node_name); |
| 272 | name_edit_popup->popup(); |
| 273 | name_edit->grab_focus(); |
| 274 | name_edit->select_all(); |
| 275 | |
| 276 | prev_name = node_rects[i].node_name; |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (node_rects[i].edit.has_point(mb->get_position())) { //edit name |
| 282 | callable_mp(this, &AnimationNodeStateMachineEditor::_open_editor).call_deferred(node_rects[i].node_name); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected |
| 287 | selected_node = node_rects[i].node_name; |
| 288 | |
| 289 | if (!selected_nodes.has(selected_node)) { |
| 290 | selected_nodes.clear(); |
| 291 | } |
| 292 | |
| 293 | selected_nodes.insert(selected_node); |
| 294 | _update_connected_nodes(selected_node); |
| 295 | |
| 296 | Ref<AnimationNode> anode = state_machine->get_node(selected_node); |
| 297 | EditorNode::get_singleton()->push_item(anode.ptr(), "", true); |
| 298 | state_machine_draw->queue_redraw(); |
| 299 | dragging_selected_attempt = true; |
| 300 | dragging_selected = false; |
| 301 | drag_from = mb->get_position(); |
| 302 | snap_x = StringName(); |
| 303 | snap_y = StringName(); |
| 304 | _update_mode(); |
| 305 | return; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Test the transition lines. |
| 310 | int closest = -1; |
| 311 | float closest_d = 1e20; |
| 312 | Vector<int> close_candidates; |
| 313 | |
| 314 | // First find closest lines using point-to-segment distance. |
| 315 | for (int i = 0; i < transition_lines.size(); i++) { |
| 316 | Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mb->get_position(), transition_lines[i].from, transition_lines[i].to); |
| 317 | float d = cpoint.distance_to(mb->get_position()); |
| 318 | |
| 319 | if (d > transition_lines[i].width) { |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | // If this is very close to our current closest distance, add it to candidates. |
| 324 | if (Math::abs(d - closest_d) < 2.0) { // Within 2 pixels. |
| 325 | close_candidates.push_back(i); |
| 326 | } else if (d < closest_d) { |
| 327 | closest_d = d; |
| 328 | closest = i; |
| 329 | close_candidates.clear(); |
| 330 | close_candidates.push_back(i); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Use midpoint distance as bias. |
| 335 | if (close_candidates.size() > 1) { |
| 336 | float best_midpoint_dist = 1e20; |
| 337 | |
| 338 | for (int idx : close_candidates) { |
| 339 | Vector2 midpoint = (transition_lines[idx].from + transition_lines[idx].to) / 2.0; |
| 340 | float midpoint_dist = midpoint.distance_to(mb->get_position()); |
| 341 | |
| 342 | if (midpoint_dist < best_midpoint_dist) { |
| 343 | best_midpoint_dist = midpoint_dist; |
| 344 | closest = idx; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (closest >= 0) { |
| 350 | _select_transition(transition_lines[closest].from_node, transition_lines[closest].to_node); |
| 351 | } |
| 352 | |
| 353 | // If no state or transition was selected, select host StateMachine node. |
| 354 | if (selected_node.is_empty() && selected_transition_index == -1) { |
| 355 | EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true); |
| 356 | } |
| 357 | |
| 358 | state_machine_draw->queue_redraw(); |
| 359 | _update_mode(); |
| 360 | } |
| 361 | |
| 362 | // End moving node |
| 363 | if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) { |
| 364 | if (dragging_selected) { |
| 365 | Ref<AnimationNode> an = state_machine->get_node(selected_node); |
| 366 | updating = true; |
| 367 | |
| 368 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 369 | undo_redo->create_action(TTR("Move Node")); |
| 370 | |
| 371 | for (int i = 0; i < node_rects.size(); i++) { |
| 372 | if (!selected_nodes.has(node_rects[i].node_name)) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | undo_redo->add_do_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name) + drag_ofs / EDSCALE); |
| 377 | undo_redo->add_undo_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name)); |
| 378 | } |
| 379 | |
| 380 | undo_redo->add_do_method(this, "_update_graph"); |
| 381 | undo_redo->add_undo_method(this, "_update_graph"); |
| 382 | undo_redo->commit_action(); |
| 383 | updating = false; |
| 384 | } |
| 385 | snap_x = StringName(); |
| 386 | snap_y = StringName(); |
| 387 | |
| 388 | dragging_selected_attempt = false; |
| 389 | dragging_selected = false; |
| 390 | state_machine_draw->queue_redraw(); |
| 391 | } |
| 392 | |
| 393 | // Connect nodes |
| 394 | if (mb.is_valid() && ((tool_select->is_pressed() && mb->is_shift_pressed()) || tool_connect->is_pressed()) && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) { |
| 395 | for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order |
| 396 | if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected |
| 397 | connecting = true; |
| 398 | connection_follows_cursor = true; |
| 399 | connecting_from = node_rects[i].node_name; |
| 400 | connecting_to = mb->get_position(); |
| 401 | connecting_to_node = StringName(); |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // End connecting nodes |
| 408 | if (mb.is_valid() && connecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) { |
| 409 | if (connecting_to_node != StringName()) { |
| 410 | Ref<AnimationNode> node = state_machine->get_node(connecting_to_node); |
| 411 | Ref<AnimationNodeStateMachine> anodesm = node; |
| 412 | Ref<AnimationNodeEndState> end_node = node; |
| 413 | |
| 414 | if (state_machine->has_transition(connecting_from, connecting_to_node) && state_machine->can_edit_node(connecting_to_node) && anodesm.is_null()) { |
| 415 | EditorNode::get_singleton()->show_warning(TTR("Transition exists!")); |
| 416 | connecting = false; |
| 417 | } else { |
| 418 | _add_transition(); |
| 419 | } |
| 420 | } else { |
| 421 | _open_menu(mb->get_position()); |
| 422 | } |
| 423 | connecting_to_node = StringName(); |
| 424 | connection_follows_cursor = false; |
| 425 | state_machine_draw->queue_redraw(); |
| 426 | } |
| 427 | |
| 428 | // Start transition reconnection. |
| 429 | if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
| 430 | // Check if we're clicking on a hovered transition endpoint to start dragging. |
| 431 | if (hovered_transition_index >= 0 && !read_only) { |
| 432 | reconnecting = true; |
| 433 | reconnecting_transition_index = hovered_transition_index; |
| 434 | reconnecting_transition_start = hovered_transition_start; |
| 435 | reconnecting_transition_pos = mb->get_position(); |
| 436 | reconnecting_transition_target = StringName(); |
| 437 | |
| 438 | StringName connected_node = reconnecting_transition_start ? transition_lines[reconnecting_transition_index].to_node : transition_lines[reconnecting_transition_index].from_node; |
| 439 | |
| 440 | reconnecting_from_node_rect_index = -1; |
| 441 | for (int i = 0; i < node_rects.size(); i++) { |
| 442 | if (node_rects[i].node_name == connected_node) { |
| 443 | reconnecting_from_node_rect_index = i; |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Clear other selections when starting transition drag. |
| 449 | selected_transition_from = StringName(); |
| 450 | selected_transition_to = StringName(); |
| 451 | selected_transition_index = -1; |
| 452 | selected_node = StringName(); |
| 453 | selected_nodes.clear(); |
| 454 | connected_nodes.clear(); |
| 455 | |
| 456 | state_machine_draw->queue_redraw(); |
| 457 | return; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // End transition reconnection. |
| 462 | if (mb.is_valid() && reconnecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) { |
| 463 | if (reconnecting_transition_target != StringName()) { |
| 464 | // Check if reconnection is valid. |
| 465 | StringName old_from = state_machine->get_transition_from(reconnecting_transition_index); |
| 466 | StringName old_to = state_machine->get_transition_to(reconnecting_transition_index); |
| 467 | StringName new_from = reconnecting_transition_start ? reconnecting_transition_target : old_from; |
| 468 | StringName new_to = reconnecting_transition_start ? old_to : reconnecting_transition_target; |
| 469 | |
| 470 | if (new_from == old_from && new_to == old_to) { |
| 471 | // No change. |
| 472 | } else if (new_from == new_to) { |
| 473 | EditorNode::get_singleton()->show_warning(TTR("Cannot transition to self!")); |
| 474 | } else if (new_to == SceneStringName(Start)) { |
| 475 | EditorNode::get_singleton()->show_warning(TTR("Cannot transition to \"Start\"!")); |
| 476 | } else if (new_from == SceneStringName(End)) { |
| 477 | EditorNode::get_singleton()->show_warning(TTR("Cannot transition from \"End\"!")); |
| 478 | } else if (state_machine->has_transition(new_from, new_to)) { |
| 479 | EditorNode::get_singleton()->show_warning(vformat(TTR("Transition from \"%s\" to \"%s\" already exists!"), new_from, new_to)); |
| 480 | } else { |
| 481 | _reconnect_transition(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Reset dragging state. |
| 486 | reconnecting = false; |
| 487 | reconnecting_transition_index = -1; |
| 488 | reconnecting_transition_start = false; |
| 489 | reconnecting_transition_target = StringName(); |
| 490 | state_machine_draw->queue_redraw(); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | // Start box selecting |
| 495 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && tool_select->is_pressed()) { |
| 496 | box_selecting = true; |
| 497 | box_selecting_from = box_selecting_to = state_machine_draw->get_local_mouse_position(); |
| 498 | box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs()); |
| 499 | |
| 500 | if (mb->is_command_or_control_pressed() || mb->is_shift_pressed()) { |
| 501 | previous_selected = selected_nodes; |
| 502 | } else { |
| 503 | selected_nodes.clear(); |
| 504 | previous_selected.clear(); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // End box selecting |
| 509 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) { |
| 510 | box_selecting = false; |
| 511 | state_machine_draw->queue_redraw(); |
| 512 | _update_mode(); |
| 513 | } |
| 514 | |
| 515 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
| 516 | StringName clicked_node; |
| 517 | for (int i = node_rects.size() - 1; i >= 0; i--) { |
| 518 | if (node_rects[i].node.has_point(mb->get_position())) { |
| 519 | clicked_node = node_rects[i].node_name; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (clicked_node != StringName()) { |
| 525 | if (selected_nodes.has(clicked_node) && mb->is_shift_pressed()) { |
| 526 | selected_nodes.erase(clicked_node); |
| 527 | } else { |
| 528 | if (!mb->is_shift_pressed()) { |
| 529 | selected_nodes.clear(); |
| 530 | } |
| 531 | selected_nodes.insert(clicked_node); |
| 532 | } |
| 533 | selected_node = clicked_node; |
| 534 | } else { |
| 535 | // Clicked on empty space. |
| 536 | selected_nodes.clear(); |
| 537 | selected_node = StringName(); |
| 538 | } |
| 539 | |
| 540 | _update_connected_nodes(selected_node); |
| 541 | state_machine_draw->queue_redraw(); |
| 542 | _update_mode(); |
| 543 | |
| 544 | if (clicked_node != StringName()) { |
| 545 | Ref<AnimationNode> anode = state_machine->get_node(clicked_node); |
| 546 | EditorNode::get_singleton()->push_item(anode.ptr(), "", true); |
| 547 | dragging_selected_attempt = true; |
| 548 | dragging_selected = false; |
| 549 | drag_from = mb->get_position(); |
| 550 | snap_x = StringName(); |
| 551 | snap_y = StringName(); |
| 552 | } |
| 553 | |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | Ref<InputEventMouseMotion> mm = p_event; |
| 558 | |
| 559 | // Pan window |
| 560 | if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) { |
| 561 | h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x); |
| 562 | v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y); |
| 563 | } |
| 564 | |
| 565 | // Move mouse while connecting |
| 566 | if (mm.is_valid() && connecting && connection_follows_cursor && !read_only) { |
| 567 | connecting_to = mm->get_position(); |
| 568 | connecting_to_node = StringName(); |
| 569 | state_machine_draw->queue_redraw(); |
| 570 | |
| 571 | for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order |
| 572 | if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected |
| 573 | connecting_to_node = node_rects[i].node_name; |
| 574 | return; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // Move mouse while reconnecting. |
| 580 | if (mm.is_valid() && reconnecting && !read_only) { |
| 581 | reconnecting_transition_pos = mm->get_position(); |
| 582 | reconnecting_transition_target = StringName(); |
| 583 | reconnecting_to_node_rect_index = -1; |
| 584 | |
| 585 | for (int i = node_rects.size() - 1; i >= 0; i--) { |
| 586 | if (node_rects[i].node.has_point(reconnecting_transition_pos)) { |
| 587 | reconnecting_transition_target = node_rects[i].node_name; |
| 588 | reconnecting_to_node_rect_index = i; |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | state_machine_draw->queue_redraw(); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | // Move mouse while moving a node |
| 598 | if (mm.is_valid() && dragging_selected_attempt && !read_only) { |
| 599 | dragging_selected = true; |
| 600 | drag_ofs = mm->get_position() - drag_from; |
| 601 | snap_x = StringName(); |
| 602 | snap_y = StringName(); |
| 603 | { |
| 604 | //snap |
| 605 | Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE; |
| 606 | LocalVector<StringName> nodes = state_machine->get_node_list(); |
| 607 | |
| 608 | float best_d_x = 1e20; |
| 609 | float best_d_y = 1e20; |
| 610 | |
| 611 | for (const StringName &E : nodes) { |
| 612 | if (E == selected_node) { |
| 613 | continue; |
| 614 | } |
| 615 | Vector2 npos = state_machine->get_node_position(E); |
| 616 | |
| 617 | float d_x = Math::abs(npos.x - cpos.x); |
| 618 | if (d_x < MIN(5, best_d_x)) { |
| 619 | drag_ofs.x -= cpos.x - npos.x; |
| 620 | best_d_x = d_x; |
| 621 | snap_x = E; |
| 622 | } |
| 623 | |
| 624 | float d_y = Math::abs(npos.y - cpos.y); |
| 625 | if (d_y < MIN(5, best_d_y)) { |
| 626 | drag_ofs.y -= cpos.y - npos.y; |
| 627 | best_d_y = d_y; |
| 628 | snap_y = E; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | state_machine_draw->queue_redraw(); |
| 634 | } |
| 635 | |
| 636 | // Move mouse while moving box select |
| 637 | if (mm.is_valid() && box_selecting) { |
| 638 | box_selecting_to = state_machine_draw->get_local_mouse_position(); |
| 639 | |
| 640 | box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs()); |
| 641 | |
| 642 | for (int i = 0; i < node_rects.size(); i++) { |
| 643 | bool in_box = node_rects[i].node.intersects(box_selecting_rect); |
| 644 | |
| 645 | if (in_box) { |
| 646 | if (previous_selected.has(node_rects[i].node_name)) { |
| 647 | selected_nodes.erase(node_rects[i].node_name); |
| 648 | } else { |
| 649 | selected_nodes.insert(node_rects[i].node_name); |
| 650 | } |
| 651 | } else { |
| 652 | if (previous_selected.has(node_rects[i].node_name)) { |
| 653 | selected_nodes.insert(node_rects[i].node_name); |
| 654 | } else { |
| 655 | selected_nodes.erase(node_rects[i].node_name); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | state_machine_draw->queue_redraw(); |
| 661 | } |
| 662 | |
| 663 | if (mm.is_valid()) { |
| 664 | String new_hovered_node_name; |
| 665 | HoveredNodeArea new_hovered_node_area = HOVER_NODE_NONE; |
| 666 | if (tool_select->is_pressed()) { |
| 667 | for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order. |
| 668 | |
| 669 | if (!state_machine->can_edit_node(node_rects[i].node_name)) { |
| 670 | continue; // start/end node can't be edited |
| 671 | } |
| 672 | |
| 673 | if (node_rects[i].node.has_point(mm->get_position())) { |
| 674 | new_hovered_node_name = node_rects[i].node_name; |
| 675 | if (node_rects[i].play.has_point(mm->get_position())) { |
| 676 | new_hovered_node_area = HOVER_NODE_PLAY; |
| 677 | } else if (node_rects[i].edit.has_point(mm->get_position())) { |
| 678 | new_hovered_node_area = HOVER_NODE_EDIT; |
| 679 | } |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (new_hovered_node_name != hovered_node_name || new_hovered_node_area != hovered_node_area) { |
| 686 | hovered_node_name = new_hovered_node_name; |
| 687 | hovered_node_area = new_hovered_node_area; |
| 688 | state_machine_draw->queue_redraw(); |
| 689 | } |
| 690 | |
| 691 | // Skip if over node. |
| 692 | for (int i = node_rects.size() - 1; i >= 0; i--) { |
| 693 | if (node_rects[i].node.has_point(mm->get_position())) { |
| 694 | if (hovered_transition_index != -1) { |
| 695 | hovered_transition_index = -1; |
| 696 | hovered_transition_start = false; |
| 697 | state_machine_draw->queue_redraw(); |
| 698 | } |
| 699 | state_machine_draw->set_tooltip_text(""); |
| 700 | return; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | // Set transition tooltip or reconnection endpoint. |
| 705 | if (tool_select->is_pressed()) { |
| 706 | int closest_for_highlight = -1; |
| 707 | int closest_for_tooltip = -1; |
| 708 | float closest_d_highlight = 1e20; |
| 709 | float closest_d_tooltip = 1e20; |
| 710 | bool closest_is_start = false; |
| 711 | |
| 712 | for (int i = 0; i < transition_lines.size(); i++) { |
| 713 | Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mm->get_position(), transition_lines[i].from, transition_lines[i].to); |
| 714 | float d = cpoint.distance_to(mm->get_position()); |
| 715 | if (d > transition_lines[i].width) { |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | // Check for tooltip (anywhere along the line). |
| 720 | if (d < closest_d_tooltip) { |
| 721 | closest_d_tooltip = d; |
| 722 | closest_for_tooltip = i; |
| 723 | } |
| 724 | |
| 725 | // Calculate dynamic hover distance based on transition length. |
| 726 | float transition_length = transition_lines[i].from.distance_to(transition_lines[i].to); |
| 727 | float hover_distance = MIN(20.0f, transition_length * 0.2f); // 20px or 20% of length, whichever is smaller. |
| 728 | |
| 729 | // Check distance to both endpoints for highlighting. |
| 730 | float dist_to_start = mm->get_position().distance_to(transition_lines[i].from); |
| 731 | float dist_to_end = mm->get_position().distance_to(transition_lines[i].to); |
| 732 | |
| 733 | bool near_start = (dist_to_start <= hover_distance); |
| 734 | bool near_end = (dist_to_end <= hover_distance); |
| 735 | |
| 736 | // Determine which end is closer if both are within range. |
| 737 | bool is_start_closer = dist_to_start < dist_to_end; |
| 738 | |
| 739 | if ((near_start || near_end) && d < closest_d_highlight) { |
| 740 | StringName from_node = transition_lines[i].from_node; |
| 741 | StringName to_node = transition_lines[i].to_node; |
| 742 | |
| 743 | bool is_start_endpoint = near_start && (is_start_closer || !near_end); |
| 744 | closest_d_highlight = d; |
| 745 | closest_for_highlight = i; |
| 746 | closest_is_start = is_start_endpoint; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // Update hovered endpoint for reconnection. |
| 751 | if (hovered_transition_index != closest_for_highlight || hovered_transition_start != closest_is_start) { |
| 752 | hovered_transition_index = closest_for_highlight; |
| 753 | hovered_transition_start = closest_is_start; |
| 754 | state_machine_draw->queue_redraw(); |
| 755 | } |
| 756 | |
| 757 | // Set tooltip for any part of the transition line. |
| 758 | if (closest_for_tooltip >= 0) { |
| 759 | String from = transition_lines[closest_for_tooltip].from_node; |
| 760 | String to = transition_lines[closest_for_tooltip].to_node; |
| 761 | String tooltip = from + U" → " + to; |
| 762 | state_machine_draw->set_tooltip_text(tooltip); |
| 763 | } else { |
| 764 | state_machine_draw->set_tooltip_text(""); |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | Ref<InputEventPanGesture> pan_gesture = p_event; |
| 770 | if (pan_gesture.is_valid()) { |
| 771 | h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8); |
| 772 | v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | Control::CursorShape AnimationNodeStateMachineEditor::get_cursor_shape(const Point2 &p_pos) const { |
| 777 | Control::CursorShape cursor_shape = get_default_cursor_shape(); |
| 778 | if (!read_only) { |
| 779 | // Put ibeam (text cursor) over names to make it clearer that they are editable. |
| 780 | Transform2D xform = panel->get_transform() * state_machine_draw->get_transform(); |
| 781 | Point2 pos = xform.xform_inv(p_pos); |
| 782 | |
| 783 | for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order. |
| 784 | if (node_rects[i].node.has_point(pos)) { |
| 785 | if (node_rects[i].name.has_point(pos)) { |
| 786 | if (state_machine->can_edit_node(node_rects[i].node_name)) { |
| 787 | cursor_shape = Control::CURSOR_IBEAM; |
| 788 | } |
| 789 | } |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | return cursor_shape; |
| 795 | } |
| 796 | |
| 797 | String AnimationNodeStateMachineEditor::get_tooltip(const Point2 &p_pos) const { |
| 798 | if (hovered_node_name == StringName()) { |
| 799 | return AnimationTreeNodeEditorPlugin::get_tooltip(p_pos); |
| 800 | } |
| 801 | |
| 802 | String tooltip_text; |
| 803 | if (hovered_node_area == HOVER_NODE_PLAY) { |
| 804 | tooltip_text = vformat(TTR("Play/Travel to %s"), hovered_node_name); |
| 805 | } else if (hovered_node_area == HOVER_NODE_EDIT) { |
| 806 | tooltip_text = vformat(TTR("Edit %s"), hovered_node_name); |
| 807 | } else { |
| 808 | tooltip_text = hovered_node_name; |
| 809 | } |
| 810 | |
| 811 | return tooltip_text; |
| 812 | } |
| 813 | |
| 814 | void AnimationNodeStateMachineEditor::_open_menu(const Vector2 &p_position) { |
| 815 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 816 | if (!tree) { |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | menu->clear(false); |
| 821 | animations_menu->clear(); |
| 822 | animations_to_add.clear(); |
| 823 | |
| 824 | List<StringName> animation_names; |
| 825 | tree->get_animation_list(&animation_names); |
| 826 | menu->add_submenu_node_item(TTR("Add Animation"), animations_menu); |
| 827 | if (animation_names.is_empty()) { |
| 828 | menu->set_item_disabled(menu->get_item_idx_from_text(TTR("Add Animation")), true); |
| 829 | } else { |
| 830 | for (const StringName &name : animation_names) { |
| 831 | animations_menu->add_icon_item(theme_cache.animation_icon, name); |
| 832 | animations_to_add.push_back(name); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | LocalVector<StringName> classes; |
| 837 | ClassDB::get_inheriters_from_class("AnimationRootNode", classes); |
| 838 | classes.sort_custom<StringName::AlphCompare>(); |
| 839 | |
| 840 | for (const StringName &class_name : classes) { |
| 841 | String name = String(class_name).replace_first("AnimationNode", ""); |
| 842 | if (name == "Animation" || name == "StartState" || name == "EndState") { |
| 843 | continue; // nope |
| 844 | } |
| 845 | int idx = menu->get_item_count(); |
| 846 | menu->add_item(vformat(TTR("Add %s"), name), idx); |
| 847 | menu->set_item_metadata(idx, class_name); |
| 848 | } |
| 849 | Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard(); |
| 850 | |
| 851 | if (clipb.is_valid()) { |
| 852 | menu->add_separator(); |
| 853 | menu->add_item(TTR("Paste"), MENU_PASTE); |
| 854 | } |
| 855 | menu->add_separator(); |
| 856 | menu->add_item(TTR("Load..."), MENU_LOAD_FILE); |
| 857 | |
| 858 | menu->set_position(state_machine_draw->get_screen_transform().xform(p_position)); |
| 859 | menu->popup(); |
| 860 | add_node_pos = p_position / EDSCALE + state_machine->get_graph_offset(); |
| 861 | } |
| 862 | |
| 863 | bool AnimationNodeStateMachineEditor::_create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path) { |
| 864 | String prev_path; |
| 865 | |
| 866 | LocalVector<StringName> nodes = p_nodesm->get_node_list(); |
| 867 | |
| 868 | PopupMenu *nodes_menu = memnew(PopupMenu); |
| 869 | nodes_menu->set_name(p_name); |
| 870 | nodes_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to)); |
| 871 | p_menu->add_child(nodes_menu); |
| 872 | |
| 873 | bool node_added = false; |
| 874 | for (const StringName &E : nodes) { |
| 875 | if (p_nodesm->can_edit_node(E)) { |
| 876 | Ref<AnimationNodeStateMachine> ansm = p_nodesm->get_node(E); |
| 877 | |
| 878 | String path = String(p_path) + "/" + E; |
| 879 | |
| 880 | if (ansm == state_machine) { |
| 881 | end_menu->add_item(E, nodes_to_connect.size()); |
| 882 | nodes_to_connect.push_back(SceneStringName(End)); |
| 883 | continue; |
| 884 | } |
| 885 | |
| 886 | if (ansm.is_valid()) { |
| 887 | state_machine_menu->add_item(E, nodes_to_connect.size()); |
| 888 | nodes_to_connect.push_back(path); |
| 889 | |
| 890 | if (_create_submenu(nodes_menu, ansm, E, path)) { |
| 891 | nodes_menu->add_submenu_item(E, E); |
| 892 | node_added = true; |
| 893 | } |
| 894 | } else { |
| 895 | nodes_menu->add_item(E, nodes_to_connect.size()); |
| 896 | nodes_to_connect.push_back(path); |
| 897 | node_added = true; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | return node_added; |
| 903 | } |
| 904 | |
| 905 | void AnimationNodeStateMachineEditor::_stop_connecting() { |
| 906 | connecting = false; |
| 907 | state_machine_draw->queue_redraw(); |
| 908 | } |
| 909 | |
| 910 | void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) { |
| 911 | file_loaded = ResourceLoader::load(p_file); |
| 912 | if (file_loaded.is_valid()) { |
| 913 | _add_menu_type(MENU_LOAD_FILE_CONFIRM); |
| 914 | } else { |
| 915 | EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed.")); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) { |
| 920 | String base_name; |
| 921 | Ref<AnimationRootNode> node; |
| 922 | |
| 923 | if (p_index == MENU_LOAD_FILE) { |
| 924 | open_file->clear_filters(); |
| 925 | List<String> filters; |
| 926 | ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters); |
| 927 | for (const String &E : filters) { |
| 928 | open_file->add_filter("*." + E); |
| 929 | } |
| 930 | open_file->popup_file_dialog(); |
| 931 | return; |
| 932 | } else if (p_index == MENU_LOAD_FILE_CONFIRM) { |
| 933 | node = file_loaded; |
| 934 | file_loaded.unref(); |
| 935 | } else if (p_index == MENU_PASTE) { |
| 936 | node = EditorSettings::get_singleton()->get_resource_clipboard(); |
| 937 | |
| 938 | } else { |
| 939 | String type = menu->get_item_metadata(p_index); |
| 940 | |
| 941 | Object *obj = ClassDB::instantiate(type); |
| 942 | ERR_FAIL_NULL(obj); |
| 943 | AnimationNode *an = Object::cast_to<AnimationNode>(obj); |
| 944 | ERR_FAIL_NULL(an); |
| 945 | |
| 946 | node = Ref<AnimationNode>(an); |
| 947 | base_name = type.replace_first("AnimationNode", ""); |
| 948 | } |
| 949 | |
| 950 | if (node.is_null()) { |
| 951 | EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed.")); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | if (base_name.is_empty()) { |
| 956 | base_name = node->get_class().replace_first("AnimationNode", ""); |
| 957 | } |
| 958 | |
| 959 | int base = 1; |
| 960 | String name = base_name; |
| 961 | while (state_machine->has_node(name)) { |
| 962 | base++; |
| 963 | name = base_name + " " + itos(base); |
| 964 | } |
| 965 | |
| 966 | updating = true; |
| 967 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 968 | undo_redo->create_action(TTR("Add Node and Transition")); |
| 969 | undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos); |
| 970 | undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name); |
| 971 | connecting_to_node = name; |
| 972 | _add_transition(true); |
| 973 | undo_redo->commit_action(); |
| 974 | updating = false; |
| 975 | |
| 976 | state_machine_draw->queue_redraw(); |
| 977 | } |
| 978 | |
| 979 | void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) { |
| 980 | Ref<AnimationNodeAnimation> anim; |
| 981 | anim.instantiate(); |
| 982 | |
| 983 | anim->set_animation(animations_to_add[p_index]); |
| 984 | |
| 985 | String base_name = animations_to_add[p_index].validate_node_name(); |
| 986 | int base = 1; |
| 987 | String name = base_name; |
| 988 | while (state_machine->has_node(name)) { |
| 989 | base++; |
| 990 | name = base_name + " " + itos(base); |
| 991 | } |
| 992 | |
| 993 | updating = true; |
| 994 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 995 | undo_redo->create_action(TTR("Add Node and Transition")); |
| 996 | undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos); |
| 997 | undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name); |
| 998 | connecting_to_node = name; |
| 999 | _add_transition(true); |
| 1000 | undo_redo->commit_action(); |
| 1001 | updating = false; |
| 1002 | |
| 1003 | state_machine_draw->queue_redraw(); |
| 1004 | } |
| 1005 | |
| 1006 | void AnimationNodeStateMachineEditor::_connect_to(int p_index) { |
| 1007 | connecting_to_node = nodes_to_connect[p_index]; |
| 1008 | _add_transition(); |
| 1009 | } |
| 1010 | |
| 1011 | void AnimationNodeStateMachineEditor::_add_transition(const bool p_nested_action) { |
| 1012 | if (connecting_from != StringName() && connecting_to_node != StringName()) { |
| 1013 | if (connecting_to_node == SceneStringName(Start)) { |
| 1014 | EditorNode::get_singleton()->show_warning(TTR("Cannot transition to \"Start\"!")); |
| 1015 | connecting = false; |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | if (connecting_from == SceneStringName(End)) { |
| 1020 | EditorNode::get_singleton()->show_warning(TTR("Cannot transition from \"End\"!")); |
| 1021 | connecting = false; |
| 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | if (state_machine->has_transition(connecting_from, connecting_to_node)) { |
| 1026 | EditorNode::get_singleton()->show_warning(TTR("Transition already exists!")); |
| 1027 | connecting = false; |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | Ref<AnimationNodeStateMachineTransition> tr; |
| 1032 | tr.instantiate(); |
| 1033 | tr->set_advance_mode(auto_advance->is_pressed() ? AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_AUTO : AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_ENABLED); |
| 1034 | tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected())); |
| 1035 | |
| 1036 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 1037 | if (!p_nested_action) { |
| 1038 | updating = true; |
| 1039 | undo_redo->create_action(TTR("Add Transition")); |
| 1040 | } |
| 1041 | |
| 1042 | undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr); |
| 1043 | undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node); |
| 1044 | undo_redo->add_do_method(this, "_update_graph"); |
| 1045 | undo_redo->add_undo_method(this, "_update_graph"); |
| 1046 | |
| 1047 | if (!p_nested_action) { |
| 1048 | undo_redo->commit_action(); |
| 1049 | updating = false; |
| 1050 | } |
| 1051 | |
| 1052 | _select_transition(connecting_from, connecting_to_node); |
| 1053 | |
| 1054 | if (selected_transition_index >= 0) { |
| 1055 | if (!state_machine->is_transition_across_group(selected_transition_index)) { |
| 1056 | EditorNode::get_singleton()->push_item(tr.ptr(), "", true); |
| 1057 | } else { |
| 1058 | EditorNode::get_singleton()->push_item(tr.ptr(), "", true); |
| 1059 | EditorNode::get_singleton()->push_item(nullptr, "", true); |
| 1060 | } |
| 1061 | } |
| 1062 | _update_mode(); |
| 1063 | } |
| 1064 | |
| 1065 | connecting = false; |
| 1066 | } |
| 1067 | |
| 1068 | void AnimationNodeStateMachineEditor::_connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, float p_fade_ratio, bool p_auto_advance, bool p_is_across_group, float p_opacity, bool p_endpoint_hovered, bool p_endpoint_hovered_start) { |
| 1069 | Color line_color = p_enabled ? theme_cache.transition_color : theme_cache.transition_disabled_color; |
| 1070 | Color icon_color = p_enabled ? theme_cache.transition_icon_color : theme_cache.transition_icon_disabled_color; |
| 1071 | Color highlight_color = p_enabled ? theme_cache.highlight_color : theme_cache.highlight_disabled_color; |
| 1072 | |
| 1073 | line_color.a *= p_opacity; |
| 1074 | icon_color.a *= p_opacity; |
| 1075 | highlight_color.a *= p_opacity; |
| 1076 | |
| 1077 | if (p_travel) { |
| 1078 | line_color = highlight_color; |
| 1079 | } |
| 1080 | |
| 1081 | // Add gradient on hovered endpoint. |
| 1082 | if (p_endpoint_hovered) { |
| 1083 | // Calculate gradient length based on transition length. |
| 1084 | float transition_length = p_from.distance_to(p_to); |
| 1085 | float gradient_distance = MIN(20.0f, transition_length * 0.2f); |
| 1086 | |
| 1087 | Vector2 gradient_start; |
| 1088 | Vector2 gradient_end; |
| 1089 | if (p_endpoint_hovered_start) { |
| 1090 | gradient_start = p_from; |
| 1091 | gradient_end = p_from + (p_to - p_from).normalized() * gradient_distance; |
| 1092 | } else { |
| 1093 | gradient_end = p_to; |
| 1094 | gradient_start = p_to - (p_to - p_from).normalized() * gradient_distance; |
| 1095 | } |
| 1096 | |
| 1097 | PackedVector2Array points; |
| 1098 | PackedColorArray colors; |
| 1099 | |
| 1100 | points.push_back(gradient_start); |
| 1101 | points.push_back(gradient_end); |
| 1102 | |
| 1103 | Color start_color = highlight_color; |
| 1104 | Color end_color = highlight_color; |
| 1105 | |
| 1106 | if (p_endpoint_hovered_start) { |
| 1107 | end_color.a = 0.0f; |
| 1108 | } else { |
| 1109 | start_color.a = 0.0f; |
| 1110 | } |
| 1111 | |
| 1112 | if (p_selected) { |
| 1113 | start_color = start_color.lightened(0.2f); |
| 1114 | end_color = end_color.lightened(0.2f); |
| 1115 | start_color.a *= 1.5f; |
| 1116 | end_color.a *= 1.5f; |
| 1117 | } |
| 1118 | |
| 1119 | colors.push_back(start_color); |
| 1120 | colors.push_back(end_color); |
| 1121 | |
| 1122 | float line_width = p_selected ? 10.0f : 8.0f; |
| 1123 | state_machine_draw->draw_polyline_colors(points, colors, line_width, true); |
| 1124 | } |
| 1125 | |
| 1126 | if (p_selected) { |
| 1127 | state_machine_draw->draw_line(p_from, p_to, highlight_color, 6, true); |
| 1128 | } |
| 1129 | state_machine_draw->draw_line(p_from, p_to, line_color, 2, true); |
| 1130 | |
| 1131 | if (p_fade_ratio > 0.0) { |
| 1132 | Color fade_line_color = highlight_color; |
| 1133 | fade_line_color.set_hsv(1.0, fade_line_color.get_s(), fade_line_color.get_v()); |
| 1134 | fade_line_color.a *= p_opacity; |
| 1135 | state_machine_draw->draw_line(p_from, p_from.lerp(p_to, p_fade_ratio), fade_line_color, 2); |
| 1136 | } |
| 1137 | |
| 1138 | const int ICON_COUNT = std_size(theme_cache.transition_icons); |
| 1139 | int icon_index = p_mode + (p_auto_advance ? ICON_COUNT / 2 : 0); |
| 1140 | ERR_FAIL_COND(icon_index >= ICON_COUNT); |
| 1141 | Ref<Texture2D> icon = theme_cache.transition_icons[icon_index]; |
| 1142 | |
| 1143 | Transform2D xf; |
| 1144 | xf.columns[0] = (p_to - p_from).normalized(); |
| 1145 | xf.columns[1] = xf.columns[0].orthogonal(); |
| 1146 | xf.columns[2] = (p_from + p_to) * 0.5 - xf.columns[1] * icon->get_height() * 0.5 - xf.columns[0] * icon->get_height() * 0.5; |
| 1147 | |
| 1148 | state_machine_draw->draw_set_transform_matrix(xf); |
| 1149 | if (!p_is_across_group) { |
| 1150 | state_machine_draw->draw_texture(icon, Vector2(), icon_color); |
| 1151 | } |
| 1152 | state_machine_draw->draw_set_transform_matrix(Transform2D()); |
| 1153 | } |
| 1154 | |
| 1155 | void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect) { |
| 1156 | if (p_to == r_from) { |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | //this could be optimized... |
| 1161 | Vector2 n = (p_to - r_from).normalized(); |
| 1162 | while (p_rect.has_point(r_from)) { |
| 1163 | r_from += n; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect) { |
| 1168 | if (r_to == p_from) { |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | //this could be optimized... |
| 1173 | Vector2 n = (r_to - p_from).normalized(); |
| 1174 | while (p_rect.has_point(r_to)) { |
| 1175 | r_to -= n; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | Ref<StyleBox> AnimationNodeStateMachineEditor::_adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity) { |
| 1180 | Ref<StyleBox> style = p_style->duplicate(); |
| 1181 | if (style->is_class("StyleBoxFlat")) { |
| 1182 | Ref<StyleBoxFlat> flat_style = style; |
| 1183 | Color bg_color = flat_style->get_bg_color(); |
| 1184 | Color border_color = flat_style->get_border_color(); |
| 1185 | Color shadow_color = flat_style->get_shadow_color(); |
| 1186 | |
| 1187 | bg_color.a *= p_opacity; |
| 1188 | border_color.a *= p_opacity; |
| 1189 | shadow_color.a *= p_opacity; |
| 1190 | |
| 1191 | flat_style->set_bg_color(bg_color); |
| 1192 | flat_style->set_border_color(border_color); |
| 1193 | flat_style->set_shadow_color(shadow_color); |
| 1194 | } |
| 1195 | return style; |
| 1196 | } |
| 1197 | |
| 1198 | void AnimationNodeStateMachineEditor::_state_machine_draw() { |
| 1199 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 1200 | if (!tree) { |
| 1201 | return; |
| 1202 | } |
| 1203 | |
| 1204 | bool playing = false; |
| 1205 | StringName current; |
| 1206 | StringName blend_from; |
| 1207 | Vector<StringName> travel_path; |
| 1208 | |
| 1209 | Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback"); |
| 1210 | if (playback.is_valid()) { |
| 1211 | playing = playback->is_playing(); |
| 1212 | current = playback->get_current_node(); |
| 1213 | blend_from = playback->get_fading_from_node(); |
| 1214 | travel_path = playback->get_travel_path(); |
| 1215 | } |
| 1216 | |
| 1217 | if (state_machine_draw->has_focus(true)) { |
| 1218 | state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), theme_cache.focus_color, false); |
| 1219 | } |
| 1220 | int sep = 3 * EDSCALE; |
| 1221 | |
| 1222 | LocalVector<StringName> nodes = state_machine->get_node_list(); |
| 1223 | |
| 1224 | node_rects.clear(); |
| 1225 | Rect2 scroll_range; |
| 1226 | |
| 1227 | //snap lines |
| 1228 | if (dragging_selected) { |
| 1229 | Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE; |
| 1230 | if (snap_x != StringName()) { |
| 1231 | Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE; |
| 1232 | state_machine_draw->draw_line(from, to, theme_cache.guideline_color, 2); |
| 1233 | } |
| 1234 | if (snap_y != StringName()) { |
| 1235 | Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE; |
| 1236 | state_machine_draw->draw_line(from, to, theme_cache.guideline_color, 2); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | //pre pass nodes so we know the rectangles |
| 1241 | for (const StringName &E : nodes) { |
| 1242 | String name = E; |
| 1243 | int name_string_size = theme_cache.node_title_font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size).width; |
| 1244 | |
| 1245 | Ref<AnimationNode> anode = state_machine->get_node(name); |
| 1246 | bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode); |
| 1247 | bool is_selected = selected_nodes.has(name); |
| 1248 | |
| 1249 | Size2 s = (is_selected ? theme_cache.node_frame_selected : theme_cache.node_frame)->get_minimum_size(); |
| 1250 | s.width += name_string_size; |
| 1251 | s.height += MAX(theme_cache.node_title_font->get_height(theme_cache.node_title_font_size), theme_cache.play_node->get_height()); |
| 1252 | s.width += sep + theme_cache.play_node->get_width(); |
| 1253 | |
| 1254 | if (needs_editor) { |
| 1255 | s.width += sep + theme_cache.edit_node->get_width(); |
| 1256 | } |
| 1257 | |
| 1258 | Vector2 offset; |
| 1259 | offset += state_machine->get_node_position(E) * EDSCALE; |
| 1260 | |
| 1261 | if (selected_nodes.has(E) && dragging_selected) { |
| 1262 | offset += drag_ofs; |
| 1263 | } |
| 1264 | |
| 1265 | offset -= s / 2; |
| 1266 | offset = offset.floor(); |
| 1267 | |
| 1268 | //prepre rect |
| 1269 | |
| 1270 | NodeRect nr; |
| 1271 | nr.node = Rect2(offset, s); |
| 1272 | nr.node_name = E; |
| 1273 | |
| 1274 | scroll_range = scroll_range.merge(nr.node); //merge with range |
| 1275 | |
| 1276 | //now scroll it to draw |
| 1277 | nr.node.position -= state_machine->get_graph_offset() * EDSCALE; |
| 1278 | |
| 1279 | node_rects.push_back(nr); |
| 1280 | } |
| 1281 | |
| 1282 | transition_lines.clear(); |
| 1283 | |
| 1284 | //draw connecting line for potential new transition |
| 1285 | if (connecting) { |
| 1286 | Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE; |
| 1287 | Vector2 to; |
| 1288 | if (connecting_to_node != StringName()) { |
| 1289 | to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE; |
| 1290 | } else { |
| 1291 | to = connecting_to; |
| 1292 | } |
| 1293 | |
| 1294 | for (int i = 0; i < node_rects.size(); i++) { |
| 1295 | if (node_rects[i].node_name == connecting_from) { |
| 1296 | _clip_src_line_to_rect(from, to, node_rects[i].node); |
| 1297 | } |
| 1298 | if (node_rects[i].node_name == connecting_to_node) { |
| 1299 | _clip_dst_line_to_rect(from, to, node_rects[i].node); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | _connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()), true, false, false, 0.0, false, false); |
| 1304 | } |
| 1305 | |
| 1306 | // TransitionImmediateBig |
| 1307 | float tr_bidi_offset = int(theme_cache.transition_icons[0]->get_height() * 0.8); |
| 1308 | |
| 1309 | //draw transition lines |
| 1310 | for (int i = 0; i < state_machine->get_transition_count(); i++) { |
| 1311 | TransitionLine tl; |
| 1312 | tl.transition_index = i; |
| 1313 | |
| 1314 | tl.from_node = state_machine->get_transition_from(i); |
| 1315 | Vector2 ofs_from = (dragging_selected && selected_nodes.has(tl.from_node)) ? drag_ofs : Vector2(); |
| 1316 | tl.from = (state_machine->get_node_position(tl.from_node) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE; |
| 1317 | |
| 1318 | tl.to_node = state_machine->get_transition_to(i); |
| 1319 | Vector2 ofs_to = (dragging_selected && selected_nodes.has(tl.to_node)) ? drag_ofs : Vector2(); |
| 1320 | tl.to = (state_machine->get_node_position(tl.to_node) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE; |
| 1321 | |
| 1322 | Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i); |
| 1323 | tl.disabled = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED); |
| 1324 | tl.auto_advance = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO); |
| 1325 | tl.advance_condition_name = tr->get_advance_condition_name(); |
| 1326 | tl.advance_condition_state = false; |
| 1327 | tl.mode = tr->get_switch_mode(); |
| 1328 | tl.width = tr_bidi_offset; |
| 1329 | tl.travel = false; |
| 1330 | tl.fade_ratio = 0.0; |
| 1331 | tl.hidden = false; |
| 1332 | tl.is_across_group = state_machine->is_transition_across_group(i); |
| 1333 | |
| 1334 | if (state_machine->has_transition(tl.to_node, tl.from_node)) { //offset if same exists |
| 1335 | Vector2 offset = -(tl.from - tl.to).normalized().orthogonal() * tr_bidi_offset; |
| 1336 | tl.from += offset; |
| 1337 | tl.to += offset; |
| 1338 | } |
| 1339 | |
| 1340 | for (int j = 0; j < node_rects.size(); j++) { |
| 1341 | if (node_rects[j].node_name == tl.from_node) { |
| 1342 | _clip_src_line_to_rect(tl.from, tl.to, node_rects[j].node); |
| 1343 | } |
| 1344 | if (node_rects[j].node_name == tl.to_node) { |
| 1345 | _clip_dst_line_to_rect(tl.from, tl.to, node_rects[j].node); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | tl.selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node; |
| 1350 | |
| 1351 | if (blend_from == tl.from_node && current == tl.to_node) { |
| 1352 | tl.travel = true; |
| 1353 | tl.fade_ratio = MIN(1.0, fading_pos / fading_time); |
| 1354 | } |
| 1355 | |
| 1356 | if (travel_path.size()) { |
| 1357 | if (current == tl.from_node && travel_path[0] == tl.to_node) { |
| 1358 | tl.travel = true; |
| 1359 | } else { |
| 1360 | for (int j = 0; j < travel_path.size() - 1; j++) { |
| 1361 | if (travel_path[j] == tl.from_node && travel_path[j + 1] == tl.to_node) { |
| 1362 | tl.travel = true; |
| 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name); |
| 1370 | if (tl.advance_condition_name != StringName() && bool(tree->get(fullpath))) { |
| 1371 | tl.advance_condition_state = true; |
| 1372 | tl.auto_advance = true; |
| 1373 | } |
| 1374 | |
| 1375 | // check if already have this transition |
| 1376 | for (int j = 0; j < transition_lines.size(); j++) { |
| 1377 | if (transition_lines[j].from_node == tl.from_node && transition_lines[j].to_node == tl.to_node) { |
| 1378 | tl.hidden = true; |
| 1379 | transition_lines.write[j].disabled = transition_lines[j].disabled && tl.disabled; |
| 1380 | } |
| 1381 | } |
| 1382 | transition_lines.push_back(tl); |
| 1383 | } |
| 1384 | |
| 1385 | for (int i = 0; i < transition_lines.size(); i++) { |
| 1386 | TransitionLine tl = transition_lines[i]; |
| 1387 | |
| 1388 | if (reconnecting && i == reconnecting_transition_index) { |
| 1389 | Vector2 transition_drag_from; |
| 1390 | Vector2 transition_drag_to; |
| 1391 | |
| 1392 | if (reconnecting_transition_start) { |
| 1393 | transition_drag_from = reconnecting_transition_pos; |
| 1394 | transition_drag_to = (state_machine->get_node_position(tl.to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE; |
| 1395 | } else { |
| 1396 | transition_drag_from = (state_machine->get_node_position(tl.from_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE; |
| 1397 | transition_drag_to = reconnecting_transition_pos; |
| 1398 | } |
| 1399 | |
| 1400 | // Check if we're attempting a self-connection. |
| 1401 | StringName connected_node = reconnecting_transition_start ? tl.to_node : tl.from_node; |
| 1402 | |
| 1403 | // Don't draw the line if attempting self-connection. |
| 1404 | if (reconnecting_transition_target != connected_node) { |
| 1405 | if (reconnecting_from_node_rect_index >= 0) { |
| 1406 | if (reconnecting_transition_start) { |
| 1407 | _clip_dst_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_from_node_rect_index].node); |
| 1408 | } else { |
| 1409 | _clip_src_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_from_node_rect_index].node); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | if (reconnecting_to_node_rect_index >= 0) { |
| 1414 | if (reconnecting_transition_start) { |
| 1415 | _clip_src_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_to_node_rect_index].node); |
| 1416 | } else { |
| 1417 | _clip_dst_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_to_node_rect_index].node); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | _connection_draw(transition_drag_from, transition_drag_to, tl.mode, !tl.disabled, false, false, 0.0f, tl.auto_advance, tl.is_across_group, 0.8f, false, false); |
| 1422 | } |
| 1423 | |
| 1424 | } else if (!tl.hidden) { |
| 1425 | float opacity = 0.2; // Default to reduced opacity. |
| 1426 | |
| 1427 | if (selected_transition_from != StringName() && selected_transition_to != StringName()) { |
| 1428 | // A transition is selected. |
| 1429 | if ((tl.from_node == selected_transition_from && tl.to_node == selected_transition_to) || (tl.from_node == selected_transition_to && tl.to_node == selected_transition_from)) { |
| 1430 | opacity = 1.0; // Full opacity for the selected transition pair. |
| 1431 | } |
| 1432 | } else if (!connected_nodes.is_empty()) { |
| 1433 | // A node is selected. |
| 1434 | if (connected_nodes.has(selected_node)) { |
| 1435 | // Only keep full opacity for transitions directly connected to the selected node. |
| 1436 | if (tl.from_node == selected_node || tl.to_node == selected_node) { |
| 1437 | opacity = 1.0; |
| 1438 | } |
| 1439 | } else { |
| 1440 | // If no node is selected, all transitions are at full opacity. |
| 1441 | opacity = 1.0; |
| 1442 | } |
| 1443 | } else { |
| 1444 | // If nothing is selected, all transitions are at full opacity. |
| 1445 | opacity = 1.0; |
| 1446 | } |
| 1447 | |
| 1448 | bool is_hovered = (hovered_transition_index == i); |
| 1449 | _connection_draw(tl.from, tl.to, tl.mode, !tl.disabled, tl.selected, tl.travel, tl.fade_ratio, tl.auto_advance, tl.is_across_group, opacity, is_hovered, hovered_transition_start); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | //draw actual nodes |
| 1454 | for (int i = 0; i < node_rects.size(); i++) { |
| 1455 | String name = node_rects[i].node_name; |
| 1456 | int name_string_size = theme_cache.node_title_font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size).width; |
| 1457 | |
| 1458 | Ref<AnimationNode> anode = state_machine->get_node(name); |
| 1459 | bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode); |
| 1460 | bool is_selected = selected_nodes.has(name); |
| 1461 | |
| 1462 | NodeRect &nr = node_rects.write[i]; |
| 1463 | Vector2 offset = nr.node.position; |
| 1464 | int h = nr.node.size.height; |
| 1465 | |
| 1466 | float opacity = 1.0; |
| 1467 | if (selected_transition_from != StringName() && selected_transition_to != StringName()) { |
| 1468 | // A transition is selected. |
| 1469 | if (name != selected_transition_from && name != selected_transition_to) { |
| 1470 | opacity = 0.2; |
| 1471 | } |
| 1472 | } else if (!connected_nodes.is_empty() && !connected_nodes.has(name)) { |
| 1473 | // A node is selected. |
| 1474 | opacity = 0.2; |
| 1475 | } |
| 1476 | |
| 1477 | Ref<StyleBox> original_style = is_selected ? theme_cache.node_frame_selected : theme_cache.node_frame; |
| 1478 | Ref<StyleBox> node_style = _adjust_stylebox_opacity(original_style, opacity); |
| 1479 | |
| 1480 | state_machine_draw->draw_style_box(node_style, nr.node); |
| 1481 | |
| 1482 | if (!is_selected && SceneStringName(Start) == name) { |
| 1483 | Ref<StyleBox> start_style = _adjust_stylebox_opacity(theme_cache.node_frame_start, opacity); |
| 1484 | state_machine_draw->draw_style_box(start_style, nr.node); |
| 1485 | } |
| 1486 | if (!is_selected && SceneStringName(End) == name) { |
| 1487 | Ref<StyleBox> end_style = _adjust_stylebox_opacity(theme_cache.node_frame_end, opacity); |
| 1488 | state_machine_draw->draw_style_box(end_style, nr.node); |
| 1489 | } |
| 1490 | if (playing && (blend_from == name || current == name || travel_path.has(name))) { |
| 1491 | Ref<StyleBox> playing_style = _adjust_stylebox_opacity(theme_cache.node_frame_playing, opacity); |
| 1492 | state_machine_draw->draw_style_box(playing_style, nr.node); |
| 1493 | } |
| 1494 | |
| 1495 | offset.x += original_style->get_offset().x; |
| 1496 | |
| 1497 | nr.play.position = offset + Vector2(0, (h - theme_cache.play_node->get_height()) / 2).floor(); |
| 1498 | nr.play.size = theme_cache.play_node->get_size(); |
| 1499 | |
| 1500 | Color color_mod = Color(1, 1, 1, opacity); |
| 1501 | if (hovered_node_name == name && hovered_node_area == HOVER_NODE_PLAY) { |
| 1502 | state_machine_draw->draw_texture(theme_cache.play_node, nr.play.position, theme_cache.highlight_color * color_mod); |
| 1503 | } else { |
| 1504 | state_machine_draw->draw_texture(theme_cache.play_node, nr.play.position, color_mod); |
| 1505 | } |
| 1506 | |
| 1507 | offset.x += sep + theme_cache.play_node->get_width(); |
| 1508 | |
| 1509 | nr.name.position = offset + Vector2(0, (h - theme_cache.node_title_font->get_height(theme_cache.node_title_font_size)) / 2).floor(); |
| 1510 | nr.name.size = Vector2(name_string_size, theme_cache.node_title_font->get_height(theme_cache.node_title_font_size)); |
| 1511 | |
| 1512 | Color font_color = theme_cache.node_title_font_color; |
| 1513 | font_color.a *= opacity; |
| 1514 | state_machine_draw->draw_string(theme_cache.node_title_font, nr.name.position + Vector2(0, theme_cache.node_title_font->get_ascent(theme_cache.node_title_font_size)), name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size, font_color); |
| 1515 | offset.x += name_string_size + sep; |
| 1516 | |
| 1517 | nr.can_edit = needs_editor; |
| 1518 | if (needs_editor) { |
| 1519 | nr.edit.position = offset + Vector2(0, (h - theme_cache.edit_node->get_height()) / 2).floor(); |
| 1520 | nr.edit.size = theme_cache.edit_node->get_size(); |
| 1521 | |
| 1522 | if (hovered_node_name == name && hovered_node_area == HOVER_NODE_EDIT) { |
| 1523 | state_machine_draw->draw_texture(theme_cache.edit_node, nr.edit.position, theme_cache.highlight_color * color_mod); |
| 1524 | } else { |
| 1525 | state_machine_draw->draw_texture(theme_cache.edit_node, nr.edit.position, color_mod); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | //draw box select |
| 1531 | if (box_selecting) { |
| 1532 | state_machine_draw->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3)); |
| 1533 | } |
| 1534 | |
| 1535 | scroll_range.position -= state_machine_draw->get_size(); |
| 1536 | scroll_range.size += state_machine_draw->get_size() * 2.0; |
| 1537 | |
| 1538 | //adjust scrollbars |
| 1539 | updating = true; |
| 1540 | h_scroll->set_min(scroll_range.position.x); |
| 1541 | h_scroll->set_max(scroll_range.position.x + scroll_range.size.x); |
| 1542 | h_scroll->set_page(state_machine_draw->get_size().x); |
| 1543 | h_scroll->set_value(state_machine->get_graph_offset().x); |
| 1544 | |
| 1545 | v_scroll->set_min(scroll_range.position.y); |
| 1546 | v_scroll->set_max(scroll_range.position.y + scroll_range.size.y); |
| 1547 | v_scroll->set_page(state_machine_draw->get_size().y); |
| 1548 | v_scroll->set_value(state_machine->get_graph_offset().y); |
| 1549 | updating = false; |
| 1550 | |
| 1551 | state_machine_play_pos->queue_redraw(); |
| 1552 | } |
| 1553 | |
| 1554 | void AnimationNodeStateMachineEditor::_update_connected_nodes(const StringName &p_node) { |
| 1555 | connected_nodes.clear(); |
| 1556 | if (p_node != StringName()) { |
| 1557 | connected_nodes.insert(p_node); |
| 1558 | |
| 1559 | Vector<StringName> nodes_to = state_machine->get_nodes_with_transitions_to(p_node); |
| 1560 | for (const StringName &node_to : nodes_to) { |
| 1561 | connected_nodes.insert(node_to); |
| 1562 | } |
| 1563 | |
| 1564 | Vector<StringName> nodes_from = state_machine->get_nodes_with_transitions_from(p_node); |
| 1565 | for (const StringName &node_from : nodes_from) { |
| 1566 | connected_nodes.insert(node_from); |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | void AnimationNodeStateMachineEditor::_state_machine_pos_draw_individual(const String &p_name, float p_ratio) { |
| 1572 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 1573 | if (!tree) { |
| 1574 | return; |
| 1575 | } |
| 1576 | |
| 1577 | Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback"); |
| 1578 | if (playback.is_null() || !playback->is_playing()) { |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | if (p_name == SceneStringName(Start) || p_name == SceneStringName(End) || p_name.is_empty()) { |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | int idx = -1; |
| 1587 | for (int i = 0; i < node_rects.size(); i++) { |
| 1588 | if (node_rects[i].node_name == p_name) { |
| 1589 | idx = i; |
| 1590 | break; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | if (idx == -1) { |
| 1595 | return; |
| 1596 | } |
| 1597 | |
| 1598 | const NodeRect &nr = node_rects[idx]; |
| 1599 | if (nr.can_edit) { |
| 1600 | return; // It is not AnimationNodeAnimation. |
| 1601 | } |
| 1602 | |
| 1603 | Vector2 from; |
| 1604 | from.x = nr.play.position.x; |
| 1605 | from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5; |
| 1606 | |
| 1607 | Vector2 to; |
| 1608 | if (nr.edit.size.x) { |
| 1609 | to.x = nr.edit.position.x + nr.edit.size.x; |
| 1610 | } else { |
| 1611 | to.x = nr.name.position.x + nr.name.size.x; |
| 1612 | } |
| 1613 | to.y = from.y; |
| 1614 | |
| 1615 | state_machine_play_pos->draw_line(from, to, theme_cache.playback_background_color, 2); |
| 1616 | to = from.lerp(to, p_ratio); |
| 1617 | state_machine_play_pos->draw_line(from, to, theme_cache.playback_color, 2); |
| 1618 | } |
| 1619 | |
| 1620 | void AnimationNodeStateMachineEditor::_state_machine_pos_draw_all() { |
| 1621 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 1622 | if (!tree) { |
| 1623 | return; |
| 1624 | } |
| 1625 | |
| 1626 | Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback"); |
| 1627 | if (playback.is_null() || !playback->is_playing()) { |
| 1628 | return; |
| 1629 | } |
| 1630 | |
| 1631 | { |
| 1632 | float len = MAX(0.0001, current_length); |
| 1633 | float pos = CLAMP(current_play_pos, 0, len); |
| 1634 | float c = pos / len; |
| 1635 | _state_machine_pos_draw_individual(playback->get_current_node(), c); |
| 1636 | } |
| 1637 | |
| 1638 | { |
| 1639 | float len = MAX(0.0001, fade_from_length); |
| 1640 | float pos = CLAMP(fade_from_current_play_pos, 0, len); |
| 1641 | float c = pos / len; |
| 1642 | _state_machine_pos_draw_individual(playback->get_fading_from_node(), c); |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | void AnimationNodeStateMachineEditor::_update_graph() { |
| 1647 | if (updating) { |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | updating = true; |
| 1652 | |
| 1653 | state_machine_draw->queue_redraw(); |
| 1654 | |
| 1655 | updating = false; |
| 1656 | } |
| 1657 | |
| 1658 | void AnimationNodeStateMachineEditor::_notification(int p_what) { |
| 1659 | switch (p_what) { |
| 1660 | case NOTIFICATION_THEME_CHANGED: { |
| 1661 | panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style); |
| 1662 | error_panel->add_theme_style_override(SceneStringName(panel), theme_cache.error_panel_style); |
| 1663 | error_label->add_theme_color_override(SceneStringName(font_color), theme_cache.error_color); |
| 1664 | |
| 1665 | tool_select->set_button_icon(theme_cache.tool_icon_select); |
| 1666 | tool_create->set_button_icon(theme_cache.tool_icon_create); |
| 1667 | tool_connect->set_button_icon(theme_cache.tool_icon_connect); |
| 1668 | |
| 1669 | switch_mode->clear(); |
| 1670 | switch_mode->add_icon_item(theme_cache.transition_icon_immediate, TTR("Immediate")); |
| 1671 | switch_mode->add_icon_item(theme_cache.transition_icon_sync, TTR("Sync")); |
| 1672 | switch_mode->add_icon_item(theme_cache.transition_icon_end, TTR("At End")); |
| 1673 | |
| 1674 | auto_advance->set_button_icon(theme_cache.play_icon_auto); |
| 1675 | |
| 1676 | tool_erase->set_button_icon(theme_cache.tool_icon_erase); |
| 1677 | |
| 1678 | play_mode->clear(); |
| 1679 | play_mode->add_icon_item(theme_cache.play_icon_travel, TTR("Travel")); |
| 1680 | play_mode->add_icon_item(theme_cache.play_icon_start, TTR("Immediate")); |
| 1681 | } break; |
| 1682 | |
| 1683 | case NOTIFICATION_PROCESS: { |
| 1684 | AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree(); |
| 1685 | if (!tree) { |
| 1686 | return; |
| 1687 | } |
| 1688 | |
| 1689 | String error; |
| 1690 | |
| 1691 | Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback"); |
| 1692 | |
| 1693 | if (error_time > 0) { |
| 1694 | error = error_text; |
| 1695 | error_time -= get_process_delta_time(); |
| 1696 | } else { |
| 1697 | error = tree->get_editor_error_message(); |
| 1698 | } |
| 1699 | |
| 1700 | if (error.is_empty() && playback.is_null()) { |
| 1701 | error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback"); |
| 1702 | } |
| 1703 | |
| 1704 | if (error != error_label->get_text()) { |
| 1705 | error_label->set_text(error); |
| 1706 | if (!error.is_empty()) { |
| 1707 | error_panel->show(); |
| 1708 | } else { |
| 1709 | error_panel->hide(); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | for (int i = 0; i < transition_lines.size(); i++) { |
| 1714 | int tidx = -1; |
| 1715 | for (int j = 0; j < state_machine->get_transition_count(); j++) { |
| 1716 | if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) { |
| 1717 | tidx = j; |
| 1718 | break; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | if (tidx == -1) { //missing transition, should redraw |
| 1723 | state_machine_draw->queue_redraw(); |
| 1724 | break; |
| 1725 | } |
| 1726 | |
| 1727 | if (transition_lines[i].disabled != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED)) { |
| 1728 | state_machine_draw->queue_redraw(); |
| 1729 | break; |
| 1730 | } |
| 1731 | |
| 1732 | if (transition_lines[i].auto_advance != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO)) { |
| 1733 | state_machine_draw->queue_redraw(); |
| 1734 | break; |
| 1735 | } |
| 1736 | |
| 1737 | if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) { |
| 1738 | state_machine_draw->queue_redraw(); |
| 1739 | break; |
| 1740 | } |
| 1741 | |
| 1742 | if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) { |
| 1743 | state_machine_draw->queue_redraw(); |
| 1744 | break; |
| 1745 | } |
| 1746 | |
| 1747 | bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name))); |
| 1748 | |
| 1749 | if (transition_lines[i].advance_condition_state != acstate) { |
| 1750 | state_machine_draw->queue_redraw(); |
| 1751 | break; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | bool same_travel_path = true; |
| 1756 | Vector<StringName> tp; |
| 1757 | bool is_playing = false; |
| 1758 | StringName current_node; |
| 1759 | StringName fading_from_node; |
| 1760 | |
| 1761 | current_play_pos = 0; |
| 1762 | current_length = 0; |
| 1763 | |
| 1764 | fade_from_current_play_pos = 0; |
| 1765 | fade_from_length = 0; |
| 1766 | |
| 1767 | fading_time = 0; |
| 1768 | fading_pos = 0; |
| 1769 | |
| 1770 | if (playback.is_valid()) { |
| 1771 | tp = playback->get_travel_path(); |
| 1772 | is_playing = playback->is_playing(); |
| 1773 | current_node = playback->get_current_node(); |
| 1774 | fading_from_node = playback->get_fading_from_node(); |
| 1775 | current_play_pos = playback->get_current_play_pos(); |
| 1776 | current_length = playback->get_current_length(); |
| 1777 | fade_from_current_play_pos = playback->get_fading_from_play_pos(); |
| 1778 | fade_from_length = playback->get_fading_from_length(); |
| 1779 | fading_time = playback->get_fading_time(); |
| 1780 | fading_pos = playback->get_fading_pos(); |
| 1781 | } |
| 1782 | |
| 1783 | { |
| 1784 | if (last_travel_path.size() != tp.size()) { |
| 1785 | same_travel_path = false; |
| 1786 | } else { |
| 1787 | for (int i = 0; i < last_travel_path.size(); i++) { |
| 1788 | if (last_travel_path[i] != tp[i]) { |
| 1789 | same_travel_path = false; |
| 1790 | break; |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | //redraw if travel state changed |
| 1797 | if (!same_travel_path || |
| 1798 | last_active != is_playing || |
| 1799 | last_current_node != current_node || |
| 1800 | last_fading_from_node != fading_from_node || |
| 1801 | last_fading_time != fading_time || |
| 1802 | last_fading_pos != fading_pos) { |
| 1803 | state_machine_draw->queue_redraw(); |
| 1804 | last_travel_path = tp; |
| 1805 | last_current_node = current_node; |
| 1806 | last_active = is_playing; |
| 1807 | last_fading_from_node = fading_from_node; |
| 1808 | last_fading_time = fading_time; |
| 1809 | last_fading_pos = fading_pos; |
| 1810 | state_machine_play_pos->queue_redraw(); |
| 1811 | } |
| 1812 | |
| 1813 | { |
| 1814 | if (current_node != StringName() && state_machine->has_node(current_node)) { |
| 1815 | String next = current_node; |
| 1816 | Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next); |
| 1817 | Ref<AnimationNodeStateMachinePlayback> current_node_playback; |
| 1818 | |
| 1819 | while (anodesm.is_valid()) { |
| 1820 | current_node_playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback"); |
| 1821 | StringName cnode = current_node_playback->get_current_node(); |
| 1822 | next += "/" + cnode; |
| 1823 | if (!anodesm->has_node(cnode)) { |
| 1824 | break; |
| 1825 | } |
| 1826 | anodesm = anodesm->get_node(cnode); |
| 1827 | } |
| 1828 | |
| 1829 | // when current_node is a state machine, use playback of current_node to set play_pos |
| 1830 | if (current_node_playback.is_valid()) { |
| 1831 | current_play_pos = current_node_playback->get_current_play_pos(); |
| 1832 | current_length = current_node_playback->get_current_length(); |
| 1833 | } |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | if (last_play_pos != current_play_pos || fade_from_last_play_pos != fade_from_current_play_pos) { |
| 1838 | last_play_pos = current_play_pos; |
| 1839 | fade_from_last_play_pos = fade_from_current_play_pos; |
| 1840 | state_machine_play_pos->queue_redraw(); |
| 1841 | } |
| 1842 | } break; |
| 1843 | |
| 1844 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 1845 | hovered_node_name = StringName(); |
| 1846 | hovered_node_area = HOVER_NODE_NONE; |
| 1847 | set_process(is_visible_in_tree()); |
| 1848 | } break; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) { |
| 1853 | AnimationTreeEditor::get_singleton()->enter_editor(p_name); |
| 1854 | } |
| 1855 | |
| 1856 | void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) { |
| 1857 | const String &new_name = p_text; |
| 1858 | |
| 1859 | ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/')); |
| 1860 | |
| 1861 | if (new_name == prev_name) { |
| 1862 | return; // Nothing to do. |
| 1863 | } |
| 1864 | |
| 1865 | const String &base_name = new_name; |
| 1866 | int base = 1; |
| 1867 | String name = base_name; |
| 1868 | while (state_machine->has_node(name)) { |
| 1869 | if (name == prev_name) { |
| 1870 | name_edit_popup->hide(); // The old name wins, the name doesn't change, just hide the popup. |
| 1871 | return; |
| 1872 | } |
| 1873 | base++; |
| 1874 | name = base_name + " " + itos(base); |
| 1875 | } |
| 1876 | |
| 1877 | updating = true; |
| 1878 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 1879 | undo_redo->create_action(TTR("Node Renamed")); |
| 1880 | undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name); |
| 1881 | undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name); |
| 1882 | undo_redo->add_do_method(this, "_update_graph"); |
| 1883 | undo_redo->add_undo_method(this, "_update_graph"); |
| 1884 | undo_redo->commit_action(); |
| 1885 | name_edit_popup->hide(); |
| 1886 | updating = false; |
| 1887 | |
| 1888 | selected_nodes.clear(); |
| 1889 | connected_nodes.clear(); |
| 1890 | state_machine_draw->queue_redraw(); |
| 1891 | } |
| 1892 | |
| 1893 | void AnimationNodeStateMachineEditor::_name_edited_focus_out() { |
| 1894 | if (updating) { |
| 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | _name_edited(name_edit->get_text()); |
| 1899 | } |
| 1900 | |
| 1901 | void AnimationNodeStateMachineEditor::_scroll_changed(double) { |
| 1902 | if (updating) { |
| 1903 | return; |
| 1904 | } |
| 1905 | |
| 1906 | state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value())); |
| 1907 | state_machine_draw->queue_redraw(); |
| 1908 | } |
| 1909 | |
| 1910 | void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action) { |
| 1911 | if (!selected_nodes.is_empty()) { |
| 1912 | if (!p_nested_action) { |
| 1913 | updating = true; |
| 1914 | } |
| 1915 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 1916 | undo_redo->create_action(TTR("Node Removed")); |
| 1917 | |
| 1918 | for (int i = 0; i < node_rects.size(); i++) { |
| 1919 | if (node_rects[i].node_name == SceneStringName(Start) || node_rects[i].node_name == SceneStringName(End)) { |
| 1920 | continue; |
| 1921 | } |
| 1922 | |
| 1923 | if (!selected_nodes.has(node_rects[i].node_name)) { |
| 1924 | continue; |
| 1925 | } |
| 1926 | |
| 1927 | undo_redo->add_do_method(state_machine.ptr(), "remove_node", node_rects[i].node_name); |
| 1928 | undo_redo->add_undo_method(state_machine.ptr(), "add_node", node_rects[i].node_name, |
| 1929 | state_machine->get_node(node_rects[i].node_name), |
| 1930 | state_machine->get_node_position(node_rects[i].node_name)); |
| 1931 | |
| 1932 | for (int j = 0; j < state_machine->get_transition_count(); j++) { |
| 1933 | String from = state_machine->get_transition_from(j); |
| 1934 | String to = state_machine->get_transition_to(j); |
| 1935 | if (from == node_rects[i].node_name || to == node_rects[i].node_name) { |
| 1936 | undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(j)); |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | undo_redo->add_do_method(this, "_update_graph"); |
| 1942 | undo_redo->add_undo_method(this, "_update_graph"); |
| 1943 | undo_redo->commit_action(); |
| 1944 | |
| 1945 | if (!p_nested_action) { |
| 1946 | updating = false; |
| 1947 | } |
| 1948 | |
| 1949 | connected_nodes.clear(); |
| 1950 | selected_nodes.clear(); |
| 1951 | selected_node = StringName(); |
| 1952 | |
| 1953 | // Return selection to host StateMachine node. |
| 1954 | EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true); |
| 1955 | } |
| 1956 | |
| 1957 | if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) { |
| 1958 | Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to)); |
| 1959 | if (!p_nested_action) { |
| 1960 | updating = true; |
| 1961 | } |
| 1962 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 1963 | undo_redo->create_action(TTR("Transition Removed")); |
| 1964 | undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to); |
| 1965 | undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr); |
| 1966 | undo_redo->add_do_method(this, "_update_graph"); |
| 1967 | undo_redo->add_undo_method(this, "_update_graph"); |
| 1968 | undo_redo->commit_action(); |
| 1969 | if (!p_nested_action) { |
| 1970 | updating = false; |
| 1971 | } |
| 1972 | selected_transition_from = StringName(); |
| 1973 | selected_transition_to = StringName(); |
| 1974 | selected_transition_index = -1; |
| 1975 | |
| 1976 | // Return selection to host StateMachine node. |
| 1977 | EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true); |
| 1978 | } |
| 1979 | |
| 1980 | _update_mode(); |
| 1981 | state_machine_draw->queue_redraw(); |
| 1982 | } |
| 1983 | |
| 1984 | void AnimationNodeStateMachineEditor::_update_mode() { |
| 1985 | if (tool_select->is_pressed()) { |
| 1986 | selection_tools_hb->show(); |
| 1987 | bool nothing_selected = selected_nodes.is_empty() && selected_transition_from == StringName() && selected_transition_to == StringName(); |
| 1988 | bool start_end_selected = selected_nodes.size() == 1 && (*selected_nodes.begin() == SceneStringName(Start) || *selected_nodes.begin() == SceneStringName(End)); |
| 1989 | tool_erase->set_disabled(nothing_selected || start_end_selected || read_only); |
| 1990 | } else { |
| 1991 | selection_tools_hb->hide(); |
| 1992 | } |
| 1993 | |
| 1994 | if (read_only) { |
| 1995 | tool_create->set_pressed(false); |
| 1996 | tool_connect->set_pressed(false); |
| 1997 | } |
| 1998 | |
| 1999 | if (tool_connect->is_pressed()) { |
| 2000 | transition_tools_hb->show(); |
| 2001 | } else { |
| 2002 | transition_tools_hb->hide(); |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | void AnimationNodeStateMachineEditor::_bind_methods() { |
| 2007 | ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph); |
| 2008 | ClassDB::bind_method("_select_transition", &AnimationNodeStateMachineEditor::_select_transition); |
| 2009 | |
| 2010 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, panel_style, "panel", "GraphStateMachine"); |
| 2011 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, error_panel_style, "error_panel", "GraphStateMachine"); |
| 2012 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, error_color, "error_color", "GraphStateMachine"); |
| 2013 | |
| 2014 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_select, "ToolSelect", "EditorIcons"); |
| 2015 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_create, "ToolAddNode", "EditorIcons"); |
| 2016 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_connect, "ToolConnect", "EditorIcons"); |
| 2017 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_erase, "Remove", "EditorIcons"); |
| 2018 | |
| 2019 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_immediate, "TransitionImmediate", "EditorIcons"); |
| 2020 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_sync, "TransitionSync", "EditorIcons"); |
| 2021 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_end, "TransitionEnd", "EditorIcons"); |
| 2022 | |
| 2023 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_start, "Play", "EditorIcons"); |
| 2024 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_travel, "PlayTravel", "EditorIcons"); |
| 2025 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_auto, "AutoPlay", "EditorIcons"); |
| 2026 | |
| 2027 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, animation_icon, "Animation", "EditorIcons"); |
| 2028 | |
| 2029 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame, "node_frame", "GraphStateMachine"); |
| 2030 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_selected, "node_frame_selected", "GraphStateMachine"); |
| 2031 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_playing, "node_frame_playing", "GraphStateMachine"); |
| 2032 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_start, "node_frame_start", "GraphStateMachine"); |
| 2033 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_end, "node_frame_end", "GraphStateMachine"); |
| 2034 | |
| 2035 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_FONT, AnimationNodeStateMachineEditor, node_title_font, "node_title_font", "GraphStateMachine"); |
| 2036 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_FONT_SIZE, AnimationNodeStateMachineEditor, node_title_font_size, "node_title_font_size", "GraphStateMachine"); |
| 2037 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, node_title_font_color, "node_title_font_color", "GraphStateMachine"); |
| 2038 | |
| 2039 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_node, "Play", "EditorIcons"); |
| 2040 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, edit_node, "Edit", "EditorIcons"); |
| 2041 | |
| 2042 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_color, "transition_color", "GraphStateMachine"); |
| 2043 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_disabled_color, "transition_disabled_color", "GraphStateMachine"); |
| 2044 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_icon_color, "transition_icon_color", "GraphStateMachine"); |
| 2045 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_icon_disabled_color, "transition_icon_disabled_color", "GraphStateMachine"); |
| 2046 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, highlight_color, "highlight_color", "GraphStateMachine"); |
| 2047 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, highlight_disabled_color, "highlight_disabled_color", "GraphStateMachine"); |
| 2048 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, focus_color, "focus_color", "GraphStateMachine"); |
| 2049 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, guideline_color, "guideline_color", "GraphStateMachine"); |
| 2050 | |
| 2051 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[0], "TransitionImmediateBig", "EditorIcons"); |
| 2052 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[1], "TransitionSyncBig", "EditorIcons"); |
| 2053 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[2], "TransitionEndBig", "EditorIcons"); |
| 2054 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[3], "TransitionImmediateAutoBig", "EditorIcons"); |
| 2055 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[4], "TransitionSyncAutoBig", "EditorIcons"); |
| 2056 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[5], "TransitionEndAutoBig", "EditorIcons"); |
| 2057 | |
| 2058 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, playback_color, "playback_color", "GraphStateMachine"); |
| 2059 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, playback_background_color, "playback_background_color", "GraphStateMachine"); |
| 2060 | } |
| 2061 | |
| 2062 | AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr; |
| 2063 | |
| 2064 | AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() { |
| 2065 | singleton = this; |
| 2066 | |
| 2067 | HBoxContainer *top_hb = memnew(HBoxContainer); |
| 2068 | add_child(top_hb); |
| 2069 | |
| 2070 | Ref<ButtonGroup> bg; |
| 2071 | bg.instantiate(); |
| 2072 | |
| 2073 | tool_select = memnew(Button); |
| 2074 | tool_select->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2075 | top_hb->add_child(tool_select); |
| 2076 | tool_select->set_toggle_mode(true); |
| 2077 | tool_select->set_button_group(bg); |
| 2078 | tool_select->set_pressed(true); |
| 2079 | tool_select->set_tooltip_text(TTR("Select and move nodes.\nRMB: Add node at position clicked.\nShift+LMB+Drag: Connects the selected node with another node or creates a new node if you select an area without nodes.")); |
| 2080 | tool_select->set_accessibility_name(TTRC("Select and move nodes.")); |
| 2081 | tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED); |
| 2082 | |
| 2083 | tool_create = memnew(Button); |
| 2084 | tool_create->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2085 | top_hb->add_child(tool_create); |
| 2086 | tool_create->set_toggle_mode(true); |
| 2087 | tool_create->set_button_group(bg); |
| 2088 | tool_create->set_tooltip_text(TTR("Create new nodes.")); |
| 2089 | tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED); |
| 2090 | |
| 2091 | tool_connect = memnew(Button); |
| 2092 | tool_connect->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2093 | top_hb->add_child(tool_connect); |
| 2094 | tool_connect->set_toggle_mode(true); |
| 2095 | tool_connect->set_button_group(bg); |
| 2096 | tool_connect->set_tooltip_text(TTR("Connect nodes.")); |
| 2097 | tool_connect->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED); |
| 2098 | |
| 2099 | // Context-sensitive selection tools: |
| 2100 | selection_tools_hb = memnew(HBoxContainer); |
| 2101 | top_hb->add_child(selection_tools_hb); |
| 2102 | selection_tools_hb->add_child(memnew(VSeparator)); |
| 2103 | |
| 2104 | tool_erase = memnew(Button); |
| 2105 | tool_erase->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2106 | tool_erase->set_tooltip_text(TTR("Remove selected node or transition.")); |
| 2107 | tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected).bind(false)); |
| 2108 | tool_erase->set_disabled(true); |
| 2109 | selection_tools_hb->add_child(tool_erase); |
| 2110 | |
| 2111 | transition_tools_hb = memnew(HBoxContainer); |
| 2112 | top_hb->add_child(transition_tools_hb); |
| 2113 | transition_tools_hb->add_child(memnew(VSeparator)); |
| 2114 | |
| 2115 | transition_tools_hb->add_child(memnew(Label(TTR("Transition:")))); |
| 2116 | switch_mode = memnew(OptionButton); |
| 2117 | transition_tools_hb->add_child(switch_mode); |
| 2118 | |
| 2119 | auto_advance = memnew(Button); |
| 2120 | auto_advance->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2121 | auto_advance->set_tooltip_text(TTR("New Transitions Should Auto Advance")); |
| 2122 | auto_advance->set_toggle_mode(true); |
| 2123 | auto_advance->set_pressed(true); |
| 2124 | transition_tools_hb->add_child(auto_advance); |
| 2125 | |
| 2126 | // |
| 2127 | |
| 2128 | top_hb->add_spacer(); |
| 2129 | |
| 2130 | top_hb->add_child(memnew(Label(TTR("Play Mode:")))); |
| 2131 | play_mode = memnew(OptionButton); |
| 2132 | top_hb->add_child(play_mode); |
| 2133 | |
| 2134 | panel = memnew(PanelContainer); |
| 2135 | panel->set_clip_contents(true); |
| 2136 | panel->set_mouse_filter(Control::MOUSE_FILTER_PASS); |
| 2137 | add_child(panel); |
| 2138 | panel->set_v_size_flags(SIZE_EXPAND_FILL); |
| 2139 | |
| 2140 | state_machine_draw = memnew(Control); |
| 2141 | panel->add_child(state_machine_draw); |
| 2142 | state_machine_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input)); |
| 2143 | state_machine_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw)); |
| 2144 | state_machine_draw->set_focus_mode(FOCUS_ALL); |
| 2145 | state_machine_draw->set_mouse_filter(Control::MOUSE_FILTER_PASS); |
| 2146 | |
| 2147 | state_machine_play_pos = memnew(Control); |
| 2148 | state_machine_draw->add_child(state_machine_play_pos); |
| 2149 | state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent |
| 2150 | state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 2151 | state_machine_play_pos->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw_all)); |
| 2152 | |
| 2153 | v_scroll = memnew(VScrollBar); |
| 2154 | state_machine_draw->add_child(v_scroll); |
| 2155 | v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE); |
| 2156 | v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed)); |
| 2157 | |
| 2158 | h_scroll = memnew(HScrollBar); |
| 2159 | state_machine_draw->add_child(h_scroll); |
| 2160 | h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE); |
| 2161 | h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE); |
| 2162 | h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed)); |
| 2163 | |
| 2164 | error_panel = memnew(PanelContainer); |
| 2165 | add_child(error_panel); |
| 2166 | error_label = memnew(Label); |
| 2167 | error_label->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 2168 | error_panel->add_child(error_label); |
| 2169 | error_panel->hide(); |
| 2170 | |
| 2171 | set_custom_minimum_size(Size2(0, 300 * EDSCALE)); |
| 2172 | |
| 2173 | menu = memnew(PopupMenu); |
| 2174 | add_child(menu); |
| 2175 | menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type)); |
| 2176 | menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting)); |
| 2177 | |
| 2178 | animations_menu = memnew(PopupMenu); |
| 2179 | animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2180 | menu->add_child(animations_menu); |
| 2181 | animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type)); |
| 2182 | |
| 2183 | connect_menu = memnew(PopupMenu); |
| 2184 | add_child(connect_menu); |
| 2185 | connect_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to)); |
| 2186 | connect_menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting)); |
| 2187 | |
| 2188 | state_machine_menu = memnew(PopupMenu); |
| 2189 | state_machine_menu->set_name("state_machines"); |
| 2190 | state_machine_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to)); |
| 2191 | connect_menu->add_child(state_machine_menu); |
| 2192 | |
| 2193 | end_menu = memnew(PopupMenu); |
| 2194 | end_menu->set_name("end_nodes"); |
| 2195 | end_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to)); |
| 2196 | connect_menu->add_child(end_menu); |
| 2197 | |
| 2198 | name_edit_popup = memnew(Popup); |
| 2199 | add_child(name_edit_popup); |
| 2200 | name_edit = memnew(LineEdit); |
| 2201 | name_edit_popup->add_child(name_edit); |
| 2202 | name_edit->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 2203 | name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited)); |
| 2204 | name_edit->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out)); |
| 2205 | |
| 2206 | open_file = memnew(EditorFileDialog); |
| 2207 | add_child(open_file); |
| 2208 | open_file->set_title(TTR("Open Animation Node")); |
| 2209 | open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
| 2210 | open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened)); |
| 2211 | } |
| 2212 | |
| 2213 | void EditorAnimationMultiTransitionEdit::add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition) { |
| 2214 | Transition tr; |
| 2215 | tr.from = p_from; |
| 2216 | tr.to = p_to; |
| 2217 | tr.transition = p_transition; |
| 2218 | transitions.push_back(tr); |
| 2219 | } |
| 2220 | |
| 2221 | bool EditorAnimationMultiTransitionEdit::_set(const StringName &p_name, const Variant &p_property) { |
| 2222 | int index = String(p_name).get_slicec('/', 0).to_int(); |
| 2223 | StringName prop = String(p_name).get_slicec('/', 1); |
| 2224 | |
| 2225 | bool found; |
| 2226 | transitions.write[index].transition->set(prop, p_property, &found); |
| 2227 | if (found) { |
| 2228 | return true; |
| 2229 | } |
| 2230 | |
| 2231 | return false; |
| 2232 | } |
| 2233 | |
| 2234 | bool EditorAnimationMultiTransitionEdit::_get(const StringName &p_name, Variant &r_property) const { |
| 2235 | int index = String(p_name).get_slicec('/', 0).to_int(); |
| 2236 | StringName prop = String(p_name).get_slicec('/', 1); |
| 2237 | |
| 2238 | if (prop == "transition_path") { |
| 2239 | r_property = String(transitions[index].from) + U" → " + transitions[index].to; |
| 2240 | return true; |
| 2241 | } |
| 2242 | |
| 2243 | bool found; |
| 2244 | r_property = transitions[index].transition->get(prop, &found); |
| 2245 | if (found) { |
| 2246 | return true; |
| 2247 | } |
| 2248 | |
| 2249 | return false; |
| 2250 | } |
| 2251 | |
| 2252 | void EditorAnimationMultiTransitionEdit::_get_property_list(List<PropertyInfo> *p_list) const { |
| 2253 | for (int i = 0; i < transitions.size(); i++) { |
| 2254 | List<PropertyInfo> plist; |
| 2255 | transitions[i].transition->get_property_list(&plist, true); |
| 2256 | |
| 2257 | PropertyInfo prop_transition_path; |
| 2258 | prop_transition_path.type = Variant::STRING; |
| 2259 | prop_transition_path.name = itos(i) + "/" + "transition_path"; |
| 2260 | p_list->push_back(prop_transition_path); |
| 2261 | |
| 2262 | for (const PropertyInfo &pi : plist) { |
| 2263 | if (pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene") { |
| 2264 | continue; |
| 2265 | } |
| 2266 | |
| 2267 | if (pi.usage != PROPERTY_USAGE_DEFAULT) { |
| 2268 | continue; |
| 2269 | } |
| 2270 | |
| 2271 | PropertyInfo prop = pi; |
| 2272 | prop.name = itos(i) + "/" + prop.name; |
| 2273 | |
| 2274 | p_list->push_back(prop); |
| 2275 | } |
| 2276 | } |
| 2277 | } |