animation_tree_editor_plugin.cpp 8.9 KB

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