root_motion_editor_plugin.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 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. AnimationMixer *mixer = Object::cast_to<AnimationMixer>(get_edited_object());
  50. if (!mixer) {
  51. EditorNode::get_singleton()->show_warning(TTR("Path to AnimationMixer is invalid"));
  52. return;
  53. }
  54. Node *base = mixer->get_node(mixer->get_root_node());
  55. if (!base) {
  56. EditorNode::get_singleton()->show_warning(TTR("AnimationMixer has no valid root node path, so unable to retrieve track names."));
  57. return;
  58. }
  59. HashSet<String> paths;
  60. {
  61. List<StringName> animations;
  62. mixer->get_animation_list(&animations);
  63. for (const StringName &E : animations) {
  64. Ref<Animation> anim = mixer->get_animation(E);
  65. for (int i = 0; i < anim->get_track_count(); i++) {
  66. String pathname = anim->track_get_path(i).get_concatenated_names();
  67. if (!paths.has(pathname)) {
  68. paths.insert(pathname);
  69. }
  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. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  112. if (skeleton) {
  113. HashMap<int, TreeItem *> items;
  114. items.insert(-1, ti);
  115. Ref<Texture> bone_icon = get_editor_theme_icon(SNAME("Bone"));
  116. Vector<int> bones_to_process = skeleton->get_parentless_bones();
  117. while (bones_to_process.size() > 0) {
  118. int current_bone_idx = bones_to_process[0];
  119. bones_to_process.erase(current_bone_idx);
  120. Vector<int> current_bone_child_bones = skeleton->get_bone_children(current_bone_idx);
  121. int child_bone_size = current_bone_child_bones.size();
  122. for (int i = 0; i < child_bone_size; i++) {
  123. bones_to_process.push_back(current_bone_child_bones[i]);
  124. }
  125. const int parent_idx = skeleton->get_bone_parent(current_bone_idx);
  126. TreeItem *parent_item = items.find(parent_idx)->value;
  127. TreeItem *joint_item = filters->create_item(parent_item);
  128. items.insert(current_bone_idx, joint_item);
  129. joint_item->set_text(0, skeleton->get_bone_name(current_bone_idx));
  130. joint_item->set_icon(0, bone_icon);
  131. joint_item->set_selectable(0, true);
  132. joint_item->set_metadata(0, accum + ":" + skeleton->get_bone_name(current_bone_idx));
  133. joint_item->set_collapsed(true);
  134. }
  135. }
  136. }
  137. filters->ensure_cursor_is_visible();
  138. filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
  139. }
  140. void EditorPropertyRootMotion::_node_clear() {
  141. emit_changed(get_edited_property(), NodePath());
  142. update_property();
  143. }
  144. void EditorPropertyRootMotion::update_property() {
  145. NodePath p = get_edited_property_value();
  146. assign->set_tooltip_text(p);
  147. if (p == NodePath()) {
  148. assign->set_button_icon(Ref<Texture2D>());
  149. assign->set_text(TTR("Assign..."));
  150. assign->set_flat(false);
  151. return;
  152. }
  153. assign->set_button_icon(Ref<Texture2D>());
  154. assign->set_text(p);
  155. }
  156. void EditorPropertyRootMotion::setup(const NodePath &p_base_hint) {
  157. base_hint = p_base_hint;
  158. }
  159. void EditorPropertyRootMotion::_notification(int p_what) {
  160. switch (p_what) {
  161. case NOTIFICATION_ENTER_TREE:
  162. case NOTIFICATION_THEME_CHANGED: {
  163. Ref<Texture2D> t = get_editor_theme_icon(SNAME("Clear"));
  164. clear->set_button_icon(t);
  165. } break;
  166. }
  167. }
  168. EditorPropertyRootMotion::EditorPropertyRootMotion() {
  169. HBoxContainer *hbc = memnew(HBoxContainer);
  170. add_child(hbc);
  171. assign = memnew(Button);
  172. assign->set_h_size_flags(SIZE_EXPAND_FILL);
  173. assign->set_clip_text(true);
  174. assign->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyRootMotion::_node_assign));
  175. hbc->add_child(assign);
  176. clear = memnew(Button);
  177. clear->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyRootMotion::_node_clear));
  178. hbc->add_child(clear);
  179. filter_dialog = memnew(ConfirmationDialog);
  180. add_child(filter_dialog);
  181. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  182. filter_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorPropertyRootMotion::_confirmed));
  183. filters = memnew(Tree);
  184. filter_dialog->add_child(filters);
  185. filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  186. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  187. filters->set_hide_root(true);
  188. filters->connect("item_activated", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
  189. //filters->connect("item_edited", this, "_filter_edited");
  190. }
  191. //////////////////////////
  192. bool EditorInspectorRootMotionPlugin::can_handle(Object *p_object) {
  193. return true; // Can handle everything.
  194. }
  195. 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) {
  196. if (p_path == "root_motion_track" && p_object->is_class("AnimationMixer") && p_type == Variant::NODE_PATH) {
  197. EditorPropertyRootMotion *editor = memnew(EditorPropertyRootMotion);
  198. add_property_editor(p_path, editor);
  199. return true;
  200. }
  201. return false;
  202. }