| 1 | /**************************************************************************/ |
| 2 | /* animation_tree_editor_plugin.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_tree_editor_plugin.h" |
| 32 | |
| 33 | #include "animation_blend_space_1d_editor.h" |
| 34 | #include "animation_blend_space_2d_editor.h" |
| 35 | #include "animation_blend_tree_editor_plugin.h" |
| 36 | #include "animation_state_machine_editor.h" |
| 37 | #include "editor/docks/editor_dock_manager.h" |
| 38 | #include "editor/editor_node.h" |
| 39 | #include "editor/gui/editor_bottom_panel.h" |
| 40 | #include "editor/settings/editor_command_palette.h" |
| 41 | #include "editor/themes/editor_scale.h" |
| 42 | #include "scene/animation/animation_blend_tree.h" |
| 43 | #include "scene/gui/button.h" |
| 44 | #include "scene/gui/margin_container.h" |
| 45 | #include "scene/gui/scroll_container.h" |
| 46 | #include "scene/gui/separator.h" |
| 47 | |
| 48 | void AnimationTreeEditor::edit(AnimationTree *p_tree) { |
| 49 | if (p_tree && !p_tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) { |
| 50 | p_tree->connect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed), CONNECT_DEFERRED); |
| 51 | } |
| 52 | |
| 53 | if (tree == p_tree) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if (tree && tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) { |
| 58 | tree->disconnect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed)); |
| 59 | } |
| 60 | |
| 61 | tree = p_tree; |
| 62 | |
| 63 | Vector<String> path; |
| 64 | if (tree) { |
| 65 | edit_path(path); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void AnimationTreeEditor::_node_removed(Node *p_node) { |
| 70 | if (p_node == tree) { |
| 71 | tree = nullptr; |
| 72 | _clear_editors(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void AnimationTreeEditor::_path_button_pressed(int p_path) { |
| 77 | edited_path.clear(); |
| 78 | for (int i = 0; i <= p_path; i++) { |
| 79 | edited_path.push_back(button_path[i]); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void AnimationTreeEditor::_animation_list_changed() { |
| 84 | AnimationNodeBlendTreeEditor *bte = AnimationNodeBlendTreeEditor::get_singleton(); |
| 85 | if (bte) { |
| 86 | bte->update_graph(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void AnimationTreeEditor::_update_path() { |
| 91 | while (path_hb->get_child_count() > 1) { |
| 92 | memdelete(path_hb->get_child(1)); |
| 93 | } |
| 94 | |
| 95 | Ref<ButtonGroup> group; |
| 96 | group.instantiate(); |
| 97 | |
| 98 | Button *b = memnew(Button); |
| 99 | b->set_text(TTR("Root")); |
| 100 | b->set_toggle_mode(true); |
| 101 | b->set_button_group(group); |
| 102 | b->set_pressed(true); |
| 103 | b->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 104 | b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(-1)); |
| 105 | path_hb->add_child(b); |
| 106 | for (int i = 0; i < button_path.size(); i++) { |
| 107 | b = memnew(Button); |
| 108 | b->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 109 | b->set_text(button_path[i]); |
| 110 | b->set_toggle_mode(true); |
| 111 | b->set_button_group(group); |
| 112 | path_hb->add_child(b); |
| 113 | b->set_pressed(true); |
| 114 | b->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 115 | b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(i)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void AnimationTreeEditor::edit_path(const Vector<String> &p_path) { |
| 120 | button_path.clear(); |
| 121 | |
| 122 | Ref<AnimationNode> node = tree->get_root_animation_node(); |
| 123 | |
| 124 | if (node.is_valid()) { |
| 125 | current_root = node->get_instance_id(); |
| 126 | |
| 127 | for (int i = 0; i < p_path.size(); i++) { |
| 128 | Ref<AnimationNode> child = node->get_child_by_name(p_path[i]); |
| 129 | ERR_BREAK(child.is_null()); |
| 130 | node = child; |
| 131 | button_path.push_back(p_path[i]); |
| 132 | } |
| 133 | |
| 134 | edited_path = button_path; |
| 135 | |
| 136 | for (int i = 0; i < editors.size(); i++) { |
| 137 | if (editors[i]->can_edit(node)) { |
| 138 | editors[i]->edit(node); |
| 139 | editors[i]->show(); |
| 140 | } else { |
| 141 | editors[i]->edit(Ref<AnimationNode>()); |
| 142 | editors[i]->hide(); |
| 143 | } |
| 144 | } |
| 145 | } else { |
| 146 | current_root = ObjectID(); |
| 147 | edited_path = button_path; |
| 148 | for (int i = 0; i < editors.size(); i++) { |
| 149 | editors[i]->edit(Ref<AnimationNode>()); |
| 150 | editors[i]->hide(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | _update_path(); |
| 155 | } |
| 156 | |
| 157 | void AnimationTreeEditor::_clear_editors() { |
| 158 | button_path.clear(); |
| 159 | current_root = ObjectID(); |
| 160 | edited_path = button_path; |
| 161 | for (int i = 0; i < editors.size(); i++) { |
| 162 | editors[i]->edit(Ref<AnimationNode>()); |
| 163 | editors[i]->hide(); |
| 164 | } |
| 165 | _update_path(); |
| 166 | } |
| 167 | |
| 168 | Vector<String> AnimationTreeEditor::get_edited_path() const { |
| 169 | return button_path; |
| 170 | } |
| 171 | |
| 172 | void AnimationTreeEditor::enter_editor(const String &p_path) { |
| 173 | Vector<String> path = edited_path; |
| 174 | path.push_back(p_path); |
| 175 | edit_path(path); |
| 176 | } |
| 177 | |
| 178 | void AnimationTreeEditor::_notification(int p_what) { |
| 179 | switch (p_what) { |
| 180 | case NOTIFICATION_PROCESS: { |
| 181 | ObjectID root; |
| 182 | if (tree && tree->get_root_animation_node().is_valid()) { |
| 183 | root = tree->get_root_animation_node()->get_instance_id(); |
| 184 | } |
| 185 | |
| 186 | if (root != current_root) { |
| 187 | edit_path(Vector<String>()); |
| 188 | } |
| 189 | |
| 190 | if (button_path.size() != edited_path.size()) { |
| 191 | edit_path(edited_path); |
| 192 | } |
| 193 | } break; |
| 194 | |
| 195 | case NOTIFICATION_ENTER_TREE: { |
| 196 | get_tree()->connect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed)); |
| 197 | } break; |
| 198 | |
| 199 | case NOTIFICATION_EXIT_TREE: { |
| 200 | get_tree()->disconnect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed)); |
| 201 | } break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | AnimationTreeEditor *AnimationTreeEditor::singleton = nullptr; |
| 206 | |
| 207 | void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) { |
| 208 | ERR_FAIL_COND(p_editor->get_parent()); |
| 209 | editor_base->add_child(p_editor); |
| 210 | editors.push_back(p_editor); |
| 211 | p_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 212 | p_editor->set_v_size_flags(SIZE_EXPAND_FILL); |
| 213 | p_editor->hide(); |
| 214 | } |
| 215 | |
| 216 | void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) { |
| 217 | ERR_FAIL_COND(p_editor->get_parent() != editor_base); |
| 218 | editor_base->remove_child(p_editor); |
| 219 | editors.erase(p_editor); |
| 220 | } |
| 221 | |
| 222 | String AnimationTreeEditor::get_base_path() { |
| 223 | String path = Animation::PARAMETERS_BASE_PATH; |
| 224 | for (int i = 0; i < edited_path.size(); i++) { |
| 225 | path += edited_path[i] + "/"; |
| 226 | } |
| 227 | return path; |
| 228 | } |
| 229 | |
| 230 | bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const { |
| 231 | for (int i = 0; i < editors.size(); i++) { |
| 232 | if (editors[i]->can_edit(p_node)) { |
| 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | Vector<String> AnimationTreeEditor::get_animation_list() { |
| 240 | // This can be called off the main thread due to resource preview generation. Quit early in that case. |
| 241 | if (!singleton->tree || !Thread::is_main_thread() || !singleton->is_visible()) { |
| 242 | // When tree is empty, singleton not in the main thread. |
| 243 | return Vector<String>(); |
| 244 | } |
| 245 | |
| 246 | AnimationTree *tree = singleton->tree; |
| 247 | if (!tree) { |
| 248 | return Vector<String>(); |
| 249 | } |
| 250 | |
| 251 | List<StringName> anims; |
| 252 | tree->get_animation_list(&anims); |
| 253 | Vector<String> ret; |
| 254 | for (const StringName &E : anims) { |
| 255 | ret.push_back(E); |
| 256 | } |
| 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | AnimationTreeEditor::AnimationTreeEditor() { |
| 262 | singleton = this; |
| 263 | AnimationNodeAnimation::get_editable_animation_list = get_animation_list; |
| 264 | |
| 265 | set_name(TTRC("AnimationTree")); |
| 266 | set_icon_name("AnimationTreeDock"); |
| 267 | set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_animation_tree_bottom_panel", TTRC("Toggle AnimationTree Dock"))); |
| 268 | set_default_slot(EditorDock::DOCK_SLOT_BOTTOM); |
| 269 | set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING); |
| 270 | set_global(false); |
| 271 | set_transient(true); |
| 272 | |
| 273 | VBoxContainer *main_vbox_container = memnew(VBoxContainer); |
| 274 | add_child(main_vbox_container); |
| 275 | |
| 276 | path_edit = memnew(ScrollContainer); |
| 277 | path_edit->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 278 | main_vbox_container->add_child(path_edit); |
| 279 | |
| 280 | path_hb = memnew(HBoxContainer); |
| 281 | path_hb->add_child(memnew(Label(TTR("Path:")))); |
| 282 | path_edit->add_child(path_hb); |
| 283 | |
| 284 | main_vbox_container->add_child(memnew(HSeparator)); |
| 285 | |
| 286 | editor_base = memnew(MarginContainer); |
| 287 | editor_base->set_v_size_flags(SIZE_EXPAND_FILL); |
| 288 | main_vbox_container->add_child(editor_base); |
| 289 | |
| 290 | add_plugin(memnew(AnimationNodeBlendTreeEditor)); |
| 291 | add_plugin(memnew(AnimationNodeBlendSpace1DEditor)); |
| 292 | add_plugin(memnew(AnimationNodeBlendSpace2DEditor)); |
| 293 | add_plugin(memnew(AnimationNodeStateMachineEditor)); |
| 294 | } |
| 295 | |
| 296 | void AnimationTreeEditorPlugin::edit(Object *p_object) { |
| 297 | anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object)); |
| 298 | } |
| 299 | |
| 300 | bool AnimationTreeEditorPlugin::handles(Object *p_object) const { |
| 301 | return p_object->is_class("AnimationTree"); |
| 302 | } |
| 303 | |
| 304 | void AnimationTreeEditorPlugin::make_visible(bool p_visible) { |
| 305 | if (p_visible) { |
| 306 | anim_tree_editor->make_visible(); |
| 307 | } else { |
| 308 | anim_tree_editor->close(); |
| 309 | } |
| 310 | |
| 311 | anim_tree_editor->set_process(p_visible); |
| 312 | } |
| 313 | |
| 314 | AnimationTreeEditorPlugin::AnimationTreeEditorPlugin() { |
| 315 | anim_tree_editor = memnew(AnimationTreeEditor); |
| 316 | anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE); |
| 317 | EditorDockManager::get_singleton()->add_dock(anim_tree_editor); |
| 318 | anim_tree_editor->close(); |
| 319 | } |