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