Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/animation/animation_tree_editor_plugin.cpp
9896 views
1
/**************************************************************************/
2
/* animation_tree_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 "animation_tree_editor_plugin.h"
32
33
#include "animation_blend_space_1d_editor.h"
34
#include "animation_blend_space_2d_editor.h"
35
#include "animation_blend_tree_editor_plugin.h"
36
#include "animation_state_machine_editor.h"
37
#include "editor/editor_node.h"
38
#include "editor/gui/editor_bottom_panel.h"
39
#include "editor/settings/editor_command_palette.h"
40
#include "editor/themes/editor_scale.h"
41
#include "scene/animation/animation_blend_tree.h"
42
#include "scene/gui/button.h"
43
#include "scene/gui/margin_container.h"
44
#include "scene/gui/scroll_container.h"
45
#include "scene/gui/separator.h"
46
47
void AnimationTreeEditor::edit(AnimationTree *p_tree) {
48
if (p_tree && !p_tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) {
49
p_tree->connect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed), CONNECT_DEFERRED);
50
}
51
52
if (tree == p_tree) {
53
return;
54
}
55
56
if (tree && tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) {
57
tree->disconnect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed));
58
}
59
60
tree = p_tree;
61
62
Vector<String> path;
63
if (tree) {
64
edit_path(path);
65
}
66
}
67
68
void AnimationTreeEditor::_node_removed(Node *p_node) {
69
if (p_node == tree) {
70
tree = nullptr;
71
_clear_editors();
72
}
73
}
74
75
void AnimationTreeEditor::_path_button_pressed(int p_path) {
76
edited_path.clear();
77
for (int i = 0; i <= p_path; i++) {
78
edited_path.push_back(button_path[i]);
79
}
80
}
81
82
void AnimationTreeEditor::_animation_list_changed() {
83
AnimationNodeBlendTreeEditor *bte = AnimationNodeBlendTreeEditor::get_singleton();
84
if (bte) {
85
bte->update_graph();
86
}
87
}
88
89
void AnimationTreeEditor::_update_path() {
90
while (path_hb->get_child_count() > 1) {
91
memdelete(path_hb->get_child(1));
92
}
93
94
Ref<ButtonGroup> group;
95
group.instantiate();
96
97
Button *b = memnew(Button);
98
b->set_text(TTR("Root"));
99
b->set_toggle_mode(true);
100
b->set_button_group(group);
101
b->set_pressed(true);
102
b->set_focus_mode(FOCUS_ACCESSIBILITY);
103
b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(-1));
104
path_hb->add_child(b);
105
for (int i = 0; i < button_path.size(); i++) {
106
b = memnew(Button);
107
b->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
108
b->set_text(button_path[i]);
109
b->set_toggle_mode(true);
110
b->set_button_group(group);
111
path_hb->add_child(b);
112
b->set_pressed(true);
113
b->set_focus_mode(FOCUS_ACCESSIBILITY);
114
b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(i));
115
}
116
}
117
118
void AnimationTreeEditor::edit_path(const Vector<String> &p_path) {
119
button_path.clear();
120
121
Ref<AnimationNode> node = tree->get_root_animation_node();
122
123
if (node.is_valid()) {
124
current_root = node->get_instance_id();
125
126
for (int i = 0; i < p_path.size(); i++) {
127
Ref<AnimationNode> child = node->get_child_by_name(p_path[i]);
128
ERR_BREAK(child.is_null());
129
node = child;
130
button_path.push_back(p_path[i]);
131
}
132
133
edited_path = button_path;
134
135
for (int i = 0; i < editors.size(); i++) {
136
if (editors[i]->can_edit(node)) {
137
editors[i]->edit(node);
138
editors[i]->show();
139
} else {
140
editors[i]->edit(Ref<AnimationNode>());
141
editors[i]->hide();
142
}
143
}
144
} else {
145
current_root = ObjectID();
146
edited_path = button_path;
147
for (int i = 0; i < editors.size(); i++) {
148
editors[i]->edit(Ref<AnimationNode>());
149
editors[i]->hide();
150
}
151
}
152
153
_update_path();
154
}
155
156
void AnimationTreeEditor::_clear_editors() {
157
button_path.clear();
158
current_root = ObjectID();
159
edited_path = button_path;
160
for (int i = 0; i < editors.size(); i++) {
161
editors[i]->edit(Ref<AnimationNode>());
162
editors[i]->hide();
163
}
164
_update_path();
165
}
166
167
Vector<String> AnimationTreeEditor::get_edited_path() const {
168
return button_path;
169
}
170
171
void AnimationTreeEditor::enter_editor(const String &p_path) {
172
Vector<String> path = edited_path;
173
path.push_back(p_path);
174
edit_path(path);
175
}
176
177
void AnimationTreeEditor::_notification(int p_what) {
178
switch (p_what) {
179
case NOTIFICATION_ENTER_TREE: {
180
get_tree()->connect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed));
181
} break;
182
case NOTIFICATION_PROCESS: {
183
ObjectID root;
184
if (tree && tree->get_root_animation_node().is_valid()) {
185
root = tree->get_root_animation_node()->get_instance_id();
186
}
187
188
if (root != current_root) {
189
edit_path(Vector<String>());
190
}
191
192
if (button_path.size() != edited_path.size()) {
193
edit_path(edited_path);
194
}
195
} break;
196
case NOTIFICATION_EXIT_TREE: {
197
get_tree()->disconnect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed));
198
} break;
199
}
200
}
201
202
AnimationTreeEditor *AnimationTreeEditor::singleton = nullptr;
203
204
void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
205
ERR_FAIL_COND(p_editor->get_parent());
206
editor_base->add_child(p_editor);
207
editors.push_back(p_editor);
208
p_editor->set_h_size_flags(SIZE_EXPAND_FILL);
209
p_editor->set_v_size_flags(SIZE_EXPAND_FILL);
210
p_editor->hide();
211
}
212
213
void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
214
ERR_FAIL_COND(p_editor->get_parent() != editor_base);
215
editor_base->remove_child(p_editor);
216
editors.erase(p_editor);
217
}
218
219
String AnimationTreeEditor::get_base_path() {
220
String path = Animation::PARAMETERS_BASE_PATH;
221
for (int i = 0; i < edited_path.size(); i++) {
222
path += edited_path[i] + "/";
223
}
224
return path;
225
}
226
227
bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const {
228
for (int i = 0; i < editors.size(); i++) {
229
if (editors[i]->can_edit(p_node)) {
230
return true;
231
}
232
}
233
return false;
234
}
235
236
Vector<String> AnimationTreeEditor::get_animation_list() {
237
// This can be called off the main thread due to resource preview generation. Quit early in that case.
238
if (!singleton->tree || !Thread::is_main_thread() || !singleton->is_visible()) {
239
// When tree is empty, singleton not in the main thread.
240
return Vector<String>();
241
}
242
243
AnimationTree *tree = singleton->tree;
244
if (!tree) {
245
return Vector<String>();
246
}
247
248
List<StringName> anims;
249
tree->get_animation_list(&anims);
250
Vector<String> ret;
251
for (const StringName &E : anims) {
252
ret.push_back(E);
253
}
254
255
return ret;
256
}
257
258
AnimationTreeEditor::AnimationTreeEditor() {
259
AnimationNodeAnimation::get_editable_animation_list = get_animation_list;
260
path_edit = memnew(ScrollContainer);
261
add_child(path_edit);
262
path_edit->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
263
path_hb = memnew(HBoxContainer);
264
path_edit->add_child(path_hb);
265
path_hb->add_child(memnew(Label(TTR("Path:"))));
266
267
add_child(memnew(HSeparator));
268
269
singleton = this;
270
editor_base = memnew(MarginContainer);
271
editor_base->set_v_size_flags(SIZE_EXPAND_FILL);
272
add_child(editor_base);
273
274
add_plugin(memnew(AnimationNodeBlendTreeEditor));
275
add_plugin(memnew(AnimationNodeBlendSpace1DEditor));
276
add_plugin(memnew(AnimationNodeBlendSpace2DEditor));
277
add_plugin(memnew(AnimationNodeStateMachineEditor));
278
}
279
280
void AnimationTreeEditorPlugin::edit(Object *p_object) {
281
anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));
282
}
283
284
bool AnimationTreeEditorPlugin::handles(Object *p_object) const {
285
return p_object->is_class("AnimationTree");
286
}
287
288
void AnimationTreeEditorPlugin::make_visible(bool p_visible) {
289
if (p_visible) {
290
//editor->hide_animation_player_editors();
291
//editor->animation_panel_make_visible(true);
292
button->show();
293
EditorNode::get_bottom_panel()->make_item_visible(anim_tree_editor);
294
anim_tree_editor->set_process(true);
295
} else {
296
if (anim_tree_editor->is_visible_in_tree()) {
297
EditorNode::get_bottom_panel()->hide_bottom_panel();
298
}
299
button->hide();
300
anim_tree_editor->set_process(false);
301
}
302
}
303
304
AnimationTreeEditorPlugin::AnimationTreeEditorPlugin() {
305
anim_tree_editor = memnew(AnimationTreeEditor);
306
anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
307
308
button = EditorNode::get_bottom_panel()->add_item(TTRC("AnimationTree"), anim_tree_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_animation_tree_bottom_panel", TTRC("Toggle AnimationTree Bottom Panel")));
309
button->hide();
310
}
311
312