Debian Codegraph @

editor/scene/3d/root_motion_editor_plugin.cpp

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