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) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
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 "core/io/resource_loader.h"
38 #include "core/math/delaunay.h"
39 #include "core/os/input.h"
40 #include "core/os/keyboard.h"
41 #include "core/project_settings.h"
42 #include "editor/editor_scale.h"
43 #include "scene/animation/animation_blend_tree.h"
44 #include "scene/animation/animation_player.h"
45 #include "scene/gui/menu_button.h"
46 #include "scene/gui/panel.h"
47 #include "scene/main/viewport.h"
48 #include "scene/scene_string_names.h"
49 
edit(AnimationTree * p_tree)50 void AnimationTreeEditor::edit(AnimationTree *p_tree) {
51 
52 	if (tree == p_tree)
53 		return;
54 
55 	tree = p_tree;
56 
57 	Vector<String> path;
58 	if (tree->has_meta("_tree_edit_path")) {
59 		path = tree->get_meta("_tree_edit_path");
60 		edit_path(path);
61 	} else {
62 		current_root = 0;
63 	}
64 }
65 
_path_button_pressed(int p_path)66 void AnimationTreeEditor::_path_button_pressed(int p_path) {
67 
68 	edited_path.clear();
69 	for (int i = 0; i <= p_path; i++) {
70 		edited_path.push_back(button_path[i]);
71 	}
72 }
73 
_update_path()74 void AnimationTreeEditor::_update_path() {
75 	while (path_hb->get_child_count() > 1) {
76 		memdelete(path_hb->get_child(1));
77 	}
78 
79 	Ref<ButtonGroup> group;
80 	group.instance();
81 
82 	Button *b = memnew(Button);
83 	b->set_text("root");
84 	b->set_toggle_mode(true);
85 	b->set_button_group(group);
86 	b->set_pressed(true);
87 	b->set_focus_mode(FOCUS_NONE);
88 	b->connect("pressed", this, "_path_button_pressed", varray(-1));
89 	path_hb->add_child(b);
90 	for (int i = 0; i < button_path.size(); i++) {
91 		b = memnew(Button);
92 		b->set_text(button_path[i]);
93 		b->set_toggle_mode(true);
94 		b->set_button_group(group);
95 		path_hb->add_child(b);
96 		b->set_pressed(true);
97 		b->set_focus_mode(FOCUS_NONE);
98 		b->connect("pressed", this, "_path_button_pressed", varray(i));
99 	}
100 }
101 
edit_path(const Vector<String> & p_path)102 void AnimationTreeEditor::edit_path(const Vector<String> &p_path) {
103 
104 	button_path.clear();
105 
106 	Ref<AnimationNode> node = tree->get_tree_root();
107 
108 	if (node.is_valid()) {
109 		current_root = node->get_instance_id();
110 
111 		for (int i = 0; i < p_path.size(); i++) {
112 
113 			Ref<AnimationNode> child = node->get_child_by_name(p_path[i]);
114 			ERR_BREAK(child.is_null());
115 			node = child;
116 			button_path.push_back(p_path[i]);
117 		}
118 
119 		edited_path = button_path;
120 
121 		for (int i = 0; i < editors.size(); i++) {
122 			if (editors[i]->can_edit(node)) {
123 				editors[i]->edit(node);
124 				editors[i]->show();
125 			} else {
126 				editors[i]->edit(Ref<AnimationNode>());
127 				editors[i]->hide();
128 			}
129 		}
130 	} else {
131 		current_root = 0;
132 		edited_path = button_path;
133 	}
134 
135 	_update_path();
136 }
137 
get_edited_path() const138 Vector<String> AnimationTreeEditor::get_edited_path() const {
139 	return button_path;
140 }
141 
enter_editor(const String & p_path)142 void AnimationTreeEditor::enter_editor(const String &p_path) {
143 
144 	Vector<String> path = edited_path;
145 	path.push_back(p_path);
146 	edit_path(path);
147 }
148 
_about_to_show_root()149 void AnimationTreeEditor::_about_to_show_root() {
150 }
151 
_notification(int p_what)152 void AnimationTreeEditor::_notification(int p_what) {
153 	if (p_what == NOTIFICATION_PROCESS) {
154 		ObjectID root = 0;
155 		if (tree && tree->get_tree_root().is_valid()) {
156 			root = tree->get_tree_root()->get_instance_id();
157 		}
158 
159 		if (root != current_root) {
160 			edit_path(Vector<String>());
161 		}
162 
163 		if (button_path.size() != edited_path.size()) {
164 			edit_path(edited_path);
165 		}
166 	}
167 }
168 
_bind_methods()169 void AnimationTreeEditor::_bind_methods() {
170 	ClassDB::bind_method("_path_button_pressed", &AnimationTreeEditor::_path_button_pressed);
171 }
172 
173 AnimationTreeEditor *AnimationTreeEditor::singleton = NULL;
174 
add_plugin(AnimationTreeNodeEditorPlugin * p_editor)175 void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
176 	ERR_FAIL_COND(p_editor->get_parent());
177 	editor_base->add_child(p_editor);
178 	editors.push_back(p_editor);
179 	p_editor->set_h_size_flags(SIZE_EXPAND_FILL);
180 	p_editor->set_v_size_flags(SIZE_EXPAND_FILL);
181 	p_editor->hide();
182 }
183 
remove_plugin(AnimationTreeNodeEditorPlugin * p_editor)184 void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) {
185 	ERR_FAIL_COND(p_editor->get_parent() != editor_base);
186 	editor_base->remove_child(p_editor);
187 	editors.erase(p_editor);
188 }
189 
get_base_path()190 String AnimationTreeEditor::get_base_path() {
191 	String path = SceneStringNames::get_singleton()->parameters_base_path;
192 	for (int i = 0; i < edited_path.size(); i++) {
193 		path += edited_path[i] + "/";
194 	}
195 	return path;
196 }
197 
can_edit(const Ref<AnimationNode> & p_node) const198 bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const {
199 	for (int i = 0; i < editors.size(); i++) {
200 		if (editors[i]->can_edit(p_node)) {
201 			return true;
202 		}
203 	}
204 	return false;
205 }
206 
get_animation_list()207 Vector<String> AnimationTreeEditor::get_animation_list() {
208 
209 	if (!singleton->is_visible()) {
210 		return Vector<String>();
211 	}
212 
213 	AnimationTree *tree = singleton->tree;
214 	if (!tree || !tree->has_node(tree->get_animation_player()))
215 		return Vector<String>();
216 
217 	AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(tree->get_node(tree->get_animation_player()));
218 
219 	if (!ap)
220 		return Vector<String>();
221 
222 	List<StringName> anims;
223 	ap->get_animation_list(&anims);
224 	Vector<String> ret;
225 	for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
226 		ret.push_back(E->get());
227 	}
228 
229 	return ret;
230 }
231 
AnimationTreeEditor()232 AnimationTreeEditor::AnimationTreeEditor() {
233 
234 	AnimationNodeAnimation::get_editable_animation_list = get_animation_list;
235 	path_edit = memnew(ScrollContainer);
236 	add_child(path_edit);
237 	path_edit->set_enable_h_scroll(true);
238 	path_edit->set_enable_v_scroll(false);
239 	path_hb = memnew(HBoxContainer);
240 	path_edit->add_child(path_hb);
241 	path_hb->add_child(memnew(Label(TTR("Path:"))));
242 
243 	add_child(memnew(HSeparator));
244 
245 	current_root = 0;
246 	singleton = this;
247 	editor_base = memnew(MarginContainer);
248 	editor_base->set_v_size_flags(SIZE_EXPAND_FILL);
249 	add_child(editor_base);
250 
251 	add_plugin(memnew(AnimationNodeBlendTreeEditor));
252 	add_plugin(memnew(AnimationNodeBlendSpace1DEditor));
253 	add_plugin(memnew(AnimationNodeBlendSpace2DEditor));
254 	add_plugin(memnew(AnimationNodeStateMachineEditor));
255 }
256 
edit(Object * p_object)257 void AnimationTreeEditorPlugin::edit(Object *p_object) {
258 
259 	anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));
260 }
261 
handles(Object * p_object) const262 bool AnimationTreeEditorPlugin::handles(Object *p_object) const {
263 
264 	return p_object->is_class("AnimationTree");
265 }
266 
make_visible(bool p_visible)267 void AnimationTreeEditorPlugin::make_visible(bool p_visible) {
268 
269 	if (p_visible) {
270 		//editor->hide_animation_player_editors();
271 		//editor->animation_panel_make_visible(true);
272 		button->show();
273 		editor->make_bottom_panel_item_visible(anim_tree_editor);
274 		anim_tree_editor->set_process(true);
275 	} else {
276 
277 		if (anim_tree_editor->is_visible_in_tree())
278 			editor->hide_bottom_panel();
279 		button->hide();
280 		anim_tree_editor->set_process(false);
281 	}
282 }
283 
AnimationTreeEditorPlugin(EditorNode * p_node)284 AnimationTreeEditorPlugin::AnimationTreeEditorPlugin(EditorNode *p_node) {
285 
286 	editor = p_node;
287 	anim_tree_editor = memnew(AnimationTreeEditor);
288 	anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
289 
290 	button = editor->add_bottom_panel_item(TTR("AnimationTree"), anim_tree_editor);
291 	button->hide();
292 }
293 
~AnimationTreeEditorPlugin()294 AnimationTreeEditorPlugin::~AnimationTreeEditorPlugin() {
295 }
296