root_motion_editor_plugin.cpp 8.5 KB

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