1 /*************************************************************************/
2 /*  animation_blend_space_1d_editor.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_blend_space_1d_editor.h"
32 
33 #include "core/os/keyboard.h"
34 #include "editor/editor_scale.h"
35 #include "scene/animation/animation_blend_tree.h"
36 
get_blend_position_path() const37 StringName AnimationNodeBlendSpace1DEditor::get_blend_position_path() const {
38 	StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";
39 	return path;
40 }
41 
_blend_space_gui_input(const Ref<InputEvent> & p_event)42 void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {
43 	Ref<InputEventKey> k = p_event;
44 
45 	if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && !k->is_echo()) {
46 		if (selected_point != -1) {
47 			_erase_selected();
48 			accept_event();
49 		}
50 	}
51 
52 	Ref<InputEventMouseButton> mb = p_event;
53 
54 	if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) || (mb->get_button_index() == BUTTON_LEFT && tool_create->is_pressed()))) {
55 		menu->clear();
56 		animations_menu->clear();
57 		animations_to_add.clear();
58 
59 		List<StringName> classes;
60 		ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
61 		classes.sort_custom<StringName::AlphCompare>();
62 
63 		menu->add_submenu_item(TTR("Add Animation"), "animations");
64 
65 		AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
66 		ERR_FAIL_COND(!gp);
67 
68 		if (gp->has_node(gp->get_animation_player())) {
69 			AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
70 
71 			if (ap) {
72 				List<StringName> names;
73 				ap->get_animation_list(&names);
74 
75 				for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
76 					animations_menu->add_icon_item(get_icon("Animation", "EditorIcons"), E->get());
77 					animations_to_add.push_back(E->get());
78 				}
79 			}
80 		}
81 
82 		for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
83 			String name = String(E->get()).replace_first("AnimationNode", "");
84 			if (name == "Animation")
85 				continue;
86 
87 			int idx = menu->get_item_count();
88 			menu->add_item(vformat("Add %s", name), idx);
89 			menu->set_item_metadata(idx, E->get());
90 		}
91 
92 		Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
93 		if (clipb.is_valid()) {
94 			menu->add_separator();
95 			menu->add_item(TTR("Paste"), MENU_PASTE);
96 		}
97 		menu->add_separator();
98 		menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
99 
100 		menu->set_global_position(blend_space_draw->get_global_transform().xform(mb->get_position()));
101 		menu->popup();
102 
103 		add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
104 		add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
105 		add_point_pos += blend_space->get_min_space();
106 
107 		if (snap->is_pressed()) {
108 			add_point_pos = Math::stepify(add_point_pos, blend_space->get_snap());
109 		}
110 	}
111 
112 	if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
113 		blend_space_draw->update(); // why not
114 
115 		// try to see if a point can be selected
116 		selected_point = -1;
117 		_update_tool_erase();
118 
119 		for (int i = 0; i < points.size(); i++) {
120 
121 			if (Math::abs(float(points[i] - mb->get_position().x)) < 10 * EDSCALE) {
122 				selected_point = i;
123 
124 				Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
125 				EditorNode::get_singleton()->push_item(node.ptr(), "", true);
126 				dragging_selected_attempt = true;
127 				drag_from = mb->get_position();
128 				_update_tool_erase();
129 				_update_edited_point_pos();
130 				return;
131 			}
132 		}
133 	}
134 
135 	if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == BUTTON_LEFT) {
136 		if (dragging_selected) {
137 			// move
138 			float point = blend_space->get_blend_point_position(selected_point);
139 			point += drag_ofs.x;
140 
141 			if (snap->is_pressed()) {
142 				point = Math::stepify(point, blend_space->get_snap());
143 			}
144 
145 			updating = true;
146 			undo_redo->create_action(TTR("Move Node Point"));
147 			undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
148 			undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
149 			undo_redo->add_do_method(this, "_update_space");
150 			undo_redo->add_undo_method(this, "_update_space");
151 			undo_redo->add_do_method(this, "_update_edited_point_pos");
152 			undo_redo->add_undo_method(this, "_update_edited_point_pos");
153 			undo_redo->commit_action();
154 			updating = false;
155 			_update_edited_point_pos();
156 		}
157 
158 		dragging_selected_attempt = false;
159 		dragging_selected = false;
160 		blend_space_draw->update();
161 	}
162 
163 	// *set* the blend
164 	if (mb.is_valid() && !mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
165 		float blend_pos = mb->get_position().x / blend_space_draw->get_size().x;
166 		blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
167 		blend_pos += blend_space->get_min_space();
168 
169 		AnimationTreeEditor::get_singleton()->get_tree()->set(get_blend_position_path(), blend_pos);
170 		blend_space_draw->update();
171 	}
172 
173 	Ref<InputEventMouseMotion> mm = p_event;
174 
175 	if (mm.is_valid() && !blend_space_draw->has_focus()) {
176 		blend_space_draw->grab_focus();
177 		blend_space_draw->update();
178 	}
179 
180 	if (mm.is_valid() && dragging_selected_attempt) {
181 		dragging_selected = true;
182 		drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * ((blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, 0));
183 		blend_space_draw->update();
184 		_update_edited_point_pos();
185 	}
186 
187 	if (mm.is_valid() && tool_blend->is_pressed() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
188 		float blend_pos = mm->get_position().x / blend_space_draw->get_size().x;
189 		blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
190 		blend_pos += blend_space->get_min_space();
191 
192 		AnimationTreeEditor::get_singleton()->get_tree()->set(get_blend_position_path(), blend_pos);
193 
194 		blend_space_draw->update();
195 	}
196 }
197 
_blend_space_draw()198 void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {
199 
200 	Color linecolor = get_color("font_color", "Label");
201 	Color linecolor_soft = linecolor;
202 	linecolor_soft.a *= 0.5;
203 
204 	Ref<Font> font = get_font("font", "Label");
205 	Ref<Texture> icon = get_icon("KeyValue", "EditorIcons");
206 	Ref<Texture> icon_selected = get_icon("KeySelected", "EditorIcons");
207 
208 	Size2 s = blend_space_draw->get_size();
209 
210 	if (blend_space_draw->has_focus()) {
211 		Color color = get_color("accent_color", "Editor");
212 		blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);
213 	}
214 
215 	blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor);
216 
217 	if (blend_space->get_min_space() < 0) {
218 		float point = 0.0;
219 		point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
220 		point *= s.width;
221 
222 		float x = point;
223 
224 		blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor);
225 		blend_space_draw->draw_string(font, Point2(x + 2 * EDSCALE, s.height - 2 * EDSCALE - font->get_height() + font->get_ascent()), "0", linecolor);
226 		blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft);
227 	}
228 
229 	if (snap->is_pressed()) {
230 
231 		linecolor_soft.a = linecolor.a * 0.1;
232 
233 		if (blend_space->get_snap() > 0) {
234 			int prev_idx = -1;
235 
236 			for (int i = 0; i < s.x; i++) {
237 				float v = blend_space->get_min_space() + i * (blend_space->get_max_space() - blend_space->get_min_space()) / s.x;
238 				int idx = int(v / blend_space->get_snap());
239 
240 				if (i > 0 && prev_idx != idx) {
241 					blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft);
242 				}
243 
244 				prev_idx = idx;
245 			}
246 		}
247 	}
248 
249 	points.clear();
250 
251 	for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
252 		float point = blend_space->get_blend_point_position(i);
253 
254 		if (dragging_selected && selected_point == i) {
255 			point += drag_ofs.x;
256 			if (snap->is_pressed()) {
257 				point = Math::stepify(point, blend_space->get_snap());
258 			}
259 		}
260 
261 		point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
262 		point *= s.width;
263 
264 		points.push_back(point);
265 
266 		Vector2 gui_point = Vector2(point, s.height / 2.0);
267 
268 		gui_point -= (icon->get_size() / 2.0);
269 
270 		gui_point = gui_point.floor();
271 
272 		if (i == selected_point) {
273 			blend_space_draw->draw_texture(icon_selected, gui_point);
274 		} else {
275 			blend_space_draw->draw_texture(icon, gui_point);
276 		}
277 	}
278 
279 	// blend position
280 	{
281 		Color color;
282 		if (tool_blend->is_pressed()) {
283 			color = get_color("accent_color", "Editor");
284 		} else {
285 			color = linecolor;
286 			color.a *= 0.5;
287 		}
288 
289 		float point = AnimationTreeEditor::get_singleton()->get_tree()->get(get_blend_position_path());
290 
291 		point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
292 		point *= s.width;
293 
294 		Vector2 gui_point = Vector2(point, s.height / 2.0);
295 
296 		float mind = 5 * EDSCALE;
297 		float maxd = 15 * EDSCALE;
298 		blend_space_draw->draw_line(gui_point + Vector2(mind, 0), gui_point + Vector2(maxd, 0), color, 2);
299 		blend_space_draw->draw_line(gui_point + Vector2(-mind, 0), gui_point + Vector2(-maxd, 0), color, 2);
300 		blend_space_draw->draw_line(gui_point + Vector2(0, mind), gui_point + Vector2(0, maxd), color, 2);
301 		blend_space_draw->draw_line(gui_point + Vector2(0, -mind), gui_point + Vector2(0, -maxd), color, 2);
302 	}
303 }
304 
_update_space()305 void AnimationNodeBlendSpace1DEditor::_update_space() {
306 
307 	if (updating)
308 		return;
309 
310 	updating = true;
311 
312 	max_value->set_value(blend_space->get_max_space());
313 	min_value->set_value(blend_space->get_min_space());
314 
315 	label_value->set_text(blend_space->get_value_label());
316 
317 	snap_value->set_value(blend_space->get_snap());
318 
319 	blend_space_draw->update();
320 
321 	updating = false;
322 }
323 
_config_changed(double)324 void AnimationNodeBlendSpace1DEditor::_config_changed(double) {
325 	if (updating)
326 		return;
327 
328 	updating = true;
329 	undo_redo->create_action(TTR("Change BlendSpace1D Limits"));
330 	undo_redo->add_do_method(blend_space.ptr(), "set_max_space", max_value->get_value());
331 	undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());
332 	undo_redo->add_do_method(blend_space.ptr(), "set_min_space", min_value->get_value());
333 	undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());
334 	undo_redo->add_do_method(blend_space.ptr(), "set_snap", snap_value->get_value());
335 	undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());
336 	undo_redo->add_do_method(this, "_update_space");
337 	undo_redo->add_undo_method(this, "_update_space");
338 	undo_redo->commit_action();
339 	updating = false;
340 
341 	blend_space_draw->update();
342 }
343 
_labels_changed(String)344 void AnimationNodeBlendSpace1DEditor::_labels_changed(String) {
345 	if (updating)
346 		return;
347 
348 	updating = true;
349 	undo_redo->create_action(TTR("Change BlendSpace1D Labels"), UndoRedo::MERGE_ENDS);
350 	undo_redo->add_do_method(blend_space.ptr(), "set_value_label", label_value->get_text());
351 	undo_redo->add_undo_method(blend_space.ptr(), "set_value_label", blend_space->get_value_label());
352 	undo_redo->add_do_method(this, "_update_space");
353 	undo_redo->add_undo_method(this, "_update_space");
354 	undo_redo->commit_action();
355 	updating = false;
356 }
357 
_snap_toggled()358 void AnimationNodeBlendSpace1DEditor::_snap_toggled() {
359 	blend_space_draw->update();
360 }
361 
_file_opened(const String & p_file)362 void AnimationNodeBlendSpace1DEditor::_file_opened(const String &p_file) {
363 
364 	file_loaded = ResourceLoader::load(p_file);
365 	if (file_loaded.is_valid()) {
366 		_add_menu_type(MENU_LOAD_FILE_CONFIRM);
367 	}
368 }
369 
_add_menu_type(int p_index)370 void AnimationNodeBlendSpace1DEditor::_add_menu_type(int p_index) {
371 	Ref<AnimationRootNode> node;
372 	if (p_index == MENU_LOAD_FILE) {
373 
374 		open_file->clear_filters();
375 		List<String> filters;
376 		ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
377 		for (List<String>::Element *E = filters.front(); E; E = E->next()) {
378 			open_file->add_filter("*." + E->get());
379 		}
380 		open_file->popup_centered_ratio();
381 		return;
382 	} else if (p_index == MENU_LOAD_FILE_CONFIRM) {
383 		node = file_loaded;
384 		file_loaded.unref();
385 	} else if (p_index == MENU_PASTE) {
386 
387 		node = EditorSettings::get_singleton()->get_resource_clipboard();
388 	} else {
389 		String type = menu->get_item_metadata(p_index);
390 
391 		Object *obj = ClassDB::instance(type);
392 		ERR_FAIL_COND(!obj);
393 		AnimationNode *an = Object::cast_to<AnimationNode>(obj);
394 		ERR_FAIL_COND(!an);
395 
396 		node = Ref<AnimationNode>(an);
397 	}
398 
399 	if (!node.is_valid()) {
400 		EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
401 		return;
402 	}
403 
404 	updating = true;
405 	undo_redo->create_action(TTR("Add Node Point"));
406 	undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);
407 	undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
408 	undo_redo->add_do_method(this, "_update_space");
409 	undo_redo->add_undo_method(this, "_update_space");
410 	undo_redo->commit_action();
411 	updating = false;
412 
413 	blend_space_draw->update();
414 }
415 
_add_animation_type(int p_index)416 void AnimationNodeBlendSpace1DEditor::_add_animation_type(int p_index) {
417 	Ref<AnimationNodeAnimation> anim;
418 	anim.instance();
419 
420 	anim->set_animation(animations_to_add[p_index]);
421 
422 	updating = true;
423 	undo_redo->create_action(TTR("Add Animation Point"));
424 	undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);
425 	undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
426 	undo_redo->add_do_method(this, "_update_space");
427 	undo_redo->add_undo_method(this, "_update_space");
428 	undo_redo->commit_action();
429 	updating = false;
430 
431 	blend_space_draw->update();
432 }
433 
_tool_switch(int p_tool)434 void AnimationNodeBlendSpace1DEditor::_tool_switch(int p_tool) {
435 
436 	if (p_tool == 0) {
437 		tool_erase->show();
438 		tool_erase_sep->show();
439 	} else {
440 		tool_erase->hide();
441 		tool_erase_sep->hide();
442 	}
443 
444 	_update_tool_erase();
445 	blend_space_draw->update();
446 }
447 
_update_edited_point_pos()448 void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {
449 	if (updating)
450 		return;
451 
452 	if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
453 		float pos = blend_space->get_blend_point_position(selected_point);
454 
455 		if (dragging_selected) {
456 			pos += drag_ofs.x;
457 
458 			if (snap->is_pressed()) {
459 				pos = Math::stepify(pos, blend_space->get_snap());
460 			}
461 		}
462 
463 		updating = true;
464 		edit_value->set_value(pos);
465 		updating = false;
466 	}
467 }
468 
_update_tool_erase()469 void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
470 
471 	bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();
472 	tool_erase->set_disabled(!point_valid);
473 
474 	if (point_valid) {
475 		Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
476 
477 		if (AnimationTreeEditor::get_singleton()->can_edit(an)) {
478 			open_editor->show();
479 		} else {
480 			open_editor->hide();
481 		}
482 
483 		edit_hb->show();
484 	} else {
485 		edit_hb->hide();
486 	}
487 }
488 
_erase_selected()489 void AnimationNodeBlendSpace1DEditor::_erase_selected() {
490 	if (selected_point != -1) {
491 		updating = true;
492 
493 		undo_redo->create_action(TTR("Remove BlendSpace1D Point"));
494 		undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);
495 		undo_redo->add_undo_method(blend_space.ptr(), "add_blend_point", blend_space->get_blend_point_node(selected_point), blend_space->get_blend_point_position(selected_point), selected_point);
496 		undo_redo->add_do_method(this, "_update_space");
497 		undo_redo->add_undo_method(this, "_update_space");
498 		undo_redo->commit_action();
499 
500 		updating = false;
501 
502 		blend_space_draw->update();
503 	}
504 }
505 
_edit_point_pos(double)506 void AnimationNodeBlendSpace1DEditor::_edit_point_pos(double) {
507 	if (updating)
508 		return;
509 
510 	updating = true;
511 	undo_redo->create_action(TTR("Move BlendSpace1D Node Point"));
512 	undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, edit_value->get_value());
513 	undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
514 	undo_redo->add_do_method(this, "_update_space");
515 	undo_redo->add_undo_method(this, "_update_space");
516 	undo_redo->add_do_method(this, "_update_edited_point_pos");
517 	undo_redo->add_undo_method(this, "_update_edited_point_pos");
518 	undo_redo->commit_action();
519 	updating = false;
520 
521 	blend_space_draw->update();
522 }
523 
_open_editor()524 void AnimationNodeBlendSpace1DEditor::_open_editor() {
525 
526 	if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
527 		Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
528 		ERR_FAIL_COND(an.is_null());
529 		AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
530 	}
531 }
532 
_notification(int p_what)533 void AnimationNodeBlendSpace1DEditor::_notification(int p_what) {
534 	if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
535 		error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
536 		error_label->add_color_override("font_color", get_color("error_color", "Editor"));
537 		panel->add_style_override("panel", get_stylebox("bg", "Tree"));
538 		tool_blend->set_icon(get_icon("EditPivot", "EditorIcons"));
539 		tool_select->set_icon(get_icon("ToolSelect", "EditorIcons"));
540 		tool_create->set_icon(get_icon("EditKey", "EditorIcons"));
541 		tool_erase->set_icon(get_icon("Remove", "EditorIcons"));
542 		snap->set_icon(get_icon("SnapGrid", "EditorIcons"));
543 		open_editor->set_icon(get_icon("Edit", "EditorIcons"));
544 	}
545 
546 	if (p_what == NOTIFICATION_PROCESS) {
547 		String error;
548 
549 		if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
550 			error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
551 		} else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
552 			error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
553 		}
554 
555 		if (error != error_label->get_text()) {
556 			error_label->set_text(error);
557 			if (error != String()) {
558 				error_panel->show();
559 			} else {
560 				error_panel->hide();
561 			}
562 		}
563 	}
564 
565 	if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
566 		set_process(is_visible_in_tree());
567 	}
568 }
569 
_bind_methods()570 void AnimationNodeBlendSpace1DEditor::_bind_methods() {
571 	ClassDB::bind_method("_blend_space_gui_input", &AnimationNodeBlendSpace1DEditor::_blend_space_gui_input);
572 	ClassDB::bind_method("_blend_space_draw", &AnimationNodeBlendSpace1DEditor::_blend_space_draw);
573 	ClassDB::bind_method("_config_changed", &AnimationNodeBlendSpace1DEditor::_config_changed);
574 	ClassDB::bind_method("_labels_changed", &AnimationNodeBlendSpace1DEditor::_labels_changed);
575 	ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace1DEditor::_update_space);
576 	ClassDB::bind_method("_snap_toggled", &AnimationNodeBlendSpace1DEditor::_snap_toggled);
577 	ClassDB::bind_method("_tool_switch", &AnimationNodeBlendSpace1DEditor::_tool_switch);
578 	ClassDB::bind_method("_erase_selected", &AnimationNodeBlendSpace1DEditor::_erase_selected);
579 	ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace1DEditor::_update_tool_erase);
580 	ClassDB::bind_method("_edit_point_pos", &AnimationNodeBlendSpace1DEditor::_edit_point_pos);
581 
582 	ClassDB::bind_method("_add_menu_type", &AnimationNodeBlendSpace1DEditor::_add_menu_type);
583 	ClassDB::bind_method("_add_animation_type", &AnimationNodeBlendSpace1DEditor::_add_animation_type);
584 
585 	ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace1DEditor::_update_edited_point_pos);
586 
587 	ClassDB::bind_method("_open_editor", &AnimationNodeBlendSpace1DEditor::_open_editor);
588 
589 	ClassDB::bind_method("_file_opened", &AnimationNodeBlendSpace1DEditor::_file_opened);
590 }
591 
can_edit(const Ref<AnimationNode> & p_node)592 bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node) {
593 
594 	Ref<AnimationNodeBlendSpace1D> b1d = p_node;
595 	return b1d.is_valid();
596 }
597 
edit(const Ref<AnimationNode> & p_node)598 void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {
599 
600 	blend_space = p_node;
601 
602 	if (!blend_space.is_null()) {
603 		_update_space();
604 	}
605 }
606 
607 AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = NULL;
608 
AnimationNodeBlendSpace1DEditor()609 AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {
610 	singleton = this;
611 	updating = false;
612 
613 	HBoxContainer *top_hb = memnew(HBoxContainer);
614 	add_child(top_hb);
615 
616 	Ref<ButtonGroup> bg;
617 	bg.instance();
618 
619 	tool_blend = memnew(ToolButton);
620 	tool_blend->set_toggle_mode(true);
621 	tool_blend->set_button_group(bg);
622 	top_hb->add_child(tool_blend);
623 	tool_blend->set_pressed(true);
624 	tool_blend->set_tooltip(TTR("Set the blending position within the space"));
625 	tool_blend->connect("pressed", this, "_tool_switch", varray(3));
626 
627 	tool_select = memnew(ToolButton);
628 	tool_select->set_toggle_mode(true);
629 	tool_select->set_button_group(bg);
630 	top_hb->add_child(tool_select);
631 	tool_select->set_tooltip(TTR("Select and move points, create points with RMB."));
632 	tool_select->connect("pressed", this, "_tool_switch", varray(0));
633 
634 	tool_create = memnew(ToolButton);
635 	tool_create->set_toggle_mode(true);
636 	tool_create->set_button_group(bg);
637 	top_hb->add_child(tool_create);
638 	tool_create->set_tooltip(TTR("Create points."));
639 	tool_create->connect("pressed", this, "_tool_switch", varray(1));
640 
641 	tool_erase_sep = memnew(VSeparator);
642 	top_hb->add_child(tool_erase_sep);
643 	tool_erase = memnew(ToolButton);
644 	top_hb->add_child(tool_erase);
645 	tool_erase->set_tooltip(TTR("Erase points."));
646 	tool_erase->connect("pressed", this, "_erase_selected");
647 
648 	top_hb->add_child(memnew(VSeparator));
649 
650 	snap = memnew(ToolButton);
651 	snap->set_toggle_mode(true);
652 	top_hb->add_child(snap);
653 	snap->set_pressed(true);
654 	snap->set_tooltip(TTR("Enable snap and show grid."));
655 	snap->connect("pressed", this, "_snap_toggled");
656 
657 	snap_value = memnew(SpinBox);
658 	top_hb->add_child(snap_value);
659 	snap_value->set_min(0.01);
660 	snap_value->set_step(0.01);
661 	snap_value->set_max(1000);
662 
663 	edit_hb = memnew(HBoxContainer);
664 	top_hb->add_child(edit_hb);
665 	edit_hb->add_child(memnew(VSeparator));
666 	edit_hb->add_child(memnew(Label(TTR("Point"))));
667 
668 	edit_value = memnew(SpinBox);
669 	edit_hb->add_child(edit_value);
670 	edit_value->set_min(-1000);
671 	edit_value->set_max(1000);
672 	edit_value->set_step(0.01);
673 	edit_value->connect("value_changed", this, "_edit_point_pos");
674 
675 	open_editor = memnew(Button);
676 	edit_hb->add_child(open_editor);
677 	open_editor->set_text(TTR("Open Editor"));
678 	open_editor->connect("pressed", this, "_open_editor", varray(), CONNECT_DEFERRED);
679 
680 	edit_hb->hide();
681 	open_editor->hide();
682 
683 	VBoxContainer *main_vb = memnew(VBoxContainer);
684 	add_child(main_vb);
685 	main_vb->set_v_size_flags(SIZE_EXPAND_FILL);
686 
687 	panel = memnew(PanelContainer);
688 	panel->set_clip_contents(true);
689 	main_vb->add_child(panel);
690 	panel->set_h_size_flags(SIZE_EXPAND_FILL);
691 	panel->set_v_size_flags(SIZE_EXPAND_FILL);
692 
693 	blend_space_draw = memnew(Control);
694 	blend_space_draw->connect("gui_input", this, "_blend_space_gui_input");
695 	blend_space_draw->connect("draw", this, "_blend_space_draw");
696 	blend_space_draw->set_focus_mode(FOCUS_ALL);
697 
698 	panel->add_child(blend_space_draw);
699 
700 	{
701 		HBoxContainer *bottom_hb = memnew(HBoxContainer);
702 		main_vb->add_child(bottom_hb);
703 		bottom_hb->set_h_size_flags(SIZE_EXPAND_FILL);
704 
705 		min_value = memnew(SpinBox);
706 		min_value->set_min(-10000);
707 		min_value->set_max(0);
708 		min_value->set_step(0.01);
709 
710 		max_value = memnew(SpinBox);
711 		max_value->set_min(0.01);
712 		max_value->set_max(10000);
713 		max_value->set_step(0.01);
714 
715 		label_value = memnew(LineEdit);
716 		label_value->set_expand_to_text_length(true);
717 
718 		// now add
719 
720 		bottom_hb->add_child(min_value);
721 		bottom_hb->add_spacer();
722 		bottom_hb->add_child(label_value);
723 		bottom_hb->add_spacer();
724 		bottom_hb->add_child(max_value);
725 	}
726 
727 	snap_value->connect("value_changed", this, "_config_changed");
728 	min_value->connect("value_changed", this, "_config_changed");
729 	max_value->connect("value_changed", this, "_config_changed");
730 	label_value->connect("text_changed", this, "_labels_changed");
731 
732 	error_panel = memnew(PanelContainer);
733 	add_child(error_panel);
734 
735 	error_label = memnew(Label);
736 	error_panel->add_child(error_label);
737 	error_label->set_text("hmmm");
738 
739 	undo_redo = EditorNode::get_undo_redo();
740 
741 	menu = memnew(PopupMenu);
742 	add_child(menu);
743 	menu->connect("id_pressed", this, "_add_menu_type");
744 
745 	animations_menu = memnew(PopupMenu);
746 	menu->add_child(animations_menu);
747 	animations_menu->set_name("animations");
748 	animations_menu->connect("index_pressed", this, "_add_animation_type");
749 
750 	open_file = memnew(EditorFileDialog);
751 	add_child(open_file);
752 	open_file->set_title(TTR("Open Animation Node"));
753 	open_file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
754 	open_file->connect("file_selected", this, "_file_opened");
755 	undo_redo = EditorNode::get_undo_redo();
756 
757 	selected_point = -1;
758 	dragging_selected = false;
759 	dragging_selected_attempt = false;
760 
761 	set_custom_minimum_size(Size2(0, 150 * EDSCALE));
762 }
763