Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/animation/animation_blend_space_1d_editor.cpp
9896 views
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) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "animation_blend_space_1d_editor.h"
32
33
#include "core/os/keyboard.h"
34
#include "editor/editor_node.h"
35
#include "editor/editor_string_names.h"
36
#include "editor/editor_undo_redo_manager.h"
37
#include "editor/gui/editor_file_dialog.h"
38
#include "editor/settings/editor_settings.h"
39
#include "editor/themes/editor_scale.h"
40
#include "scene/animation/animation_blend_tree.h"
41
#include "scene/gui/button.h"
42
#include "scene/gui/check_box.h"
43
#include "scene/gui/line_edit.h"
44
#include "scene/gui/option_button.h"
45
#include "scene/gui/panel_container.h"
46
#include "scene/gui/separator.h"
47
#include "scene/gui/spin_box.h"
48
49
StringName AnimationNodeBlendSpace1DEditor::get_blend_position_path() const {
50
StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";
51
return path;
52
}
53
54
void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {
55
AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
56
if (!tree) {
57
return;
58
}
59
60
Ref<InputEventKey> k = p_event;
61
62
if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
63
if (selected_point != -1) {
64
if (!read_only) {
65
_erase_selected();
66
}
67
accept_event();
68
}
69
}
70
71
Ref<InputEventMouseButton> mb = p_event;
72
73
if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (mb->get_button_index() == MouseButton::LEFT && tool_create->is_pressed()))) {
74
if (!read_only) {
75
menu->clear(false);
76
animations_menu->clear();
77
animations_to_add.clear();
78
79
LocalVector<StringName> classes;
80
ClassDB::get_inheriters_from_class("AnimationRootNode", classes);
81
classes.sort_custom<StringName::AlphCompare>();
82
83
menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);
84
85
List<StringName> names;
86
tree->get_animation_list(&names);
87
88
for (const StringName &E : names) {
89
animations_menu->add_icon_item(get_editor_theme_icon(SNAME("Animation")), E);
90
animations_to_add.push_back(E);
91
}
92
93
for (const StringName &E : classes) {
94
String name = String(E).replace_first("AnimationNode", "");
95
if (name == "Animation" || name == "StartState" || name == "EndState") {
96
continue;
97
}
98
99
int idx = menu->get_item_count();
100
menu->add_item(vformat(TTR("Add %s"), name), idx);
101
menu->set_item_metadata(idx, E);
102
}
103
104
Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
105
if (clipb.is_valid()) {
106
menu->add_separator();
107
menu->add_item(TTR("Paste"), MENU_PASTE);
108
}
109
menu->add_separator();
110
menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
111
112
menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());
113
menu->reset_size();
114
menu->popup();
115
116
add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
117
add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
118
add_point_pos += blend_space->get_min_space();
119
120
if (snap->is_pressed()) {
121
add_point_pos = Math::snapped(add_point_pos, blend_space->get_snap());
122
}
123
}
124
}
125
126
if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
127
blend_space_draw->queue_redraw(); // why not
128
129
// try to see if a point can be selected
130
selected_point = -1;
131
_update_tool_erase();
132
133
for (int i = 0; i < points.size(); i++) {
134
if (Math::abs(float(points[i] - mb->get_position().x)) < 10 * EDSCALE) {
135
selected_point = i;
136
137
Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
138
EditorNode::get_singleton()->push_item(node.ptr(), "", true);
139
dragging_selected_attempt = true;
140
drag_from = mb->get_position();
141
_update_tool_erase();
142
_update_edited_point_pos();
143
return;
144
}
145
}
146
}
147
148
if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {
149
if (!read_only) {
150
if (dragging_selected) {
151
// move
152
float point = blend_space->get_blend_point_position(selected_point);
153
point += drag_ofs.x;
154
155
if (snap->is_pressed()) {
156
point = Math::snapped(point, blend_space->get_snap());
157
}
158
159
updating = true;
160
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
161
undo_redo->create_action(TTR("Move Node Point"));
162
undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
163
undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
164
undo_redo->add_do_method(this, "_update_space");
165
undo_redo->add_undo_method(this, "_update_space");
166
undo_redo->add_do_method(this, "_update_edited_point_pos");
167
undo_redo->add_undo_method(this, "_update_edited_point_pos");
168
undo_redo->commit_action();
169
updating = false;
170
}
171
172
dragging_selected_attempt = false;
173
dragging_selected = false;
174
blend_space_draw->queue_redraw();
175
}
176
}
177
178
// *set* the blend
179
if (mb.is_valid() && !mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
180
float blend_pos = mb->get_position().x / blend_space_draw->get_size().x;
181
blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
182
blend_pos += blend_space->get_min_space();
183
184
tree->set(get_blend_position_path(), blend_pos);
185
blend_space_draw->queue_redraw();
186
}
187
188
Ref<InputEventMouseMotion> mm = p_event;
189
190
if (mm.is_valid() && dragging_selected_attempt) {
191
dragging_selected = true;
192
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));
193
blend_space_draw->queue_redraw();
194
_update_edited_point_pos();
195
}
196
197
if (mm.is_valid() && tool_blend->is_pressed() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
198
float blend_pos = mm->get_position().x / blend_space_draw->get_size().x;
199
blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
200
blend_pos += blend_space->get_min_space();
201
202
tree->set(get_blend_position_path(), blend_pos);
203
204
blend_space_draw->queue_redraw();
205
}
206
}
207
208
void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {
209
AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
210
if (!tree) {
211
return;
212
}
213
214
Color linecolor = get_theme_color(SceneStringName(font_color), SNAME("Label"));
215
Color linecolor_soft = linecolor;
216
linecolor_soft.a *= 0.5;
217
218
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
219
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
220
Ref<Texture2D> icon = get_editor_theme_icon(SNAME("KeyValue"));
221
Ref<Texture2D> icon_selected = get_editor_theme_icon(SNAME("KeySelected"));
222
223
Size2 s = blend_space_draw->get_size();
224
225
if (blend_space_draw->has_focus()) {
226
Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
227
blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);
228
}
229
230
blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor, Math::round(EDSCALE));
231
232
if (blend_space->get_min_space() < 0) {
233
float point = 0.0;
234
point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
235
point *= s.width;
236
237
float x = point;
238
239
blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor, Math::round(EDSCALE));
240
blend_space_draw->draw_string(font, Point2(x + 2 * EDSCALE, s.height - 2 * EDSCALE - font->get_height(font_size) + font->get_ascent(font_size)), "0", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, linecolor);
241
blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft, Math::round(EDSCALE));
242
}
243
244
if (snap->is_pressed()) {
245
linecolor_soft.a = linecolor.a * 0.1;
246
247
if (blend_space->get_snap() > 0) {
248
int prev_idx = -1;
249
250
for (int i = 0; i < s.x; i++) {
251
float v = blend_space->get_min_space() + i * (blend_space->get_max_space() - blend_space->get_min_space()) / s.x;
252
int idx = int(v / blend_space->get_snap());
253
254
if (i > 0 && prev_idx != idx) {
255
blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft, Math::round(EDSCALE));
256
}
257
258
prev_idx = idx;
259
}
260
}
261
}
262
263
points.clear();
264
265
for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
266
float point = blend_space->get_blend_point_position(i);
267
268
if (!read_only) {
269
if (dragging_selected && selected_point == i) {
270
point += drag_ofs.x;
271
if (snap->is_pressed()) {
272
point = Math::snapped(point, blend_space->get_snap());
273
}
274
}
275
}
276
277
point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
278
point *= s.width;
279
280
points.push_back(point);
281
282
Vector2 gui_point = Vector2(point, s.height / 2.0);
283
284
gui_point -= (icon->get_size() / 2.0);
285
286
gui_point = gui_point.floor();
287
288
if (i == selected_point) {
289
blend_space_draw->draw_texture(icon_selected, gui_point);
290
} else {
291
blend_space_draw->draw_texture(icon, gui_point);
292
}
293
}
294
295
// blend position
296
{
297
Color color;
298
if (tool_blend->is_pressed()) {
299
color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
300
} else {
301
color = linecolor;
302
color.a *= 0.5;
303
}
304
305
float point = tree->get(get_blend_position_path());
306
307
point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
308
point *= s.width;
309
310
Vector2 gui_point = Vector2(point, s.height / 2.0);
311
312
float mind = 5 * EDSCALE;
313
float maxd = 15 * EDSCALE;
314
blend_space_draw->draw_line(gui_point + Vector2(mind, 0), gui_point + Vector2(maxd, 0), color, Math::round(2 * EDSCALE));
315
blend_space_draw->draw_line(gui_point + Vector2(-mind, 0), gui_point + Vector2(-maxd, 0), color, Math::round(2 * EDSCALE));
316
blend_space_draw->draw_line(gui_point + Vector2(0, mind), gui_point + Vector2(0, maxd), color, Math::round(2 * EDSCALE));
317
blend_space_draw->draw_line(gui_point + Vector2(0, -mind), gui_point + Vector2(0, -maxd), color, Math::round(2 * EDSCALE));
318
}
319
}
320
321
void AnimationNodeBlendSpace1DEditor::_update_space() {
322
if (updating) {
323
return;
324
}
325
326
updating = true;
327
328
max_value->set_value(blend_space->get_max_space());
329
min_value->set_value(blend_space->get_min_space());
330
331
sync->set_pressed(blend_space->is_using_sync());
332
interpolation->select(blend_space->get_blend_mode());
333
334
label_value->set_text(blend_space->get_value_label());
335
336
snap_value->set_value(blend_space->get_snap());
337
338
blend_space_draw->queue_redraw();
339
340
updating = false;
341
}
342
343
void AnimationNodeBlendSpace1DEditor::_config_changed(double) {
344
if (updating) {
345
return;
346
}
347
348
updating = true;
349
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
350
undo_redo->create_action(TTR("Change BlendSpace1D Config"));
351
undo_redo->add_do_method(blend_space.ptr(), "set_max_space", max_value->get_value());
352
undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());
353
undo_redo->add_do_method(blend_space.ptr(), "set_min_space", min_value->get_value());
354
undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());
355
undo_redo->add_do_method(blend_space.ptr(), "set_snap", snap_value->get_value());
356
undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());
357
undo_redo->add_do_method(blend_space.ptr(), "set_use_sync", sync->is_pressed());
358
undo_redo->add_undo_method(blend_space.ptr(), "set_use_sync", blend_space->is_using_sync());
359
undo_redo->add_do_method(blend_space.ptr(), "set_blend_mode", interpolation->get_selected());
360
undo_redo->add_undo_method(blend_space.ptr(), "set_blend_mode", blend_space->get_blend_mode());
361
undo_redo->add_do_method(this, "_update_space");
362
undo_redo->add_undo_method(this, "_update_space");
363
undo_redo->commit_action();
364
updating = false;
365
366
blend_space_draw->queue_redraw();
367
}
368
369
void AnimationNodeBlendSpace1DEditor::_labels_changed(String) {
370
if (updating) {
371
return;
372
}
373
374
updating = true;
375
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
376
undo_redo->create_action(TTR("Change BlendSpace1D Labels"), UndoRedo::MERGE_ENDS);
377
undo_redo->add_do_method(blend_space.ptr(), "set_value_label", label_value->get_text());
378
undo_redo->add_undo_method(blend_space.ptr(), "set_value_label", blend_space->get_value_label());
379
undo_redo->add_do_method(this, "_update_space");
380
undo_redo->add_undo_method(this, "_update_space");
381
undo_redo->commit_action();
382
updating = false;
383
}
384
385
void AnimationNodeBlendSpace1DEditor::_snap_toggled() {
386
blend_space_draw->queue_redraw();
387
}
388
389
void AnimationNodeBlendSpace1DEditor::_file_opened(const String &p_file) {
390
file_loaded = ResourceLoader::load(p_file);
391
if (file_loaded.is_valid()) {
392
_add_menu_type(MENU_LOAD_FILE_CONFIRM);
393
} else {
394
EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
395
}
396
}
397
398
void AnimationNodeBlendSpace1DEditor::_add_menu_type(int p_index) {
399
Ref<AnimationRootNode> node;
400
if (p_index == MENU_LOAD_FILE) {
401
open_file->clear_filters();
402
List<String> filters;
403
ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
404
for (const String &E : filters) {
405
open_file->add_filter("*." + E);
406
}
407
open_file->popup_file_dialog();
408
return;
409
} else if (p_index == MENU_LOAD_FILE_CONFIRM) {
410
node = file_loaded;
411
file_loaded.unref();
412
} else if (p_index == MENU_PASTE) {
413
node = EditorSettings::get_singleton()->get_resource_clipboard();
414
} else {
415
String type = menu->get_item_metadata(p_index);
416
417
Object *obj = ClassDB::instantiate(type);
418
ERR_FAIL_NULL(obj);
419
AnimationNode *an = Object::cast_to<AnimationNode>(obj);
420
ERR_FAIL_NULL(an);
421
422
node = Ref<AnimationNode>(an);
423
}
424
425
if (node.is_null()) {
426
EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
427
return;
428
}
429
430
updating = true;
431
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
432
undo_redo->create_action(TTR("Add Node Point"));
433
undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);
434
undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
435
undo_redo->add_do_method(this, "_update_space");
436
undo_redo->add_undo_method(this, "_update_space");
437
undo_redo->commit_action();
438
updating = false;
439
440
blend_space_draw->queue_redraw();
441
}
442
443
void AnimationNodeBlendSpace1DEditor::_add_animation_type(int p_index) {
444
Ref<AnimationNodeAnimation> anim;
445
anim.instantiate();
446
447
anim->set_animation(animations_to_add[p_index]);
448
449
updating = true;
450
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
451
undo_redo->create_action(TTR("Add Animation Point"));
452
undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);
453
undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
454
undo_redo->add_do_method(this, "_update_space");
455
undo_redo->add_undo_method(this, "_update_space");
456
undo_redo->commit_action();
457
updating = false;
458
459
blend_space_draw->queue_redraw();
460
}
461
462
void AnimationNodeBlendSpace1DEditor::_tool_switch(int p_tool) {
463
if (p_tool == 0) {
464
tool_erase->show();
465
tool_erase_sep->show();
466
} else {
467
tool_erase->hide();
468
tool_erase_sep->hide();
469
}
470
471
_update_tool_erase();
472
blend_space_draw->queue_redraw();
473
}
474
475
void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {
476
if (updating) {
477
return;
478
}
479
480
if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
481
float pos = blend_space->get_blend_point_position(selected_point);
482
483
if (dragging_selected) {
484
pos += drag_ofs.x;
485
486
if (snap->is_pressed()) {
487
pos = Math::snapped(pos, blend_space->get_snap());
488
}
489
}
490
491
updating = true;
492
edit_value->set_value(pos);
493
updating = false;
494
}
495
}
496
497
void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
498
bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();
499
tool_erase->set_disabled(!point_valid || read_only);
500
501
if (point_valid) {
502
Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
503
504
if (AnimationTreeEditor::get_singleton()->can_edit(an)) {
505
open_editor->show();
506
} else {
507
open_editor->hide();
508
}
509
510
if (!read_only) {
511
edit_hb->show();
512
} else {
513
edit_hb->hide();
514
}
515
} else {
516
edit_hb->hide();
517
}
518
}
519
520
void AnimationNodeBlendSpace1DEditor::_erase_selected() {
521
if (selected_point != -1) {
522
updating = true;
523
524
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
525
undo_redo->create_action(TTR("Remove BlendSpace1D Point"));
526
undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);
527
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);
528
undo_redo->add_do_method(this, "_update_space");
529
undo_redo->add_undo_method(this, "_update_space");
530
undo_redo->commit_action();
531
532
updating = false;
533
534
blend_space_draw->queue_redraw();
535
}
536
}
537
538
void AnimationNodeBlendSpace1DEditor::_edit_point_pos(double) {
539
if (updating) {
540
return;
541
}
542
543
updating = true;
544
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
545
undo_redo->create_action(TTR("Move BlendSpace1D Node Point"));
546
undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, edit_value->get_value());
547
undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
548
undo_redo->add_do_method(this, "_update_space");
549
undo_redo->add_undo_method(this, "_update_space");
550
undo_redo->add_do_method(this, "_update_edited_point_pos");
551
undo_redo->add_undo_method(this, "_update_edited_point_pos");
552
undo_redo->commit_action();
553
updating = false;
554
555
blend_space_draw->queue_redraw();
556
}
557
558
void AnimationNodeBlendSpace1DEditor::_open_editor() {
559
if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
560
Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
561
ERR_FAIL_COND(an.is_null());
562
AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
563
}
564
}
565
566
void AnimationNodeBlendSpace1DEditor::_notification(int p_what) {
567
switch (p_what) {
568
case NOTIFICATION_THEME_CHANGED: {
569
error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
570
error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
571
panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
572
tool_blend->set_button_icon(get_editor_theme_icon(SNAME("EditPivot")));
573
tool_select->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
574
tool_create->set_button_icon(get_editor_theme_icon(SNAME("EditKey")));
575
tool_erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
576
snap->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
577
open_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
578
interpolation->clear();
579
interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), 0);
580
interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), 1);
581
interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), 2);
582
} break;
583
584
case NOTIFICATION_PROCESS: {
585
AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
586
if (!tree) {
587
return;
588
}
589
590
String error;
591
592
if (!tree->is_active()) {
593
error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
594
} else if (tree->is_state_invalid()) {
595
error = tree->get_invalid_state_reason();
596
}
597
598
if (error != error_label->get_text()) {
599
error_label->set_text(error);
600
if (!error.is_empty()) {
601
error_panel->show();
602
} else {
603
error_panel->hide();
604
}
605
}
606
} break;
607
608
case NOTIFICATION_VISIBILITY_CHANGED: {
609
set_process(is_visible_in_tree());
610
} break;
611
}
612
}
613
614
void AnimationNodeBlendSpace1DEditor::_bind_methods() {
615
ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace1DEditor::_update_space);
616
ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace1DEditor::_update_tool_erase);
617
618
ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace1DEditor::_update_edited_point_pos);
619
}
620
621
bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node) {
622
Ref<AnimationNodeBlendSpace1D> b1d = p_node;
623
return b1d.is_valid();
624
}
625
626
void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {
627
blend_space = p_node;
628
read_only = false;
629
630
if (blend_space.is_valid()) {
631
read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);
632
633
_update_space();
634
}
635
636
tool_create->set_disabled(read_only);
637
edit_value->set_editable(!read_only);
638
label_value->set_editable(!read_only);
639
min_value->set_editable(!read_only);
640
max_value->set_editable(!read_only);
641
sync->set_disabled(read_only);
642
interpolation->set_disabled(read_only);
643
}
644
645
AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = nullptr;
646
647
AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {
648
singleton = this;
649
650
HBoxContainer *top_hb = memnew(HBoxContainer);
651
add_child(top_hb);
652
653
Ref<ButtonGroup> bg;
654
bg.instantiate();
655
656
tool_blend = memnew(Button);
657
tool_blend->set_theme_type_variation(SceneStringName(FlatButton));
658
tool_blend->set_toggle_mode(true);
659
tool_blend->set_button_group(bg);
660
top_hb->add_child(tool_blend);
661
tool_blend->set_pressed(true);
662
tool_blend->set_tooltip_text(TTR("Set the blending position within the space"));
663
tool_blend->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(3));
664
665
tool_select = memnew(Button);
666
tool_select->set_theme_type_variation(SceneStringName(FlatButton));
667
tool_select->set_toggle_mode(true);
668
tool_select->set_button_group(bg);
669
top_hb->add_child(tool_select);
670
tool_select->set_tooltip_text(TTR("Select and move points, create points with RMB."));
671
tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(0));
672
673
tool_create = memnew(Button);
674
tool_create->set_theme_type_variation(SceneStringName(FlatButton));
675
tool_create->set_toggle_mode(true);
676
tool_create->set_button_group(bg);
677
top_hb->add_child(tool_create);
678
tool_create->set_tooltip_text(TTR("Create points."));
679
tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(1));
680
681
tool_erase_sep = memnew(VSeparator);
682
top_hb->add_child(tool_erase_sep);
683
tool_erase = memnew(Button);
684
tool_erase->set_theme_type_variation(SceneStringName(FlatButton));
685
top_hb->add_child(tool_erase);
686
tool_erase->set_tooltip_text(TTR("Erase points."));
687
tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_erase_selected));
688
689
top_hb->add_child(memnew(VSeparator));
690
691
snap = memnew(Button);
692
snap->set_theme_type_variation(SceneStringName(FlatButton));
693
snap->set_toggle_mode(true);
694
top_hb->add_child(snap);
695
snap->set_pressed(true);
696
snap->set_tooltip_text(TTR("Enable snap and show grid."));
697
snap->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_snap_toggled));
698
699
snap_value = memnew(SpinBox);
700
top_hb->add_child(snap_value);
701
snap_value->set_min(0.01);
702
snap_value->set_step(0.01);
703
snap_value->set_max(1000);
704
snap_value->set_accessibility_name(TTRC("Grid Step"));
705
706
top_hb->add_child(memnew(VSeparator));
707
top_hb->add_child(memnew(Label(TTR("Sync:"))));
708
sync = memnew(CheckBox);
709
top_hb->add_child(sync);
710
sync->connect(SceneStringName(toggled), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
711
712
top_hb->add_child(memnew(VSeparator));
713
714
top_hb->add_child(memnew(Label(TTR("Blend:"))));
715
interpolation = memnew(OptionButton);
716
top_hb->add_child(interpolation);
717
interpolation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
718
719
edit_hb = memnew(HBoxContainer);
720
top_hb->add_child(edit_hb);
721
edit_hb->add_child(memnew(VSeparator));
722
edit_hb->add_child(memnew(Label(TTR("Point"))));
723
724
edit_value = memnew(SpinBox);
725
edit_hb->add_child(edit_value);
726
edit_value->set_min(-1000);
727
edit_value->set_max(1000);
728
edit_value->set_step(0.01);
729
edit_value->set_accessibility_name(TTRC("Blend Value"));
730
edit_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_edit_point_pos));
731
732
open_editor = memnew(Button);
733
edit_hb->add_child(open_editor);
734
open_editor->set_text(TTR("Open Editor"));
735
open_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_open_editor), CONNECT_DEFERRED);
736
737
edit_hb->hide();
738
open_editor->hide();
739
740
VBoxContainer *main_vb = memnew(VBoxContainer);
741
add_child(main_vb);
742
main_vb->set_v_size_flags(SIZE_EXPAND_FILL);
743
744
panel = memnew(PanelContainer);
745
panel->set_clip_contents(true);
746
main_vb->add_child(panel);
747
panel->set_h_size_flags(SIZE_EXPAND_FILL);
748
panel->set_v_size_flags(SIZE_EXPAND_FILL);
749
750
blend_space_draw = memnew(Control);
751
blend_space_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_gui_input));
752
blend_space_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_draw));
753
blend_space_draw->set_focus_mode(FOCUS_ALL);
754
755
panel->add_child(blend_space_draw);
756
757
{
758
HBoxContainer *bottom_hb = memnew(HBoxContainer);
759
main_vb->add_child(bottom_hb);
760
bottom_hb->set_h_size_flags(SIZE_EXPAND_FILL);
761
762
min_value = memnew(SpinBox);
763
min_value->set_min(-10000);
764
min_value->set_max(0);
765
min_value->set_step(0.01);
766
min_value->set_accessibility_name(TTRC("Min"));
767
768
max_value = memnew(SpinBox);
769
max_value->set_min(0.01);
770
max_value->set_max(10000);
771
max_value->set_step(0.01);
772
max_value->set_accessibility_name(TTRC("Max"));
773
774
label_value = memnew(LineEdit);
775
label_value->set_expand_to_text_length_enabled(true);
776
label_value->set_accessibility_name(TTRC("Value"));
777
778
// now add
779
780
bottom_hb->add_child(min_value);
781
bottom_hb->add_spacer();
782
bottom_hb->add_child(label_value);
783
bottom_hb->add_spacer();
784
bottom_hb->add_child(max_value);
785
}
786
787
snap_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
788
min_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
789
max_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
790
label_value->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_labels_changed));
791
792
error_panel = memnew(PanelContainer);
793
add_child(error_panel);
794
795
error_label = memnew(Label);
796
error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
797
error_panel->add_child(error_label);
798
error_panel->hide();
799
800
menu = memnew(PopupMenu);
801
add_child(menu);
802
menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_menu_type));
803
804
animations_menu = memnew(PopupMenu);
805
animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
806
menu->add_child(animations_menu);
807
animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_animation_type));
808
809
open_file = memnew(EditorFileDialog);
810
add_child(open_file);
811
open_file->set_title(TTR("Open Animation Node"));
812
open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
813
open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_file_opened));
814
815
set_custom_minimum_size(Size2(0, 150 * EDSCALE));
816
}
817
818