animation_tree_editor_plugin.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_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/window.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->has_meta("_tree_edit_path")) {
  54. path = tree->get_meta("_tree_edit_path");
  55. edit_path(path);
  56. } else {
  57. current_root = ObjectID();
  58. }
  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.instantiate();
  72. Button *b = memnew(Button);
  73. b->set_text(TTR("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", callable_mp(this, &AnimationTreeEditor::_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", callable_mp(this, &AnimationTreeEditor::_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 = ObjectID();
  114. edited_path = button_path;
  115. }
  116. _update_path();
  117. }
  118. Vector<String> AnimationTreeEditor::get_edited_path() const {
  119. return button_path;
  120. }
  121. void AnimationTreeEditor::enter_editor(const String &p_path) {
  122. Vector<String> path = edited_path;
  123. path.push_back(p_path);
  124. edit_path(path);
  125. }
  126. void AnimationTreeEditor::_notification(int p_what) {
  127. if (p_what == NOTIFICATION_PROCESS) {
  128. ObjectID root;
  129. if (tree && tree->get_tree_root().is_valid()) {
  130. root = tree->get_tree_root()->get_instance_id();
  131. }
  132. if (root != current_root) {
  133. edit_path(Vector<String>());
  134. }
  135. if (button_path.size() != edited_path.size()) {
  136. edit_path(edited_path);
  137. }
  138. }
  139. }
  140. void AnimationTreeEditor::_bind_methods() {
  141. }
  142. AnimationTreeEditor *AnimationTreeEditor::singleton = nullptr;
  143. void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
  144. ERR_FAIL_COND(p_editor->get_parent());
  145. editor_base->add_child(p_editor);
  146. editors.push_back(p_editor);
  147. p_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  148. p_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  149. p_editor->hide();
  150. }
  151. void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
  152. ERR_FAIL_COND(p_editor->get_parent() != editor_base);
  153. editor_base->remove_child(p_editor);
  154. editors.erase(p_editor);
  155. }
  156. String AnimationTreeEditor::get_base_path() {
  157. String path = SceneStringNames::get_singleton()->parameters_base_path;
  158. for (int i = 0; i < edited_path.size(); i++) {
  159. path += edited_path[i] + "/";
  160. }
  161. return path;
  162. }
  163. bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const {
  164. for (int i = 0; i < editors.size(); i++) {
  165. if (editors[i]->can_edit(p_node)) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. Vector<String> AnimationTreeEditor::get_animation_list() {
  172. if (!singleton->is_visible()) {
  173. return Vector<String>();
  174. }
  175. AnimationTree *tree = singleton->tree;
  176. if (!tree || !tree->has_node(tree->get_animation_player())) {
  177. return Vector<String>();
  178. }
  179. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(tree->get_node(tree->get_animation_player()));
  180. if (!ap) {
  181. return Vector<String>();
  182. }
  183. List<StringName> anims;
  184. ap->get_animation_list(&anims);
  185. Vector<String> ret;
  186. for (const StringName &E : anims) {
  187. ret.push_back(E);
  188. }
  189. return ret;
  190. }
  191. AnimationTreeEditor::AnimationTreeEditor() {
  192. AnimationNodeAnimation::get_editable_animation_list = get_animation_list;
  193. path_edit = memnew(ScrollContainer);
  194. add_child(path_edit);
  195. path_edit->set_enable_h_scroll(true);
  196. path_edit->set_enable_v_scroll(false);
  197. path_hb = memnew(HBoxContainer);
  198. path_edit->add_child(path_hb);
  199. path_hb->add_child(memnew(Label(TTR("Path:"))));
  200. add_child(memnew(HSeparator));
  201. singleton = this;
  202. editor_base = memnew(MarginContainer);
  203. editor_base->set_v_size_flags(SIZE_EXPAND_FILL);
  204. add_child(editor_base);
  205. add_plugin(memnew(AnimationNodeBlendTreeEditor));
  206. add_plugin(memnew(AnimationNodeBlendSpace1DEditor));
  207. add_plugin(memnew(AnimationNodeBlendSpace2DEditor));
  208. add_plugin(memnew(AnimationNodeStateMachineEditor));
  209. }
  210. void AnimationTreeEditorPlugin::edit(Object *p_object) {
  211. anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));
  212. }
  213. bool AnimationTreeEditorPlugin::handles(Object *p_object) const {
  214. return p_object->is_class("AnimationTree");
  215. }
  216. void AnimationTreeEditorPlugin::make_visible(bool p_visible) {
  217. if (p_visible) {
  218. //editor->hide_animation_player_editors();
  219. //editor->animation_panel_make_visible(true);
  220. button->show();
  221. editor->make_bottom_panel_item_visible(anim_tree_editor);
  222. anim_tree_editor->set_process(true);
  223. } else {
  224. if (anim_tree_editor->is_visible_in_tree()) {
  225. editor->hide_bottom_panel();
  226. }
  227. button->hide();
  228. anim_tree_editor->set_process(false);
  229. }
  230. }
  231. AnimationTreeEditorPlugin::AnimationTreeEditorPlugin(EditorNode *p_node) {
  232. editor = p_node;
  233. anim_tree_editor = memnew(AnimationTreeEditor);
  234. anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  235. button = editor->add_bottom_panel_item(TTR("AnimationTree"), anim_tree_editor);
  236. button->hide();
  237. }
  238. AnimationTreeEditorPlugin::~AnimationTreeEditorPlugin() {
  239. }