root_motion_editor_plugin.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/animation/animation_player.h"
  33. #include "scene/animation/animation_tree.h"
  34. #include "scene/gui/tree.h"
  35. #include "scene/main/window.h"
  36. void EditorPropertyRootMotion::_confirmed() {
  37. TreeItem *ti = filters->get_selected();
  38. if (!ti) {
  39. return;
  40. }
  41. NodePath path = ti->get_metadata(0);
  42. emit_changed(get_edited_property(), path);
  43. update_property();
  44. filter_dialog->hide(); //may come from activated
  45. }
  46. void EditorPropertyRootMotion::_node_assign() {
  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. String pathname = anim->track_get_path(i).get_concatenated_names();
  70. if (!paths.has(pathname)) {
  71. paths.insert(pathname);
  72. }
  73. }
  74. }
  75. }
  76. filters->clear();
  77. TreeItem *root = filters->create_item();
  78. HashMap<String, TreeItem *> parenthood;
  79. for (const String &E : paths) {
  80. NodePath path = E;
  81. TreeItem *ti = nullptr;
  82. String accum;
  83. for (int i = 0; i < path.get_name_count(); i++) {
  84. String name = path.get_name(i);
  85. if (!accum.is_empty()) {
  86. accum += "/";
  87. }
  88. accum += name;
  89. if (!parenthood.has(accum)) {
  90. if (ti) {
  91. ti = filters->create_item(ti);
  92. } else {
  93. ti = filters->create_item(root);
  94. }
  95. parenthood[accum] = ti;
  96. ti->set_text(0, name);
  97. ti->set_selectable(0, false);
  98. ti->set_editable(0, false);
  99. if (base->has_node(accum)) {
  100. Node *node = base->get_node(accum);
  101. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  102. }
  103. } else {
  104. ti = parenthood[accum];
  105. }
  106. }
  107. Node *node = nullptr;
  108. if (base->has_node(accum)) {
  109. node = base->get_node(accum);
  110. }
  111. if (!node) {
  112. continue; //no node, can't edit
  113. }
  114. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  115. if (skeleton) {
  116. HashMap<int, TreeItem *> items;
  117. items.insert(-1, ti);
  118. Ref<Texture> bone_icon = get_theme_icon(SNAME("BoneAttachment3D"), SNAME("EditorIcons"));
  119. Vector<int> bones_to_process = skeleton->get_parentless_bones();
  120. while (bones_to_process.size() > 0) {
  121. int current_bone_idx = bones_to_process[0];
  122. bones_to_process.erase(current_bone_idx);
  123. Vector<int> current_bone_child_bones = skeleton->get_bone_children(current_bone_idx);
  124. int child_bone_size = current_bone_child_bones.size();
  125. for (int i = 0; i < child_bone_size; i++) {
  126. bones_to_process.push_back(current_bone_child_bones[i]);
  127. }
  128. const int parent_idx = skeleton->get_bone_parent(current_bone_idx);
  129. TreeItem *parent_item = items.find(parent_idx)->value;
  130. TreeItem *joint_item = filters->create_item(parent_item);
  131. items.insert(current_bone_idx, joint_item);
  132. joint_item->set_text(0, skeleton->get_bone_name(current_bone_idx));
  133. joint_item->set_icon(0, bone_icon);
  134. joint_item->set_selectable(0, true);
  135. joint_item->set_metadata(0, accum + ":" + skeleton->get_bone_name(current_bone_idx));
  136. joint_item->set_collapsed(true);
  137. }
  138. }
  139. }
  140. filters->ensure_cursor_is_visible();
  141. filter_dialog->popup_centered_ratio();
  142. }
  143. void EditorPropertyRootMotion::_node_clear() {
  144. emit_changed(get_edited_property(), NodePath());
  145. update_property();
  146. }
  147. void EditorPropertyRootMotion::update_property() {
  148. NodePath p = get_edited_object()->get(get_edited_property());
  149. assign->set_tooltip_text(p);
  150. if (p == NodePath()) {
  151. assign->set_icon(Ref<Texture2D>());
  152. assign->set_text(TTR("Assign..."));
  153. assign->set_flat(false);
  154. return;
  155. }
  156. assign->set_icon(Ref<Texture2D>());
  157. assign->set_text(p);
  158. }
  159. void EditorPropertyRootMotion::setup(const NodePath &p_base_hint) {
  160. base_hint = p_base_hint;
  161. }
  162. void EditorPropertyRootMotion::_notification(int p_what) {
  163. switch (p_what) {
  164. case NOTIFICATION_ENTER_TREE:
  165. case NOTIFICATION_THEME_CHANGED: {
  166. Ref<Texture2D> t = get_theme_icon(SNAME("Clear"), SNAME("EditorIcons"));
  167. clear->set_icon(t);
  168. } break;
  169. }
  170. }
  171. void EditorPropertyRootMotion::_bind_methods() {
  172. }
  173. EditorPropertyRootMotion::EditorPropertyRootMotion() {
  174. HBoxContainer *hbc = memnew(HBoxContainer);
  175. add_child(hbc);
  176. assign = memnew(Button);
  177. assign->set_h_size_flags(SIZE_EXPAND_FILL);
  178. assign->set_clip_text(true);
  179. assign->connect("pressed", callable_mp(this, &EditorPropertyRootMotion::_node_assign));
  180. hbc->add_child(assign);
  181. clear = memnew(Button);
  182. clear->connect("pressed", callable_mp(this, &EditorPropertyRootMotion::_node_clear));
  183. hbc->add_child(clear);
  184. filter_dialog = memnew(ConfirmationDialog);
  185. add_child(filter_dialog);
  186. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  187. filter_dialog->connect("confirmed", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
  188. filters = memnew(Tree);
  189. filter_dialog->add_child(filters);
  190. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  191. filters->set_hide_root(true);
  192. filters->connect("item_activated", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
  193. //filters->connect("item_edited", this, "_filter_edited");
  194. }
  195. //////////////////////////
  196. bool EditorInspectorRootMotionPlugin::can_handle(Object *p_object) {
  197. return true; // Can handle everything.
  198. }
  199. 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 BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  200. if (p_path == "root_motion_track" && p_object->is_class("AnimationTree") && p_type == Variant::NODE_PATH) {
  201. EditorPropertyRootMotion *editor = memnew(EditorPropertyRootMotion);
  202. add_property_editor(p_path, editor);
  203. return true;
  204. }
  205. return false;
  206. }