animation_tree_editor_plugin.cpp 8.9 KB

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