animation_tree_editor_plugin.cpp 8.7 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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "scene/animation/animation_blend_tree.h"
  41. #include "scene/animation/animation_player.h"
  42. #include "scene/gui/menu_button.h"
  43. #include "scene/gui/panel.h"
  44. #include "scene/main/viewport.h"
  45. #include "scene/scene_string_names.h"
  46. void AnimationTreeEditor::edit(AnimationTree *p_tree) {
  47. if (tree == p_tree)
  48. return;
  49. tree = p_tree;
  50. Vector<String> path;
  51. if (tree->has_meta("_tree_edit_path")) {
  52. path = tree->get_meta("_tree_edit_path");
  53. edit_path(path);
  54. } else {
  55. current_root = 0;
  56. }
  57. }
  58. void AnimationTreeEditor::_path_button_pressed(int p_path) {
  59. edited_path.clear();
  60. for (int i = 0; i <= p_path; i++) {
  61. edited_path.push_back(button_path[i]);
  62. }
  63. }
  64. void AnimationTreeEditor::_update_path() {
  65. while (path_hb->get_child_count() > 1) {
  66. memdelete(path_hb->get_child(1));
  67. }
  68. Ref<ButtonGroup> group;
  69. group.instance();
  70. Button *b = memnew(Button);
  71. b->set_text("root");
  72. b->set_toggle_mode(true);
  73. b->set_button_group(group);
  74. b->set_pressed(true);
  75. b->set_focus_mode(FOCUS_NONE);
  76. b->connect("pressed", this, "_path_button_pressed", varray(-1));
  77. path_hb->add_child(b);
  78. for (int i = 0; i < button_path.size(); i++) {
  79. b = memnew(Button);
  80. b->set_text(button_path[i]);
  81. b->set_toggle_mode(true);
  82. b->set_button_group(group);
  83. path_hb->add_child(b);
  84. b->set_pressed(true);
  85. b->set_focus_mode(FOCUS_NONE);
  86. b->connect("pressed", this, "_path_button_pressed", varray(i));
  87. }
  88. }
  89. void AnimationTreeEditor::edit_path(const Vector<String> &p_path) {
  90. button_path.clear();
  91. Ref<AnimationNode> node = tree->get_tree_root();
  92. if (node.is_valid()) {
  93. current_root = node->get_instance_id();
  94. for (int i = 0; i < p_path.size(); i++) {
  95. Ref<AnimationNode> child = node->get_child_by_name(p_path[i]);
  96. ERR_BREAK(child.is_null());
  97. node = child;
  98. button_path.push_back(p_path[i]);
  99. }
  100. for (int i = 0; i < editors.size(); i++) {
  101. if (editors[i]->can_edit(node)) {
  102. editors[i]->edit(node);
  103. editors[i]->show();
  104. } else {
  105. editors[i]->edit(Ref<AnimationNode>());
  106. editors[i]->hide();
  107. }
  108. }
  109. } else {
  110. current_root = 0;
  111. }
  112. edited_path = button_path;
  113. _update_path();
  114. }
  115. Vector<String> AnimationTreeEditor::get_edited_path() const {
  116. return button_path;
  117. }
  118. void AnimationTreeEditor::enter_editor(const String &p_path) {
  119. Vector<String> path = edited_path;
  120. path.push_back(p_path);
  121. edit_path(path);
  122. }
  123. void AnimationTreeEditor::_about_to_show_root() {
  124. }
  125. void AnimationTreeEditor::_notification(int p_what) {
  126. if (p_what == NOTIFICATION_PROCESS) {
  127. ObjectID root = 0;
  128. if (tree && tree->get_tree_root().is_valid()) {
  129. root = tree->get_tree_root()->get_instance_id();
  130. }
  131. if (root != current_root) {
  132. edit_path(Vector<String>());
  133. }
  134. if (button_path.size() != edited_path.size()) {
  135. edit_path(edited_path);
  136. }
  137. }
  138. }
  139. void AnimationTreeEditor::_bind_methods() {
  140. ClassDB::bind_method("_path_button_pressed", &AnimationTreeEditor::_path_button_pressed);
  141. }
  142. AnimationTreeEditor *AnimationTreeEditor::singleton = NULL;
  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. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(tree->get_node(tree->get_animation_player()));
  179. if (!ap)
  180. return Vector<String>();
  181. List<StringName> anims;
  182. ap->get_animation_list(&anims);
  183. Vector<String> ret;
  184. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  185. ret.push_back(E->get());
  186. }
  187. return ret;
  188. }
  189. AnimationTreeEditor::AnimationTreeEditor() {
  190. AnimationNodeAnimation::get_editable_animation_list = get_animation_list;
  191. path_edit = memnew(ScrollContainer);
  192. add_child(path_edit);
  193. path_edit->set_enable_h_scroll(true);
  194. path_edit->set_enable_v_scroll(false);
  195. path_hb = memnew(HBoxContainer);
  196. path_edit->add_child(path_hb);
  197. path_hb->add_child(memnew(Label(TTR("Path:"))));
  198. add_child(memnew(HSeparator));
  199. current_root = 0;
  200. singleton = this;
  201. editor_base = memnew(PanelContainer);
  202. editor_base->set_v_size_flags(SIZE_EXPAND_FILL);
  203. add_child(editor_base);
  204. add_plugin(memnew(AnimationNodeBlendTreeEditor));
  205. add_plugin(memnew(AnimationNodeBlendSpace1DEditor));
  206. add_plugin(memnew(AnimationNodeBlendSpace2DEditor));
  207. add_plugin(memnew(AnimationNodeStateMachineEditor));
  208. }
  209. void AnimationTreeEditorPlugin::edit(Object *p_object) {
  210. anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));
  211. }
  212. bool AnimationTreeEditorPlugin::handles(Object *p_object) const {
  213. return p_object->is_class("AnimationTree");
  214. }
  215. void AnimationTreeEditorPlugin::make_visible(bool p_visible) {
  216. if (p_visible) {
  217. //editor->hide_animation_player_editors();
  218. //editor->animation_panel_make_visible(true);
  219. button->show();
  220. editor->make_bottom_panel_item_visible(anim_tree_editor);
  221. anim_tree_editor->set_process(true);
  222. } else {
  223. if (anim_tree_editor->is_visible_in_tree())
  224. editor->hide_bottom_panel();
  225. button->hide();
  226. anim_tree_editor->set_process(false);
  227. }
  228. }
  229. AnimationTreeEditorPlugin::AnimationTreeEditorPlugin(EditorNode *p_node) {
  230. editor = p_node;
  231. anim_tree_editor = memnew(AnimationTreeEditor);
  232. anim_tree_editor->set_custom_minimum_size(Size2(0, 300));
  233. button = editor->add_bottom_panel_item(TTR("AnimationTree"), anim_tree_editor);
  234. button->hide();
  235. }
  236. AnimationTreeEditorPlugin::~AnimationTreeEditorPlugin() {
  237. }