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