animation_tree_editor_plugin.cpp 10 KB

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