Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/gradient_editor_plugin.cpp
20843 views
1
/**************************************************************************/
2
/* gradient_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 "gradient_editor_plugin.h"
32
33
#include "core/input/input.h"
34
#include "core/os/keyboard.h"
35
#include "editor/editor_node.h"
36
#include "editor/editor_string_names.h"
37
#include "editor/editor_undo_redo_manager.h"
38
#include "editor/gui/editor_spin_slider.h"
39
#include "editor/scene/3d/node_3d_editor_plugin.h"
40
#include "editor/scene/canvas_item_editor_plugin.h"
41
#include "editor/settings/editor_settings.h"
42
#include "editor/themes/editor_scale.h"
43
#include "scene/gui/color_picker.h"
44
#include "scene/gui/flow_container.h"
45
#include "scene/gui/popup.h"
46
#include "scene/gui/separator.h"
47
#include "scene/resources/gradient_texture.h"
48
49
int GradientEdit::_get_point_at(int p_xpos) const {
50
int result = -1;
51
int total_w = _get_gradient_rect_width();
52
float min_distance = handle_width * 0.8; // Allow the cursor to be more than half a handle width away for ease of use.
53
for (int i = 0; i < gradient->get_point_count(); i++) {
54
// Ignore points outside of [0, 1].
55
if (gradient->get_offset(i) < 0) {
56
continue;
57
} else if (gradient->get_offset(i) > 1) {
58
break;
59
}
60
// Check if we clicked at point.
61
float distance = Math::abs(p_xpos - gradient->get_offset(i) * total_w);
62
if (distance < min_distance) {
63
result = i;
64
min_distance = distance;
65
}
66
}
67
return result;
68
}
69
70
int GradientEdit::_predict_insertion_index(float p_offset) {
71
int result = 0;
72
while (result < gradient->get_point_count() && gradient->get_offset(result) < p_offset) {
73
result++;
74
}
75
return result;
76
}
77
78
int GradientEdit::_get_gradient_rect_width() const {
79
return get_size().width - get_size().height - draw_spacing - handle_width;
80
}
81
82
void GradientEdit::_show_color_picker() {
83
if (selected_index == -1) {
84
return;
85
}
86
87
picker->set_pick_color(gradient->get_color(selected_index));
88
Size2 minsize = popup->get_contents_minimum_size();
89
float viewport_height = get_viewport_rect().size.y;
90
91
// Determine in which direction to show the popup. By default popup below.
92
// But if the popup doesn't fit below and the Gradient Editor is in the bottom half of the viewport, show above.
93
bool show_above = get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height;
94
95
float v_offset = show_above ? -minsize.y : get_size().y;
96
popup->set_position(get_screen_position() + Vector2(0, v_offset));
97
popup->popup();
98
}
99
100
void GradientEdit::_color_changed(const Color &p_color) {
101
set_color(selected_index, p_color);
102
}
103
104
void GradientEdit::set_gradient(const Ref<Gradient> &p_gradient) {
105
gradient = p_gradient;
106
gradient->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
107
}
108
109
const Ref<Gradient> &GradientEdit::get_gradient() const {
110
return gradient;
111
}
112
113
void GradientEdit::add_point(float p_offset, const Color &p_color) {
114
int new_idx = _predict_insertion_index(p_offset);
115
116
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
117
undo_redo->create_action(TTR("Add Gradient Point"));
118
undo_redo->add_do_method(*gradient, "add_point", p_offset, p_color);
119
undo_redo->add_do_method(this, "set_selected_index", new_idx);
120
undo_redo->add_undo_method(*gradient, "remove_point", new_idx);
121
undo_redo->add_undo_method(this, "set_selected_index", -1);
122
undo_redo->commit_action();
123
}
124
125
void GradientEdit::remove_point(int p_index) {
126
ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");
127
128
if (gradient->get_point_count() <= 1) {
129
return;
130
}
131
132
// If the point is removed while it's being moved, remember its old offset.
133
float old_offset = (grabbing == GRAB_MOVE) ? pre_grab_offset : gradient->get_offset(p_index);
134
Color old_color = gradient->get_color(p_index);
135
136
int new_selected_index = selected_index;
137
// Reselect the old selected point if it's not the deleted one.
138
if (new_selected_index > p_index) {
139
new_selected_index -= 1;
140
} else if (new_selected_index == p_index) {
141
new_selected_index = -1;
142
}
143
144
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
145
undo_redo->create_action(TTR("Remove Gradient Point"));
146
undo_redo->add_do_method(*gradient, "remove_point", p_index);
147
undo_redo->add_do_method(this, "set_selected_index", new_selected_index);
148
undo_redo->add_undo_method(*gradient, "add_point", old_offset, old_color);
149
undo_redo->add_undo_method(this, "set_selected_index", selected_index);
150
undo_redo->commit_action();
151
}
152
153
void GradientEdit::set_offset(int p_index, float p_offset) {
154
ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");
155
156
// Use pre_grab_offset to determine things for the undo/redo.
157
if (Math::is_equal_approx(pre_grab_offset, p_offset)) {
158
return;
159
}
160
161
int new_idx = _predict_insertion_index(p_offset);
162
163
gradient->set_offset(p_index, pre_grab_offset); // Pretend the point started from its old place.
164
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
165
undo_redo->create_action(TTR("Move Gradient Point"));
166
undo_redo->add_do_method(*gradient, "set_offset", pre_grab_index, p_offset);
167
undo_redo->add_do_method(this, "set_selected_index", new_idx);
168
undo_redo->add_undo_method(*gradient, "set_offset", new_idx, pre_grab_offset);
169
undo_redo->add_undo_method(this, "set_selected_index", pre_grab_index);
170
undo_redo->commit_action();
171
queue_redraw();
172
}
173
174
void GradientEdit::set_color(int p_index, const Color &p_color) {
175
ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");
176
177
Color old_color = gradient->get_color(p_index);
178
if (old_color == p_color) {
179
return;
180
}
181
182
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
183
undo_redo->create_action(TTR("Recolor Gradient Point"), UndoRedo::MERGE_ENDS);
184
undo_redo->add_do_method(*gradient, "set_color", p_index, p_color);
185
undo_redo->add_undo_method(*gradient, "set_color", p_index, old_color);
186
undo_redo->commit_action();
187
queue_redraw();
188
}
189
190
void GradientEdit::reverse_gradient() {
191
int new_selected_idx = (selected_index == -1) ? -1 : (gradient->get_point_count() - selected_index - 1);
192
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
193
undo_redo->create_action(TTR("Reverse Gradient"), UndoRedo::MERGE_DISABLE, *gradient);
194
undo_redo->add_do_method(*gradient, "reverse");
195
undo_redo->add_do_method(this, "set_selected_index", new_selected_idx);
196
undo_redo->add_undo_method(*gradient, "reverse");
197
undo_redo->add_undo_method(this, "set_selected_index", selected_index);
198
undo_redo->commit_action();
199
}
200
201
void GradientEdit::set_selected_index(int p_index) {
202
selected_index = p_index;
203
queue_redraw();
204
}
205
206
void GradientEdit::set_snap_enabled(bool p_enabled) {
207
snap_enabled = p_enabled;
208
queue_redraw();
209
if (gradient.is_valid()) {
210
if (snap_enabled) {
211
gradient->set_meta(SNAME("_snap_enabled"), true);
212
} else {
213
gradient->remove_meta(SNAME("_snap_enabled"));
214
}
215
}
216
}
217
218
void GradientEdit::set_snap_count(int p_count) {
219
snap_count = p_count;
220
queue_redraw();
221
if (gradient.is_valid()) {
222
if (snap_count != GradientEditor::DEFAULT_SNAP) {
223
gradient->set_meta(SNAME("_snap_count"), snap_count);
224
} else {
225
gradient->remove_meta(SNAME("_snap_count"));
226
}
227
}
228
}
229
230
ColorPicker *GradientEdit::get_picker() const {
231
return picker;
232
}
233
234
PopupPanel *GradientEdit::get_popup() const {
235
return popup;
236
}
237
238
void GradientEdit::gui_input(const Ref<InputEvent> &p_event) {
239
ERR_FAIL_COND(p_event.is_null());
240
241
Ref<InputEventKey> k = p_event;
242
243
if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && selected_index != -1) {
244
if (grabbing == GRAB_ADD) {
245
gradient->remove_point(selected_index); // Point is temporary, so remove directly from gradient.
246
set_selected_index(-1);
247
} else {
248
remove_point(selected_index);
249
}
250
grabbing = GRAB_NONE;
251
hovered_index = -1;
252
accept_event();
253
}
254
255
Ref<InputEventMouseButton> mb = p_event;
256
257
if (mb.is_valid() && mb->is_pressed()) {
258
float adjusted_mb_x = mb->get_position().x - handle_width / 2;
259
bool should_snap = snap_enabled || mb->is_ctrl_pressed();
260
261
// Delete point or move it to old position on middle or right click.
262
if (mb->get_button_index() == MouseButton::RIGHT || mb->get_button_index() == MouseButton::MIDDLE) {
263
if (grabbing == GRAB_MOVE && mb->get_button_index() == MouseButton::RIGHT) {
264
gradient->set_offset(selected_index, pre_grab_offset);
265
set_selected_index(pre_grab_index);
266
} else {
267
int point_to_remove = _get_point_at(adjusted_mb_x);
268
if (point_to_remove == -1) {
269
set_selected_index(-1); // Nothing on the place of the click, just deselect any handle.
270
} else {
271
if (grabbing == GRAB_ADD) {
272
gradient->remove_point(point_to_remove); // Point is temporary, so remove directly from gradient.
273
set_selected_index(-1);
274
} else {
275
remove_point(point_to_remove);
276
}
277
hovered_index = -1;
278
}
279
}
280
grabbing = GRAB_NONE;
281
accept_event();
282
}
283
284
// Select point.
285
if (mb->get_button_index() == MouseButton::LEFT) {
286
int total_w = _get_gradient_rect_width();
287
288
// Check if color picker was clicked or gradient was double-clicked.
289
if (adjusted_mb_x > total_w + draw_spacing) {
290
if (!mb->is_double_click()) {
291
_show_color_picker();
292
}
293
accept_event();
294
return;
295
} else if (mb->is_double_click()) {
296
set_selected_index(_get_point_at(adjusted_mb_x));
297
_show_color_picker();
298
accept_event();
299
return;
300
}
301
302
if (grabbing == GRAB_NONE) {
303
set_selected_index(_get_point_at(adjusted_mb_x));
304
}
305
306
if (selected_index != -1 && !mb->is_alt_pressed()) {
307
// An existing point was grabbed.
308
grabbing = GRAB_MOVE;
309
pre_grab_offset = gradient->get_offset(selected_index);
310
pre_grab_index = selected_index;
311
} else if (grabbing == GRAB_NONE) {
312
// Adding a new point. Insert a temporary point for the user to adjust, so it's not in the undo/redo.
313
float new_offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1);
314
if (should_snap) {
315
new_offset = Math::snapped(new_offset, 1.0 / snap_count);
316
}
317
318
for (int i = 0; i < gradient->get_point_count(); i++) {
319
if (gradient->get_offset(i) == new_offset) {
320
// If another point with the same offset is found, then
321
// tweak it if Alt was pressed, otherwise something has gone wrong, so stop the operation.
322
if (mb->is_alt_pressed()) {
323
new_offset = MIN(gradient->get_offset(i) + 0.00001, 1);
324
} else {
325
return;
326
}
327
}
328
}
329
330
Color new_color = gradient->get_color_at_offset(new_offset);
331
if (mb->is_alt_pressed()) {
332
// Alt + Click on a point duplicates it. So copy its color.
333
int point_to_copy = _get_point_at(adjusted_mb_x);
334
if (point_to_copy != -1) {
335
new_color = gradient->get_color(point_to_copy);
336
}
337
}
338
// Add a temporary point for the user to adjust before adding it permanently.
339
gradient->add_point(new_offset, new_color);
340
set_selected_index(_predict_insertion_index(new_offset));
341
grabbing = GRAB_ADD;
342
}
343
}
344
}
345
346
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
347
if (grabbing == GRAB_MOVE) {
348
// Finish moving a point.
349
set_offset(selected_index, gradient->get_offset(selected_index));
350
grabbing = GRAB_NONE;
351
} else if (grabbing == GRAB_ADD) {
352
// Finish inserting a new point. Remove the temporary point and insert the permanent one in its place.
353
float new_offset = gradient->get_offset(selected_index);
354
Color new_color = gradient->get_color(selected_index);
355
gradient->remove_point(selected_index);
356
add_point(new_offset, new_color);
357
grabbing = GRAB_NONE;
358
}
359
}
360
361
Ref<InputEventMouseMotion> mm = p_event;
362
363
if (mm.is_valid()) {
364
int total_w = _get_gradient_rect_width();
365
float adjusted_mm_x = mm->get_position().x - handle_width / 2;
366
bool should_snap = snap_enabled || mm->is_ctrl_pressed();
367
368
// Hovering logic.
369
if (grabbing == GRAB_NONE) {
370
int nearest_point = _get_point_at(adjusted_mm_x);
371
if (hovered_index != nearest_point) {
372
hovered_index = nearest_point;
373
queue_redraw();
374
}
375
return;
376
} else {
377
hovered_index = -1;
378
}
379
380
// Grabbing logic.
381
float new_offset = CLAMP(adjusted_mm_x / float(total_w), 0, 1);
382
383
// Give the ability to snap right next to a point when using Shift.
384
if (mm->is_shift_pressed()) {
385
float smallest_offset = should_snap ? (0.5 / snap_count) : 0.01;
386
int nearest_idx = -1;
387
// Only check the two adjacent points to find which one is the nearest.
388
if (selected_index > 0) {
389
float temp_offset = Math::abs(gradient->get_offset(selected_index - 1) - new_offset);
390
if (temp_offset < smallest_offset) {
391
smallest_offset = temp_offset;
392
nearest_idx = selected_index - 1;
393
}
394
}
395
if (selected_index < gradient->get_point_count() - 1) {
396
float temp_offset = Math::abs(gradient->get_offset(selected_index + 1) - new_offset);
397
if (temp_offset < smallest_offset) {
398
smallest_offset = temp_offset;
399
nearest_idx = selected_index + 1;
400
}
401
}
402
if (nearest_idx != -1) {
403
// Snap to the point with a slight adjustment to the left or right.
404
float adjustment = gradient->get_offset(nearest_idx) < new_offset ? 0.00001 : -0.00001;
405
new_offset = CLAMP(gradient->get_offset(nearest_idx) + adjustment, 0, 1);
406
} else if (should_snap) {
407
new_offset = Math::snapped(new_offset, 1.0 / snap_count);
408
}
409
} else if (should_snap) {
410
// Shift is not pressed, so snap fully without adjustments.
411
new_offset = Math::snapped(new_offset, 1.0 / snap_count);
412
}
413
414
// Don't move the point if its new offset would be the same as another point's.
415
for (int i = 0; i < gradient->get_point_count(); i++) {
416
if (gradient->get_offset(i) == new_offset && i != selected_index) {
417
return;
418
}
419
}
420
421
if (selected_index == -1) {
422
return;
423
}
424
425
// We want to only save this action for undo/redo when released, so don't use set_offset() yet.
426
gradient->set_offset(selected_index, new_offset);
427
428
// Update selected_index after the gradient updates its indices, so you keep holding the same color.
429
for (int i = 0; i < gradient->get_point_count(); i++) {
430
if (gradient->get_offset(i) == new_offset) {
431
set_selected_index(i);
432
break;
433
}
434
}
435
}
436
}
437
438
void GradientEdit::_redraw() {
439
int w = get_size().x;
440
int h = get_size().y - draw_spacing; // A bit of spacing below the gradient too.
441
442
if (w == 0 || h == 0) {
443
return; // Safety check as there is nothing to draw with such size.
444
}
445
446
int total_w = _get_gradient_rect_width();
447
int half_handle_width = handle_width * 0.5;
448
449
// Draw gradient.
450
draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(half_handle_width, 0, total_w, h), true);
451
preview_texture->set_gradient(gradient);
452
draw_texture_rect(preview_texture, Rect2(half_handle_width, 0, total_w, h));
453
454
// Draw vertical snap lines.
455
if (snap_enabled || (Input::get_singleton()->is_key_pressed(Key::CTRL) && grabbing != GRAB_NONE)) {
456
const Color line_color = Color(0.5, 0.5, 0.5, 0.5);
457
for (int idx = 1; idx < snap_count; idx++) {
458
float offset_x = idx * total_w / (float)snap_count + half_handle_width;
459
draw_line(Point2(offset_x, 0), Point2(offset_x, h), line_color);
460
}
461
}
462
463
// Draw handles.
464
for (int i = 0; i < gradient->get_point_count(); i++) {
465
// Only draw handles for points in [0, 1]. If there are points before or after, draw a little indicator.
466
if (gradient->get_offset(i) < 0.0) {
467
continue;
468
} else if (gradient->get_offset(i) > 1.0) {
469
break;
470
}
471
// White or black handle color, to contrast with the selected color's brightness.
472
// Also consider the fact that the color may be translucent.
473
// The checkerboard pattern in the background has an average luminance of 0.75.
474
Color inside_col = gradient->get_color(i);
475
Color border_col = Math::lerp(0.75f, inside_col.get_luminance(), inside_col.a) > 0.455 ? Color(0, 0, 0) : Color(1, 1, 1);
476
477
int handle_thickness = MAX(1, Math::round(EDSCALE));
478
float handle_x_pos = gradient->get_offset(i) * total_w + half_handle_width;
479
float handle_start_x = handle_x_pos - half_handle_width;
480
Rect2 rect = Rect2(handle_start_x, h / 2, handle_width, h / 2);
481
482
if (inside_col.a < 1) {
483
// If the color is translucent, draw a little opaque rectangle at the bottom to more easily see it.
484
draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), rect, true);
485
draw_rect(rect, inside_col, true);
486
Color inside_col_opaque = inside_col;
487
inside_col_opaque.a = 1.0;
488
draw_rect(Rect2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness / 2.0, handle_width - handle_thickness, h * 0.1), inside_col_opaque, true);
489
} else {
490
draw_rect(rect, inside_col, true);
491
}
492
493
if (selected_index == i) {
494
// Handle is selected.
495
draw_rect(rect, border_col, false, handle_thickness);
496
draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);
497
if (inside_col.a < 1) {
498
draw_line(Vector2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness), Vector2(handle_start_x + handle_width - handle_thickness / 2.0, h * 0.9 - handle_thickness), border_col, handle_thickness);
499
}
500
rect = rect.grow(-handle_thickness);
501
const Color focus_col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
502
draw_rect(rect, has_focus() ? focus_col : focus_col.darkened(0.4), false, handle_thickness);
503
rect = rect.grow(-handle_thickness);
504
draw_rect(rect, border_col, false, handle_thickness);
505
} else {
506
// Handle isn't selected.
507
border_col.a = 0.9;
508
draw_rect(rect, border_col, false, handle_thickness);
509
draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);
510
if (inside_col.a < 1) {
511
draw_line(Vector2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness), Vector2(handle_start_x + handle_width - handle_thickness / 2.0, h * 0.9 - handle_thickness), border_col, handle_thickness);
512
}
513
if (hovered_index == i) {
514
// Draw a subtle translucent rect inside the handle if it's being hovered.
515
rect = rect.grow(-handle_thickness);
516
border_col.a = 0.54;
517
draw_rect(rect, border_col, false, handle_thickness);
518
}
519
}
520
}
521
522
// Draw "button" for color selector.
523
int button_offset = total_w + handle_width + draw_spacing;
524
if (selected_index != -1) {
525
Color grabbed_col = gradient->get_color(selected_index);
526
if (grabbed_col.a < 1) {
527
draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(button_offset, 0, h, h), true);
528
}
529
draw_rect(Rect2(button_offset, 0, h, h), grabbed_col);
530
if (grabbed_col.r > 1 || grabbed_col.g > 1 || grabbed_col.b > 1) {
531
// Draw an indicator to denote that the currently selected color is "overbright".
532
draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2(button_offset, 0));
533
}
534
} else {
535
// If no color is selected, draw gray color with 'X' on top.
536
draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1));
537
draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(0.8, 0.8, 0.8));
538
draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(0.8, 0.8, 0.8));
539
}
540
}
541
542
void GradientEdit::_notification(int p_what) {
543
switch (p_what) {
544
case NOTIFICATION_THEME_CHANGED: {
545
draw_spacing = BASE_SPACING * get_theme_default_base_scale();
546
handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale();
547
} break;
548
case NOTIFICATION_DRAW: {
549
_redraw();
550
} break;
551
case NOTIFICATION_MOUSE_EXIT: {
552
if (hovered_index != -1) {
553
hovered_index = -1;
554
queue_redraw();
555
}
556
} break;
557
case NOTIFICATION_VISIBILITY_CHANGED: {
558
if (!is_visible()) {
559
grabbing = GRAB_NONE;
560
}
561
} break;
562
}
563
}
564
565
void GradientEdit::_bind_methods() {
566
ClassDB::bind_method(D_METHOD("set_selected_index", "index"), &GradientEdit::set_selected_index);
567
}
568
569
GradientEdit::GradientEdit() {
570
set_focus_mode(FOCUS_ALL);
571
set_custom_minimum_size(Size2(0, 60) * EDSCALE);
572
573
picker = memnew(ColorPicker);
574
int picker_shape = EDITOR_GET("interface/inspector/default_color_picker_shape");
575
picker->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);
576
picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));
577
578
popup = memnew(PopupPanel);
579
popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(picker));
580
581
add_child(popup, false, INTERNAL_MODE_FRONT);
582
popup->add_child(picker);
583
584
preview_texture.instantiate();
585
preview_texture->set_width(1024);
586
}
587
588
///////////////////////
589
590
const int GradientEditor::DEFAULT_SNAP = 10;
591
592
void GradientEditor::_set_snap_enabled(bool p_enabled) {
593
gradient_editor_rect->set_snap_enabled(p_enabled);
594
snap_count_edit->set_visible(p_enabled);
595
}
596
597
void GradientEditor::_set_snap_count(int p_count) {
598
gradient_editor_rect->set_snap_count(CLAMP(p_count, 2, 100));
599
}
600
601
void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
602
gradient_editor_rect->set_gradient(p_gradient);
603
}
604
605
void GradientEditor::_notification(int p_what) {
606
switch (p_what) {
607
case NOTIFICATION_THEME_CHANGED: {
608
reverse_button->set_button_icon(get_editor_theme_icon(SNAME("ReverseGradient")));
609
snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
610
} break;
611
case NOTIFICATION_READY: {
612
Ref<Gradient> gradient = gradient_editor_rect->get_gradient();
613
if (gradient.is_valid()) {
614
// Set snapping settings based on the gradient's meta.
615
snap_button->set_pressed(gradient->get_meta("_snap_enabled", false));
616
snap_count_edit->set_value(gradient->get_meta("_snap_count", DEFAULT_SNAP));
617
}
618
} break;
619
}
620
}
621
622
GradientEditor::GradientEditor() {
623
HFlowContainer *toolbar = memnew(HFlowContainer);
624
add_child(toolbar);
625
626
reverse_button = memnew(Button);
627
reverse_button->set_tooltip_text(TTR("Reverse/Mirror Gradient"));
628
toolbar->add_child(reverse_button);
629
630
toolbar->add_child(memnew(VSeparator));
631
632
snap_button = memnew(Button);
633
snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
634
snap_button->set_toggle_mode(true);
635
toolbar->add_child(snap_button);
636
snap_button->connect(SceneStringName(toggled), callable_mp(this, &GradientEditor::_set_snap_enabled));
637
638
snap_count_edit = memnew(EditorSpinSlider);
639
snap_count_edit->set_min(2);
640
snap_count_edit->set_max(100);
641
snap_count_edit->set_accessibility_name(TTRC("Grid Step"));
642
snap_count_edit->set_value(DEFAULT_SNAP);
643
snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
644
toolbar->add_child(snap_count_edit);
645
snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &GradientEditor::_set_snap_count));
646
647
gradient_editor_rect = memnew(GradientEdit);
648
add_child(gradient_editor_rect);
649
reverse_button->connect(SceneStringName(pressed), callable_mp(gradient_editor_rect, &GradientEdit::reverse_gradient));
650
651
set_mouse_filter(MOUSE_FILTER_STOP);
652
_set_snap_enabled(snap_button->is_pressed());
653
_set_snap_count(snap_count_edit->get_value());
654
}
655
656
///////////////////////
657
658
bool EditorInspectorPluginGradient::can_handle(Object *p_object) {
659
return Object::cast_to<Gradient>(p_object) != nullptr;
660
}
661
662
void EditorInspectorPluginGradient::parse_begin(Object *p_object) {
663
Gradient *gradient = Object::cast_to<Gradient>(p_object);
664
ERR_FAIL_NULL(gradient);
665
Ref<Gradient> g(gradient);
666
667
GradientEditor *editor = memnew(GradientEditor);
668
editor->set_gradient(g);
669
add_custom_control(editor);
670
}
671
672
///////////////////////
673
674
GradientEditorPlugin::GradientEditorPlugin() {
675
Ref<EditorInspectorPluginGradient> plugin;
676
plugin.instantiate();
677
add_inspector_plugin(plugin);
678
}
679
680