animation_tree_editor_plugin.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/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. tree = p_tree;
  51. Vector<String> path;
  52. if (tree->has_meta("_tree_edit_path")) {
  53. path = tree->get_meta("_tree_edit_path");
  54. edit_path(path);
  55. } else {
  56. current_root = 0;
  57. }
  58. }
  59. void AnimationTreeEditor::_path_button_pressed(int p_path) {
  60. edited_path.clear();
  61. for (int i = 0; i <= p_path; i++) {
  62. edited_path.push_back(button_path[i]);
  63. }
  64. }
  65. void AnimationTreeEditor::_update_path() {
  66. while (path_hb->get_child_count() > 1) {
  67. memdelete(path_hb->get_child(1));
  68. }
  69. Ref<ButtonGroup> group;
  70. group.instance();
  71. Button *b = memnew(Button);
  72. b->set_text("root");
  73. b->set_toggle_mode(true);
  74. b->set_button_group(group);
  75. b->set_pressed(true);
  76. b->set_focus_mode(FOCUS_NONE);
  77. b->connect("pressed", this, "_path_button_pressed", varray(-1));
  78. path_hb->add_child(b);
  79. for (int i = 0; i < button_path.size(); i++) {
  80. b = memnew(Button);
  81. b->set_text(button_path[i]);
  82. b->set_toggle_mode(true);
  83. b->set_button_group(group);
  84. path_hb->add_child(b);
  85. b->set_pressed(true);
  86. b->set_focus_mode(FOCUS_NONE);
  87. b->connect("pressed", this, "_path_button_pressed", varray(i));
  88. }
  89. }
  90. void AnimationTreeEditor::edit_path(const Vector<String> &p_path) {
  91. button_path.clear();
  92. Ref<AnimationNode> node = tree->get_tree_root();
  93. if (node.is_valid()) {
  94. current_root = node->get_instance_id();
  95. for (int i = 0; i < p_path.size(); i++) {
  96. Ref<AnimationNode> child = node->get_child_by_name(p_path[i]);
  97. ERR_BREAK(child.is_null());
  98. node = child;
  99. button_path.push_back(p_path[i]);
  100. }
  101. edited_path = button_path;
  102. for (int i = 0; i < editors.size(); i++) {
  103. if (editors[i]->can_edit(node)) {
  104. editors[i]->edit(node);
  105. editors[i]->show();
  106. } else {
  107. editors[i]->edit(Ref<AnimationNode>());
  108. editors[i]->hide();
  109. }
  110. }
  111. } else {
  112. current_root = 0;
  113. edited_path = button_path;
  114. }
  115. _update_path();
  116. }
  117. Vector<String> AnimationTreeEditor::get_edited_path() const {
  118. return button_path;
  119. }
  120. void AnimationTreeEditor::enter_editor(const String &p_path) {
  121. Vector<String> path = edited_path;
  122. path.push_back(p_path);
  123. edit_path(path);
  124. }
  125. void AnimationTreeEditor::_about_to_show_root() {
  126. }
  127. void AnimationTreeEditor::_notification(int p_what) {
  128. if (p_what == NOTIFICATION_PROCESS) {
  129. ObjectID root = 0;
  130. if (tree && tree->get_tree_root().is_valid()) {
  131. root = tree->get_tree_root()->get_instance_id();
  132. }
  133. if (root != current_root) {
  134. edit_path(Vector<String>());
  135. }
  136. if (button_path.size() != edited_path.size()) {
  137. edit_path(edited_path);
  138. }
  139. }
  140. }
  141. void AnimationTreeEditor::_bind_methods() {
  142. ClassDB::bind_method("_path_button_pressed", &AnimationTreeEditor::_path_button_pressed);
  143. }
  144. AnimationTreeEditor *AnimationTreeEditor::singleton = NULL;
  145. void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
  146. ERR_FAIL_COND(p_editor->get_parent());
  147. editor_base->add_child(p_editor);
  148. editors.push_back(p_editor);
  149. p_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  150. p_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  151. p_editor->hide();
  152. }
  153. void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
  154. ERR_FAIL_COND(p_editor->get_parent() != editor_base);
  155. editor_base->remove_child(p_editor);
  156. editors.erase(p_editor);
  157. }
  158. String AnimationTreeEditor::get_base_path() {
  159. String path = SceneStringNames::get_singleton()->parameters_base_path;
  160. for (int i = 0; i < edited_path.size(); i++) {
  161. path += edited_path[i] + "/";
  162. }
  163. return path;
  164. }
  165. bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const {
  166. for (int i = 0; i < editors.size(); i++) {
  167. if (editors[i]->can_edit(p_node)) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. Vector<String> AnimationTreeEditor::get_animation_list() {
  174. if (!singleton->is_visible()) {
  175. return Vector<String>();
  176. }
  177. AnimationTree *tree = singleton->tree;
  178. if (!tree || !tree->has_node(tree->get_animation_player()))
  179. return Vector<String>();
  180. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(tree->get_node(tree->get_animation_player()));
  181. if (!ap)
  182. return Vector<String>();
  183. List<StringName> anims;
  184. ap->get_animation_list(&anims);
  185. Vector<String> ret;
  186. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  187. ret.push_back(E->get());
  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. current_root = 0;
  202. singleton = this;
  203. editor_base = memnew(MarginContainer);
  204. editor_base->set_v_size_flags(SIZE_EXPAND_FILL);
  205. add_child(editor_base);
  206. add_plugin(memnew(AnimationNodeBlendTreeEditor));
  207. add_plugin(memnew(AnimationNodeBlendSpace1DEditor));
  208. add_plugin(memnew(AnimationNodeBlendSpace2DEditor));
  209. add_plugin(memnew(AnimationNodeStateMachineEditor));
  210. }
  211. void AnimationTreeEditorPlugin::edit(Object *p_object) {
  212. anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));
  213. }
  214. bool AnimationTreeEditorPlugin::handles(Object *p_object) const {
  215. return p_object->is_class("AnimationTree");
  216. }
  217. void AnimationTreeEditorPlugin::make_visible(bool p_visible) {
  218. if (p_visible) {
  219. //editor->hide_animation_player_editors();
  220. //editor->animation_panel_make_visible(true);
  221. button->show();
  222. editor->make_bottom_panel_item_visible(anim_tree_editor);
  223. anim_tree_editor->set_process(true);
  224. } else {
  225. if (anim_tree_editor->is_visible_in_tree())
  226. editor->hide_bottom_panel();
  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. }