root_motion_editor_plugin.cpp 9.7 KB

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