1 /*************************************************************************/
2 /*  animation_tree_player_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_player_editor_plugin.h"
32 
33 #include "core/io/resource_loader.h"
34 #include "core/os/input.h"
35 #include "core/os/keyboard.h"
36 #include "core/project_settings.h"
37 #include "editor/editor_scale.h"
38 #include "scene/gui/menu_button.h"
39 #include "scene/gui/panel.h"
40 #include "scene/main/viewport.h"
41 
edit(AnimationTreePlayer * p_anim_tree)42 void AnimationTreePlayerEditor::edit(AnimationTreePlayer *p_anim_tree) {
43 
44 	anim_tree = p_anim_tree;
45 
46 	if (!anim_tree) {
47 		hide();
48 	} else {
49 		order.clear();
50 		p_anim_tree->get_node_list(&order);
51 		/*
52 		for(List<StringName>::Element* E=order.front();E;E=E->next()) {
53 
54 			if (E->get() >= (int)last_id)
55 				last_id=E->get()+1;
56 		}*/
57 		play_button->set_pressed(p_anim_tree->is_active());
58 		//read the orders
59 	}
60 }
61 
_get_maximum_size()62 Size2 AnimationTreePlayerEditor::_get_maximum_size() {
63 
64 	Size2 max;
65 
66 	for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
67 
68 		Point2 pos = anim_tree->node_get_position(E->get());
69 
70 		if (click_type == CLICK_NODE && click_node == E->get()) {
71 
72 			pos += click_motion - click_pos;
73 		}
74 		pos += get_node_size(E->get());
75 		if (pos.x > max.x)
76 			max.x = pos.x;
77 		if (pos.y > max.y)
78 			max.y = pos.y;
79 	}
80 
81 	return max;
82 }
83 
84 const char *AnimationTreePlayerEditor::_node_type_names[] = { "Output", "Animation", "OneShot", "Mix", "Blend2", "Blend3", "Blend4", "TimeScale", "TimeSeek", "Transition" };
85 
get_node_size(const StringName & p_node) const86 Size2 AnimationTreePlayerEditor::get_node_size(const StringName &p_node) const {
87 
88 	AnimationTreePlayer::NodeType type = anim_tree->node_get_type(p_node);
89 
90 	Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
91 	Ref<Font> font = get_font("font", "PopupMenu");
92 
93 	Size2 size = style->get_minimum_size();
94 
95 	int count = 2; // title and name
96 	int inputs = anim_tree->node_get_input_count(p_node);
97 	count += inputs ? inputs : 1;
98 	String name = p_node;
99 
100 	float name_w = font->get_string_size(name).width;
101 	float type_w = font->get_string_size(String(_node_type_names[type])).width;
102 	float max_w = MAX(name_w, type_w);
103 
104 	switch (type) {
105 		case AnimationTreePlayer::NODE_TIMESEEK:
106 		case AnimationTreePlayer::NODE_OUTPUT: {
107 		} break;
108 		case AnimationTreePlayer::NODE_ANIMATION:
109 		case AnimationTreePlayer::NODE_ONESHOT:
110 		case AnimationTreePlayer::NODE_MIX:
111 		case AnimationTreePlayer::NODE_BLEND2:
112 		case AnimationTreePlayer::NODE_BLEND3:
113 		case AnimationTreePlayer::NODE_BLEND4:
114 		case AnimationTreePlayer::NODE_TIMESCALE:
115 		case AnimationTreePlayer::NODE_TRANSITION: {
116 
117 			size.height += font->get_height();
118 		} break;
119 		case AnimationTreePlayer::NODE_MAX: {
120 		}
121 	}
122 
123 	size.x += max_w + 20;
124 	size.y += count * (font->get_height() + get_constant("vseparation", "PopupMenu"));
125 
126 	return size;
127 }
128 
_edit_dialog_changede(String)129 void AnimationTreePlayerEditor::_edit_dialog_changede(String) {
130 
131 	edit_dialog->hide();
132 }
133 
_edit_dialog_changeds(String s)134 void AnimationTreePlayerEditor::_edit_dialog_changeds(String s) {
135 
136 	_edit_dialog_changed();
137 }
138 
_edit_dialog_changedf(float)139 void AnimationTreePlayerEditor::_edit_dialog_changedf(float) {
140 
141 	_edit_dialog_changed();
142 }
143 
_edit_dialog_changed()144 void AnimationTreePlayerEditor::_edit_dialog_changed() {
145 
146 	if (updating_edit)
147 		return;
148 
149 	if (renaming_edit) {
150 
151 		if (anim_tree->node_rename(edited_node, edit_line[0]->get_text()) == OK) {
152 			for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
153 
154 				if (E->get() == edited_node)
155 					E->get() = edit_line[0]->get_text();
156 			}
157 			edited_node = edit_line[0]->get_text();
158 		}
159 		update();
160 		return;
161 	}
162 
163 	AnimationTreePlayer::NodeType type = anim_tree->node_get_type(edited_node);
164 
165 	switch (type) {
166 
167 		case AnimationTreePlayer::NODE_TIMESCALE:
168 			anim_tree->timescale_node_set_scale(edited_node, edit_line[0]->get_text().to_double());
169 			break;
170 		case AnimationTreePlayer::NODE_ONESHOT:
171 			anim_tree->oneshot_node_set_fadein_time(edited_node, edit_line[0]->get_text().to_double());
172 			anim_tree->oneshot_node_set_fadeout_time(edited_node, edit_line[1]->get_text().to_double());
173 			anim_tree->oneshot_node_set_autorestart_delay(edited_node, edit_line[2]->get_text().to_double());
174 			anim_tree->oneshot_node_set_autorestart_random_delay(edited_node, edit_line[3]->get_text().to_double());
175 			anim_tree->oneshot_node_set_autorestart(edited_node, edit_check->is_pressed());
176 			anim_tree->oneshot_node_set_mix_mode(edited_node, edit_option->get_selected());
177 
178 			break;
179 
180 		case AnimationTreePlayer::NODE_MIX:
181 
182 			anim_tree->mix_node_set_amount(edited_node, edit_scroll[0]->get_value());
183 			break;
184 		case AnimationTreePlayer::NODE_BLEND2:
185 			anim_tree->blend2_node_set_amount(edited_node, edit_scroll[0]->get_value());
186 
187 			break;
188 
189 		case AnimationTreePlayer::NODE_BLEND3:
190 			anim_tree->blend3_node_set_amount(edited_node, edit_scroll[0]->get_value());
191 
192 			break;
193 		case AnimationTreePlayer::NODE_BLEND4:
194 
195 			anim_tree->blend4_node_set_amount(edited_node, Point2(edit_scroll[0]->get_value(), edit_scroll[1]->get_value()));
196 
197 			break;
198 
199 		case AnimationTreePlayer::NODE_TRANSITION: {
200 			anim_tree->transition_node_set_xfade_time(edited_node, edit_line[0]->get_text().to_double());
201 			if (anim_tree->transition_node_get_current(edited_node) != edit_option->get_selected())
202 				anim_tree->transition_node_set_current(edited_node, edit_option->get_selected());
203 		} break;
204 		default: {
205 		}
206 	}
207 }
208 
_edit_dialog_animation_changed()209 void AnimationTreePlayerEditor::_edit_dialog_animation_changed() {
210 
211 	Ref<Animation> anim = property_editor->get_variant().operator RefPtr();
212 	anim_tree->animation_node_set_animation(edited_node, anim);
213 	update();
214 }
215 
_edit_dialog_edit_animation()216 void AnimationTreePlayerEditor::_edit_dialog_edit_animation() {
217 
218 	if (Engine::get_singleton()->is_editor_hint()) {
219 		get_tree()->get_root()->get_child(0)->call("_resource_selected", property_editor->get_variant().operator RefPtr());
220 	};
221 };
222 
_edit_oneshot_start()223 void AnimationTreePlayerEditor::_edit_oneshot_start() {
224 
225 	anim_tree->oneshot_node_start(edited_node);
226 }
227 
_play_toggled()228 void AnimationTreePlayerEditor::_play_toggled() {
229 
230 	anim_tree->set_active(play_button->is_pressed());
231 }
232 
_master_anim_menu_item(int p_item)233 void AnimationTreePlayerEditor::_master_anim_menu_item(int p_item) {
234 
235 	if (p_item == 0)
236 		_edit_filters();
237 	else {
238 
239 		String str = master_anim_popup->get_item_text(p_item);
240 		anim_tree->animation_node_set_master_animation(edited_node, str);
241 	}
242 	update();
243 }
244 
_popup_edit_dialog()245 void AnimationTreePlayerEditor::_popup_edit_dialog() {
246 
247 	updating_edit = true;
248 
249 	for (int i = 0; i < 2; i++)
250 		edit_scroll[i]->hide();
251 
252 	for (int i = 0; i < 4; i++) {
253 
254 		edit_line[i]->hide();
255 		edit_label[i]->hide();
256 	}
257 
258 	edit_option->hide();
259 	edit_button->hide();
260 	filter_button->hide();
261 	edit_check->hide();
262 
263 	Point2 pos = anim_tree->node_get_position(edited_node) - Point2(h_scroll->get_value(), v_scroll->get_value());
264 	Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
265 	Size2 size = get_node_size(edited_node);
266 	Point2 popup_pos(pos.x + style->get_margin(MARGIN_LEFT), pos.y + size.y - style->get_margin(MARGIN_BOTTOM));
267 	popup_pos += get_global_position();
268 
269 	if (renaming_edit) {
270 
271 		edit_label[0]->set_text(TTR("New name:"));
272 		edit_label[0]->set_position(Point2(5, 5));
273 		edit_label[0]->show();
274 		edit_line[0]->set_begin(Point2(15, 25));
275 		edit_line[0]->set_text(edited_node);
276 		edit_line[0]->show();
277 		edit_dialog->set_size(Size2(150, 50));
278 
279 	} else {
280 
281 		AnimationTreePlayer::NodeType type = anim_tree->node_get_type(edited_node);
282 
283 		switch (type) {
284 
285 			case AnimationTreePlayer::NODE_ANIMATION:
286 
287 				if (anim_tree->get_master_player() != NodePath() && anim_tree->has_node(anim_tree->get_master_player()) && Object::cast_to<AnimationPlayer>(anim_tree->get_node(anim_tree->get_master_player()))) {
288 
289 					AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(anim_tree->get_node(anim_tree->get_master_player()));
290 					master_anim_popup->clear();
291 					master_anim_popup->add_item(TTR("Edit Filters"));
292 					master_anim_popup->add_separator();
293 					List<StringName> sn;
294 					ap->get_animation_list(&sn);
295 					sn.sort_custom<StringName::AlphCompare>();
296 					for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
297 						master_anim_popup->add_item(E->get());
298 					}
299 
300 					master_anim_popup->set_position(popup_pos);
301 					master_anim_popup->popup();
302 				} else {
303 					property_editor->edit(this, "", Variant::OBJECT, anim_tree->animation_node_get_animation(edited_node), PROPERTY_HINT_RESOURCE_TYPE, "Animation");
304 					property_editor->set_position(popup_pos);
305 					property_editor->popup();
306 					updating_edit = false;
307 				}
308 				return;
309 			case AnimationTreePlayer::NODE_TIMESCALE:
310 				edit_label[0]->set_text(TTR("Scale:"));
311 				edit_label[0]->set_position(Point2(5, 5));
312 				edit_label[0]->show();
313 				edit_line[0]->set_begin(Point2(15, 25));
314 				edit_line[0]->set_text(rtos(anim_tree->timescale_node_get_scale(edited_node)));
315 				edit_line[0]->show();
316 				edit_dialog->set_size(Size2(150, 50));
317 				break;
318 			case AnimationTreePlayer::NODE_ONESHOT:
319 				edit_label[0]->set_text(TTR("Fade In (s):"));
320 				edit_label[0]->set_position(Point2(5, 5));
321 				edit_label[0]->show();
322 				edit_line[0]->set_begin(Point2(15, 25));
323 				edit_line[0]->set_text(rtos(anim_tree->oneshot_node_get_fadein_time(edited_node)));
324 				edit_line[0]->show();
325 				edit_label[1]->set_text(TTR("Fade Out (s):"));
326 				edit_label[1]->set_position(Point2(5, 55));
327 				edit_label[1]->show();
328 				edit_line[1]->set_begin(Point2(15, 75));
329 				edit_line[1]->set_text(rtos(anim_tree->oneshot_node_get_fadeout_time(edited_node)));
330 				edit_line[1]->show();
331 
332 				edit_option->clear();
333 				edit_option->add_item(TTR("Blend"), 0);
334 				edit_option->add_item(TTR("Mix"), 1);
335 				edit_option->set_begin(Point2(15, 105));
336 
337 				edit_option->select(anim_tree->oneshot_node_get_mix_mode(edited_node));
338 				edit_option->show();
339 
340 				edit_check->set_text(TTR("Auto Restart:"));
341 				edit_check->set_begin(Point2(15, 125));
342 				edit_check->set_pressed(anim_tree->oneshot_node_has_autorestart(edited_node));
343 				edit_check->show();
344 
345 				edit_label[2]->set_text(TTR("Restart (s):"));
346 				edit_label[2]->set_position(Point2(5, 145));
347 				edit_label[2]->show();
348 				edit_line[2]->set_begin(Point2(15, 165));
349 				edit_line[2]->set_text(rtos(anim_tree->oneshot_node_get_autorestart_delay(edited_node)));
350 				edit_line[2]->show();
351 				edit_label[3]->set_text(TTR("Random Restart (s):"));
352 				edit_label[3]->set_position(Point2(5, 195));
353 				edit_label[3]->show();
354 				edit_line[3]->set_begin(Point2(15, 215));
355 				edit_line[3]->set_text(rtos(anim_tree->oneshot_node_get_autorestart_random_delay(edited_node)));
356 				edit_line[3]->show();
357 
358 				filter_button->set_begin(Point2(10, 245));
359 				filter_button->show();
360 
361 				edit_button->set_begin(Point2(10, 268));
362 				edit_button->set_text(TTR("Start!"));
363 
364 				edit_button->show();
365 
366 				edit_dialog->set_size(Size2(180, 293));
367 
368 				break;
369 
370 			case AnimationTreePlayer::NODE_MIX:
371 
372 				edit_label[0]->set_text(TTR("Amount:"));
373 				edit_label[0]->set_position(Point2(5, 5));
374 				edit_label[0]->show();
375 				edit_scroll[0]->set_min(0);
376 				edit_scroll[0]->set_max(1);
377 				edit_scroll[0]->set_step(0.01);
378 				edit_scroll[0]->set_value(anim_tree->mix_node_get_amount(edited_node));
379 				edit_scroll[0]->set_begin(Point2(15, 25));
380 				edit_scroll[0]->show();
381 				edit_dialog->set_size(Size2(150, 50));
382 
383 				break;
384 			case AnimationTreePlayer::NODE_BLEND2:
385 				edit_label[0]->set_text(TTR("Blend:"));
386 				edit_label[0]->set_position(Point2(5, 5));
387 				edit_label[0]->show();
388 				edit_scroll[0]->set_min(0);
389 				edit_scroll[0]->set_max(1);
390 				edit_scroll[0]->set_step(0.01);
391 				edit_scroll[0]->set_value(anim_tree->blend2_node_get_amount(edited_node));
392 				edit_scroll[0]->set_begin(Point2(15, 25));
393 				edit_scroll[0]->show();
394 				filter_button->set_begin(Point2(10, 47));
395 				filter_button->show();
396 				edit_dialog->set_size(Size2(150, 74));
397 
398 				break;
399 
400 			case AnimationTreePlayer::NODE_BLEND3:
401 				edit_label[0]->set_text(TTR("Blend:"));
402 				edit_label[0]->set_position(Point2(5, 5));
403 				edit_label[0]->show();
404 				edit_scroll[0]->set_min(-1);
405 				edit_scroll[0]->set_max(1);
406 				edit_scroll[0]->set_step(0.01);
407 				edit_scroll[0]->set_value(anim_tree->blend3_node_get_amount(edited_node));
408 				edit_scroll[0]->set_begin(Point2(15, 25));
409 				edit_scroll[0]->show();
410 				edit_dialog->set_size(Size2(150, 50));
411 
412 				break;
413 			case AnimationTreePlayer::NODE_BLEND4:
414 
415 				edit_label[0]->set_text(TTR("Blend 0:"));
416 				edit_label[0]->set_position(Point2(5, 5));
417 				edit_label[0]->show();
418 				edit_scroll[0]->set_min(0);
419 				edit_scroll[0]->set_max(1);
420 				edit_scroll[0]->set_step(0.01);
421 				edit_scroll[0]->set_value(anim_tree->blend4_node_get_amount(edited_node).x);
422 				edit_scroll[0]->set_begin(Point2(15, 25));
423 				edit_scroll[0]->show();
424 				edit_label[1]->set_text(TTR("Blend 1:"));
425 				edit_label[1]->set_position(Point2(5, 55));
426 				edit_label[1]->show();
427 				edit_scroll[1]->set_min(0);
428 				edit_scroll[1]->set_max(1);
429 				edit_scroll[1]->set_step(0.01);
430 				edit_scroll[1]->set_value(anim_tree->blend4_node_get_amount(edited_node).y);
431 				edit_scroll[1]->set_begin(Point2(15, 75));
432 				edit_scroll[1]->show();
433 				edit_dialog->set_size(Size2(150, 100));
434 
435 				break;
436 
437 			case AnimationTreePlayer::NODE_TRANSITION: {
438 
439 				edit_label[0]->set_text(TTR("X-Fade Time (s):"));
440 				edit_label[0]->set_position(Point2(5, 5));
441 				edit_label[0]->show();
442 				edit_line[0]->set_begin(Point2(15, 25));
443 				edit_line[0]->set_text(rtos(anim_tree->transition_node_get_xfade_time(edited_node)));
444 				edit_line[0]->show();
445 
446 				edit_label[1]->set_text(TTR("Current:"));
447 				edit_label[1]->set_position(Point2(5, 55));
448 				edit_label[1]->show();
449 				edit_option->set_begin(Point2(15, 75));
450 
451 				edit_option->clear();
452 
453 				for (int i = 0; i < anim_tree->transition_node_get_input_count(edited_node); i++) {
454 					edit_option->add_item(itos(i), i);
455 				}
456 
457 				edit_option->select(anim_tree->transition_node_get_current(edited_node));
458 				edit_option->show();
459 				edit_dialog->set_size(Size2(150, 100));
460 
461 			} break;
462 			default: {
463 			}
464 		}
465 	}
466 
467 	edit_dialog->set_position(popup_pos);
468 	edit_dialog->popup();
469 
470 	updating_edit = false;
471 }
472 
_draw_node(const StringName & p_node)473 void AnimationTreePlayerEditor::_draw_node(const StringName &p_node) {
474 
475 	RID ci = get_canvas_item();
476 	AnimationTreePlayer::NodeType type = anim_tree->node_get_type(p_node);
477 
478 	Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
479 	Ref<Font> font = get_font("font", "PopupMenu");
480 	Color font_color = get_color("font_color", "PopupMenu");
481 	Color font_color_title = get_color("font_color_hover", "PopupMenu");
482 	font_color_title.a *= 0.8;
483 	Ref<Texture> slot_icon = get_icon("VisualShaderPort", "EditorIcons");
484 
485 	Size2 size = get_node_size(p_node);
486 	Point2 pos = anim_tree->node_get_position(p_node);
487 	if (click_type == CLICK_NODE && click_node == p_node) {
488 
489 		pos += click_motion - click_pos;
490 		if (pos.x < 5)
491 			pos.x = 5;
492 		if (pos.y < 5)
493 			pos.y = 5;
494 	}
495 
496 	pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
497 
498 	style->draw(ci, Rect2(pos, size));
499 
500 	float w = size.width - style->get_minimum_size().width;
501 	float h = font->get_height() + get_constant("vseparation", "PopupMenu");
502 
503 	Point2 ofs = style->get_offset() + pos;
504 	Point2 ascofs(0, font->get_ascent());
505 
506 	Color bx = font_color_title;
507 	bx.a *= 0.1;
508 	draw_rect(Rect2(ofs, Size2(size.width - style->get_minimum_size().width, font->get_height())), bx);
509 	font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, String(_node_type_names[type]), font_color_title);
510 
511 	ofs.y += h;
512 	font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, p_node, font_color);
513 	ofs.y += h;
514 
515 	int inputs = anim_tree->node_get_input_count(p_node);
516 
517 	float icon_h_ofs = Math::floor((font->get_height() - slot_icon->get_height()) / 2.0) + 1;
518 
519 	if (type != AnimationTreePlayer::NODE_OUTPUT)
520 		slot_icon->draw(ci, ofs + Point2(w, icon_h_ofs)); //output
521 
522 	if (inputs) {
523 		for (int i = 0; i < inputs; i++) {
524 
525 			slot_icon->draw(ci, ofs + Point2(-slot_icon->get_width(), icon_h_ofs));
526 			String text;
527 			switch (type) {
528 
529 				case AnimationTreePlayer::NODE_TIMESCALE:
530 				case AnimationTreePlayer::NODE_TIMESEEK: text = "in"; break;
531 				case AnimationTreePlayer::NODE_OUTPUT: text = "out"; break;
532 				case AnimationTreePlayer::NODE_ANIMATION: break;
533 				case AnimationTreePlayer::NODE_ONESHOT: text = (i == 0 ? "in" : "add"); break;
534 				case AnimationTreePlayer::NODE_BLEND2:
535 				case AnimationTreePlayer::NODE_MIX: text = (i == 0 ? "a" : "b"); break;
536 				case AnimationTreePlayer::NODE_BLEND3:
537 					switch (i) {
538 						case 0: text = "b-"; break;
539 						case 1: text = "a"; break;
540 						case 2: text = "b+"; break;
541 					}
542 					break;
543 
544 				case AnimationTreePlayer::NODE_BLEND4:
545 					switch (i) {
546 						case 0: text = "a0"; break;
547 						case 1: text = "b0"; break;
548 						case 2: text = "a1"; break;
549 						case 3: text = "b1"; break;
550 					}
551 					break;
552 
553 				case AnimationTreePlayer::NODE_TRANSITION:
554 					text = itos(i);
555 					if (anim_tree->transition_node_has_input_auto_advance(p_node, i))
556 						text += "->";
557 
558 					break;
559 				default: {
560 				}
561 			}
562 			font->draw(ci, ofs + ascofs + Point2(3, 0), text, font_color);
563 
564 			ofs.y += h;
565 		}
566 	} else {
567 		ofs.y += h;
568 	}
569 
570 	Ref<StyleBox> pg_bg = get_stylebox("bg", "ProgressBar");
571 	Ref<StyleBox> pg_fill = get_stylebox("fill", "ProgressBar");
572 	Rect2 pg_rect(ofs, Size2(w, h));
573 
574 	bool editable = true;
575 	switch (type) {
576 		case AnimationTreePlayer::NODE_ANIMATION: {
577 
578 			Ref<Animation> anim = anim_tree->animation_node_get_animation(p_node);
579 			String text;
580 			if (anim_tree->animation_node_get_master_animation(p_node) != "")
581 				text = anim_tree->animation_node_get_master_animation(p_node);
582 			else if (anim.is_null())
583 				text = "load...";
584 			else
585 				text = anim->get_name();
586 
587 			font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, text, font_color_title);
588 
589 		} break;
590 		case AnimationTreePlayer::NODE_ONESHOT:
591 		case AnimationTreePlayer::NODE_MIX:
592 		case AnimationTreePlayer::NODE_BLEND2:
593 		case AnimationTreePlayer::NODE_BLEND3:
594 		case AnimationTreePlayer::NODE_BLEND4:
595 		case AnimationTreePlayer::NODE_TIMESCALE:
596 		case AnimationTreePlayer::NODE_TRANSITION: {
597 
598 			font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, "edit...", font_color_title);
599 		} break;
600 		default: editable = false;
601 	}
602 
603 	if (editable) {
604 
605 		Ref<Texture> arrow = get_icon("GuiDropdown", "EditorIcons");
606 		Point2 arrow_ofs(w - arrow->get_width(), Math::floor((h - arrow->get_height()) / 2));
607 		arrow->draw(ci, ofs + arrow_ofs);
608 	}
609 }
610 
_locate_click(const Point2 & p_click,StringName * p_node_id,int * p_slot_index) const611 AnimationTreePlayerEditor::ClickType AnimationTreePlayerEditor::_locate_click(const Point2 &p_click, StringName *p_node_id, int *p_slot_index) const {
612 
613 	Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
614 	Ref<Font> font = get_font("font", "PopupMenu");
615 
616 	float h = (font->get_height() + get_constant("vseparation", "PopupMenu"));
617 
618 	for (const List<StringName>::Element *E = order.back(); E; E = E->prev()) {
619 
620 		const StringName &node = E->get();
621 
622 		AnimationTreePlayer::NodeType type = anim_tree->node_get_type(node);
623 
624 		Point2 pos = anim_tree->node_get_position(node);
625 		Size2 size = get_node_size(node);
626 
627 		pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
628 
629 		if (!Rect2(pos, size).has_point(p_click))
630 			continue;
631 
632 		if (p_node_id)
633 			*p_node_id = node;
634 
635 		pos = p_click - pos;
636 
637 		float y = pos.y - style->get_offset().height;
638 
639 		if (y < 2 * h)
640 			return CLICK_NODE;
641 		y -= 2 * h;
642 
643 		int inputs = anim_tree->node_get_input_count(node);
644 		int count = MAX(inputs, 1);
645 
646 		if (inputs == 0 || (pos.x > size.width / 2 && type != AnimationTreePlayer::NODE_OUTPUT)) {
647 
648 			if (y < count * h) {
649 
650 				if (p_slot_index)
651 					*p_slot_index = 0;
652 				return CLICK_OUTPUT_SLOT;
653 			}
654 		}
655 
656 		for (int i = 0; i < count; i++) {
657 
658 			if (y < h) {
659 				if (p_slot_index)
660 					*p_slot_index = i;
661 				return CLICK_INPUT_SLOT;
662 			}
663 			y -= h;
664 		}
665 
666 		bool has_parameters = type != AnimationTreePlayer::NODE_OUTPUT && type != AnimationTreePlayer::NODE_TIMESEEK;
667 		return has_parameters ? CLICK_PARAMETER : CLICK_NODE;
668 	}
669 
670 	return CLICK_NONE;
671 }
672 
_get_slot_pos(const StringName & p_node_id,bool p_input,int p_slot)673 Point2 AnimationTreePlayerEditor::_get_slot_pos(const StringName &p_node_id, bool p_input, int p_slot) {
674 
675 	Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
676 	Ref<Font> font = get_font("font", "PopupMenu");
677 	Ref<Texture> slot_icon = get_icon("VisualShaderPort", "EditorIcons");
678 
679 	Size2 size = get_node_size(p_node_id);
680 	Point2 pos = anim_tree->node_get_position(p_node_id);
681 
682 	if (click_type == CLICK_NODE && click_node == p_node_id) {
683 
684 		pos += click_motion - click_pos;
685 		if (pos.x < 5)
686 			pos.x = 5;
687 		if (pos.y < 5)
688 			pos.y = 5;
689 	}
690 
691 	pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
692 
693 	float w = size.width - style->get_minimum_size().width;
694 	float h = font->get_height() + get_constant("vseparation", "PopupMenu");
695 
696 	pos += style->get_offset();
697 
698 	pos.y += h * 2;
699 
700 	pos.y += h * p_slot;
701 
702 	pos += Point2(-slot_icon->get_width() / 2.0, h / 2.0).floor();
703 
704 	if (!p_input) {
705 		pos.x += w + slot_icon->get_width();
706 	}
707 
708 	return pos;
709 }
710 
_gui_input(Ref<InputEvent> p_event)711 void AnimationTreePlayerEditor::_gui_input(Ref<InputEvent> p_event) {
712 
713 	Ref<InputEventMouseButton> mb = p_event;
714 
715 	if (mb.is_valid()) {
716 
717 		if (mb->is_pressed()) {
718 
719 			if (mb->get_button_index() == 1) {
720 				click_pos = Point2(mb->get_position().x, mb->get_position().y);
721 				click_motion = click_pos;
722 				click_type = _locate_click(click_pos, &click_node, &click_slot);
723 				if (click_type != CLICK_NONE) {
724 
725 					order.erase(click_node);
726 					order.push_back(click_node);
727 					update();
728 				}
729 
730 				switch (click_type) {
731 					case CLICK_INPUT_SLOT: {
732 						click_pos = _get_slot_pos(click_node, true, click_slot);
733 					} break;
734 					case CLICK_OUTPUT_SLOT: {
735 						click_pos = _get_slot_pos(click_node, false, click_slot);
736 					} break;
737 					case CLICK_PARAMETER: {
738 
739 						edited_node = click_node;
740 						renaming_edit = false;
741 						_popup_edit_dialog();
742 						//open editor
743 						//_node_edit_property(click_node);
744 					} break;
745 					default: {
746 					}
747 				}
748 			}
749 			if (mb->get_button_index() == 2) {
750 
751 				if (click_type != CLICK_NONE) {
752 					click_type = CLICK_NONE;
753 					update();
754 				} else {
755 					// try to disconnect/remove
756 
757 					Point2 rclick_pos = Point2(mb->get_position().x, mb->get_position().y);
758 					rclick_type = _locate_click(rclick_pos, &rclick_node, &rclick_slot);
759 					if (rclick_type == CLICK_INPUT_SLOT || rclick_type == CLICK_OUTPUT_SLOT) {
760 
761 						node_popup->clear();
762 						node_popup->set_size(Size2(1, 1));
763 						node_popup->add_item(TTR("Disconnect"), NODE_DISCONNECT);
764 						if (anim_tree->node_get_type(rclick_node) == AnimationTreePlayer::NODE_TRANSITION) {
765 							node_popup->add_item(TTR("Add Input"), NODE_ADD_INPUT);
766 							if (rclick_type == CLICK_INPUT_SLOT) {
767 								if (anim_tree->transition_node_has_input_auto_advance(rclick_node, rclick_slot))
768 									node_popup->add_item(TTR("Clear Auto-Advance"), NODE_CLEAR_AUTOADVANCE);
769 								else
770 									node_popup->add_item(TTR("Set Auto-Advance"), NODE_SET_AUTOADVANCE);
771 								node_popup->add_item(TTR("Delete Input"), NODE_DELETE_INPUT);
772 							}
773 						}
774 
775 						node_popup->set_position(rclick_pos + get_global_position());
776 						node_popup->popup();
777 					}
778 
779 					if (rclick_type == CLICK_NODE) {
780 						node_popup->clear();
781 						node_popup->set_size(Size2(1, 1));
782 						node_popup->add_item(TTR("Rename"), NODE_RENAME);
783 						node_popup->add_item(TTR("Remove"), NODE_ERASE);
784 						if (anim_tree->node_get_type(rclick_node) == AnimationTreePlayer::NODE_TRANSITION)
785 							node_popup->add_item(TTR("Add Input"), NODE_ADD_INPUT);
786 						node_popup->set_position(rclick_pos + get_global_position());
787 						node_popup->popup();
788 					}
789 				}
790 			}
791 		} else {
792 
793 			if (mb->get_button_index() == 1 && click_type != CLICK_NONE) {
794 
795 				switch (click_type) {
796 					case CLICK_INPUT_SLOT:
797 					case CLICK_OUTPUT_SLOT: {
798 
799 						Point2 dst_click_pos = Point2(mb->get_position().x, mb->get_position().y);
800 						StringName id;
801 						int slot;
802 						ClickType dst_click_type = _locate_click(dst_click_pos, &id, &slot);
803 
804 						if (dst_click_type == CLICK_INPUT_SLOT && click_type == CLICK_OUTPUT_SLOT) {
805 
806 							anim_tree->connect_nodes(click_node, id, slot);
807 						}
808 						if (click_type == CLICK_INPUT_SLOT && dst_click_type == CLICK_OUTPUT_SLOT) {
809 
810 							anim_tree->connect_nodes(id, click_node, click_slot);
811 						}
812 
813 					} break;
814 					case CLICK_NODE: {
815 						Point2 new_pos = anim_tree->node_get_position(click_node) + (click_motion - click_pos);
816 						if (new_pos.x < 5)
817 							new_pos.x = 5;
818 						if (new_pos.y < 5)
819 							new_pos.y = 5;
820 						anim_tree->node_set_position(click_node, new_pos);
821 
822 					} break;
823 					default: {
824 					}
825 				}
826 
827 				click_type = CLICK_NONE;
828 				update();
829 			}
830 		}
831 	}
832 
833 	Ref<InputEventMouseMotion> mm = p_event;
834 
835 	if (mm.is_valid()) {
836 
837 		if (mm->get_button_mask() & 1 && click_type != CLICK_NONE) {
838 
839 			click_motion = Point2(mm->get_position().x, mm->get_position().y);
840 			update();
841 		}
842 		if (mm->get_button_mask() & 4 || Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
843 
844 			h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
845 			v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
846 			update();
847 		}
848 	}
849 }
850 
_draw_cos_line(const Vector2 & p_from,const Vector2 & p_to,const Color & p_color)851 void AnimationTreePlayerEditor::_draw_cos_line(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color) {
852 
853 	static const int steps = 20;
854 
855 	Rect2 r;
856 	r.position = p_from;
857 	r.expand_to(p_to);
858 	Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1);
859 	bool flip = sign.x * sign.y < 0;
860 
861 	Vector2 prev;
862 	for (int i = 0; i <= steps; i++) {
863 
864 		float d = i / float(steps);
865 		float c = -Math::cos(d * Math_PI) * 0.5 + 0.5;
866 		if (flip)
867 			c = 1.0 - c;
868 		Vector2 p = r.position + Vector2(d * r.size.width, c * r.size.height);
869 
870 		if (i > 0) {
871 
872 			draw_line(prev, p, p_color, 2);
873 		}
874 
875 		prev = p;
876 	}
877 }
878 
_notification(int p_what)879 void AnimationTreePlayerEditor::_notification(int p_what) {
880 
881 	switch (p_what) {
882 
883 		case NOTIFICATION_ENTER_TREE: {
884 
885 			play_button->set_icon(get_icon("Play", "EditorIcons"));
886 			add_menu->set_icon(get_icon("Add", "EditorIcons"));
887 		} break;
888 		case NOTIFICATION_DRAW: {
889 
890 			_update_scrollbars();
891 			//VisualServer::get_singleton()->canvas_item_add_rect(get_canvas_item(),Rect2(Point2(),get_size()),Color(0,0,0,1));
892 			get_stylebox("bg", "Tree")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
893 
894 			for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
895 
896 				_draw_node(E->get());
897 			}
898 
899 			if (click_type == CLICK_INPUT_SLOT || click_type == CLICK_OUTPUT_SLOT) {
900 
901 				_draw_cos_line(click_pos, click_motion, Color(0.5, 1, 0.5, 0.8));
902 			}
903 
904 			List<AnimationTreePlayer::Connection> connections;
905 			anim_tree->get_connection_list(&connections);
906 
907 			for (List<AnimationTreePlayer::Connection>::Element *E = connections.front(); E; E = E->next()) {
908 
909 				const AnimationTreePlayer::Connection &c = E->get();
910 				Point2 source = _get_slot_pos(c.src_node, false, 0);
911 				Point2 dest = _get_slot_pos(c.dst_node, true, c.dst_input);
912 				Color col = Color(1, 1, 0.5, 0.8);
913 				/*
914 				if (click_type==CLICK_NODE && click_node==c.src_node) {
915 
916 					source+=click_motion-click_pos;
917 				}
918 
919 				if (click_type==CLICK_NODE && click_node==c.dst_node) {
920 
921 					dest+=click_motion-click_pos;
922 				}*/
923 
924 				_draw_cos_line(source, dest, col);
925 			}
926 
927 			const Ref<Font> f = get_font("font", "Label");
928 			const Point2 status_offset = Point2(5, 25) * EDSCALE + Point2(0, f->get_ascent());
929 
930 			switch (anim_tree->get_last_error()) {
931 
932 				case AnimationTreePlayer::CONNECT_OK: {
933 
934 					f->draw(get_canvas_item(), status_offset, TTR("Animation tree is valid."), Color(0, 1, 0.6, 0.8));
935 				} break;
936 				default: {
937 
938 					f->draw(get_canvas_item(), status_offset, TTR("Animation tree is invalid."), Color(1, 0.6, 0.0, 0.8));
939 				} break;
940 			}
941 
942 		} break;
943 	}
944 }
945 
_update_scrollbars()946 void AnimationTreePlayerEditor::_update_scrollbars() {
947 
948 	Size2 size = get_size();
949 	Size2 hmin = h_scroll->get_combined_minimum_size();
950 	Size2 vmin = v_scroll->get_combined_minimum_size();
951 
952 	v_scroll->set_begin(Point2(size.width - vmin.width, 0));
953 	v_scroll->set_end(Point2(size.width, size.height));
954 
955 	h_scroll->set_begin(Point2(0, size.height - hmin.height));
956 	h_scroll->set_end(Point2(size.width - vmin.width, size.height));
957 
958 	Size2 min = _get_maximum_size();
959 
960 	if (min.height < size.height - hmin.height) {
961 
962 		v_scroll->hide();
963 		offset.y = 0;
964 	} else {
965 
966 		v_scroll->show();
967 		v_scroll->set_max(min.height);
968 		v_scroll->set_page(size.height - hmin.height);
969 		offset.y = v_scroll->get_value();
970 	}
971 
972 	if (min.width < size.width - vmin.width) {
973 
974 		h_scroll->hide();
975 		offset.x = 0;
976 	} else {
977 
978 		h_scroll->show();
979 		h_scroll->set_max(min.width);
980 		h_scroll->set_page(size.width - vmin.width);
981 		offset.x = h_scroll->get_value();
982 	}
983 }
984 
_scroll_moved(float)985 void AnimationTreePlayerEditor::_scroll_moved(float) {
986 
987 	offset.x = h_scroll->get_value();
988 	offset.y = v_scroll->get_value();
989 	update();
990 }
991 
_node_menu_item(int p_item)992 void AnimationTreePlayerEditor::_node_menu_item(int p_item) {
993 
994 	switch (p_item) {
995 
996 		case NODE_DISCONNECT: {
997 
998 			if (rclick_type == CLICK_INPUT_SLOT) {
999 
1000 				anim_tree->disconnect_nodes(rclick_node, rclick_slot);
1001 				update();
1002 			}
1003 
1004 			if (rclick_type == CLICK_OUTPUT_SLOT) {
1005 
1006 				List<AnimationTreePlayer::Connection> connections;
1007 				anim_tree->get_connection_list(&connections);
1008 
1009 				for (List<AnimationTreePlayer::Connection>::Element *E = connections.front(); E; E = E->next()) {
1010 
1011 					const AnimationTreePlayer::Connection &c = E->get();
1012 					if (c.dst_node == rclick_node) {
1013 
1014 						anim_tree->disconnect_nodes(c.dst_node, c.dst_input);
1015 					}
1016 				}
1017 				update();
1018 			}
1019 
1020 		} break;
1021 		case NODE_RENAME: {
1022 
1023 			renaming_edit = true;
1024 			edited_node = rclick_node;
1025 			_popup_edit_dialog();
1026 
1027 		} break;
1028 		case NODE_ADD_INPUT: {
1029 
1030 			anim_tree->transition_node_set_input_count(rclick_node, anim_tree->transition_node_get_input_count(rclick_node) + 1);
1031 			update();
1032 		} break;
1033 		case NODE_DELETE_INPUT: {
1034 
1035 			anim_tree->transition_node_delete_input(rclick_node, rclick_slot);
1036 			update();
1037 		} break;
1038 		case NODE_SET_AUTOADVANCE: {
1039 
1040 			anim_tree->transition_node_set_input_auto_advance(rclick_node, rclick_slot, true);
1041 			update();
1042 
1043 		} break;
1044 		case NODE_CLEAR_AUTOADVANCE: {
1045 
1046 			anim_tree->transition_node_set_input_auto_advance(rclick_node, rclick_slot, false);
1047 			update();
1048 
1049 		} break;
1050 
1051 		case NODE_ERASE: {
1052 
1053 			if (rclick_node == "out")
1054 				break;
1055 			order.erase(rclick_node);
1056 			anim_tree->remove_node(rclick_node);
1057 			update();
1058 		} break;
1059 	}
1060 }
1061 
_add_node(int p_item)1062 StringName AnimationTreePlayerEditor::_add_node(int p_item) {
1063 
1064 	static const char *bname[] = {
1065 		"out",
1066 		"anim",
1067 		"oneshot",
1068 		"mix",
1069 		"blend2",
1070 		"blend3",
1071 		"blend4",
1072 		"scale",
1073 		"seek",
1074 		"transition"
1075 	};
1076 
1077 	String name;
1078 	int idx = 1;
1079 
1080 	while (true) {
1081 
1082 		name = bname[p_item];
1083 		if (idx > 1)
1084 			name += " " + itos(idx);
1085 		if (anim_tree->node_exists(name))
1086 			idx++;
1087 		else
1088 			break;
1089 	}
1090 
1091 	anim_tree->add_node((AnimationTreePlayer::NodeType)p_item, name);
1092 	anim_tree->node_set_position(name, Point2(last_x, last_y));
1093 	order.push_back(name);
1094 	last_x += 10;
1095 	last_y += 10;
1096 	last_x = last_x % (int)get_size().width;
1097 	last_y = last_y % (int)get_size().height;
1098 	update();
1099 
1100 	return name;
1101 };
1102 
_file_dialog_selected(String p_path)1103 void AnimationTreePlayerEditor::_file_dialog_selected(String p_path) {
1104 
1105 	switch (file_op) {
1106 
1107 		case MENU_IMPORT_ANIMATIONS: {
1108 			Vector<String> files = file_dialog->get_selected_files();
1109 
1110 			for (int i = 0; i < files.size(); i++) {
1111 
1112 				StringName node = _add_node(AnimationTreePlayer::NODE_ANIMATION);
1113 
1114 				RES anim = ResourceLoader::load(files[i]);
1115 				anim_tree->animation_node_set_animation(node, anim);
1116 				//anim_tree->node_set_name(node, files[i].get_file());
1117 			};
1118 		} break;
1119 
1120 		default:
1121 			break;
1122 	};
1123 };
1124 
_add_menu_item(int p_item)1125 void AnimationTreePlayerEditor::_add_menu_item(int p_item) {
1126 
1127 	if (p_item == MENU_GRAPH_CLEAR) {
1128 
1129 		//clear
1130 	} else if (p_item == MENU_IMPORT_ANIMATIONS) {
1131 
1132 		file_op = MENU_IMPORT_ANIMATIONS;
1133 		file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
1134 		file_dialog->popup_centered_ratio();
1135 
1136 	} else {
1137 
1138 		_add_node(p_item);
1139 	}
1140 }
1141 
get_minimum_size() const1142 Size2 AnimationTreePlayerEditor::get_minimum_size() const {
1143 
1144 	return Size2(10, 200);
1145 }
1146 
_find_paths_for_filter(const StringName & p_node,Set<String> & paths)1147 void AnimationTreePlayerEditor::_find_paths_for_filter(const StringName &p_node, Set<String> &paths) {
1148 
1149 	ERR_FAIL_COND(!anim_tree->node_exists(p_node));
1150 
1151 	for (int i = 0; i < anim_tree->node_get_input_count(p_node); i++) {
1152 
1153 		StringName port = anim_tree->node_get_input_source(p_node, i);
1154 		if (port == StringName())
1155 			continue;
1156 		_find_paths_for_filter(port, paths);
1157 	}
1158 
1159 	if (anim_tree->node_get_type(p_node) == AnimationTreePlayer::NODE_ANIMATION) {
1160 
1161 		Ref<Animation> anim = anim_tree->animation_node_get_animation(p_node);
1162 		if (anim.is_valid()) {
1163 
1164 			for (int i = 0; i < anim->get_track_count(); i++) {
1165 				paths.insert(anim->track_get_path(i));
1166 			}
1167 		}
1168 	}
1169 }
1170 
_filter_edited()1171 void AnimationTreePlayerEditor::_filter_edited() {
1172 
1173 	TreeItem *ed = filter->get_edited();
1174 	if (!ed)
1175 		return;
1176 
1177 	if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ONESHOT) {
1178 		anim_tree->oneshot_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
1179 	} else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_BLEND2) {
1180 		anim_tree->blend2_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
1181 	} else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ANIMATION) {
1182 		anim_tree->animation_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
1183 	}
1184 }
1185 
_edit_filters()1186 void AnimationTreePlayerEditor::_edit_filters() {
1187 
1188 	filter_dialog->popup_centered_ratio();
1189 	filter->clear();
1190 
1191 	Set<String> npb;
1192 	_find_paths_for_filter(edited_node, npb);
1193 
1194 	TreeItem *root = filter->create_item();
1195 	filter->set_hide_root(true);
1196 	Map<String, TreeItem *> pm;
1197 
1198 	Node *base = anim_tree->get_node(anim_tree->get_base_path());
1199 
1200 	for (Set<String>::Element *E = npb.front(); E; E = E->next()) {
1201 
1202 		TreeItem *parent = root;
1203 		String descr = E->get();
1204 		if (base) {
1205 			NodePath np = E->get();
1206 
1207 			if (np.get_subname_count() == 1) {
1208 				Node *n = base->get_node(np);
1209 				Skeleton *s = Object::cast_to<Skeleton>(n);
1210 				if (s) {
1211 
1212 					String skelbase = E->get().substr(0, E->get().find(":"));
1213 
1214 					int bidx = s->find_bone(np.get_subname(0));
1215 
1216 					if (bidx != -1) {
1217 						int bparent = s->get_bone_parent(bidx);
1218 						//
1219 						if (bparent != -1) {
1220 
1221 							String bpn = skelbase + ":" + s->get_bone_name(bparent);
1222 							if (pm.has(bpn)) {
1223 								parent = pm[bpn];
1224 								descr = np.get_subname(0);
1225 							}
1226 						} else {
1227 
1228 							if (pm.has(skelbase)) {
1229 								parent = pm[skelbase];
1230 							}
1231 						}
1232 					}
1233 				}
1234 			}
1235 		}
1236 
1237 		TreeItem *it = filter->create_item(parent);
1238 		it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1239 		it->set_text(0, descr);
1240 		it->set_metadata(0, NodePath(E->get()));
1241 		it->set_editable(0, true);
1242 		if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ONESHOT) {
1243 			it->set_checked(0, anim_tree->oneshot_node_is_path_filtered(edited_node, E->get()));
1244 		} else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_BLEND2) {
1245 			it->set_checked(0, anim_tree->blend2_node_is_path_filtered(edited_node, E->get()));
1246 		} else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ANIMATION) {
1247 			it->set_checked(0, anim_tree->animation_node_is_path_filtered(edited_node, E->get()));
1248 		}
1249 		pm[E->get()] = it;
1250 	}
1251 }
1252 
_bind_methods()1253 void AnimationTreePlayerEditor::_bind_methods() {
1254 
1255 	ClassDB::bind_method("_add_menu_item", &AnimationTreePlayerEditor::_add_menu_item);
1256 	ClassDB::bind_method("_node_menu_item", &AnimationTreePlayerEditor::_node_menu_item);
1257 	ClassDB::bind_method("_gui_input", &AnimationTreePlayerEditor::_gui_input);
1258 	//ClassDB::bind_method( "_node_param_changed", &AnimationTreeEditor::_node_param_changed );
1259 	ClassDB::bind_method("_scroll_moved", &AnimationTreePlayerEditor::_scroll_moved);
1260 	ClassDB::bind_method("_edit_dialog_changeds", &AnimationTreePlayerEditor::_edit_dialog_changeds);
1261 	ClassDB::bind_method("_edit_dialog_changede", &AnimationTreePlayerEditor::_edit_dialog_changede);
1262 	ClassDB::bind_method("_edit_dialog_changedf", &AnimationTreePlayerEditor::_edit_dialog_changedf);
1263 	ClassDB::bind_method("_edit_dialog_changed", &AnimationTreePlayerEditor::_edit_dialog_changed);
1264 	ClassDB::bind_method("_edit_dialog_animation_changed", &AnimationTreePlayerEditor::_edit_dialog_animation_changed);
1265 	ClassDB::bind_method("_edit_dialog_edit_animation", &AnimationTreePlayerEditor::_edit_dialog_edit_animation);
1266 	ClassDB::bind_method("_play_toggled", &AnimationTreePlayerEditor::_play_toggled);
1267 	ClassDB::bind_method("_edit_oneshot_start", &AnimationTreePlayerEditor::_edit_oneshot_start);
1268 	ClassDB::bind_method("_file_dialog_selected", &AnimationTreePlayerEditor::_file_dialog_selected);
1269 	ClassDB::bind_method("_master_anim_menu_item", &AnimationTreePlayerEditor::_master_anim_menu_item);
1270 	ClassDB::bind_method("_edit_filters", &AnimationTreePlayerEditor::_edit_filters);
1271 	ClassDB::bind_method("_filter_edited", &AnimationTreePlayerEditor::_filter_edited);
1272 }
1273 
AnimationTreePlayerEditor()1274 AnimationTreePlayerEditor::AnimationTreePlayerEditor() {
1275 
1276 	set_focus_mode(FOCUS_ALL);
1277 
1278 	PopupMenu *p;
1279 	List<PropertyInfo> defaults;
1280 
1281 	add_menu = memnew(MenuButton);
1282 	//add_menu->set_
1283 	add_menu->set_position(Point2(0, 0));
1284 	add_menu->set_size(Point2(25, 15));
1285 	add_child(add_menu);
1286 
1287 	p = add_menu->get_popup();
1288 	p->add_item(TTR("Animation Node"), AnimationTreePlayer::NODE_ANIMATION);
1289 	p->add_item(TTR("OneShot Node"), AnimationTreePlayer::NODE_ONESHOT);
1290 	p->add_item(TTR("Mix Node"), AnimationTreePlayer::NODE_MIX);
1291 	p->add_item(TTR("Blend2 Node"), AnimationTreePlayer::NODE_BLEND2);
1292 	p->add_item(TTR("Blend3 Node"), AnimationTreePlayer::NODE_BLEND3);
1293 	p->add_item(TTR("Blend4 Node"), AnimationTreePlayer::NODE_BLEND4);
1294 	p->add_item(TTR("TimeScale Node"), AnimationTreePlayer::NODE_TIMESCALE);
1295 	p->add_item(TTR("TimeSeek Node"), AnimationTreePlayer::NODE_TIMESEEK);
1296 	p->add_item(TTR("Transition Node"), AnimationTreePlayer::NODE_TRANSITION);
1297 	p->add_separator();
1298 	p->add_item(TTR("Import Animations..."), MENU_IMPORT_ANIMATIONS); // wtf
1299 	p->add_separator();
1300 	p->add_item(TTR("Clear"), MENU_GRAPH_CLEAR);
1301 
1302 	p->connect("id_pressed", this, "_add_menu_item");
1303 
1304 	play_button = memnew(Button);
1305 	play_button->set_position(Point2(25, 0) * EDSCALE);
1306 	play_button->set_size(Point2(25, 15));
1307 	add_child(play_button);
1308 	play_button->set_toggle_mode(true);
1309 	play_button->connect("pressed", this, "_play_toggled");
1310 
1311 	last_x = 50;
1312 	last_y = 50;
1313 
1314 	property_editor = memnew(CustomPropertyEditor);
1315 	add_child(property_editor);
1316 	property_editor->connect("variant_changed", this, "_edit_dialog_animation_changed");
1317 	property_editor->connect("resource_edit_request", this, "_edit_dialog_edit_animation");
1318 
1319 	h_scroll = memnew(HScrollBar);
1320 	v_scroll = memnew(VScrollBar);
1321 
1322 	add_child(h_scroll);
1323 	add_child(v_scroll);
1324 
1325 	h_scroll->connect("value_changed", this, "_scroll_moved");
1326 	v_scroll->connect("value_changed", this, "_scroll_moved");
1327 
1328 	node_popup = memnew(PopupMenu);
1329 	add_child(node_popup);
1330 	node_popup->set_as_toplevel(true);
1331 
1332 	master_anim_popup = memnew(PopupMenu);
1333 	add_child(master_anim_popup);
1334 	master_anim_popup->connect("id_pressed", this, "_master_anim_menu_item");
1335 
1336 	node_popup->connect("id_pressed", this, "_node_menu_item");
1337 
1338 	updating_edit = false;
1339 
1340 	edit_dialog = memnew(PopupPanel);
1341 	//edit_dialog->get_ok()->hide();
1342 	//edit_dialog->get_cancel()->hide();
1343 	add_child(edit_dialog);
1344 
1345 	edit_option = memnew(OptionButton);
1346 	edit_option->set_anchor(MARGIN_RIGHT, ANCHOR_END);
1347 	edit_option->set_margin(MARGIN_RIGHT, -10);
1348 	edit_dialog->add_child(edit_option);
1349 	edit_option->connect("item_selected", this, "_edit_dialog_changedf");
1350 	edit_option->hide();
1351 
1352 	for (int i = 0; i < 2; i++) {
1353 		edit_scroll[i] = memnew(HSlider);
1354 		edit_scroll[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
1355 		edit_scroll[i]->set_margin(MARGIN_RIGHT, -10);
1356 		edit_dialog->add_child(edit_scroll[i]);
1357 		edit_scroll[i]->hide();
1358 		edit_scroll[i]->connect("value_changed", this, "_edit_dialog_changedf");
1359 	}
1360 	for (int i = 0; i < 4; i++) {
1361 		edit_line[i] = memnew(LineEdit);
1362 		edit_line[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
1363 		edit_line[i]->set_margin(MARGIN_RIGHT, -10);
1364 		edit_dialog->add_child(edit_line[i]);
1365 		edit_line[i]->hide();
1366 		edit_line[i]->connect("text_changed", this, "_edit_dialog_changeds");
1367 		edit_line[i]->connect("text_entered", this, "_edit_dialog_changede");
1368 		edit_label[i] = memnew(Label);
1369 		edit_dialog->add_child(edit_label[i]);
1370 		edit_label[i]->hide();
1371 	}
1372 
1373 	edit_button = memnew(Button);
1374 	edit_button->set_anchor(MARGIN_RIGHT, ANCHOR_END);
1375 	edit_button->set_margin(MARGIN_RIGHT, -10);
1376 	edit_dialog->add_child(edit_button);
1377 	edit_button->hide();
1378 	edit_button->connect("pressed", this, "_edit_oneshot_start");
1379 
1380 	edit_check = memnew(CheckButton);
1381 	edit_check->set_anchor(MARGIN_RIGHT, ANCHOR_END);
1382 	edit_check->set_margin(MARGIN_RIGHT, -10);
1383 	edit_dialog->add_child(edit_check);
1384 	edit_check->hide();
1385 	edit_check->connect("pressed", this, "_edit_dialog_changed");
1386 
1387 	file_dialog = memnew(EditorFileDialog);
1388 	file_dialog->set_enable_multiple_selection(true);
1389 	file_dialog->set_current_dir(ProjectSettings::get_singleton()->get_resource_path());
1390 	add_child(file_dialog);
1391 	file_dialog->connect("file_selected", this, "_file_dialog_selected");
1392 
1393 	filter_dialog = memnew(AcceptDialog);
1394 	filter_dialog->set_title(TTR("Edit Node Filters"));
1395 	add_child(filter_dialog);
1396 
1397 	filter = memnew(Tree);
1398 	filter_dialog->add_child(filter);
1399 	//filter_dialog->set_child_rect(filter);
1400 	filter->connect("item_edited", this, "_filter_edited");
1401 
1402 	filter_button = memnew(Button);
1403 	filter_button->set_anchor(MARGIN_RIGHT, ANCHOR_END);
1404 	filter_button->set_margin(MARGIN_RIGHT, -10);
1405 	edit_dialog->add_child(filter_button);
1406 	filter_button->hide();
1407 	filter_button->set_text(TTR("Filters..."));
1408 	filter_button->connect("pressed", this, "_edit_filters");
1409 
1410 	set_clip_contents(true);
1411 }
1412 
edit(Object * p_object)1413 void AnimationTreePlayerEditorPlugin::edit(Object *p_object) {
1414 
1415 	anim_tree_editor->edit(Object::cast_to<AnimationTreePlayer>(p_object));
1416 }
1417 
handles(Object * p_object) const1418 bool AnimationTreePlayerEditorPlugin::handles(Object *p_object) const {
1419 
1420 	return p_object->is_class("AnimationTreePlayer");
1421 }
1422 
make_visible(bool p_visible)1423 void AnimationTreePlayerEditorPlugin::make_visible(bool p_visible) {
1424 
1425 	if (p_visible) {
1426 		//editor->hide_animation_player_editors();
1427 		//editor->animation_panel_make_visible(true);
1428 		button->show();
1429 		editor->make_bottom_panel_item_visible(anim_tree_editor);
1430 		anim_tree_editor->set_physics_process(true);
1431 	} else {
1432 
1433 		if (anim_tree_editor->is_visible_in_tree())
1434 			editor->hide_bottom_panel();
1435 		button->hide();
1436 		anim_tree_editor->set_physics_process(false);
1437 	}
1438 }
1439 
AnimationTreePlayerEditorPlugin(EditorNode * p_node)1440 AnimationTreePlayerEditorPlugin::AnimationTreePlayerEditorPlugin(EditorNode *p_node) {
1441 
1442 	editor = p_node;
1443 	anim_tree_editor = memnew(AnimationTreePlayerEditor);
1444 	anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
1445 
1446 	button = editor->add_bottom_panel_item(TTR("AnimationTree"), anim_tree_editor);
1447 	button->hide();
1448 }
1449 
~AnimationTreePlayerEditorPlugin()1450 AnimationTreePlayerEditorPlugin::~AnimationTreePlayerEditorPlugin() {
1451 }
1452