animation_tree_editor_plugin.cpp 10 KB

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