root_motion_editor_plugin.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************/
  2. /* root_motion_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 "root_motion_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "scene/main/viewport.h"
  33. void EditorPropertyRootMotion::_confirmed() {
  34. TreeItem *ti = filters->get_selected();
  35. if (!ti) {
  36. return;
  37. }
  38. NodePath path = ti->get_metadata(0);
  39. emit_changed(get_edited_property(), path);
  40. update_property();
  41. filter_dialog->hide(); //may come from activated
  42. }
  43. void EditorPropertyRootMotion::_node_assign() {
  44. NodePath current = get_edited_object()->get(get_edited_property());
  45. AnimationTree *atree = Object::cast_to<AnimationTree>(get_edited_object());
  46. if (!atree->has_node(atree->get_animation_player())) {
  47. EditorNode::get_singleton()->show_warning(TTR("AnimationTree has no path set to an AnimationPlayer"));
  48. return;
  49. }
  50. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(atree->get_node(atree->get_animation_player()));
  51. if (!player) {
  52. EditorNode::get_singleton()->show_warning(TTR("Path to AnimationPlayer is invalid"));
  53. return;
  54. }
  55. Node *base = player->get_node(player->get_root());
  56. if (!base) {
  57. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  58. return;
  59. }
  60. Set<String> paths;
  61. {
  62. List<StringName> animations;
  63. player->get_animation_list(&animations);
  64. for (List<StringName>::Element *E = animations.front(); E; E = E->next()) {
  65. Ref<Animation> anim = player->get_animation(E->get());
  66. for (int i = 0; i < anim->get_track_count(); i++) {
  67. paths.insert(anim->track_get_path(i));
  68. }
  69. }
  70. }
  71. filters->clear();
  72. TreeItem *root = filters->create_item();
  73. Map<String, TreeItem *> parenthood;
  74. for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
  75. NodePath path = E->get();
  76. TreeItem *ti = nullptr;
  77. String accum;
  78. for (int i = 0; i < path.get_name_count(); i++) {
  79. String name = path.get_name(i);
  80. if (accum != String()) {
  81. accum += "/";
  82. }
  83. accum += name;
  84. if (!parenthood.has(accum)) {
  85. if (ti) {
  86. ti = filters->create_item(ti);
  87. } else {
  88. ti = filters->create_item(root);
  89. }
  90. parenthood[accum] = ti;
  91. ti->set_text(0, name);
  92. ti->set_selectable(0, false);
  93. ti->set_editable(0, false);
  94. if (base->has_node(accum)) {
  95. Node *node = base->get_node(accum);
  96. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  97. }
  98. } else {
  99. ti = parenthood[accum];
  100. }
  101. }
  102. Node *node = nullptr;
  103. if (base->has_node(accum)) {
  104. node = base->get_node(accum);
  105. }
  106. if (!node) {
  107. continue; //no node, can't edit
  108. }
  109. if (path.get_subname_count()) {
  110. String concat = path.get_concatenated_subnames();
  111. Skeleton *skeleton = Object::cast_to<Skeleton>(node);
  112. if (skeleton && skeleton->find_bone(concat) != -1) {
  113. //path in skeleton
  114. const String &bone = concat;
  115. int idx = skeleton->find_bone(bone);
  116. List<String> bone_path;
  117. while (idx != -1) {
  118. bone_path.push_front(skeleton->get_bone_name(idx));
  119. idx = skeleton->get_bone_parent(idx);
  120. }
  121. accum += ":";
  122. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  123. if (F != bone_path.front()) {
  124. accum += "/";
  125. }
  126. accum += F->get();
  127. if (!parenthood.has(accum)) {
  128. ti = filters->create_item(ti);
  129. parenthood[accum] = ti;
  130. ti->set_text(0, F->get());
  131. ti->set_selectable(0, true);
  132. ti->set_editable(0, false);
  133. ti->set_icon(0, get_icon("BoneAttachment", "EditorIcons"));
  134. ti->set_metadata(0, accum);
  135. } else {
  136. ti = parenthood[accum];
  137. }
  138. }
  139. ti->set_selectable(0, true);
  140. ti->set_text(0, concat);
  141. ti->set_icon(0, get_icon("BoneAttachment", "EditorIcons"));
  142. ti->set_metadata(0, path);
  143. if (path == current) {
  144. ti->select(0);
  145. }
  146. } else {
  147. //just a property
  148. ti = filters->create_item(ti);
  149. ti->set_text(0, concat);
  150. ti->set_selectable(0, true);
  151. ti->set_metadata(0, path);
  152. if (path == current) {
  153. ti->select(0);
  154. }
  155. }
  156. } else {
  157. if (ti) {
  158. //just a node, likely call or animation track
  159. ti->set_selectable(0, true);
  160. ti->set_metadata(0, path);
  161. if (path == current) {
  162. ti->select(0);
  163. }
  164. }
  165. }
  166. }
  167. filters->ensure_cursor_is_visible();
  168. filter_dialog->popup_centered_ratio();
  169. }
  170. void EditorPropertyRootMotion::_node_clear() {
  171. emit_changed(get_edited_property(), NodePath());
  172. update_property();
  173. }
  174. void EditorPropertyRootMotion::update_property() {
  175. NodePath p = get_edited_object()->get(get_edited_property());
  176. assign->set_tooltip(p);
  177. if (p == NodePath()) {
  178. assign->set_icon(Ref<Texture>());
  179. assign->set_text(TTR("Assign..."));
  180. assign->set_flat(false);
  181. return;
  182. }
  183. assign->set_flat(true);
  184. Node *base_node = nullptr;
  185. if (base_hint != NodePath()) {
  186. if (get_tree()->get_root()->has_node(base_hint)) {
  187. base_node = get_tree()->get_root()->get_node(base_hint);
  188. }
  189. } else {
  190. base_node = Object::cast_to<Node>(get_edited_object());
  191. }
  192. if (!base_node || !base_node->has_node(p)) {
  193. assign->set_icon(Ref<Texture>());
  194. assign->set_text(p);
  195. return;
  196. }
  197. Node *target_node = base_node->get_node(p);
  198. ERR_FAIL_COND(!target_node);
  199. assign->set_text(target_node->get_name());
  200. assign->set_icon(EditorNode::get_singleton()->get_object_icon(target_node, "Node"));
  201. }
  202. void EditorPropertyRootMotion::setup(const NodePath &p_base_hint) {
  203. base_hint = p_base_hint;
  204. }
  205. void EditorPropertyRootMotion::_notification(int p_what) {
  206. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  207. Ref<Texture> t = get_icon("Clear", "EditorIcons");
  208. clear->set_icon(t);
  209. }
  210. }
  211. void EditorPropertyRootMotion::_bind_methods() {
  212. ClassDB::bind_method(D_METHOD("_confirmed"), &EditorPropertyRootMotion::_confirmed);
  213. ClassDB::bind_method(D_METHOD("_node_assign"), &EditorPropertyRootMotion::_node_assign);
  214. ClassDB::bind_method(D_METHOD("_node_clear"), &EditorPropertyRootMotion::_node_clear);
  215. }
  216. EditorPropertyRootMotion::EditorPropertyRootMotion() {
  217. HBoxContainer *hbc = memnew(HBoxContainer);
  218. add_child(hbc);
  219. assign = memnew(Button);
  220. assign->set_flat(true);
  221. assign->set_h_size_flags(SIZE_EXPAND_FILL);
  222. assign->set_clip_text(true);
  223. assign->connect("pressed", this, "_node_assign");
  224. hbc->add_child(assign);
  225. clear = memnew(Button);
  226. clear->set_flat(true);
  227. clear->connect("pressed", this, "_node_clear");
  228. hbc->add_child(clear);
  229. filter_dialog = memnew(ConfirmationDialog);
  230. add_child(filter_dialog);
  231. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  232. filter_dialog->connect("confirmed", this, "_confirmed");
  233. filters = memnew(Tree);
  234. filter_dialog->add_child(filters);
  235. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  236. filters->set_hide_root(true);
  237. filters->connect("item_activated", this, "_confirmed");
  238. //filters->connect("item_edited", this, "_filter_edited");
  239. }
  240. //////////////////////////
  241. bool EditorInspectorRootMotionPlugin::can_handle(Object *p_object) {
  242. return true; //can handle everything
  243. }
  244. void EditorInspectorRootMotionPlugin::parse_begin(Object *p_object) {
  245. //do none
  246. }
  247. bool EditorInspectorRootMotionPlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  248. if (p_path == "root_motion_track" && p_object->is_class("AnimationTree") && p_type == Variant::NODE_PATH) {
  249. EditorPropertyRootMotion *editor = memnew(EditorPropertyRootMotion);
  250. if (p_hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && p_hint_text != String()) {
  251. editor->setup(p_hint_text);
  252. }
  253. add_property_editor(p_path, editor);
  254. return true;
  255. }
  256. return false; //can be overridden, although it will most likely be last anyway
  257. }
  258. void EditorInspectorRootMotionPlugin::parse_end() {
  259. //do none
  260. }