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