Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/curve_editor_plugin.cpp
20852 views
1
/**************************************************************************/
2
/* curve_editor_plugin.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "curve_editor_plugin.h"
32
33
#include "canvas_item_editor_plugin.h"
34
#include "core/input/input.h"
35
#include "core/math/geometry_2d.h"
36
#include "core/os/keyboard.h"
37
#include "editor/editor_interface.h"
38
#include "editor/editor_node.h"
39
#include "editor/editor_string_names.h"
40
#include "editor/editor_undo_redo_manager.h"
41
#include "editor/gui/editor_spin_slider.h"
42
#include "editor/settings/editor_settings.h"
43
#include "editor/themes/editor_scale.h"
44
#include "scene/gui/flow_container.h"
45
#include "scene/gui/menu_button.h"
46
#include "scene/gui/popup_menu.h"
47
#include "scene/gui/separator.h"
48
#include "scene/resources/image_texture.h"
49
50
CurveEdit::CurveEdit() {
51
set_focus_mode(FOCUS_ALL);
52
set_clip_contents(true);
53
}
54
55
void CurveEdit::_bind_methods() {
56
ClassDB::bind_method(D_METHOD("set_selected_index", "index"), &CurveEdit::set_selected_index);
57
}
58
59
void CurveEdit::set_curve(Ref<Curve> p_curve) {
60
if (p_curve == curve) {
61
return;
62
}
63
64
if (curve.is_valid()) {
65
curve->disconnect_changed(callable_mp(this, &CurveEdit::_curve_changed));
66
curve->disconnect(Curve::SIGNAL_RANGE_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
67
curve->disconnect(Curve::SIGNAL_DOMAIN_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
68
}
69
70
curve = p_curve;
71
72
if (curve.is_valid()) {
73
curve->connect_changed(callable_mp(this, &CurveEdit::_curve_changed));
74
curve->connect(Curve::SIGNAL_RANGE_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
75
curve->connect(Curve::SIGNAL_DOMAIN_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
76
}
77
78
// Note: if you edit a curve, then set another, and try to undo,
79
// it will normally apply on the previous curve, but you won't see it.
80
}
81
82
Ref<Curve> CurveEdit::get_curve() {
83
return curve;
84
}
85
86
void CurveEdit::set_snap_enabled(bool p_enabled) {
87
snap_enabled = p_enabled;
88
queue_redraw();
89
if (curve.is_valid()) {
90
if (snap_enabled) {
91
curve->set_meta(SNAME("_snap_enabled"), true);
92
} else {
93
curve->remove_meta(SNAME("_snap_enabled"));
94
}
95
}
96
}
97
98
void CurveEdit::set_snap_count(int p_snap_count) {
99
snap_count = p_snap_count;
100
queue_redraw();
101
if (curve.is_valid()) {
102
if (snap_count != CurveEditor::DEFAULT_SNAP) {
103
curve->set_meta(SNAME("_snap_count"), snap_count);
104
} else {
105
curve->remove_meta(SNAME("_snap_count"));
106
}
107
}
108
}
109
110
Size2 CurveEdit::get_minimum_size() const {
111
return Vector2(64, MAX(135, get_size().x * ASPECT_RATIO)) * EDSCALE;
112
}
113
114
void CurveEdit::_notification(int p_what) {
115
switch (p_what) {
116
case NOTIFICATION_MOUSE_EXIT: {
117
if (hovered_index != -1 || hovered_tangent_index != TANGENT_NONE) {
118
hovered_index = -1;
119
hovered_tangent_index = TANGENT_NONE;
120
queue_redraw();
121
}
122
} break;
123
case NOTIFICATION_THEME_CHANGED: {
124
float gizmo_scale = EDITOR_GET("interface/touchscreen/scale_gizmo_handles");
125
point_radius = Math::round(BASE_POINT_RADIUS * get_theme_default_base_scale() * gizmo_scale);
126
hover_radius = Math::round(BASE_HOVER_RADIUS * get_theme_default_base_scale() * gizmo_scale);
127
tangent_radius = Math::round(BASE_TANGENT_RADIUS * get_theme_default_base_scale() * gizmo_scale);
128
tangent_hover_radius = Math::round(BASE_TANGENT_HOVER_RADIUS * get_theme_default_base_scale() * gizmo_scale);
129
tangent_length = Math::round(BASE_TANGENT_LENGTH * get_theme_default_base_scale());
130
} break;
131
case NOTIFICATION_DRAW: {
132
_redraw();
133
} break;
134
case NOTIFICATION_VISIBILITY_CHANGED: {
135
if (!is_visible()) {
136
grabbing = GRAB_NONE;
137
}
138
} break;
139
}
140
}
141
142
void CurveEdit::gui_input(const Ref<InputEvent> &p_event) {
143
ERR_FAIL_COND(p_event.is_null());
144
if (curve.is_null()) {
145
return;
146
}
147
148
Ref<InputEventKey> k = p_event;
149
if (k.is_valid()) {
150
// Deleting points or making tangents linear.
151
if (k->is_pressed() && k->get_keycode() == Key::KEY_DELETE) {
152
if (selected_tangent_index != TANGENT_NONE) {
153
toggle_linear(selected_index, selected_tangent_index);
154
} else if (selected_index != -1) {
155
if (grabbing == GRAB_ADD) {
156
curve->remove_point(selected_index); // Point is temporary, so remove directly from curve.
157
set_selected_index(-1);
158
} else {
159
remove_point(selected_index);
160
}
161
grabbing = GRAB_NONE;
162
hovered_index = -1;
163
hovered_tangent_index = TANGENT_NONE;
164
}
165
accept_event();
166
}
167
168
if (k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::ALT) {
169
queue_redraw(); // Redraw to show the axes or constraints.
170
}
171
}
172
173
Ref<InputEventMouseButton> mb = p_event;
174
if (mb.is_valid() && mb->is_pressed()) {
175
Vector2 mpos = mb->get_position();
176
177
if (mb->get_button_index() == MouseButton::RIGHT || mb->get_button_index() == MouseButton::MIDDLE) {
178
if (mb->get_button_index() == MouseButton::RIGHT && grabbing == GRAB_MOVE) {
179
// Move a point to its old position.
180
curve->set_point_value(selected_index, initial_grab_pos.y);
181
curve->set_point_offset(selected_index, initial_grab_pos.x);
182
set_selected_index(initial_grab_index);
183
hovered_index = get_point_at(mpos);
184
grabbing = GRAB_NONE;
185
} else {
186
// Remove a point or make a tangent linear.
187
selected_tangent_index = get_tangent_at(mpos);
188
if (selected_tangent_index != TANGENT_NONE) {
189
toggle_linear(selected_index, selected_tangent_index);
190
} else {
191
int point_to_remove = get_point_at(mpos);
192
if (point_to_remove == -1) {
193
set_selected_index(-1); // Nothing on the place of the click, just deselect the point.
194
} else {
195
if (grabbing == GRAB_ADD) {
196
curve->remove_point(point_to_remove); // Point is temporary, so remove directly from curve.
197
set_selected_index(-1);
198
} else {
199
remove_point(point_to_remove);
200
}
201
hovered_index = get_point_at(mpos);
202
grabbing = GRAB_NONE;
203
}
204
}
205
}
206
}
207
208
// Selecting or creating points.
209
if (mb->get_button_index() == MouseButton::LEFT) {
210
if (grabbing == GRAB_NONE) {
211
selected_tangent_index = get_tangent_at(mpos);
212
if (selected_tangent_index == TANGENT_NONE) {
213
set_selected_index(get_point_at(mpos));
214
}
215
queue_redraw();
216
}
217
218
if (selected_index != -1) {
219
// If an existing point/tangent was grabbed, remember a few things about it.
220
grabbing = GRAB_MOVE;
221
initial_grab_pos = curve->get_point_position(selected_index);
222
initial_grab_index = selected_index;
223
if (selected_index > 0) {
224
initial_grab_left_tangent = curve->get_point_left_tangent(selected_index);
225
}
226
if (selected_index < curve->get_point_count() - 1) {
227
initial_grab_right_tangent = curve->get_point_right_tangent(selected_index);
228
}
229
} else if (grabbing == GRAB_NONE) {
230
// Adding a new point. Insert a temporary point for the user to adjust, so it's not in the undo/redo.
231
Vector2 new_pos = get_world_pos(mpos).clamp(Vector2(curve->get_min_domain(), curve->get_min_value()), Vector2(curve->get_max_domain(), curve->get_max_value()));
232
if (snap_enabled || mb->is_command_or_control_pressed()) {
233
new_pos.x = Math::snapped(new_pos.x - curve->get_min_domain(), curve->get_domain_range() / snap_count) + curve->get_min_domain();
234
new_pos.y = Math::snapped(new_pos.y - curve->get_min_value(), curve->get_value_range() / snap_count) + curve->get_min_value();
235
}
236
237
new_pos.x = get_offset_without_collision(selected_index, new_pos.x, mpos.x >= get_view_pos(new_pos).x);
238
239
// Add a temporary point for the user to adjust before adding it permanently.
240
int new_idx = curve->add_point_no_update(new_pos);
241
set_selected_index(new_idx);
242
grabbing = GRAB_ADD;
243
initial_grab_pos = new_pos;
244
}
245
}
246
}
247
248
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
249
if (selected_tangent_index != TANGENT_NONE) {
250
// Finish moving a tangent control.
251
if (selected_index == 0) {
252
set_point_right_tangent(selected_index, curve->get_point_right_tangent(selected_index));
253
} else if (selected_index == curve->get_point_count() - 1) {
254
set_point_left_tangent(selected_index, curve->get_point_left_tangent(selected_index));
255
} else {
256
set_point_tangents(selected_index, curve->get_point_left_tangent(selected_index), curve->get_point_right_tangent(selected_index));
257
}
258
grabbing = GRAB_NONE;
259
} else if (grabbing == GRAB_MOVE) {
260
// Finish moving a point.
261
set_point_position(selected_index, curve->get_point_position(selected_index));
262
grabbing = GRAB_NONE;
263
} else if (grabbing == GRAB_ADD) {
264
// Finish inserting a new point. Remove the temporary point and insert a permanent one in its place.
265
Vector2 new_pos = curve->get_point_position(selected_index);
266
curve->remove_point(selected_index);
267
add_point(new_pos);
268
grabbing = GRAB_NONE;
269
}
270
queue_redraw();
271
}
272
273
Ref<InputEventMouseMotion> mm = p_event;
274
if (mm.is_valid()) {
275
Vector2 mpos = mm->get_position();
276
277
if (grabbing != GRAB_NONE && curve.is_valid()) {
278
if (selected_index != -1) {
279
if (selected_tangent_index == TANGENT_NONE) {
280
// Drag point.
281
Vector2 new_pos = get_world_pos(mpos).clamp(Vector2(curve->get_min_domain(), curve->get_min_value()), Vector2(curve->get_max_domain(), curve->get_max_value()));
282
283
if (snap_enabled || mm->is_command_or_control_pressed()) {
284
new_pos.x = Math::snapped(new_pos.x - curve->get_min_domain(), curve->get_domain_range() / snap_count) + curve->get_min_domain();
285
new_pos.y = Math::snapped(new_pos.y - curve->get_min_value(), curve->get_value_range() / snap_count) + curve->get_min_value();
286
}
287
288
// Allow to snap to axes with Shift.
289
if (mm->is_shift_pressed()) {
290
Vector2 initial_mpos = get_view_pos(initial_grab_pos);
291
if (Math::abs(mpos.x - initial_mpos.x) > Math::abs(mpos.y - initial_mpos.y)) {
292
new_pos.y = initial_grab_pos.y;
293
} else {
294
new_pos.x = initial_grab_pos.x;
295
}
296
}
297
298
// Allow to constraint the point between the adjacent two with Alt.
299
if (mm->is_alt_pressed()) {
300
float prev_point_offset = (selected_index > 0) ? (curve->get_point_position(selected_index - 1).x + 0.00001) : curve->get_min_domain();
301
float next_point_offset = (selected_index < curve->get_point_count() - 1) ? (curve->get_point_position(selected_index + 1).x - 0.00001) : curve->get_max_domain();
302
new_pos.x = CLAMP(new_pos.x, prev_point_offset, next_point_offset);
303
}
304
305
new_pos.x = get_offset_without_collision(selected_index, new_pos.x, mpos.x >= get_view_pos(new_pos).x);
306
307
// The index may change if the point is dragged across another one.
308
int i = curve->set_point_offset(selected_index, new_pos.x);
309
hovered_index = i;
310
set_selected_index(i);
311
312
new_pos.y = CLAMP(new_pos.y, curve->get_min_value(), curve->get_max_value());
313
curve->set_point_value(selected_index, new_pos.y);
314
315
} else {
316
// Drag tangent.
317
318
const Vector2 new_pos = curve->get_point_position(selected_index);
319
const Vector2 control_pos = get_world_pos(mpos);
320
321
Vector2 dir = (control_pos - new_pos).normalized();
322
real_t tangent = dir.y / (dir.x > 0 ? MAX(dir.x, 0.00001) : MIN(dir.x, -0.00001));
323
324
// Must keep track of the hovered index as the cursor might move outside of the editor while dragging.
325
hovered_tangent_index = selected_tangent_index;
326
327
// Adjust the tangents.
328
if (selected_tangent_index == TANGENT_LEFT) {
329
curve->set_point_left_tangent(selected_index, tangent);
330
331
// Align the other tangent if it isn't linear and Shift is not pressed.
332
// If Shift is pressed at any point, restore the initial angle of the other tangent.
333
if (selected_index != (curve->get_point_count() - 1) && curve->get_point_right_mode(selected_index) != Curve::TANGENT_LINEAR) {
334
curve->set_point_right_tangent(selected_index, mm->is_shift_pressed() ? initial_grab_right_tangent : tangent);
335
}
336
337
} else {
338
curve->set_point_right_tangent(selected_index, tangent);
339
340
if (selected_index != 0 && curve->get_point_left_mode(selected_index) != Curve::TANGENT_LINEAR) {
341
curve->set_point_left_tangent(selected_index, mm->is_shift_pressed() ? initial_grab_left_tangent : tangent);
342
}
343
}
344
}
345
}
346
} else {
347
// Grab mode is GRAB_NONE, so do hovering logic.
348
hovered_index = get_point_at(mpos);
349
hovered_tangent_index = get_tangent_at(mpos);
350
queue_redraw();
351
}
352
}
353
}
354
355
void CurveEdit::use_preset(int p_preset_id) {
356
ERR_FAIL_COND(p_preset_id < 0 || p_preset_id >= PRESET_COUNT);
357
ERR_FAIL_COND(curve.is_null());
358
359
Array previous_data = curve->get_data();
360
curve->clear_points();
361
362
const float min_y = curve->get_min_value();
363
const float max_y = curve->get_max_value();
364
const float min_x = curve->get_min_domain();
365
const float max_x = curve->get_max_domain();
366
367
switch (p_preset_id) {
368
case PRESET_CONSTANT:
369
curve->add_point(Vector2(min_x, (min_y + max_y) / 2.0));
370
curve->add_point(Vector2(max_x, (min_y + max_y) / 2.0));
371
curve->set_point_right_mode(0, Curve::TANGENT_LINEAR);
372
curve->set_point_left_mode(1, Curve::TANGENT_LINEAR);
373
break;
374
375
case PRESET_LINEAR:
376
curve->add_point(Vector2(min_x, min_y));
377
curve->add_point(Vector2(max_x, max_y));
378
curve->set_point_right_mode(0, Curve::TANGENT_LINEAR);
379
curve->set_point_left_mode(1, Curve::TANGENT_LINEAR);
380
break;
381
382
case PRESET_EASE_IN:
383
curve->add_point(Vector2(min_x, min_y));
384
curve->add_point(Vector2(max_x, max_y), curve->get_value_range() / curve->get_domain_range() * 1.4, 0);
385
break;
386
387
case PRESET_EASE_OUT:
388
curve->add_point(Vector2(min_x, min_y), 0, curve->get_value_range() / curve->get_domain_range() * 1.4);
389
curve->add_point(Vector2(max_x, max_y));
390
break;
391
392
case PRESET_SMOOTHSTEP:
393
curve->add_point(Vector2(min_x, min_y));
394
curve->add_point(Vector2(max_x, max_y));
395
break;
396
397
default:
398
break;
399
}
400
401
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
402
undo_redo->create_action(TTR("Load Curve Preset"));
403
undo_redo->add_do_method(*curve, "_set_data", curve->get_data());
404
undo_redo->add_do_method(this, "set_selected_index", -1);
405
undo_redo->add_undo_method(*curve, "_set_data", previous_data);
406
undo_redo->add_undo_method(this, "set_selected_index", selected_index);
407
undo_redo->commit_action();
408
}
409
410
void CurveEdit::_curve_changed() {
411
queue_redraw();
412
// Point count can change in case of undo.
413
if (selected_index >= curve->get_point_count()) {
414
set_selected_index(-1);
415
}
416
}
417
418
int CurveEdit::get_point_at(const Vector2 &p_pos) const {
419
if (curve.is_null()) {
420
return -1;
421
}
422
423
// Use a square-shaped hover region. If hovering multiple points, pick the closer one.
424
const Rect2 hover_rect = Rect2(p_pos, Vector2(0, 0)).grow(hover_radius);
425
int closest_idx = -1;
426
float closest_dist_squared = hover_radius * hover_radius * 2;
427
428
for (int i = 0; i < curve->get_point_count(); ++i) {
429
Vector2 p = get_view_pos(curve->get_point_position(i));
430
if (hover_rect.has_point(p) && p.distance_squared_to(p_pos) < closest_dist_squared) {
431
closest_dist_squared = p.distance_squared_to(p_pos);
432
closest_idx = i;
433
}
434
}
435
436
return closest_idx;
437
}
438
439
CurveEdit::TangentIndex CurveEdit::get_tangent_at(const Vector2 &p_pos) const {
440
if (curve.is_null() || selected_index < 0) {
441
return TANGENT_NONE;
442
}
443
444
const Rect2 hover_rect = Rect2(p_pos, Vector2(0, 0)).grow(tangent_hover_radius);
445
446
if (selected_index != 0) {
447
Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_LEFT);
448
if (hover_rect.has_point(control_pos)) {
449
return TANGENT_LEFT;
450
}
451
}
452
453
if (selected_index != curve->get_point_count() - 1) {
454
Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_RIGHT);
455
if (hover_rect.has_point(control_pos)) {
456
return TANGENT_RIGHT;
457
}
458
}
459
460
return TANGENT_NONE;
461
}
462
463
// FIXME: This function should be bounded better.
464
float CurveEdit::get_offset_without_collision(int p_current_index, float p_offset, bool p_prioritize_right) {
465
float safe_offset = p_offset;
466
bool prioritizing_right = p_prioritize_right;
467
468
for (int i = 0; i < curve->get_point_count(); i++) {
469
if (i == p_current_index) {
470
continue;
471
}
472
473
if (curve->get_point_position(i).x > safe_offset) {
474
break;
475
}
476
477
if (curve->get_point_position(i).x == safe_offset) {
478
if (prioritizing_right) {
479
safe_offset += 0.00001;
480
if (safe_offset > 1.0) {
481
safe_offset = 1.0;
482
prioritizing_right = false;
483
}
484
} else {
485
safe_offset -= 0.00001;
486
if (safe_offset < 0.0) {
487
safe_offset = 0.0;
488
prioritizing_right = true;
489
}
490
}
491
i = -1;
492
}
493
}
494
495
return safe_offset;
496
}
497
498
void CurveEdit::add_point(const Vector2 &p_pos) {
499
ERR_FAIL_COND(curve.is_null());
500
501
// Add a point to get its index, then remove it immediately. Trick to feed the UndoRedo.
502
int new_idx = curve->add_point(p_pos);
503
curve->remove_point(new_idx);
504
505
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
506
undo_redo->create_action(TTR("Add Curve Point"));
507
undo_redo->add_do_method(*curve, "add_point", p_pos);
508
undo_redo->add_do_method(this, "set_selected_index", new_idx);
509
undo_redo->add_undo_method(*curve, "remove_point", new_idx);
510
undo_redo->add_undo_method(this, "set_selected_index", -1);
511
undo_redo->commit_action();
512
}
513
514
void CurveEdit::remove_point(int p_index) {
515
ERR_FAIL_COND(curve.is_null());
516
ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
517
518
Curve::Point p = curve->get_point(p_index);
519
Vector2 old_pos = (grabbing == GRAB_MOVE) ? initial_grab_pos : p.position;
520
521
int new_selected_index = selected_index;
522
// Reselect the old selected point if it's not the deleted one.
523
if (new_selected_index > p_index) {
524
new_selected_index -= 1;
525
} else if (new_selected_index == p_index) {
526
new_selected_index = -1;
527
}
528
529
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
530
undo_redo->create_action(TTR("Remove Curve Point"));
531
undo_redo->add_do_method(*curve, "remove_point", p_index);
532
undo_redo->add_do_method(this, "set_selected_index", new_selected_index);
533
undo_redo->add_undo_method(*curve, "add_point", old_pos, p.left_tangent, p.right_tangent, p.left_mode, p.right_mode);
534
undo_redo->add_undo_method(this, "set_selected_index", selected_index);
535
undo_redo->commit_action();
536
}
537
538
void CurveEdit::set_point_position(int p_index, const Vector2 &p_pos) {
539
ERR_FAIL_COND(curve.is_null());
540
ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
541
542
if (initial_grab_pos == p_pos) {
543
return;
544
}
545
546
// Pretend the point started from its old place.
547
curve->set_point_value(p_index, initial_grab_pos.y);
548
curve->set_point_offset(p_index, initial_grab_pos.x);
549
// Note: Changing the offset may modify the order.
550
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
551
undo_redo->create_action(TTR("Modify Curve Point"));
552
undo_redo->add_do_method(*curve, "set_point_value", initial_grab_index, p_pos.y);
553
undo_redo->add_do_method(*curve, "set_point_offset", initial_grab_index, p_pos.x);
554
undo_redo->add_do_method(this, "set_selected_index", p_index);
555
undo_redo->add_undo_method(*curve, "set_point_value", p_index, initial_grab_pos.y);
556
undo_redo->add_undo_method(*curve, "set_point_offset", p_index, initial_grab_pos.x);
557
undo_redo->add_undo_method(this, "set_selected_index", initial_grab_index);
558
undo_redo->commit_action();
559
}
560
561
void CurveEdit::set_point_tangents(int p_index, float p_left, float p_right) {
562
ERR_FAIL_COND(curve.is_null());
563
ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
564
565
if (initial_grab_left_tangent == p_left) {
566
set_point_right_tangent(p_index, p_right);
567
return;
568
} else if (initial_grab_right_tangent == p_right) {
569
set_point_left_tangent(p_index, p_left);
570
return;
571
}
572
573
curve->set_point_left_tangent(p_index, initial_grab_left_tangent);
574
curve->set_point_right_tangent(p_index, initial_grab_right_tangent);
575
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
576
undo_redo->create_action(TTR("Modify Curve Point's Tangents"));
577
undo_redo->add_do_method(*curve, "set_point_left_tangent", p_index, p_left);
578
undo_redo->add_do_method(*curve, "set_point_right_tangent", p_index, p_right);
579
undo_redo->add_do_method(this, "set_selected_index", p_index);
580
undo_redo->add_undo_method(*curve, "set_point_left_tangent", p_index, initial_grab_left_tangent);
581
undo_redo->add_undo_method(*curve, "set_point_right_tangent", p_index, initial_grab_right_tangent);
582
undo_redo->add_undo_method(this, "set_selected_index", p_index);
583
undo_redo->commit_action();
584
}
585
586
void CurveEdit::set_point_left_tangent(int p_index, float p_tangent) {
587
ERR_FAIL_COND(curve.is_null());
588
ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
589
590
if (initial_grab_left_tangent == p_tangent) {
591
return;
592
}
593
594
curve->set_point_left_tangent(p_index, initial_grab_left_tangent);
595
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
596
undo_redo->create_action(TTR("Modify Curve Point's Left Tangent"));
597
undo_redo->add_do_method(*curve, "set_point_left_tangent", p_index, p_tangent);
598
undo_redo->add_do_method(this, "set_selected_index", p_index);
599
undo_redo->add_undo_method(*curve, "set_point_left_tangent", p_index, initial_grab_left_tangent);
600
undo_redo->add_undo_method(this, "set_selected_index", p_index);
601
undo_redo->commit_action();
602
}
603
604
void CurveEdit::set_point_right_tangent(int p_index, float p_tangent) {
605
ERR_FAIL_COND(curve.is_null());
606
ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
607
608
if (initial_grab_right_tangent == p_tangent) {
609
return;
610
}
611
612
curve->set_point_right_tangent(p_index, initial_grab_right_tangent);
613
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
614
undo_redo->create_action(TTR("Modify Curve Point's Right Tangent"));
615
undo_redo->add_do_method(*curve, "set_point_right_tangent", p_index, p_tangent);
616
undo_redo->add_do_method(this, "set_selected_index", p_index);
617
undo_redo->add_undo_method(*curve, "set_point_right_tangent", p_index, initial_grab_right_tangent);
618
undo_redo->add_undo_method(this, "set_selected_index", p_index);
619
undo_redo->commit_action();
620
}
621
622
void CurveEdit::toggle_linear(int p_index, TangentIndex p_tangent) {
623
ERR_FAIL_COND(curve.is_null());
624
ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
625
626
if (p_tangent == TANGENT_NONE) {
627
return;
628
}
629
630
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
631
undo_redo->create_action(TTR("Toggle Linear Curve Point's Tangent"));
632
633
Curve::TangentMode prev_mode = (p_tangent == TANGENT_LEFT) ? curve->get_point_left_mode(p_index) : curve->get_point_right_mode(p_index);
634
Curve::TangentMode mode = (prev_mode == Curve::TANGENT_LINEAR) ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR;
635
float prev_angle = (p_tangent == TANGENT_LEFT) ? curve->get_point_left_tangent(p_index) : curve->get_point_right_tangent(p_index);
636
637
// Add different methods in the UndoRedo based on the tangent passed.
638
if (p_tangent == TANGENT_LEFT) {
639
undo_redo->add_do_method(*curve, "set_point_left_mode", p_index, mode);
640
undo_redo->add_undo_method(*curve, "set_point_left_mode", p_index, prev_mode);
641
undo_redo->add_undo_method(*curve, "set_point_left_tangent", p_index, prev_angle);
642
} else {
643
undo_redo->add_do_method(*curve, "set_point_right_mode", p_index, mode);
644
undo_redo->add_undo_method(*curve, "set_point_right_mode", p_index, prev_mode);
645
undo_redo->add_undo_method(*curve, "set_point_right_tangent", p_index, prev_angle);
646
}
647
648
undo_redo->commit_action();
649
}
650
651
void CurveEdit::set_selected_index(int p_index) {
652
if (p_index != selected_index) {
653
selected_index = p_index;
654
queue_redraw();
655
}
656
}
657
658
void CurveEdit::update_view_transform() {
659
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
660
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
661
662
const real_t margin = font->get_height(font_size) + 2 * EDSCALE;
663
664
float min_x = curve.is_valid() ? curve->get_min_domain() : 0.0;
665
float max_x = curve.is_valid() ? curve->get_max_domain() : 1.0;
666
float min_y = curve.is_valid() ? curve->get_min_value() : 0.0;
667
float max_y = curve.is_valid() ? curve->get_max_value() : 1.0;
668
669
const Rect2 world_rect = Rect2(min_x, min_y, max_x - min_x, max_y - min_y);
670
const Size2 view_margin(margin, margin);
671
const Size2 view_size = get_size() - view_margin * 2;
672
const Vector2 scale = view_size / world_rect.size;
673
674
Transform2D world_trans;
675
world_trans.translate_local(-world_rect.position - Vector2(0, world_rect.size.y));
676
world_trans.scale(Vector2(scale.x, -scale.y));
677
678
Transform2D view_trans;
679
view_trans.translate_local(view_margin);
680
681
_world_to_view = view_trans * world_trans;
682
}
683
684
Vector2 CurveEdit::get_tangent_view_pos(int p_index, TangentIndex p_tangent) const {
685
Vector2 dir;
686
if (p_tangent == TANGENT_LEFT) {
687
dir = -Vector2(1, curve->get_point_left_tangent(p_index));
688
} else {
689
dir = Vector2(1, curve->get_point_right_tangent(p_index));
690
}
691
692
Vector2 point_pos = curve->get_point_position(p_index);
693
Vector2 point_view_pos = get_view_pos(point_pos);
694
Vector2 control_view_pos = get_view_pos(point_pos + dir);
695
696
Vector2 distance_from_point = tangent_length * (control_view_pos - point_view_pos).normalized();
697
Vector2 tangent_view_pos = point_view_pos + distance_from_point;
698
699
// Since the tangent is long, it might slip outside of the area of the editor for points close to the domain/range boundaries.
700
// The code below shrinks the tangent control by up to 50% so it always stays inside the editor for points within the bounds.
701
float fraction_inside = 1.0;
702
if (distance_from_point.x != 0.0) {
703
fraction_inside = MIN(fraction_inside, ((distance_from_point.x > 0 ? get_rect().size.x : 0) - point_view_pos.x) / distance_from_point.x);
704
}
705
if (distance_from_point.y != 0.0) {
706
fraction_inside = MIN(fraction_inside, ((distance_from_point.y > 0 ? get_rect().size.y : 0) - point_view_pos.y) / distance_from_point.y);
707
}
708
709
if (fraction_inside < 1.0 && fraction_inside > 0.5) {
710
tangent_view_pos = point_view_pos + distance_from_point * fraction_inside;
711
}
712
713
return tangent_view_pos;
714
}
715
716
Vector2 CurveEdit::get_view_pos(const Vector2 &p_world_pos) const {
717
return _world_to_view.xform(p_world_pos);
718
}
719
720
Vector2 CurveEdit::get_world_pos(const Vector2 &p_view_pos) const {
721
return _world_to_view.affine_inverse().xform(p_view_pos);
722
}
723
724
// Uses non-baked points, but takes advantage of ordered iteration to be faster.
725
void CurveEdit::plot_curve_accurate(float p_step, const Color &p_line_color, const Color &p_edge_line_color) {
726
const real_t min_x = curve->get_min_domain();
727
const real_t max_x = curve->get_max_domain();
728
if (curve->get_point_count() <= 1) { // Draw single line through entire plot.
729
real_t y = curve->sample(0);
730
draw_line(get_view_pos(Vector2(min_x, y)) + Vector2(0.5, 0), get_view_pos(Vector2(max_x, y)) - Vector2(1.5, 0), p_line_color, LINE_WIDTH, true);
731
return;
732
}
733
734
Vector2 first_point = curve->get_point_position(0);
735
Vector2 last_point = curve->get_point_position(curve->get_point_count() - 1);
736
737
// Transform pixels-per-step into curve domain. Only works for non-rotated transforms.
738
const float world_step_size = p_step / _world_to_view.get_scale().x;
739
740
// Edge lines.
741
draw_line(get_view_pos(Vector2(min_x, first_point.y)) + Vector2(0.5, 0), get_view_pos(first_point), p_edge_line_color, LINE_WIDTH, true);
742
draw_line(get_view_pos(last_point), get_view_pos(Vector2(max_x, last_point.y)) - Vector2(1.5, 0), p_edge_line_color, LINE_WIDTH, true);
743
744
// Draw section by section, so that we get maximum precision near points.
745
// It's an accurate representation, but slower than using the baked one.
746
for (int i = 1; i < curve->get_point_count(); ++i) {
747
Vector2 a = curve->get_point_position(i - 1);
748
Vector2 b = curve->get_point_position(i);
749
750
Vector2 pos = a;
751
Vector2 prev_pos = a;
752
753
float samples = (b.x - a.x) / world_step_size;
754
755
for (int j = 1; j < samples; j++) {
756
float x = j * world_step_size;
757
pos.x = a.x + x;
758
pos.y = curve->sample_local_nocheck(i - 1, x);
759
draw_line(get_view_pos(prev_pos), get_view_pos(pos), p_line_color, LINE_WIDTH, true);
760
prev_pos = pos;
761
}
762
763
draw_line(get_view_pos(prev_pos), get_view_pos(b), p_line_color, LINE_WIDTH, true);
764
}
765
}
766
767
void CurveEdit::_redraw() {
768
if (curve.is_null()) {
769
return;
770
}
771
772
update_view_transform();
773
774
// Draw background.
775
776
Vector2 view_size = get_rect().size;
777
draw_style_box(get_theme_stylebox(SceneStringName(panel), SNAME("Tree")), Rect2(Point2(), view_size));
778
779
// Draw primary grid.
780
draw_set_transform_matrix(_world_to_view);
781
782
Vector2 min_edge = get_world_pos(Vector2(0, view_size.y));
783
Vector2 max_edge = get_world_pos(Vector2(view_size.x, 0));
784
785
const Color grid_color_primary = get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.25);
786
const Color grid_color = get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.1);
787
788
const Vector2i grid_steps = Vector2i(4, 2);
789
const Vector2 step_size = Vector2(curve->get_domain_range(), curve->get_value_range()) / grid_steps;
790
791
draw_line(Vector2(min_edge.x, curve->get_min_value()), Vector2(max_edge.x, curve->get_min_value()), grid_color_primary);
792
draw_line(Vector2(max_edge.x, curve->get_max_value()), Vector2(min_edge.x, curve->get_max_value()), grid_color_primary);
793
draw_line(Vector2(curve->get_min_domain(), min_edge.y), Vector2(curve->get_min_domain(), max_edge.y), grid_color_primary);
794
draw_line(Vector2(curve->get_max_domain(), max_edge.y), Vector2(curve->get_max_domain(), min_edge.y), grid_color_primary);
795
796
for (int i = 1; i < grid_steps.x; i++) {
797
real_t x = curve->get_min_domain() + i * step_size.x;
798
draw_line(Vector2(x, min_edge.y), Vector2(x, max_edge.y), grid_color);
799
}
800
801
for (int i = 1; i < grid_steps.y; i++) {
802
real_t y = curve->get_min_value() + i * step_size.y;
803
draw_line(Vector2(min_edge.x, y), Vector2(max_edge.x, y), grid_color);
804
}
805
806
// Draw number markings.
807
draw_set_transform_matrix(Transform2D());
808
809
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
810
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
811
float font_height = font->get_height(font_size);
812
Color text_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
813
814
int pad = Math::round(2 * EDSCALE);
815
816
for (int i = 0; i <= grid_steps.x; ++i) {
817
real_t x = curve->get_min_domain() + i * step_size.x;
818
draw_string(font, get_view_pos(Vector2(x, curve->get_min_value())) + Vector2(pad, font_height - pad), String::num(x, 2), HORIZONTAL_ALIGNMENT_CENTER, -1, font_size, text_color);
819
}
820
821
for (int i = 0; i <= grid_steps.y; ++i) {
822
real_t y = curve->get_min_value() + i * step_size.y;
823
draw_string(font, get_view_pos(Vector2(curve->get_min_domain(), y)) + Vector2(pad, -pad), String::num(y, 2), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color);
824
}
825
826
// Draw curve in view coordinates. Curve world-to-view point conversion happens in plot_curve_accurate().
827
828
const Color line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
829
const Color edge_line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)) * Color(1, 1, 1, 0.75);
830
831
plot_curve_accurate(STEP_SIZE, line_color, edge_line_color);
832
833
// Draw points, except for the selected one.
834
835
bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
836
837
const Color point_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
838
839
for (int i = 0; i < curve->get_point_count(); ++i) {
840
Vector2 pos = get_view_pos(curve->get_point_position(i));
841
if (selected_index != i) {
842
draw_rect(Rect2(pos, Vector2(0, 0)).grow(point_radius), point_color);
843
}
844
if (hovered_index == i && hovered_tangent_index == TANGENT_NONE) {
845
draw_rect(Rect2(pos, Vector2(0, 0)).grow(hover_radius - Math::round(3 * EDSCALE)), line_color, false, Math::round(1 * EDSCALE));
846
}
847
}
848
849
// Draw selected point and its tangents.
850
851
if (selected_index >= 0) {
852
const Vector2 point_pos = curve->get_point_position(selected_index);
853
const Color selected_point_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
854
855
// Draw tangents if not dragging a point, or if holding a point without having moved it yet.
856
if (grabbing == GRAB_NONE || initial_grab_pos == point_pos || selected_tangent_index != TANGENT_NONE) {
857
const Color selected_tangent_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)).darkened(0.25);
858
const Color tangent_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)).darkened(0.25);
859
860
if (selected_index != 0) {
861
Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_LEFT);
862
Color left_tangent_color = (selected_tangent_index == TANGENT_LEFT) ? selected_tangent_color : tangent_color;
863
864
draw_line(get_view_pos(point_pos), control_pos, left_tangent_color, 0.5 * EDSCALE, true);
865
// Square for linear mode, circle otherwise.
866
if (curve->get_point_left_mode(selected_index) == Curve::TANGENT_FREE) {
867
draw_circle(control_pos, tangent_radius, left_tangent_color);
868
} else {
869
draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_radius), left_tangent_color);
870
}
871
// Hover indicator.
872
if (hovered_tangent_index == TANGENT_LEFT || (hovered_tangent_index == TANGENT_RIGHT && !shift_pressed && curve->get_point_left_mode(selected_index) != Curve::TANGENT_LINEAR)) {
873
draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_hover_radius - Math::round(3 * EDSCALE)), tangent_color, false, Math::round(1 * EDSCALE));
874
}
875
}
876
877
if (selected_index != curve->get_point_count() - 1) {
878
Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_RIGHT);
879
Color right_tangent_color = (selected_tangent_index == TANGENT_RIGHT) ? selected_tangent_color : tangent_color;
880
881
draw_line(get_view_pos(point_pos), control_pos, right_tangent_color, 0.5 * EDSCALE, true);
882
// Square for linear mode, circle otherwise.
883
if (curve->get_point_right_mode(selected_index) == Curve::TANGENT_FREE) {
884
draw_circle(control_pos, tangent_radius, right_tangent_color);
885
} else {
886
draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_radius), right_tangent_color);
887
}
888
// Hover indicator.
889
if (hovered_tangent_index == TANGENT_RIGHT || (hovered_tangent_index == TANGENT_LEFT && !shift_pressed && curve->get_point_right_mode(selected_index) != Curve::TANGENT_LINEAR)) {
890
draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_hover_radius - Math::round(3 * EDSCALE)), tangent_color, false, Math::round(1 * EDSCALE));
891
}
892
}
893
}
894
895
draw_rect(Rect2(get_view_pos(point_pos), Vector2(0, 0)).grow(point_radius), selected_point_color);
896
}
897
898
// Draw help text.
899
900
if (selected_index > 0 && selected_index < curve->get_point_count() - 1 && selected_tangent_index == TANGENT_NONE && hovered_tangent_index != TANGENT_NONE && !shift_pressed) {
901
float width = view_size.x - 50 * EDSCALE;
902
text_color.a *= 0.4;
903
904
draw_multiline_string(font, Vector2(25 * EDSCALE, font_height - Math::round(2 * EDSCALE)), TTR("Hold Shift to edit tangents individually"), HORIZONTAL_ALIGNMENT_CENTER, width, font_size, -1, text_color);
905
906
} else if (selected_index != -1 && selected_tangent_index == TANGENT_NONE) {
907
const Vector2 point_pos = curve->get_point_position(selected_index);
908
float width = view_size.x - 50 * EDSCALE;
909
text_color.a *= 0.8;
910
911
draw_string(font, Vector2(25 * EDSCALE, font_height - Math::round(2 * EDSCALE)), vformat("(%.2f, %.2f)", point_pos.x, point_pos.y), HORIZONTAL_ALIGNMENT_CENTER, width, font_size, text_color);
912
913
} else if (selected_index != -1 && selected_tangent_index != TANGENT_NONE) {
914
float width = view_size.x - 50 * EDSCALE;
915
text_color.a *= 0.8;
916
real_t theta = Math::rad_to_deg(Math::atan(selected_tangent_index == TANGENT_LEFT ? -1 * curve->get_point_left_tangent(selected_index) : curve->get_point_right_tangent(selected_index)));
917
918
draw_string(font, Vector2(25 * EDSCALE, font_height - Math::round(2 * EDSCALE)), String::num(theta, 1) + String::utf8(" °"), HORIZONTAL_ALIGNMENT_CENTER, width, font_size, text_color);
919
}
920
921
// Draw temporary constraints and snapping axes.
922
draw_set_transform_matrix(_world_to_view);
923
924
if (Input::get_singleton()->is_key_pressed(Key::ALT) && grabbing != GRAB_NONE && selected_tangent_index == TANGENT_NONE) {
925
float prev_point_offset = (selected_index > 0) ? curve->get_point_position(selected_index - 1).x : curve->get_min_domain();
926
float next_point_offset = (selected_index < curve->get_point_count() - 1) ? curve->get_point_position(selected_index + 1).x : curve->get_max_domain();
927
928
draw_line(Vector2(prev_point_offset, curve->get_min_value()), Vector2(prev_point_offset, curve->get_max_value()), Color(point_color, 0.6));
929
draw_line(Vector2(next_point_offset, curve->get_min_value()), Vector2(next_point_offset, curve->get_max_value()), Color(point_color, 0.6));
930
}
931
932
if (shift_pressed && grabbing != GRAB_NONE && selected_tangent_index == TANGENT_NONE) {
933
draw_line(Vector2(initial_grab_pos.x, curve->get_min_value()), Vector2(initial_grab_pos.x, curve->get_max_value()), get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)).darkened(0.4));
934
draw_line(Vector2(curve->get_min_domain(), initial_grab_pos.y), Vector2(curve->get_max_domain(), initial_grab_pos.y), get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)).darkened(0.4));
935
}
936
}
937
938
///////////////////////
939
940
const int CurveEditor::DEFAULT_SNAP = 10;
941
942
void CurveEditor::_set_snap_enabled(bool p_enabled) {
943
curve_editor_rect->set_snap_enabled(p_enabled);
944
snap_count_edit->set_visible(p_enabled);
945
}
946
947
void CurveEditor::_set_snap_count(int p_snap_count) {
948
curve_editor_rect->set_snap_count(CLAMP(p_snap_count, 2, 100));
949
}
950
951
void CurveEditor::_on_preset_item_selected(int p_preset_id) {
952
curve_editor_rect->use_preset(p_preset_id);
953
}
954
955
void CurveEditor::set_curve(const Ref<Curve> &p_curve) {
956
curve_editor_rect->set_curve(p_curve);
957
}
958
959
void CurveEditor::_notification(int p_what) {
960
switch (p_what) {
961
case NOTIFICATION_THEME_CHANGED: {
962
spacing = Math::round(BASE_SPACING * get_theme_default_base_scale());
963
snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
964
PopupMenu *p = presets_button->get_popup();
965
p->clear();
966
p->add_icon_item(get_editor_theme_icon(SNAME("CurveConstant")), TTR("Constant"), CurveEdit::PRESET_CONSTANT);
967
p->add_icon_item(get_editor_theme_icon(SNAME("CurveLinear")), TTR("Linear"), CurveEdit::PRESET_LINEAR);
968
p->add_icon_item(get_editor_theme_icon(SNAME("CurveIn")), TTR("Ease In"), CurveEdit::PRESET_EASE_IN);
969
p->add_icon_item(get_editor_theme_icon(SNAME("CurveOut")), TTR("Ease Out"), CurveEdit::PRESET_EASE_OUT);
970
p->add_icon_item(get_editor_theme_icon(SNAME("CurveInOut")), TTR("Smoothstep"), CurveEdit::PRESET_SMOOTHSTEP);
971
} break;
972
case NOTIFICATION_READY: {
973
Ref<Curve> curve = curve_editor_rect->get_curve();
974
if (curve.is_valid()) {
975
// Set snapping settings based on the curve's meta.
976
snap_button->set_pressed(curve->get_meta("_snap_enabled", false));
977
snap_count_edit->set_value(curve->get_meta("_snap_count", DEFAULT_SNAP));
978
}
979
} break;
980
case NOTIFICATION_RESIZED:
981
curve_editor_rect->update_minimum_size();
982
break;
983
}
984
}
985
986
CurveEditor::CurveEditor() {
987
HFlowContainer *toolbar = memnew(HFlowContainer);
988
add_child(toolbar);
989
990
snap_button = memnew(Button);
991
snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
992
snap_button->set_toggle_mode(true);
993
toolbar->add_child(snap_button);
994
snap_button->connect(SceneStringName(toggled), callable_mp(this, &CurveEditor::_set_snap_enabled));
995
996
toolbar->add_child(memnew(VSeparator));
997
998
snap_count_edit = memnew(EditorSpinSlider);
999
snap_count_edit->set_min(2);
1000
snap_count_edit->set_max(100);
1001
snap_count_edit->set_accessibility_name(TTRC("Snap Step"));
1002
snap_count_edit->set_value(DEFAULT_SNAP);
1003
snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
1004
toolbar->add_child(snap_count_edit);
1005
snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &CurveEditor::_set_snap_count));
1006
1007
presets_button = memnew(MenuButton);
1008
presets_button->set_text(TTR("Presets"));
1009
presets_button->set_switch_on_hover(true);
1010
presets_button->set_h_size_flags(SIZE_EXPAND | SIZE_SHRINK_END);
1011
toolbar->add_child(presets_button);
1012
presets_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &CurveEditor::_on_preset_item_selected));
1013
1014
curve_editor_rect = memnew(CurveEdit);
1015
add_child(curve_editor_rect);
1016
1017
// Some empty space below. Not a part of the curve editor so it can't draw in it.
1018
Control *empty_space = memnew(Control);
1019
empty_space->set_custom_minimum_size(Vector2(0, spacing));
1020
add_child(empty_space);
1021
1022
set_mouse_filter(MOUSE_FILTER_STOP);
1023
_set_snap_enabled(snap_button->is_pressed());
1024
_set_snap_count(snap_count_edit->get_value());
1025
}
1026
1027
///////////////////////
1028
1029
bool EditorInspectorPluginCurve::can_handle(Object *p_object) {
1030
return Object::cast_to<Curve>(p_object) != nullptr;
1031
}
1032
1033
void EditorInspectorPluginCurve::parse_begin(Object *p_object) {
1034
Curve *curve = Object::cast_to<Curve>(p_object);
1035
ERR_FAIL_NULL(curve);
1036
Ref<Curve> c(curve);
1037
1038
CurveEditor *editor = memnew(CurveEditor);
1039
editor->set_curve(c);
1040
add_custom_control(editor);
1041
}
1042
1043
CurveEditorPlugin::CurveEditorPlugin() {
1044
Ref<EditorInspectorPluginCurve> plugin;
1045
plugin.instantiate();
1046
add_inspector_plugin(plugin);
1047
1048
EditorInterface::get_singleton()->get_resource_previewer()->add_preview_generator(memnew(CurvePreviewGenerator));
1049
}
1050
1051
///////////////////////
1052
1053
bool CurvePreviewGenerator::handles(const String &p_type) const {
1054
return p_type == "Curve";
1055
}
1056
1057
Ref<Texture2D> CurvePreviewGenerator::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
1058
Ref<Curve> curve = p_from;
1059
if (curve.is_null()) {
1060
return Ref<Texture2D>();
1061
}
1062
1063
Ref<Image> img_ref;
1064
img_ref.instantiate();
1065
Image &im = **img_ref;
1066
im.initialize_data(p_size.x, p_size.y, false, Image::FORMAT_RGBA8);
1067
1068
Color line_color = EditorInterface::get_singleton()->get_editor_theme()->get_color(SceneStringName(font_color), EditorStringName(Editor));
1069
1070
// Set the first pixel of the thumbnail.
1071
float v = (curve->sample_baked(curve->get_min_domain()) - curve->get_min_value()) / curve->get_value_range();
1072
int y = CLAMP(im.get_height() - v * im.get_height(), 0, im.get_height() - 1);
1073
im.set_pixel(0, y, line_color);
1074
1075
// Plot a line towards the next point.
1076
int prev_y = y;
1077
for (int x = 1; x < im.get_width(); ++x) {
1078
float t = static_cast<float>(x) / im.get_width() * curve->get_domain_range() + curve->get_min_domain();
1079
v = (curve->sample_baked(t) - curve->get_min_value()) / curve->get_value_range();
1080
y = CLAMP(im.get_height() - v * im.get_height(), 0, im.get_height() - 1);
1081
1082
Vector<Point2i> points = Geometry2D::bresenham_line(Point2i(x - 1, prev_y), Point2i(x, y));
1083
for (Point2i point : points) {
1084
im.set_pixelv(point, line_color);
1085
}
1086
prev_y = y;
1087
}
1088
1089
return ImageTexture::create_from_image(img_ref);
1090
}
1091
1092