root_motion_editor_plugin.cpp 9.4 KB

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