Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/color_picker.cpp
9902 views
1
/**************************************************************************/
2
/* color_picker.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 "color_picker.h"
32
33
#include "core/io/image.h"
34
#include "core/math/expression.h"
35
#include "scene/gui/color_mode.h"
36
#include "scene/gui/color_picker_shape.h"
37
#include "scene/gui/file_dialog.h"
38
#include "scene/gui/grid_container.h"
39
#include "scene/gui/label.h"
40
#include "scene/gui/line_edit.h"
41
#include "scene/gui/link_button.h"
42
#include "scene/gui/margin_container.h"
43
#include "scene/gui/menu_button.h"
44
#include "scene/gui/panel.h"
45
#include "scene/gui/popup_menu.h"
46
#include "scene/gui/slider.h"
47
#include "scene/gui/spin_box.h"
48
#include "scene/gui/texture_rect.h"
49
#include "scene/resources/atlas_texture.h"
50
#include "scene/resources/color_palette.h"
51
#include "scene/resources/image_texture.h"
52
#include "scene/resources/style_box_flat.h"
53
#include "scene/resources/style_box_texture.h"
54
#include "scene/theme/theme_db.h"
55
#include "thirdparty/misc/ok_color_shader.h"
56
57
static inline bool is_color_overbright(const Color &color) {
58
return (color.r > 1.0) || (color.g > 1.0) || (color.b > 1.0);
59
}
60
61
static inline bool is_color_valid_hex(const Color &color) {
62
return !is_color_overbright(color) && color.r >= 0 && color.g >= 0 && color.b >= 0;
63
}
64
65
static inline String color_to_string(const Color &color, bool show_alpha = true, bool force_value_format = false) {
66
if (!force_value_format && !is_color_overbright(color)) {
67
return "#" + color.to_html(show_alpha);
68
}
69
String t = "(" + String::num(color.r, 3) + ", " + String::num(color.g, 3) + ", " + String::num(color.b, 3);
70
if (show_alpha) {
71
t += ", " + String::num(color.a, 3) + ")";
72
} else {
73
t += ")";
74
}
75
return t;
76
}
77
78
void ColorPicker::_notification(int p_what) {
79
switch (p_what) {
80
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
81
RID ae = get_accessibility_element();
82
ERR_FAIL_COND(ae.is_null());
83
84
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_COLOR_PICKER);
85
DisplayServer::get_singleton()->accessibility_update_set_color_value(ae, color);
86
} break;
87
88
case NOTIFICATION_ENTER_TREE: {
89
_update_color();
90
} break;
91
92
#ifdef MACOS_ENABLED
93
case NOTIFICATION_VISIBILITY_CHANGED: {
94
if (is_visible_in_tree()) {
95
perm_hb->set_visible(!OS::get_singleton()->get_granted_permissions().has("macos.permission.RECORD_SCREEN"));
96
}
97
} break;
98
#endif
99
100
case NOTIFICATION_READY: {
101
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_COLOR_PICKER)) {
102
btn_pick->set_tooltip_text(ETR("Pick a color from the screen."));
103
btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed_native));
104
} else if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_CAPTURE) && !get_tree()->get_root()->is_embedding_subwindows()) {
105
// FIXME: The embedding check is needed to fix a bug in single-window mode (GH-93718).
106
btn_pick->set_tooltip_text(ETR("Pick a color from the screen."));
107
btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed));
108
} else {
109
// On unsupported platforms, use a legacy method for color picking.
110
btn_pick->set_tooltip_text(ETR("Pick a color from the application window."));
111
btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed_legacy));
112
}
113
} break;
114
115
case NOTIFICATION_THEME_CHANGED: {
116
btn_pick->set_button_icon(theme_cache.screen_picker);
117
_update_drop_down_arrow(btn_preset->is_pressed(), btn_preset);
118
_update_drop_down_arrow(btn_recent_preset->is_pressed(), btn_recent_preset);
119
btn_add_preset->set_button_icon(theme_cache.add_preset);
120
menu_btn->set_button_icon(theme_cache.menu_option);
121
btn_mode->set_button_icon(theme_cache.menu_option);
122
123
btn_pick->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
124
btn_shape->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
125
btn_mode->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
126
127
{
128
int i = 0;
129
for (ColorPickerShape *shape : shapes) {
130
if (shape->is_initialized) {
131
shape->update_theme();
132
}
133
shape_popup->set_item_icon(i, shape->get_icon());
134
i++;
135
}
136
}
137
138
if (current_shape != SHAPE_NONE) {
139
btn_shape->set_button_icon(shape_popup->get_item_icon(get_current_shape_index()));
140
}
141
142
for (int i = 0; i < MODE_SLIDER_COUNT; i++) {
143
labels[i]->set_custom_minimum_size(Size2(theme_cache.label_width, 0));
144
sliders[i]->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers);
145
}
146
alpha_label->set_custom_minimum_size(Size2(theme_cache.label_width, 0));
147
alpha_slider->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers);
148
intensity_label->set_custom_minimum_size(Size2(theme_cache.label_width, 0));
149
150
for (int i = 0; i < MODE_BUTTON_COUNT; i++) {
151
mode_btns[i]->begin_bulk_theme_override();
152
mode_btns[i]->add_theme_style_override(SceneStringName(pressed), theme_cache.mode_button_pressed);
153
mode_btns[i]->add_theme_style_override(CoreStringName(normal), theme_cache.mode_button_normal);
154
mode_btns[i]->add_theme_style_override(SceneStringName(hover), theme_cache.mode_button_hover);
155
mode_btns[i]->end_bulk_theme_override();
156
}
157
158
internal_margin->begin_bulk_theme_override();
159
internal_margin->add_theme_constant_override(SNAME("margin_bottom"), theme_cache.content_margin);
160
internal_margin->add_theme_constant_override(SNAME("margin_left"), theme_cache.content_margin);
161
internal_margin->add_theme_constant_override(SNAME("margin_right"), theme_cache.content_margin);
162
internal_margin->add_theme_constant_override(SNAME("margin_top"), theme_cache.content_margin);
163
internal_margin->end_bulk_theme_override();
164
165
_reset_sliders_theme();
166
167
hex_label->set_custom_minimum_size(Size2(38 * theme_cache.base_scale, 0));
168
// Adjust for the width of the "script" icon.
169
text_type->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
170
171
_update_presets();
172
_update_recent_presets();
173
_update_controls();
174
} break;
175
176
case NOTIFICATION_WM_CLOSE_REQUEST: {
177
if (picker_window != nullptr && picker_window->is_visible()) {
178
picker_window->hide();
179
}
180
} break;
181
182
case NOTIFICATION_FOCUS_ENTER:
183
case NOTIFICATION_FOCUS_EXIT: {
184
if (current_shape != SHAPE_NONE) {
185
shapes[get_current_shape_index()]->cursor_editing = false;
186
}
187
} break;
188
189
case NOTIFICATION_INTERNAL_PROCESS: {
190
if (!is_picking_color) {
191
Input *input = Input::get_singleton();
192
if (input->is_action_just_released("ui_left") ||
193
input->is_action_just_released("ui_right") ||
194
input->is_action_just_released("ui_up") ||
195
input->is_action_just_released("ui_down")) {
196
gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
197
if (current_shape == SHAPE_NONE) {
198
shapes[get_current_shape_index()]->echo_multiplier = 1;
199
}
200
accept_event();
201
set_process_internal(false);
202
return;
203
}
204
205
if (current_shape == SHAPE_NONE) {
206
return;
207
}
208
209
gamepad_event_delay_ms -= get_process_delta_time();
210
if (gamepad_event_delay_ms <= 0) {
211
gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
212
// Treat any input from joypad axis as -1, 0, or 1, as the value is added to Vector2i and would be lost.
213
Vector2 color_change_vector = Vector2(
214
input->is_action_pressed("ui_right") - input->is_action_pressed("ui_left"),
215
input->is_action_pressed("ui_down") - input->is_action_pressed("ui_up"));
216
217
shapes[get_current_shape_index()]->update_cursor(color_change_vector, true);
218
accept_event();
219
}
220
return;
221
}
222
DisplayServer *ds = DisplayServer::get_singleton();
223
Vector2 ofs = ds->mouse_get_position();
224
225
Color c = DisplayServer::get_singleton()->screen_get_pixel(ofs);
226
227
picker_preview_style_box_color->set_bg_color(c);
228
picker_preview_style_box->set_bg_color(c.get_luminance() < 0.5 ? Color(1.0f, 1.0f, 1.0f) : Color(0.0f, 0.0f, 0.0f));
229
230
if (ds->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE)) {
231
Ref<Image> zoom_preview_img = ds->screen_get_image_rect(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17));
232
picker_window->set_position(ofs - Vector2(28, 28));
233
picker_texture_zoom->set_texture(ImageTexture::create_from_image(zoom_preview_img));
234
} else {
235
Size2i screen_size = ds->screen_get_size(DisplayServer::SCREEN_WITH_MOUSE_FOCUS);
236
Vector2i screen_position = ds->screen_get_position(DisplayServer::SCREEN_WITH_MOUSE_FOCUS);
237
238
float ofs_decal_x = (ofs.x < screen_position.x + screen_size.width - 51) ? 8 : -36;
239
float ofs_decal_y = (ofs.y < screen_position.y + screen_size.height - 51) ? 8 : -36;
240
241
picker_window->set_position(ofs + Vector2(ofs_decal_x, ofs_decal_y));
242
}
243
244
set_pick_color(c);
245
} break;
246
}
247
}
248
249
void ColorPicker::_update_theme_item_cache() {
250
VBoxContainer::_update_theme_item_cache();
251
252
theme_cache.base_scale = get_theme_default_base_scale();
253
}
254
255
void ColorPicker::init_shaders() {
256
wheel_shader.instantiate();
257
wheel_shader->set_code(R"(
258
// ColorPicker wheel shader.
259
260
shader_type canvas_item;
261
262
uniform float wheel_radius = 0.42;
263
264
void fragment() {
265
float x = UV.x - 0.5;
266
float y = UV.y - 0.5;
267
float a = atan(y, x);
268
x += 0.001;
269
y += 0.001;
270
float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
271
x -= 0.002;
272
float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
273
y -= 0.002;
274
float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
275
x += 0.002;
276
float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
277
278
COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00);
279
}
280
)");
281
282
circle_shader.instantiate();
283
circle_shader->set_code(R"(
284
// ColorPicker circle shader.
285
286
shader_type canvas_item;
287
288
uniform float v = 1.0;
289
290
void fragment() {
291
float x = UV.x - 0.5;
292
float y = UV.y - 0.5;
293
float a = atan(y, x);
294
x += 0.001;
295
y += 0.001;
296
float b = float(sqrt(x * x + y * y) < 0.5);
297
x -= 0.002;
298
float b2 = float(sqrt(x * x + y * y) < 0.5);
299
y -= 0.002;
300
float b3 = float(sqrt(x * x + y * y) < 0.5);
301
x += 0.002;
302
float b4 = float(sqrt(x * x + y * y) < 0.5);
303
304
COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00);
305
})");
306
307
circle_ok_color_shader.instantiate();
308
circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
309
// ColorPicker ok color hsl circle shader.
310
311
uniform float ok_hsl_l = 1.0;
312
313
void fragment() {
314
float x = UV.x - 0.5;
315
float y = UV.y - 0.5;
316
float h = atan(y, x) / (2.0 * M_PI);
317
float s = sqrt(x * x + y * y) * 2.0;
318
vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
319
x += 0.001;
320
y += 0.001;
321
float b = float(sqrt(x * x + y * y) < 0.5);
322
x -= 0.002;
323
float b2 = float(sqrt(x * x + y * y) < 0.5);
324
y -= 0.002;
325
float b3 = float(sqrt(x * x + y * y) < 0.5);
326
x += 0.002;
327
float b4 = float(sqrt(x * x + y * y) < 0.5);
328
COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00);
329
})");
330
331
rectangle_ok_color_hs_shader.instantiate();
332
rectangle_ok_color_hs_shader->set_code(OK_COLOR_SHADER + R"(
333
// ColorPicker ok color hs rectangle shader.
334
335
uniform float ok_hsl_l = 0.0;
336
337
void fragment() {
338
float h = UV.x;
339
float s = 1.0 - UV.y;
340
vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
341
COLOR = vec4(col, 1.0);
342
})");
343
344
rectangle_ok_color_hl_shader.instantiate();
345
rectangle_ok_color_hl_shader->set_code(OK_COLOR_SHADER + R"(
346
// ColorPicker ok color hl rectangle shader.
347
348
uniform float ok_hsl_s = 0.0;
349
350
void fragment() {
351
float h = UV.x;
352
float l = 1.0 - UV.y;
353
vec3 col = okhsl_to_srgb(vec3(h, ok_hsl_s, l));
354
COLOR = vec4(col, 1.0);
355
})");
356
}
357
358
void ColorPicker::finish_shaders() {
359
wheel_shader.unref();
360
circle_shader.unref();
361
circle_ok_color_shader.unref();
362
rectangle_ok_color_hs_shader.unref();
363
rectangle_ok_color_hl_shader.unref();
364
}
365
366
void ColorPicker::set_focus_on_line_edit() {
367
callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
368
}
369
370
void ColorPicker::set_focus_on_picker_shape() {
371
shapes[get_current_shape_index()]->grab_focus();
372
}
373
374
void ColorPicker::_update_controls() {
375
int mode_sliders_count = modes[current_mode]->get_slider_count();
376
377
for (int i = current_slider_count; i < mode_sliders_count; i++) {
378
sliders[i]->show();
379
labels[i]->show();
380
values[i]->show();
381
}
382
for (int i = mode_sliders_count; i < current_slider_count; i++) {
383
sliders[i]->hide();
384
labels[i]->hide();
385
values[i]->hide();
386
}
387
current_slider_count = mode_sliders_count;
388
389
for (int i = 0; i < current_slider_count; i++) {
390
labels[i]->set_text(modes[current_mode]->get_slider_label(i));
391
sliders[i]->set_accessibility_name(modes[current_mode]->get_slider_label(i));
392
values[i]->set_accessibility_name(modes[current_mode]->get_slider_label(i));
393
}
394
alpha_label->set_text("A");
395
alpha_slider->set_accessibility_name(ETR("Alpha"));
396
alpha_value->set_accessibility_name(ETR("Alpha"));
397
398
intensity_label->set_text("I");
399
intensity_slider->set_accessibility_name(ETR("Intensity"));
400
intensity_value->set_accessibility_name(ETR("Intensity"));
401
402
alpha_value->set_visible(edit_alpha);
403
alpha_slider->set_visible(edit_alpha);
404
alpha_label->set_visible(edit_alpha);
405
406
intensity_value->set_visible(edit_intensity);
407
intensity_slider->set_visible(edit_intensity);
408
intensity_label->set_visible(edit_intensity);
409
410
int i = 0;
411
for (ColorPickerShape *shape : shapes) {
412
bool is_active = get_current_shape_index() == i;
413
i++;
414
415
if (!shape->is_initialized) {
416
if (is_active) {
417
// Controls are initialized on demand, because ColorPicker does not need them all at once.
418
shape->initialize_controls();
419
} else {
420
continue;
421
}
422
}
423
424
for (Control *control : shape->controls) {
425
control->set_visible(is_active);
426
}
427
}
428
btn_shape->set_visible(current_shape != SHAPE_NONE);
429
}
430
431
void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders, bool p_calc_intensity) {
432
if (text_changed) {
433
add_recent_preset(color);
434
text_changed = false;
435
}
436
437
color = p_color;
438
if (p_calc_intensity) {
439
_copy_color_to_normalized_and_intensity();
440
}
441
_copy_normalized_to_hsv_okhsl();
442
443
if (!is_inside_tree()) {
444
return;
445
}
446
447
_update_color(p_update_sliders);
448
}
449
450
void ColorPicker::set_pick_color(const Color &p_color) {
451
_set_pick_color(p_color, true, true); // Because setters can't have more arguments.
452
}
453
454
void ColorPicker::set_old_color(const Color &p_color) {
455
old_color = p_color;
456
}
457
458
void ColorPicker::set_display_old_color(bool p_enabled) {
459
display_old_color = p_enabled;
460
}
461
462
bool ColorPicker::is_displaying_old_color() const {
463
return display_old_color;
464
}
465
466
void ColorPicker::set_edit_alpha(bool p_show) {
467
if (edit_alpha == p_show) {
468
return;
469
}
470
edit_alpha = p_show;
471
_update_controls();
472
473
if (!is_inside_tree()) {
474
return;
475
}
476
477
_update_color();
478
sample->queue_redraw();
479
}
480
481
bool ColorPicker::is_editing_alpha() const {
482
return edit_alpha;
483
}
484
485
void ColorPicker::set_edit_intensity(bool p_show) {
486
if (edit_intensity == p_show) {
487
return;
488
}
489
if (p_show) {
490
set_pick_color(color);
491
} else {
492
_normalized_apply_intensity_to_color();
493
color_normalized = color;
494
intensity = 0;
495
}
496
edit_intensity = p_show;
497
_update_controls();
498
499
if (!is_inside_tree()) {
500
return;
501
}
502
503
_update_color();
504
sample->queue_redraw();
505
}
506
507
bool ColorPicker::is_editing_intensity() const {
508
return edit_intensity;
509
}
510
511
void ColorPicker::_slider_drag_started() {
512
currently_dragging = true;
513
}
514
515
void ColorPicker::_slider_value_changed() {
516
if (updating) {
517
return;
518
}
519
520
intensity = intensity_value->get_value();
521
color_normalized = modes[current_mode]->get_color();
522
if (edit_intensity && is_color_overbright(color_normalized)) {
523
modes[current_mode]->_greater_value_inputted();
524
color_normalized = modes[current_mode]->get_color();
525
}
526
_normalized_apply_intensity_to_color();
527
intensity_value->set_prefix(intensity < 0 ? "" : "+");
528
529
modes[current_mode]->_value_changed();
530
531
_set_pick_color(color, false, false);
532
if (!deferred_mode_enabled || !currently_dragging) {
533
emit_signal(SNAME("color_changed"), color);
534
}
535
}
536
537
void ColorPicker::_slider_drag_ended() {
538
currently_dragging = false;
539
if (deferred_mode_enabled) {
540
emit_signal(SNAME("color_changed"), color);
541
}
542
}
543
544
void ColorPicker::add_mode(ColorMode *p_mode) {
545
modes.push_back(p_mode);
546
}
547
548
void ColorPicker::add_shape(ColorPickerShape *p_shape) {
549
shapes.push_back(p_shape);
550
}
551
552
void ColorPicker::create_slider(GridContainer *gc, int idx) {
553
Label *lbl = memnew(Label);
554
lbl->set_v_size_flags(SIZE_SHRINK_CENTER);
555
lbl->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
556
gc->add_child(lbl);
557
558
HSlider *slider = memnew(HSlider);
559
slider->set_v_size_flags(SIZE_SHRINK_CENTER);
560
slider->set_focus_mode(FOCUS_ACCESSIBILITY);
561
gc->add_child(slider);
562
563
SpinBox *val = memnew(SpinBox);
564
slider->share(val);
565
val->set_select_all_on_focus(true);
566
gc->add_child(val);
567
568
LineEdit *vle = val->get_line_edit();
569
vle->connect(SceneStringName(text_changed), callable_mp(this, &ColorPicker::_text_changed));
570
vle->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_line_edit_input));
571
vle->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
572
573
val->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_slider_or_spin_input));
574
575
slider->set_h_size_flags(SIZE_EXPAND_FILL);
576
577
slider->connect("drag_started", callable_mp(this, &ColorPicker::_slider_drag_started));
578
slider->connect(SceneStringName(value_changed), callable_mp(this, &ColorPicker::_slider_value_changed).unbind(1));
579
slider->connect("drag_ended", callable_mp(this, &ColorPicker::_slider_drag_ended).unbind(1));
580
if (idx < MODE_SLIDER_COUNT) {
581
slider->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_slider_draw).bind(idx));
582
} else if (idx == SLIDER_ALPHA) {
583
slider->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_alpha_slider_draw));
584
}
585
slider->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_slider_or_spin_input));
586
587
if (idx < MODE_SLIDER_COUNT) {
588
sliders[idx] = slider;
589
values[idx] = val;
590
labels[idx] = lbl;
591
} else if (idx == SLIDER_INTENSITY) {
592
intensity_slider = slider;
593
intensity_value = val;
594
intensity_label = lbl;
595
} else if (idx == SLIDER_ALPHA) {
596
alpha_slider = slider;
597
alpha_value = val;
598
alpha_label = lbl;
599
}
600
}
601
602
#ifdef TOOLS_ENABLED
603
void ColorPicker::set_editor_settings(Object *p_editor_settings) {
604
if (editor_settings) {
605
return;
606
}
607
editor_settings = p_editor_settings;
608
609
if (preset_cache.is_empty()) {
610
PackedColorArray saved_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "presets", PackedColorArray());
611
for (int i = 0; i < saved_presets.size(); i++) {
612
preset_cache.push_back(saved_presets[i]);
613
}
614
}
615
616
for (const Color &preset : preset_cache) {
617
presets.push_back(preset);
618
}
619
620
if (recent_preset_cache.is_empty()) {
621
PackedColorArray saved_recent_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "recent_presets", PackedColorArray());
622
for (int i = 0; i < saved_recent_presets.size(); i++) {
623
recent_preset_cache.push_back(saved_recent_presets[i]);
624
}
625
}
626
627
for (const Color &preset : recent_preset_cache) {
628
recent_presets.push_back(preset);
629
}
630
631
_update_presets();
632
_update_recent_presets();
633
}
634
635
void ColorPicker::set_quick_open_callback(const Callable &p_file_selected) {
636
quick_open_callback = p_file_selected;
637
}
638
639
void ColorPicker::set_palette_saved_callback(const Callable &p_palette_saved) {
640
palette_saved_callback = p_palette_saved;
641
}
642
643
#endif
644
645
HSlider *ColorPicker::get_slider(int p_idx) {
646
ERR_FAIL_INDEX_V(p_idx, MODE_MAX, nullptr);
647
return sliders[p_idx];
648
}
649
650
Vector<float> ColorPicker::get_active_slider_values() {
651
Vector<float> cur_values;
652
for (int i = 0; i < current_slider_count; i++) {
653
cur_values.push_back(sliders[i]->get_value());
654
}
655
cur_values.push_back(alpha_slider->get_value());
656
return cur_values;
657
}
658
659
void ColorPicker::_copy_normalized_to_hsv_okhsl() {
660
if (!okhsl_cached) {
661
ok_hsl_h = color_normalized.get_ok_hsl_h();
662
ok_hsl_s = color_normalized.get_ok_hsl_s();
663
ok_hsl_l = color_normalized.get_ok_hsl_l();
664
}
665
if (!hsv_cached) {
666
h = color_normalized.get_h();
667
s = color_normalized.get_s();
668
v = color_normalized.get_v();
669
}
670
hsv_cached = false;
671
okhsl_cached = false;
672
}
673
674
void ColorPicker::_copy_hsv_okhsl_to_normalized() {
675
if (current_shape != SHAPE_NONE && shapes[get_current_shape_index()]->is_ok_hsl()) {
676
color_normalized.set_ok_hsl(ok_hsl_h, ok_hsl_s, ok_hsl_l, color_normalized.a);
677
} else {
678
color_normalized.set_hsv(h, s, v, color_normalized.a);
679
}
680
}
681
682
Color ColorPicker::_color_apply_intensity(const Color &col) const {
683
if (intensity == 0.0f) {
684
return col;
685
}
686
Color linear_color = col.srgb_to_linear();
687
Color result;
688
float multiplier = Math::pow(2, intensity);
689
for (int i = 0; i < 3; i++) {
690
result.components[i] = linear_color.components[i] * multiplier;
691
}
692
result.a = col.a;
693
return result.linear_to_srgb();
694
}
695
696
void ColorPicker::_normalized_apply_intensity_to_color() {
697
color = _color_apply_intensity(color_normalized);
698
}
699
700
void ColorPicker::_copy_color_to_normalized_and_intensity() {
701
Color linear_color = color.srgb_to_linear();
702
float multiplier = MAX(1, MAX(MAX(linear_color.r, linear_color.g), linear_color.b));
703
for (int i = 0; i < 3; i++) {
704
color_normalized.components[i] = linear_color.components[i] / multiplier;
705
}
706
color_normalized.a = linear_color.a;
707
color_normalized = color_normalized.linear_to_srgb();
708
intensity = Math::log2(multiplier);
709
}
710
711
void ColorPicker::_select_from_preset_container(const Color &p_color) {
712
if (preset_group->get_pressed_button()) {
713
preset_group->get_pressed_button()->set_pressed(false);
714
}
715
716
for (int i = 1; i < preset_container->get_child_count(); i++) {
717
ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
718
if (current_btn && p_color == current_btn->get_preset_color()) {
719
current_btn->set_pressed(true);
720
break;
721
}
722
}
723
}
724
725
bool ColorPicker::_select_from_recent_preset_hbc(const Color &p_color) {
726
for (int i = 0; i < recent_preset_hbc->get_child_count(); i++) {
727
ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i));
728
if (current_btn && p_color == current_btn->get_preset_color()) {
729
current_btn->set_pressed(true);
730
return true;
731
}
732
}
733
return false;
734
}
735
736
void ColorPicker::_reset_sliders_theme() {
737
Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
738
style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale);
739
style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp());
740
741
for (int i = 0; i < MODE_SLIDER_COUNT; i++) {
742
sliders[i]->begin_bulk_theme_override();
743
sliders[i]->add_theme_icon_override(SNAME("grabber"), theme_cache.bar_arrow);
744
sliders[i]->add_theme_icon_override(SNAME("grabber_highlight"), theme_cache.bar_arrow);
745
sliders[i]->add_theme_constant_override(SNAME("grabber_offset"), 8 * theme_cache.base_scale);
746
if (!colorize_sliders) {
747
sliders[i]->add_theme_style_override(SNAME("slider"), style_box_flat);
748
}
749
sliders[i]->end_bulk_theme_override();
750
}
751
752
alpha_slider->begin_bulk_theme_override();
753
alpha_slider->add_theme_icon_override(SNAME("grabber"), theme_cache.bar_arrow);
754
alpha_slider->add_theme_icon_override(SNAME("grabber_highlight"), theme_cache.bar_arrow);
755
alpha_slider->add_theme_constant_override(SNAME("grabber_offset"), 8 * theme_cache.base_scale);
756
if (!colorize_sliders) {
757
alpha_slider->add_theme_style_override(SNAME("slider"), style_box_flat);
758
}
759
alpha_slider->end_bulk_theme_override();
760
}
761
762
void ColorPicker::_html_submitted(const String &p_html) {
763
if (updating) {
764
return;
765
}
766
Color new_color = color;
767
if (text_is_constructor || !is_color_valid_hex(color)) {
768
Ref<Expression> expr;
769
expr.instantiate();
770
Error err = expr->parse(p_html);
771
if (err == OK) {
772
Variant result = expr->execute(Array(), nullptr, false, true);
773
// This is basically the same as Variant::operator Color(), but remains original color if Color::from_string() fails
774
if (result.get_type() == Variant::COLOR) {
775
new_color = result;
776
} else if (result.get_type() == Variant::STRING) {
777
new_color = Color::from_string(result, color);
778
} else if (result.get_type() == Variant::INT) {
779
new_color = Color::hex(result);
780
}
781
}
782
} else {
783
new_color = Color::from_string(p_html.strip_edges(), color);
784
String html_no_prefix = p_html.strip_edges().trim_prefix("#");
785
if (html_no_prefix.is_valid_hex_number(false)) {
786
// Convert invalid HTML color codes that software like Figma supports.
787
if (html_no_prefix.length() == 1) {
788
// Turn `#1` into `#111111`.
789
html_no_prefix = html_no_prefix.repeat(6);
790
} else if (html_no_prefix.length() == 2) {
791
// Turn `#12` into `#121212`.
792
html_no_prefix = html_no_prefix.repeat(3);
793
} else if (html_no_prefix.length() == 5) {
794
// Turn `#12345` into `#11223344`.
795
html_no_prefix = html_no_prefix.left(4);
796
} else if (html_no_prefix.length() == 7) {
797
// Turn `#1234567` into `#123456`.
798
html_no_prefix = html_no_prefix.left(6);
799
}
800
}
801
new_color = Color::from_string(html_no_prefix, new_color);
802
}
803
804
if (!is_editing_alpha()) {
805
new_color.a = color.a;
806
}
807
808
if (new_color == color) {
809
return;
810
}
811
color = new_color;
812
813
if (!is_inside_tree()) {
814
return;
815
}
816
817
set_pick_color(color);
818
emit_signal(SNAME("color_changed"), color);
819
}
820
821
void ColorPicker::_update_color(bool p_update_sliders) {
822
updating = true;
823
824
if (p_update_sliders) {
825
float step = modes[current_mode]->get_slider_step();
826
float spinbox_arrow_step = modes[current_mode]->get_spinbox_arrow_step();
827
for (int i = 0; i < current_slider_count; i++) {
828
sliders[i]->set_max(modes[current_mode]->get_slider_max(i));
829
sliders[i]->set_step(step);
830
sliders[i]->set_value(modes[current_mode]->get_slider_value(i));
831
values[i]->set_custom_arrow_step(spinbox_arrow_step);
832
values[i]->set_allow_greater(modes[current_mode]->get_allow_greater());
833
}
834
alpha_slider->set_max(modes[current_mode]->get_alpha_slider_max());
835
alpha_slider->set_step(step);
836
alpha_slider->set_value(modes[current_mode]->get_alpha_slider_value());
837
intensity_slider->set_value(intensity);
838
intensity_value->set_prefix(intensity < 0 ? "" : "+");
839
}
840
841
_update_text_value();
842
843
if (current_shape != SHAPE_NONE) {
844
for (Control *control : shapes[get_current_shape_index()]->controls) {
845
control->queue_redraw();
846
}
847
}
848
849
sample->queue_redraw();
850
851
for (int i = 0; i < current_slider_count; i++) {
852
sliders[i]->queue_redraw();
853
}
854
alpha_slider->queue_redraw();
855
updating = false;
856
queue_accessibility_update();
857
}
858
859
void ColorPicker::_update_presets() {
860
int preset_size = _get_preset_size();
861
// Only update the preset button size if it has changed.
862
if (preset_size != prev_preset_size) {
863
prev_preset_size = preset_size;
864
btn_add_preset->set_custom_minimum_size(Size2(preset_size, preset_size));
865
for (int i = 1; i < preset_container->get_child_count(); i++) {
866
ColorPresetButton *cpb = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
867
cpb->set_custom_minimum_size(Size2(preset_size, preset_size));
868
}
869
}
870
871
#ifdef TOOLS_ENABLED
872
if (editor_settings) {
873
String cached_name = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "palette_name", String());
874
palette_path = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "palette_path", String());
875
bool palette_edited = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "palette_edited", false);
876
if (!cached_name.is_empty()) {
877
palette_name->set_text(cached_name);
878
if (btn_preset->is_pressed() && !presets.is_empty()) {
879
palette_name->show();
880
}
881
882
if (palette_edited) {
883
palette_name->set_text(vformat("%s*", palette_name->get_text().remove_char('*')));
884
palette_name->set_tooltip_text(TTRC("The changes to this palette have not been saved to a file."));
885
}
886
}
887
}
888
#endif
889
890
// Rebuild swatch color buttons, keeping the add-preset button in the first position.
891
for (int i = 1; i < preset_container->get_child_count(); i++) {
892
preset_container->get_child(i)->queue_free();
893
}
894
895
presets = preset_cache;
896
for (const Color &preset : preset_cache) {
897
_add_preset_button(preset_size, preset);
898
}
899
900
_notification(NOTIFICATION_VISIBILITY_CHANGED);
901
}
902
903
void ColorPicker::_update_recent_presets() {
904
#ifdef TOOLS_ENABLED
905
if (editor_settings) {
906
int recent_preset_count = recent_preset_hbc->get_child_count();
907
for (int i = 0; i < recent_preset_count; i++) {
908
memdelete(recent_preset_hbc->get_child(0));
909
}
910
911
recent_presets.clear();
912
for (const Color &preset : recent_preset_cache) {
913
recent_presets.push_back(preset);
914
}
915
916
int preset_size = _get_preset_size();
917
for (const Color &preset : recent_presets) {
918
_add_recent_preset_button(preset_size, preset);
919
}
920
921
_notification(NOTIFICATION_VISIBILITY_CHANGED);
922
}
923
#endif
924
}
925
926
#ifdef TOOLS_ENABLED
927
void ColorPicker::_text_type_toggled() {
928
text_is_constructor = !text_is_constructor;
929
if (text_is_constructor) {
930
hex_label->set_text(ETR("Expr"));
931
text_type->set_text("");
932
text_type->set_button_icon(theme_cache.color_script);
933
934
c_text->set_tooltip_text(RTR("Execute an expression as a color."));
935
} else {
936
hex_label->set_text(ETR("Hex"));
937
text_type->set_text("#");
938
text_type->set_button_icon(nullptr);
939
940
c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\")."));
941
}
942
_update_color();
943
}
944
#endif // TOOLS_ENABLED
945
946
Color ColorPicker::get_pick_color() const {
947
return color;
948
}
949
950
Color ColorPicker::get_old_color() const {
951
return old_color;
952
}
953
954
void ColorPicker::set_picker_shape(PickerShapeType p_shape) {
955
ERR_FAIL_INDEX(p_shape, SHAPE_MAX);
956
if (p_shape == current_shape) {
957
return;
958
}
959
if (current_shape != SHAPE_NONE) {
960
shape_popup->set_item_checked(get_current_shape_index(), false);
961
}
962
if (p_shape != SHAPE_NONE) {
963
shape_popup->set_item_checked(shape_to_index(p_shape), true);
964
btn_shape->set_button_icon(shape_popup->get_item_icon(shape_to_index(p_shape)));
965
}
966
967
current_shape = p_shape;
968
969
#ifdef TOOLS_ENABLED
970
if (editor_settings) {
971
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "picker_shape", current_shape);
972
}
973
#endif
974
975
_copy_normalized_to_hsv_okhsl();
976
_update_controls();
977
_update_color();
978
}
979
980
ColorPicker::PickerShapeType ColorPicker::get_picker_shape() const {
981
return current_shape;
982
}
983
984
inline int ColorPicker::_get_preset_size() {
985
return (int(get_minimum_size().width) - (preset_container->get_h_separation() * (PRESET_COLUMN_COUNT - 1))) / PRESET_COLUMN_COUNT;
986
}
987
988
void ColorPicker::_add_preset_button(int p_size, const Color &p_color) {
989
ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size, false));
990
SET_DRAG_FORWARDING_GCDU(btn_preset_new, ColorPicker);
991
btn_preset_new->set_button_group(preset_group);
992
preset_container->add_child(btn_preset_new);
993
btn_preset_new->set_pressed(true);
994
btn_preset_new->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_preset_input).bind(p_color));
995
}
996
997
void ColorPicker::_add_recent_preset_button(int p_size, const Color &p_color) {
998
ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size, true));
999
btn_preset_new->set_button_group(recent_preset_group);
1000
recent_preset_hbc->add_child(btn_preset_new);
1001
recent_preset_hbc->move_child(btn_preset_new, 0);
1002
btn_preset_new->set_pressed(true);
1003
btn_preset_new->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_recent_preset_pressed).bind(btn_preset_new));
1004
}
1005
1006
void ColorPicker::_load_palette() {
1007
List<String> extensions;
1008
ResourceLoader::get_recognized_extensions_for_type("ColorPalette", &extensions);
1009
1010
file_dialog->set_title(ETR("Load Color Palette"));
1011
file_dialog->clear_filters();
1012
for (const String &K : extensions) {
1013
file_dialog->add_filter("*." + K);
1014
}
1015
1016
file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
1017
file_dialog->set_current_file("");
1018
file_dialog->popup_centered_ratio();
1019
}
1020
1021
void ColorPicker::_save_palette(bool p_is_save_as) {
1022
if (!p_is_save_as && !palette_path.is_empty()) {
1023
file_dialog->set_file_mode(FileDialog::FILE_MODE_SAVE_FILE);
1024
_palette_file_selected(palette_path);
1025
return;
1026
} else {
1027
List<String> extensions;
1028
ResourceLoader::get_recognized_extensions_for_type("ColorPalette", &extensions);
1029
1030
file_dialog->set_title(ETR("Save Color Palette"));
1031
file_dialog->clear_filters();
1032
for (const String &K : extensions) {
1033
file_dialog->add_filter("*." + K);
1034
}
1035
1036
file_dialog->set_file_mode(FileDialog::FILE_MODE_SAVE_FILE);
1037
file_dialog->set_current_file("new_palette.tres");
1038
file_dialog->popup_centered_ratio();
1039
}
1040
}
1041
1042
#ifdef TOOLS_ENABLED
1043
void ColorPicker::_quick_open_palette_file_selected(const String &p_path) {
1044
_ensure_file_dialog();
1045
file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
1046
_palette_file_selected(p_path);
1047
}
1048
1049
GridContainer *ColorPicker::get_slider_container() {
1050
return slider_gc;
1051
}
1052
1053
#endif // ifdef TOOLS_ENABLED
1054
1055
void ColorPicker::_palette_file_selected(const String &p_path) {
1056
switch (file_dialog->get_file_mode()) {
1057
case FileDialog::FileMode::FILE_MODE_OPEN_FILE: {
1058
Ref<ColorPalette> palette = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
1059
ERR_FAIL_COND_MSG(palette.is_null(), vformat("Cannot open color palette file for reading at: %s", p_path));
1060
preset_cache.clear();
1061
presets.clear();
1062
1063
PackedColorArray saved_presets = palette->get_colors();
1064
for (const Color &saved_preset : saved_presets) {
1065
preset_cache.push_back(saved_preset);
1066
presets.push_back(saved_preset);
1067
}
1068
1069
#ifdef TOOLS_ENABLED
1070
if (editor_settings) {
1071
const StringName set_project_metadata = SNAME("set_project_metadata");
1072
editor_settings->call(set_project_metadata, "color_picker", "presets", saved_presets);
1073
editor_settings->call(set_project_metadata, "color_picker", "palette_edited", false);
1074
}
1075
#endif
1076
} break;
1077
case FileDialog::FileMode::FILE_MODE_SAVE_FILE: {
1078
ColorPalette *palette = memnew(ColorPalette);
1079
palette->set_colors(get_presets());
1080
Error error = ResourceSaver::save(palette, p_path);
1081
ERR_FAIL_COND_MSG(error != Error::OK, vformat("Cannot open color palette file for writing at: %s", p_path));
1082
#ifdef TOOLS_ENABLED
1083
if (palette_saved_callback.is_valid()) {
1084
palette_saved_callback.call_deferred(p_path);
1085
}
1086
#endif // TOOLS_ENABLED
1087
} break;
1088
default:
1089
break;
1090
}
1091
1092
palette_name->set_text(p_path.get_file().get_basename());
1093
palette_name->set_tooltip_text("");
1094
palette_name->show();
1095
palette_path = p_path;
1096
btn_preset->set_pressed(true);
1097
#ifdef TOOLS_ENABLED
1098
if (editor_settings) {
1099
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_name", palette_name->get_text());
1100
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_path", palette_path);
1101
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_edited", false);
1102
}
1103
#endif
1104
_update_presets();
1105
}
1106
1107
void ColorPicker::_show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container) {
1108
if (p_is_btn_pressed) {
1109
p_preset_container->show();
1110
} else {
1111
p_preset_container->hide();
1112
}
1113
_update_drop_down_arrow(p_is_btn_pressed, p_btn_preset);
1114
1115
palette_name->hide();
1116
if (btn_preset->is_pressed() && !palette_name->get_text().is_empty()) {
1117
palette_name->show();
1118
}
1119
}
1120
1121
void ColorPicker::_update_drop_down_arrow(const bool &p_is_btn_pressed, Button *p_btn_preset) {
1122
if (p_is_btn_pressed) {
1123
p_btn_preset->set_button_icon(theme_cache.expanded_arrow);
1124
} else {
1125
p_btn_preset->set_button_icon(theme_cache.folded_arrow);
1126
}
1127
}
1128
1129
void ColorPicker::_set_mode_popup_value(ColorModeType p_mode) {
1130
ERR_FAIL_INDEX(p_mode, MODE_MAX + 1);
1131
1132
if (p_mode == MODE_MAX) {
1133
set_colorize_sliders(!colorize_sliders);
1134
} else {
1135
set_color_mode(p_mode);
1136
}
1137
}
1138
1139
Variant ColorPicker::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) {
1140
ColorPresetButton *dragged_preset_button = Object::cast_to<ColorPresetButton>(p_from_control);
1141
1142
if (!dragged_preset_button) {
1143
return Variant();
1144
}
1145
1146
ColorPresetButton *drag_preview = memnew(ColorPresetButton(dragged_preset_button->get_preset_color(), _get_preset_size(), false));
1147
set_drag_preview(drag_preview);
1148
1149
Dictionary drag_data;
1150
drag_data["type"] = "color_preset";
1151
drag_data["color_preset"] = dragged_preset_button->get_index();
1152
1153
return drag_data;
1154
}
1155
1156
bool ColorPicker::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const {
1157
Dictionary d = p_data;
1158
if (!d.has("type") || String(d["type"]) != "color_preset") {
1159
return false;
1160
}
1161
return true;
1162
}
1163
1164
void ColorPicker::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) {
1165
Dictionary d = p_data;
1166
if (!d.has("type")) {
1167
return;
1168
}
1169
1170
if (String(d["type"]) == "color_preset") {
1171
int preset_from_id = d["color_preset"];
1172
int hover_now = p_from_control->get_index();
1173
1174
if (preset_from_id == hover_now || hover_now == -1) {
1175
return;
1176
}
1177
preset_container->move_child(preset_container->get_child(preset_from_id), hover_now);
1178
}
1179
}
1180
1181
void ColorPicker::_ensure_file_dialog() {
1182
if (file_dialog) {
1183
return;
1184
}
1185
1186
file_dialog = memnew(FileDialog);
1187
file_dialog->set_mode_overrides_title(false);
1188
file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
1189
file_dialog->set_current_dir(Engine::get_singleton()->is_editor_hint() ? "res://" : "user://");
1190
add_child(file_dialog, false, INTERNAL_MODE_FRONT);
1191
file_dialog->connect("file_selected", callable_mp(this, &ColorPicker::_palette_file_selected));
1192
}
1193
1194
void ColorPicker::add_preset(const Color &p_color) {
1195
List<Color>::Element *e = presets.find(p_color);
1196
if (e) {
1197
presets.move_to_back(e);
1198
1199
preset_container->move_child(preset_group->get_pressed_button(), preset_container->get_child_count() - 1);
1200
} else {
1201
presets.push_back(p_color);
1202
1203
_add_preset_button(_get_preset_size(), p_color);
1204
}
1205
1206
List<Color>::Element *cache_e = preset_cache.find(p_color);
1207
if (cache_e) {
1208
preset_cache.move_to_back(cache_e);
1209
} else {
1210
preset_cache.push_back(p_color);
1211
}
1212
1213
if (!palette_name->get_text().is_empty()) {
1214
palette_name->set_text(vformat("%s*", palette_name->get_text().trim_suffix("*")));
1215
palette_name->set_tooltip_text(ETR("The changes to this palette have not been saved to a file."));
1216
}
1217
1218
#ifdef TOOLS_ENABLED
1219
if (editor_settings) {
1220
PackedColorArray arr_to_save = get_presets();
1221
const StringName set_project_metadata = SNAME("set_project_metadata");
1222
editor_settings->call(set_project_metadata, "color_picker", "presets", arr_to_save);
1223
editor_settings->call(set_project_metadata, "color_picker", "palette_edited", true);
1224
}
1225
#endif
1226
}
1227
1228
void ColorPicker::add_recent_preset(const Color &p_color) {
1229
if (!_select_from_recent_preset_hbc(p_color)) {
1230
if (recent_preset_hbc->get_child_count() >= PRESET_COLUMN_COUNT) {
1231
recent_preset_cache.pop_front();
1232
recent_presets.pop_front();
1233
recent_preset_hbc->get_child(PRESET_COLUMN_COUNT - 1)->queue_free();
1234
}
1235
recent_presets.push_back(p_color);
1236
recent_preset_cache.push_back(p_color);
1237
_add_recent_preset_button(_get_preset_size(), p_color);
1238
}
1239
_select_from_preset_container(p_color);
1240
1241
#ifdef TOOLS_ENABLED
1242
if (editor_settings) {
1243
PackedColorArray arr_to_save = get_recent_presets();
1244
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save);
1245
}
1246
#endif
1247
}
1248
1249
void ColorPicker::erase_preset(const Color &p_color) {
1250
List<Color>::Element *e = presets.find(p_color);
1251
if (e) {
1252
presets.erase(e);
1253
preset_cache.erase(preset_cache.find(p_color));
1254
1255
// Find preset button to remove.
1256
for (int i = 1; i < preset_container->get_child_count(); i++) {
1257
ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
1258
if (current_btn && p_color == current_btn->get_preset_color()) {
1259
current_btn->queue_free();
1260
// Removing focused control loose the focus totally. We focus on previous button to keep it possible to navigate with keyboard/joypad.
1261
Control *focus_target = Object::cast_to<Control>(preset_container->get_child(i - 1));
1262
focus_target->grab_focus();
1263
break;
1264
}
1265
}
1266
1267
palette_name->set_text(vformat("%s*", palette_name->get_text().remove_char('*')));
1268
palette_name->set_tooltip_text(ETR("The changes to this palette have not been saved to a file."));
1269
if (presets.is_empty()) {
1270
palette_name->set_text("");
1271
palette_path = String();
1272
palette_name->hide();
1273
}
1274
1275
#ifdef TOOLS_ENABLED
1276
if (editor_settings) {
1277
PackedColorArray arr_to_save = get_presets();
1278
const StringName set_project_metadata = SNAME("set_project_metadata");
1279
editor_settings->call(set_project_metadata, "color_picker", "presets", arr_to_save);
1280
editor_settings->call(set_project_metadata, "color_picker", "palette_edited", true);
1281
editor_settings->call(set_project_metadata, "color_picker", "palette_name", palette_name->get_text());
1282
editor_settings->call(set_project_metadata, "color_picker", "palette_path", palette_path);
1283
}
1284
#endif
1285
}
1286
}
1287
1288
void ColorPicker::erase_recent_preset(const Color &p_color) {
1289
List<Color>::Element *e = recent_presets.find(p_color);
1290
if (e) {
1291
recent_presets.erase(e);
1292
recent_preset_cache.erase(recent_preset_cache.find(p_color));
1293
1294
// Find recent preset button to remove.
1295
for (int i = 1; i < recent_preset_hbc->get_child_count(); i++) {
1296
ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i));
1297
if (current_btn && p_color == current_btn->get_preset_color()) {
1298
current_btn->queue_free();
1299
break;
1300
}
1301
}
1302
1303
#ifdef TOOLS_ENABLED
1304
if (editor_settings) {
1305
PackedColorArray arr_to_save = get_recent_presets();
1306
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save);
1307
}
1308
#endif
1309
}
1310
}
1311
1312
PackedColorArray ColorPicker::get_presets() const {
1313
PackedColorArray arr;
1314
arr.resize(presets.size());
1315
int i = 0;
1316
for (List<Color>::ConstIterator itr = presets.begin(); itr != presets.end(); ++itr, ++i) {
1317
arr.set(i, *itr);
1318
}
1319
return arr;
1320
}
1321
1322
PackedColorArray ColorPicker::get_recent_presets() const {
1323
PackedColorArray arr;
1324
arr.resize(recent_presets.size());
1325
int i = 0;
1326
for (List<Color>::ConstIterator itr = recent_presets.begin(); itr != recent_presets.end(); ++itr, ++i) {
1327
arr.set(i, *itr);
1328
}
1329
return arr;
1330
}
1331
1332
void ColorPicker::set_color_mode(ColorModeType p_mode) {
1333
ERR_FAIL_INDEX(p_mode, MODE_MAX);
1334
1335
if (current_mode == p_mode) {
1336
return;
1337
}
1338
1339
mode_popup->set_item_checked(current_mode, false);
1340
mode_popup->set_item_checked(p_mode, true);
1341
1342
if (p_mode < MODE_BUTTON_COUNT) {
1343
mode_btns[p_mode]->set_pressed(true);
1344
} else if (current_mode < MODE_BUTTON_COUNT) {
1345
mode_btns[current_mode]->set_pressed(false);
1346
}
1347
1348
current_mode = p_mode;
1349
1350
#ifdef TOOLS_ENABLED
1351
if (editor_settings) {
1352
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "color_mode", current_mode);
1353
}
1354
#endif
1355
1356
if (!is_inside_tree()) {
1357
return;
1358
}
1359
1360
_update_controls();
1361
_update_color();
1362
}
1363
1364
ColorPicker::ColorModeType ColorPicker::get_color_mode() const {
1365
return current_mode;
1366
}
1367
1368
void ColorPicker::set_colorize_sliders(bool p_colorize_sliders) {
1369
if (colorize_sliders == p_colorize_sliders) {
1370
return;
1371
}
1372
1373
colorize_sliders = p_colorize_sliders;
1374
mode_popup->set_item_checked(MODE_MAX + 1, colorize_sliders);
1375
1376
if (colorize_sliders) {
1377
Ref<StyleBoxEmpty> style_box_empty(memnew(StyleBoxEmpty));
1378
1379
for (int i = 0; i < MODE_SLIDER_COUNT; i++) {
1380
sliders[i]->add_theme_style_override("slider", style_box_empty);
1381
}
1382
1383
alpha_slider->add_theme_style_override("slider", style_box_empty);
1384
} else {
1385
Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
1386
style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale);
1387
style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp());
1388
1389
for (int i = 0; i < MODE_SLIDER_COUNT; i++) {
1390
sliders[i]->add_theme_style_override("slider", style_box_flat);
1391
}
1392
1393
alpha_slider->add_theme_style_override("slider", style_box_flat);
1394
}
1395
}
1396
1397
bool ColorPicker::is_colorizing_sliders() const {
1398
return colorize_sliders;
1399
}
1400
1401
void ColorPicker::set_deferred_mode(bool p_enabled) {
1402
deferred_mode_enabled = p_enabled;
1403
}
1404
1405
bool ColorPicker::is_deferred_mode() const {
1406
return deferred_mode_enabled;
1407
}
1408
1409
void ColorPicker::_update_text_value() {
1410
if (text_is_constructor || !is_color_valid_hex(color)) {
1411
String t = "Color" + color_to_string(color, edit_alpha && color.a < 1, true);
1412
1413
text_type->set_text("");
1414
text_type->set_button_icon(theme_cache.color_script);
1415
text_type->set_disabled(!is_color_valid_hex(color));
1416
hex_label->set_text(ETR("Expr"));
1417
c_text->set_text(t);
1418
} else {
1419
text_type->set_text("#");
1420
text_type->set_button_icon(nullptr);
1421
text_type->set_disabled(false);
1422
hex_label->set_text(ETR("Hex"));
1423
c_text->set_text(color.to_html(edit_alpha && color.a < 1));
1424
}
1425
}
1426
1427
void ColorPicker::_sample_input(const Ref<InputEvent> &p_event) {
1428
if (!display_old_color) {
1429
return;
1430
}
1431
1432
const Ref<InputEventMouseButton> mb = p_event;
1433
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1434
const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
1435
if (rect_old.has_point(mb->get_position())) {
1436
// Revert to the old color when left-clicking the old color sample.
1437
set_pick_color(old_color);
1438
1439
sample->set_focus_mode(FOCUS_NONE);
1440
emit_signal(SNAME("color_changed"), color);
1441
}
1442
}
1443
1444
if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
1445
set_pick_color(old_color);
1446
emit_signal(SNAME("color_changed"), color);
1447
}
1448
}
1449
1450
void ColorPicker::_sample_draw() {
1451
// Covers the right half of the sample if the old color is being displayed,
1452
// or the whole sample if it's not being displayed.
1453
Rect2 rect_new;
1454
Rect2 rect_old;
1455
1456
if (display_old_color) {
1457
rect_new = Rect2(Point2(sample->get_size().width * 0.5, 0), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
1458
1459
// Draw both old and new colors for easier comparison (only if spawned from a ColorPickerButton).
1460
rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
1461
1462
if (old_color.a < 1.0) {
1463
sample->draw_texture_rect(theme_cache.sample_bg, rect_old, true);
1464
}
1465
1466
sample->draw_rect(rect_old, old_color);
1467
1468
if (!old_color.is_equal_approx(color)) {
1469
// Draw a revert indicator to indicate that the old sample can be clicked to revert to this old color.
1470
// Adapt icon color to the background color (taking alpha checkerboard into account) so that it's always visible.
1471
sample->draw_texture(theme_cache.sample_revert,
1472
rect_old.size * 0.5 - theme_cache.sample_revert->get_size() * 0.5,
1473
Math::lerp(0.75f, old_color.get_luminance(), old_color.a) < 0.455 ? Color(1, 1, 1) : (Color(0.01, 0.01, 0.01)));
1474
1475
sample->set_focus_mode(FOCUS_ALL);
1476
} else {
1477
sample->set_focus_mode(FOCUS_NONE);
1478
}
1479
1480
if (is_color_overbright(color)) {
1481
// Draw an indicator to denote that the old color is "overbright" and can't be displayed accurately in the preview.
1482
sample->draw_texture(theme_cache.overbright_indicator, Point2());
1483
}
1484
} else {
1485
rect_new = Rect2(Point2(), Size2(sample->get_size().width, sample->get_size().height * 0.95));
1486
}
1487
1488
if (color.a < 1.0) {
1489
sample->draw_texture_rect(theme_cache.sample_bg, rect_new, true);
1490
}
1491
1492
sample->draw_rect(rect_new, color);
1493
1494
if (display_old_color && !old_color.is_equal_approx(color) && sample->has_focus()) {
1495
RID ci = sample->get_canvas_item();
1496
theme_cache.sample_focus->draw(ci, rect_old);
1497
}
1498
1499
if (is_color_overbright(color)) {
1500
// Draw an indicator to denote that the new color is "overbright" and can't be displayed accurately in the preview.
1501
sample->draw_texture(theme_cache.overbright_indicator, Point2(sample->get_size().width * 0.5, 0));
1502
}
1503
}
1504
1505
void ColorPicker::_slider_draw(int p_which) {
1506
if (colorize_sliders) {
1507
modes[current_mode]->slider_draw(p_which);
1508
}
1509
}
1510
1511
void ColorPicker::_alpha_slider_draw() {
1512
if (!colorize_sliders) {
1513
return;
1514
}
1515
Vector<Vector2> pos;
1516
pos.resize(4);
1517
Vector<Color> col;
1518
col.resize(4);
1519
Size2 size = alpha_slider->get_size();
1520
Color left_color;
1521
Color right_color;
1522
const real_t margin = 16 * theme_cache.base_scale;
1523
alpha_slider->draw_texture_rect(theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
1524
1525
left_color = color_normalized;
1526
left_color.a = 0;
1527
right_color = color_normalized;
1528
right_color.a = 1;
1529
1530
col.set(0, left_color);
1531
col.set(1, right_color);
1532
col.set(2, right_color);
1533
col.set(3, left_color);
1534
pos.set(0, Vector2(0, 0));
1535
pos.set(1, Vector2(size.x, 0));
1536
pos.set(2, Vector2(size.x, margin));
1537
pos.set(3, Vector2(0, margin));
1538
1539
alpha_slider->draw_polygon(pos, col);
1540
}
1541
1542
void ColorPicker::_slider_or_spin_input(const Ref<InputEvent> &p_event) {
1543
if (line_edit_mouse_release) {
1544
line_edit_mouse_release = false;
1545
return;
1546
}
1547
Ref<InputEventMouseButton> bev = p_event;
1548
if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
1549
add_recent_preset(color);
1550
}
1551
}
1552
1553
void ColorPicker::_line_edit_input(const Ref<InputEvent> &p_event) {
1554
Ref<InputEventMouseButton> bev = p_event;
1555
if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
1556
line_edit_mouse_release = true;
1557
}
1558
}
1559
1560
void ColorPicker::_preset_input(const Ref<InputEvent> &p_event, const Color &p_color) {
1561
Ref<InputEventMouseButton> bev = p_event;
1562
1563
if (bev.is_valid()) {
1564
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
1565
set_pick_color(p_color);
1566
add_recent_preset(color);
1567
emit_signal(SNAME("color_changed"), p_color);
1568
} else if (bev->is_pressed() && bev->get_button_index() == MouseButton::RIGHT && can_add_swatches) {
1569
erase_preset(p_color);
1570
emit_signal(SNAME("preset_removed"), p_color);
1571
}
1572
}
1573
1574
if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
1575
set_pick_color(p_color);
1576
add_recent_preset(color);
1577
emit_signal(SNAME("color_changed"), p_color);
1578
} else if (p_event->is_action_pressed(SNAME("ui_colorpicker_delete_preset"), false, true) && can_add_swatches) {
1579
erase_preset(p_color);
1580
emit_signal(SNAME("preset_removed"), p_color);
1581
}
1582
}
1583
1584
void ColorPicker::_recent_preset_pressed(const bool p_pressed, ColorPresetButton *p_preset) {
1585
if (!p_pressed) {
1586
return;
1587
}
1588
1589
// Avoid applying and recalculating the intensity for non-overbright color if it doesn't change.
1590
if (color != p_preset->get_preset_color()) {
1591
set_pick_color(p_preset->get_preset_color());
1592
}
1593
1594
recent_presets.move_to_back(recent_presets.find(p_preset->get_preset_color()));
1595
List<Color>::Element *e = recent_preset_cache.find(p_preset->get_preset_color());
1596
if (e) {
1597
recent_preset_cache.move_to_back(e);
1598
}
1599
1600
recent_preset_hbc->move_child(p_preset, 0);
1601
emit_signal(SNAME("color_changed"), p_preset->get_preset_color());
1602
}
1603
1604
void ColorPicker::_text_changed(const String &) {
1605
text_changed = true;
1606
}
1607
1608
void ColorPicker::_add_preset_pressed() {
1609
add_preset(color);
1610
emit_signal(SNAME("preset_added"), color);
1611
}
1612
1613
void ColorPicker::_pick_button_pressed_native() {
1614
if (!DisplayServer::get_singleton()->color_picker(callable_mp(this, &ColorPicker::_native_cb))) {
1615
// Fallback to default/legacy picker.
1616
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_CAPTURE) && !get_tree()->get_root()->is_embedding_subwindows()) {
1617
_pick_button_pressed();
1618
} else {
1619
_pick_button_pressed_legacy();
1620
}
1621
}
1622
}
1623
1624
void ColorPicker::_native_cb(bool p_status, const Color &p_color) {
1625
if (p_status) {
1626
set_pick_color(p_color);
1627
if (!deferred_mode_enabled) {
1628
emit_signal(SNAME("color_changed"), color);
1629
}
1630
}
1631
}
1632
1633
void ColorPicker::_pick_button_pressed() {
1634
is_picking_color = true;
1635
pre_picking_color = color;
1636
1637
if (!picker_window) {
1638
picker_window = memnew(Popup);
1639
bool has_feature_exclude_from_capture = DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE);
1640
if (!has_feature_exclude_from_capture) {
1641
picker_window->set_size(Vector2i(28, 28));
1642
} else {
1643
picker_window->set_size(Vector2i(55, 72));
1644
picker_window->set_flag(Window::FLAG_EXCLUDE_FROM_CAPTURE, true); // Only supported on MacOS and Windows.
1645
}
1646
picker_window->connect(SceneStringName(visibility_changed), callable_mp(this, &ColorPicker::_pick_finished));
1647
picker_window->connect(SceneStringName(window_input), callable_mp(this, &ColorPicker::_target_gui_input));
1648
1649
picker_preview = memnew(Panel);
1650
picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE);
1651
picker_preview->set_size(Vector2i(55, 72));
1652
picker_window->add_child(picker_preview);
1653
1654
picker_preview_color = memnew(Panel);
1655
picker_preview_color->set_mouse_filter(MOUSE_FILTER_IGNORE);
1656
if (!has_feature_exclude_from_capture) {
1657
picker_preview_color->set_size(Vector2i(24, 24));
1658
picker_preview_color->set_position(Vector2i(2, 2));
1659
} else {
1660
picker_preview_color->set_size(Vector2i(51, 15));
1661
picker_preview_color->set_position(Vector2i(2, 55));
1662
}
1663
picker_preview->add_child(picker_preview_color);
1664
1665
if (has_feature_exclude_from_capture) {
1666
picker_texture_zoom = memnew(TextureRect);
1667
picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE);
1668
picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51));
1669
picker_texture_zoom->set_position(Vector2i(2, 2));
1670
picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
1671
picker_preview->add_child(picker_texture_zoom);
1672
}
1673
1674
picker_preview_style_box.instantiate();
1675
picker_preview->add_theme_style_override(SceneStringName(panel), picker_preview_style_box);
1676
1677
picker_preview_style_box_color.instantiate();
1678
picker_preview_color->add_theme_style_override(SceneStringName(panel), picker_preview_style_box_color);
1679
1680
add_child(picker_window, false, INTERNAL_MODE_FRONT);
1681
}
1682
set_process_internal(true);
1683
1684
picker_window->popup();
1685
}
1686
1687
void ColorPicker::_target_gui_input(const Ref<InputEvent> &p_event) {
1688
const Ref<InputEventMouseButton> mouse_event = p_event;
1689
if (mouse_event.is_null()) {
1690
return;
1691
}
1692
if (mouse_event->get_button_index() == MouseButton::LEFT) {
1693
if (mouse_event->is_pressed()) {
1694
picker_window->hide();
1695
_pick_finished();
1696
}
1697
} else if (mouse_event->get_button_index() == MouseButton::RIGHT) {
1698
set_pick_color(pre_picking_color); // Cancel.
1699
is_picking_color = false;
1700
set_process_internal(false);
1701
picker_window->hide();
1702
} else {
1703
Window *w = picker_window->get_parent_visible_window();
1704
while (w) {
1705
Point2i win_mpos = w->get_mouse_position(); // Mouse position local to the window.
1706
Size2i win_size = w->get_size();
1707
if (win_mpos.x >= 0 && win_mpos.y >= 0 && win_mpos.x <= win_size.x && win_mpos.y <= win_size.y) {
1708
// Mouse event inside window bounds, forward this event to the window.
1709
Ref<InputEventMouseButton> new_ev = p_event->duplicate();
1710
new_ev->set_position(win_mpos);
1711
new_ev->set_global_position(win_mpos);
1712
w->push_input(new_ev, true);
1713
return;
1714
}
1715
w = w->get_parent_visible_window();
1716
}
1717
}
1718
}
1719
1720
void ColorPicker::_pick_finished() {
1721
if (picker_window->is_visible()) {
1722
return;
1723
}
1724
1725
if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
1726
set_pick_color(pre_picking_color);
1727
} else {
1728
emit_signal(SNAME("color_changed"), color);
1729
}
1730
is_picking_color = false;
1731
set_process_internal(false);
1732
picker_window->hide();
1733
}
1734
1735
void ColorPicker::_update_menu_items() {
1736
options_menu->clear();
1737
options_menu->reset_size();
1738
1739
options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), ETR("Save"), static_cast<int>(MenuOption::MENU_SAVE));
1740
options_menu->set_item_tooltip(-1, ETR("Save the current color palette to reuse later."));
1741
options_menu->set_item_disabled(-1, presets.is_empty());
1742
1743
options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), ETR("Save As"), static_cast<int>(MenuOption::MENU_SAVE_AS));
1744
options_menu->set_item_tooltip(-1, ETR("Save the current color palette as a new to reuse later."));
1745
options_menu->set_item_disabled(-1, palette_path.is_empty());
1746
1747
options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), ETR("Load"), static_cast<int>(MenuOption::MENU_LOAD));
1748
options_menu->set_item_tooltip(-1, ETR("Load existing color palette."));
1749
1750
#ifdef TOOLS_ENABLED
1751
if (Engine::get_singleton()->is_editor_hint()) {
1752
options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), TTRC("Quick Load"), static_cast<int>(MenuOption::MENU_QUICKLOAD));
1753
options_menu->set_item_tooltip(-1, TTRC("Load existing color palette."));
1754
}
1755
#endif // TOOLS_ENABLED
1756
1757
options_menu->add_icon_item(get_theme_icon(SNAME("clear"), SNAME("FileDialog")), ETR("Clear"), static_cast<int>(MenuOption::MENU_CLEAR));
1758
options_menu->set_item_tooltip(-1, ETR("Clear the currently loaded color palettes in the picker."));
1759
options_menu->set_item_disabled(-1, presets.is_empty());
1760
}
1761
1762
void ColorPicker::_options_menu_cbk(int p_which) {
1763
_ensure_file_dialog();
1764
1765
MenuOption option = static_cast<MenuOption>(p_which);
1766
switch (option) {
1767
case MenuOption::MENU_SAVE:
1768
_save_palette(false);
1769
break;
1770
case MenuOption::MENU_SAVE_AS:
1771
_save_palette(true);
1772
break;
1773
case MenuOption::MENU_LOAD:
1774
_load_palette();
1775
break;
1776
1777
#ifdef TOOLS_ENABLED
1778
case MenuOption::MENU_QUICKLOAD:
1779
if (quick_open_callback.is_valid()) {
1780
file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
1781
quick_open_callback.call_deferred();
1782
}
1783
break;
1784
#endif // TOOLS_ENABLED
1785
case MenuOption::MENU_CLEAR: {
1786
PackedColorArray colors = get_presets();
1787
for (Color c : colors) {
1788
erase_preset(c);
1789
}
1790
1791
palette_name->set_text("");
1792
palette_name->set_tooltip_text("");
1793
palette_path = String();
1794
btn_preset->set_pressed(false);
1795
1796
#ifdef TOOLS_ENABLED
1797
if (editor_settings) {
1798
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_name", palette_name->get_text());
1799
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_path", palette_path);
1800
editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_edited", false);
1801
}
1802
#endif // TOOLS_ENABLED
1803
1804
}
1805
1806
break;
1807
default:
1808
break;
1809
}
1810
}
1811
1812
void ColorPicker::_block_input_on_popup_show() {
1813
if (!get_tree()->get_root()->is_embedding_subwindows()) {
1814
get_viewport()->set_disable_input(true);
1815
}
1816
}
1817
1818
void ColorPicker::_enable_input_on_popup_hide() {
1819
if (!get_tree()->get_root()->is_embedding_subwindows()) {
1820
get_viewport()->set_disable_input(false);
1821
}
1822
}
1823
1824
void ColorPicker::_pick_button_pressed_legacy() {
1825
if (!is_inside_tree()) {
1826
return;
1827
}
1828
pre_picking_color = color;
1829
1830
if (!picker_window) {
1831
picker_window = memnew(Popup);
1832
picker_window->hide();
1833
picker_window->set_transient(true);
1834
add_child(picker_window, false, INTERNAL_MODE_FRONT);
1835
1836
picker_texture_rect = memnew(TextureRect);
1837
picker_texture_rect->set_anchors_preset(Control::PRESET_FULL_RECT);
1838
picker_texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
1839
picker_texture_rect->set_default_cursor_shape(Control::CURSOR_CROSS);
1840
picker_window->add_child(picker_texture_rect);
1841
picker_texture_rect->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_picker_texture_input));
1842
1843
picker_preview = memnew(Panel);
1844
picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE);
1845
picker_preview->set_size(Vector2i(55, 72));
1846
picker_window->add_child(picker_preview);
1847
1848
picker_preview_color = memnew(Panel);
1849
picker_preview_color->set_mouse_filter(MOUSE_FILTER_IGNORE);
1850
picker_preview_color->set_size(Vector2i(51, 15));
1851
picker_preview_color->set_position(Vector2i(2, 55));
1852
picker_preview->add_child(picker_preview_color);
1853
1854
picker_texture_zoom = memnew(TextureRect);
1855
picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE);
1856
picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51));
1857
picker_texture_zoom->set_position(Vector2i(2, 2));
1858
picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
1859
picker_preview->add_child(picker_texture_zoom);
1860
1861
picker_preview_style_box.instantiate();
1862
picker_preview->add_theme_style_override(SceneStringName(panel), picker_preview_style_box);
1863
1864
picker_preview_style_box_color.instantiate();
1865
picker_preview_color->add_theme_style_override(SceneStringName(panel), picker_preview_style_box_color);
1866
}
1867
1868
Rect2i screen_rect;
1869
if (picker_window->is_embedded()) {
1870
Ref<ImageTexture> tx = ImageTexture::create_from_image(picker_window->get_embedder()->get_texture()->get_image());
1871
screen_rect = picker_window->get_embedder()->get_visible_rect();
1872
picker_window->set_position(Point2i());
1873
picker_texture_rect->set_texture(tx);
1874
1875
Vector2 ofs = picker_window->get_mouse_position();
1876
picker_preview->set_position(ofs - Vector2(28, 28));
1877
1878
Vector2 scale = screen_rect.size / tx->get_image()->get_size();
1879
ofs /= scale;
1880
1881
Ref<AtlasTexture> atlas;
1882
atlas.instantiate();
1883
atlas->set_atlas(tx);
1884
atlas->set_region(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17));
1885
picker_texture_zoom->set_texture(atlas);
1886
} else {
1887
screen_rect = picker_window->get_parent_rect();
1888
picker_window->set_position(screen_rect.position);
1889
1890
Ref<Image> target_image = Image::create_empty(screen_rect.size.x, screen_rect.size.y, false, Image::FORMAT_RGB8);
1891
DisplayServer *ds = DisplayServer::get_singleton();
1892
1893
// Add the Texture of each Window to the Image.
1894
Vector<DisplayServer::WindowID> wl = ds->get_window_list();
1895
// FIXME: sort windows by visibility.
1896
for (const DisplayServer::WindowID &window_id : wl) {
1897
Window *w = Window::get_from_id(window_id);
1898
if (!w) {
1899
continue;
1900
}
1901
1902
Ref<Image> img = w->get_texture()->get_image();
1903
if (img.is_null() || img->is_empty()) {
1904
continue;
1905
}
1906
img->convert(Image::FORMAT_RGB8);
1907
target_image->blit_rect(img, Rect2i(Point2i(0, 0), img->get_size()), w->get_position());
1908
}
1909
1910
Ref<ImageTexture> tx = ImageTexture::create_from_image(target_image);
1911
picker_texture_rect->set_texture(tx);
1912
1913
Vector2 ofs = screen_rect.position - DisplayServer::get_singleton()->mouse_get_position();
1914
picker_preview->set_position(ofs - Vector2(28, 28));
1915
1916
Ref<AtlasTexture> atlas;
1917
atlas.instantiate();
1918
atlas->set_atlas(tx);
1919
atlas->set_region(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17));
1920
picker_texture_zoom->set_texture(atlas);
1921
}
1922
1923
picker_window->set_size(screen_rect.size);
1924
picker_window->popup();
1925
}
1926
1927
void ColorPicker::_picker_texture_input(const Ref<InputEvent> &p_event) {
1928
if (!is_inside_tree()) {
1929
return;
1930
}
1931
1932
Ref<InputEventMouseButton> bev = p_event;
1933
if (bev.is_valid() && bev->get_button_index() == MouseButton::LEFT && !bev->is_pressed()) {
1934
set_pick_color(picker_color);
1935
emit_signal(SNAME("color_changed"), color);
1936
picker_window->hide();
1937
}
1938
1939
Ref<InputEventMouseMotion> mev = p_event;
1940
if (mev.is_valid()) {
1941
Ref<Image> img = picker_texture_rect->get_texture()->get_image();
1942
if (img.is_valid() && !img->is_empty()) {
1943
Vector2 ofs = mev->get_position();
1944
picker_preview->set_position(ofs - Vector2(28, 28));
1945
Vector2 scale = picker_texture_rect->get_size() / img->get_size();
1946
ofs /= scale;
1947
picker_color = img->get_pixel(ofs.x, ofs.y);
1948
picker_preview_style_box_color->set_bg_color(picker_color);
1949
picker_preview_style_box->set_bg_color(picker_color.get_luminance() < 0.5 ? Color(1.0f, 1.0f, 1.0f) : Color(0.0f, 0.0f, 0.0f));
1950
1951
Ref<AtlasTexture> atlas = picker_texture_zoom->get_texture();
1952
if (atlas.is_valid()) {
1953
atlas->set_region(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17));
1954
}
1955
}
1956
}
1957
}
1958
1959
void ColorPicker::_html_focus_exit() {
1960
if (c_text->is_menu_visible()) {
1961
return;
1962
}
1963
1964
if (is_visible_in_tree()) {
1965
_html_submitted(c_text->get_text());
1966
} else {
1967
_update_text_value();
1968
}
1969
}
1970
1971
void ColorPicker::set_can_add_swatches(bool p_enabled) {
1972
if (can_add_swatches == p_enabled) {
1973
return;
1974
}
1975
can_add_swatches = p_enabled;
1976
if (!p_enabled) {
1977
btn_add_preset->set_disabled(true);
1978
btn_add_preset->set_focus_mode(FOCUS_NONE);
1979
} else {
1980
btn_add_preset->set_disabled(false);
1981
btn_add_preset->set_focus_mode(FOCUS_ALL);
1982
}
1983
}
1984
1985
bool ColorPicker::are_swatches_enabled() const {
1986
return can_add_swatches;
1987
}
1988
1989
void ColorPicker::set_presets_visible(bool p_visible) {
1990
if (presets_visible == p_visible) {
1991
return;
1992
}
1993
presets_visible = p_visible;
1994
swatches_vbc->set_visible(p_visible);
1995
}
1996
1997
bool ColorPicker::are_presets_visible() const {
1998
return presets_visible;
1999
}
2000
2001
void ColorPicker::set_modes_visible(bool p_visible) {
2002
if (color_modes_visible == p_visible) {
2003
return;
2004
}
2005
color_modes_visible = p_visible;
2006
mode_hbc->set_visible(p_visible);
2007
}
2008
2009
bool ColorPicker::are_modes_visible() const {
2010
return color_modes_visible;
2011
}
2012
2013
void ColorPicker::set_sampler_visible(bool p_visible) {
2014
if (sampler_visible == p_visible) {
2015
return;
2016
}
2017
sampler_visible = p_visible;
2018
sample_hbc->set_visible(p_visible);
2019
}
2020
2021
bool ColorPicker::is_sampler_visible() const {
2022
return sampler_visible;
2023
}
2024
2025
void ColorPicker::set_sliders_visible(bool p_visible) {
2026
if (sliders_visible == p_visible) {
2027
return;
2028
}
2029
sliders_visible = p_visible;
2030
slider_gc->set_visible(p_visible);
2031
}
2032
2033
bool ColorPicker::are_sliders_visible() const {
2034
return sliders_visible;
2035
}
2036
2037
void ColorPicker::set_hex_visible(bool p_visible) {
2038
if (hex_visible == p_visible) {
2039
return;
2040
}
2041
hex_visible = p_visible;
2042
hex_hbc->set_visible(p_visible);
2043
}
2044
2045
bool ColorPicker::is_hex_visible() const {
2046
return hex_visible;
2047
}
2048
2049
void ColorPicker::_bind_methods() {
2050
ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
2051
ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
2052
ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode);
2053
ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode);
2054
ClassDB::bind_method(D_METHOD("set_color_mode", "color_mode"), &ColorPicker::set_color_mode);
2055
ClassDB::bind_method(D_METHOD("get_color_mode"), &ColorPicker::get_color_mode);
2056
ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
2057
ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
2058
ClassDB::bind_method(D_METHOD("set_edit_intensity", "show"), &ColorPicker::set_edit_intensity);
2059
ClassDB::bind_method(D_METHOD("is_editing_intensity"), &ColorPicker::is_editing_intensity);
2060
ClassDB::bind_method(D_METHOD("set_can_add_swatches", "enabled"), &ColorPicker::set_can_add_swatches);
2061
ClassDB::bind_method(D_METHOD("are_swatches_enabled"), &ColorPicker::are_swatches_enabled);
2062
ClassDB::bind_method(D_METHOD("set_presets_visible", "visible"), &ColorPicker::set_presets_visible);
2063
ClassDB::bind_method(D_METHOD("are_presets_visible"), &ColorPicker::are_presets_visible);
2064
ClassDB::bind_method(D_METHOD("set_modes_visible", "visible"), &ColorPicker::set_modes_visible);
2065
ClassDB::bind_method(D_METHOD("are_modes_visible"), &ColorPicker::are_modes_visible);
2066
ClassDB::bind_method(D_METHOD("set_sampler_visible", "visible"), &ColorPicker::set_sampler_visible);
2067
ClassDB::bind_method(D_METHOD("is_sampler_visible"), &ColorPicker::is_sampler_visible);
2068
ClassDB::bind_method(D_METHOD("set_sliders_visible", "visible"), &ColorPicker::set_sliders_visible);
2069
ClassDB::bind_method(D_METHOD("are_sliders_visible"), &ColorPicker::are_sliders_visible);
2070
ClassDB::bind_method(D_METHOD("set_hex_visible", "visible"), &ColorPicker::set_hex_visible);
2071
ClassDB::bind_method(D_METHOD("is_hex_visible"), &ColorPicker::is_hex_visible);
2072
ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
2073
ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
2074
ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
2075
ClassDB::bind_method(D_METHOD("add_recent_preset", "color"), &ColorPicker::add_recent_preset);
2076
ClassDB::bind_method(D_METHOD("erase_recent_preset", "color"), &ColorPicker::erase_recent_preset);
2077
ClassDB::bind_method(D_METHOD("get_recent_presets"), &ColorPicker::get_recent_presets);
2078
ClassDB::bind_method(D_METHOD("set_picker_shape", "shape"), &ColorPicker::set_picker_shape);
2079
ClassDB::bind_method(D_METHOD("get_picker_shape"), &ColorPicker::get_picker_shape);
2080
2081
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
2082
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
2083
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_intensity"), "set_edit_intensity", "is_editing_intensity");
2084
ADD_PROPERTY(PropertyInfo(Variant::INT, "color_mode", PROPERTY_HINT_ENUM, "RGB,HSV,LINEAR,OKHSL"), "set_color_mode", "get_color_mode");
2085
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
2086
ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle,OKHSL Circle,OK HS Rectangle:5,OK HL Rectangle,None:4"), "set_picker_shape", "get_picker_shape");
2087
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_add_swatches"), "set_can_add_swatches", "are_swatches_enabled");
2088
ADD_GROUP("Customization", "");
2089
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sampler_visible"), "set_sampler_visible", "is_sampler_visible");
2090
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "color_modes_visible"), "set_modes_visible", "are_modes_visible");
2091
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sliders_visible"), "set_sliders_visible", "are_sliders_visible");
2092
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hex_visible"), "set_hex_visible", "is_hex_visible");
2093
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible");
2094
2095
ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
2096
ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
2097
ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
2098
2099
BIND_ENUM_CONSTANT(MODE_RGB);
2100
BIND_ENUM_CONSTANT(MODE_HSV);
2101
#ifndef DISABLE_DEPRECATED
2102
BIND_ENUM_CONSTANT(MODE_RAW);
2103
#endif
2104
BIND_ENUM_CONSTANT(MODE_LINEAR);
2105
BIND_ENUM_CONSTANT(MODE_OKHSL);
2106
2107
BIND_ENUM_CONSTANT(SHAPE_HSV_RECTANGLE);
2108
BIND_ENUM_CONSTANT(SHAPE_HSV_WHEEL);
2109
BIND_ENUM_CONSTANT(SHAPE_VHS_CIRCLE);
2110
BIND_ENUM_CONSTANT(SHAPE_OKHSL_CIRCLE);
2111
BIND_ENUM_CONSTANT(SHAPE_NONE);
2112
BIND_ENUM_CONSTANT(SHAPE_OK_HS_RECTANGLE);
2113
BIND_ENUM_CONSTANT(SHAPE_OK_HL_RECTANGLE);
2114
2115
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ColorPicker, content_margin, "margin");
2116
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, label_width);
2117
2118
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, sv_width);
2119
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, sv_height);
2120
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, h_width);
2121
2122
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, center_slider_grabbers);
2123
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, ColorPicker, sample_focus);
2124
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, ColorPicker, picker_focus_rectangle);
2125
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, ColorPicker, picker_focus_circle);
2126
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ColorPicker, focused_not_editing_cursor_color);
2127
2128
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, menu_option);
2129
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, screen_picker);
2130
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, expanded_arrow);
2131
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, folded_arrow);
2132
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, add_preset);
2133
2134
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_rect);
2135
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_rect_wheel);
2136
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_circle);
2137
2138
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, bar_arrow);
2139
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_bg);
2140
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_revert);
2141
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, overbright_indicator);
2142
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor);
2143
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor_bg);
2144
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_hue);
2145
2146
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_script);
2147
2148
BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_normal, "tab_unselected", "TabContainer");
2149
BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_pressed, "tab_selected", "TabContainer");
2150
BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_hover, "tab_selected", "TabContainer");
2151
2152
ADD_CLASS_DEPENDENCY("LineEdit");
2153
ADD_CLASS_DEPENDENCY("MenuButton");
2154
ADD_CLASS_DEPENDENCY("PopupMenu");
2155
}
2156
2157
ColorPicker::ColorPicker() {
2158
internal_margin = memnew(MarginContainer);
2159
add_child(internal_margin, false, INTERNAL_MODE_FRONT);
2160
2161
VBoxContainer *real_vbox = memnew(VBoxContainer);
2162
internal_margin->add_child(real_vbox);
2163
2164
shape_container = memnew(HBoxContainer);
2165
shape_container->set_v_size_flags(SIZE_SHRINK_BEGIN);
2166
real_vbox->add_child(shape_container);
2167
2168
sample_hbc = memnew(HBoxContainer);
2169
real_vbox->add_child(sample_hbc);
2170
2171
btn_pick = memnew(Button);
2172
btn_pick->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
2173
btn_pick->set_accessibility_name(ETR("Pick"));
2174
sample_hbc->add_child(btn_pick);
2175
2176
sample = memnew(TextureRect);
2177
sample_hbc->add_child(sample);
2178
sample->set_h_size_flags(SIZE_EXPAND_FILL);
2179
sample->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_sample_input));
2180
sample->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_sample_draw));
2181
2182
btn_shape = memnew(MenuButton);
2183
btn_shape->set_flat(false);
2184
sample_hbc->add_child(btn_shape);
2185
btn_shape->set_toggle_mode(true);
2186
btn_shape->set_tooltip_text(ETR("Select a picker shape."));
2187
btn_shape->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
2188
btn_shape->set_focus_mode(FOCUS_ALL);
2189
2190
add_shape(memnew(ColorPickerShapeRectangle(this)));
2191
add_shape(memnew(ColorPickerShapeWheel(this)));
2192
add_shape(memnew(ColorPickerShapeVHSCircle(this)));
2193
add_shape(memnew(ColorPickerShapeOKHSLCircle(this)));
2194
add_shape(memnew(ColorPickerShapeOKHSRectangle(this)));
2195
add_shape(memnew(ColorPickerShapeOKHLRectangle(this)));
2196
2197
shape_popup = btn_shape->get_popup();
2198
{
2199
int i = 0;
2200
for (const ColorPickerShape *shape : shapes) {
2201
shape_popup->add_radio_check_item(shape->get_name(), index_to_shape(i));
2202
i++;
2203
}
2204
}
2205
shape_popup->set_item_checked(get_current_shape_index(), true);
2206
shape_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::set_picker_shape));
2207
shape_popup->connect("about_to_popup", callable_mp(this, &ColorPicker::_block_input_on_popup_show));
2208
shape_popup->connect(SNAME("popup_hide"), callable_mp(this, &ColorPicker::_enable_input_on_popup_hide));
2209
2210
add_mode(memnew(ColorModeRGB(this)));
2211
add_mode(memnew(ColorModeHSV(this)));
2212
add_mode(memnew(ColorModeLinear(this)));
2213
add_mode(memnew(ColorModeOKHSL(this)));
2214
2215
mode_hbc = memnew(HBoxContainer);
2216
real_vbox->add_child(mode_hbc);
2217
2218
mode_group.instantiate();
2219
2220
for (int i = 0; i < MODE_BUTTON_COUNT; i++) {
2221
mode_btns[i] = memnew(Button);
2222
mode_hbc->add_child(mode_btns[i]);
2223
mode_btns[i]->set_focus_mode(FOCUS_ALL);
2224
mode_btns[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2225
mode_btns[i]->set_toggle_mode(true);
2226
mode_btns[i]->set_text(modes[i]->get_name());
2227
mode_btns[i]->set_button_group(mode_group);
2228
mode_btns[i]->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::set_color_mode).bind((ColorModeType)i));
2229
}
2230
mode_btns[0]->set_pressed(true);
2231
2232
btn_mode = memnew(MenuButton);
2233
btn_mode->set_flat(false);
2234
mode_hbc->add_child(btn_mode);
2235
btn_mode->set_toggle_mode(true);
2236
btn_mode->set_accessibility_name(ETR("Select a picker mode."));
2237
btn_mode->set_tooltip_text(ETR("Select a picker mode."));
2238
btn_mode->set_focus_mode(FOCUS_ALL);
2239
2240
mode_popup = btn_mode->get_popup();
2241
{
2242
int i = 0;
2243
for (const ColorMode *mode : modes) {
2244
mode_popup->add_radio_check_item(mode->get_name(), i);
2245
i++;
2246
}
2247
}
2248
mode_popup->add_separator();
2249
mode_popup->add_check_item(ETR("Colorized Sliders"), MODE_MAX);
2250
mode_popup->set_item_checked(current_mode, true);
2251
mode_popup->set_item_checked(MODE_MAX + 1, true);
2252
mode_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::_set_mode_popup_value));
2253
mode_popup->connect("about_to_popup", callable_mp(this, &ColorPicker::_block_input_on_popup_show));
2254
mode_popup->connect(SNAME("popup_hide"), callable_mp(this, &ColorPicker::_enable_input_on_popup_hide));
2255
2256
slider_gc = memnew(GridContainer);
2257
2258
real_vbox->add_child(slider_gc);
2259
slider_gc->set_h_size_flags(SIZE_EXPAND_FILL);
2260
slider_gc->set_columns(3);
2261
2262
for (int i = 0; i < SLIDER_MAX; i++) {
2263
create_slider(slider_gc, i);
2264
}
2265
alpha_label->set_text("A");
2266
2267
intensity_label->set_text("I");
2268
intensity_slider->set_min(-10);
2269
intensity_slider->set_max(10);
2270
intensity_slider->set_step(0.001);
2271
intensity_value->set_allow_greater(true);
2272
intensity_value->set_custom_arrow_step(1);
2273
2274
hex_hbc = memnew(HBoxContainer);
2275
hex_hbc->set_alignment(ALIGNMENT_BEGIN);
2276
real_vbox->add_child(hex_hbc);
2277
hex_label = memnew(Label(ETR("Hex")));
2278
hex_hbc->add_child(hex_label);
2279
2280
text_type = memnew(Button);
2281
hex_hbc->add_child(text_type);
2282
text_type->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2283
text_type->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
2284
text_type->set_text("#");
2285
#ifdef TOOLS_ENABLED
2286
if (Engine::get_singleton()->is_editor_hint()) {
2287
text_type->set_tooltip_text(TTRC("Switch between hexadecimal and code values."));
2288
text_type->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_text_type_toggled));
2289
} else {
2290
text_type->set_accessibility_name(ETR("Hexadecimal Values"));
2291
#else
2292
{
2293
text_type->set_accessibility_name(ETR("Hexadecimal Values"));
2294
#endif // TOOLS_ENABLED
2295
text_type->set_flat(true);
2296
}
2297
2298
c_text = memnew(LineEdit);
2299
hex_hbc->add_child(c_text);
2300
c_text->set_h_size_flags(SIZE_EXPAND_FILL);
2301
c_text->set_select_all_on_focus(true);
2302
c_text->set_accessibility_name(ETR("Hex code or named color"));
2303
c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\")."));
2304
c_text->set_placeholder(ETR("Hex code or named color"));
2305
c_text->connect(SceneStringName(text_submitted), callable_mp(this, &ColorPicker::_html_submitted));
2306
c_text->connect(SceneStringName(text_changed), callable_mp(this, &ColorPicker::_text_changed));
2307
c_text->connect(SceneStringName(focus_exited), callable_mp(this, &ColorPicker::_html_focus_exit));
2308
2309
_update_controls();
2310
updating = false;
2311
2312
swatches_vbc = memnew(VBoxContainer);
2313
real_vbox->add_child(swatches_vbc);
2314
2315
preset_container = memnew(GridContainer);
2316
preset_container->set_h_size_flags(SIZE_EXPAND_FILL);
2317
preset_container->set_columns(PRESET_COLUMN_COUNT);
2318
preset_container->hide();
2319
2320
preset_group.instantiate();
2321
2322
HBoxContainer *palette_box = memnew(HBoxContainer);
2323
palette_box->set_h_size_flags(SIZE_EXPAND_FILL);
2324
swatches_vbc->add_child(palette_box);
2325
2326
btn_preset = memnew(Button);
2327
btn_preset->set_text(ETR("Swatches"));
2328
btn_preset->set_flat(true);
2329
btn_preset->set_toggle_mode(true);
2330
btn_preset->set_focus_mode(FOCUS_ALL);
2331
btn_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
2332
btn_preset->set_h_size_flags(SIZE_EXPAND_FILL);
2333
btn_preset->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_preset, preset_container));
2334
palette_box->add_child(btn_preset);
2335
2336
menu_btn = memnew(MenuButton);
2337
menu_btn->set_flat(false);
2338
menu_btn->set_focus_mode(FOCUS_ALL);
2339
menu_btn->set_tooltip_text(ETR("Show all options available."));
2340
menu_btn->connect("about_to_popup", callable_mp(this, &ColorPicker::_update_menu_items));
2341
palette_box->add_child(menu_btn);
2342
2343
options_menu = menu_btn->get_popup();
2344
options_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::_options_menu_cbk));
2345
options_menu->connect("about_to_popup", callable_mp(this, &ColorPicker::_block_input_on_popup_show));
2346
options_menu->connect(SNAME("popup_hide"), callable_mp(this, &ColorPicker::_enable_input_on_popup_hide));
2347
2348
palette_name = memnew(Label);
2349
palette_name->hide();
2350
palette_name->set_mouse_filter(MOUSE_FILTER_PASS);
2351
swatches_vbc->add_child(palette_name);
2352
2353
swatches_vbc->add_child(preset_container);
2354
2355
recent_preset_hbc = memnew(HBoxContainer);
2356
recent_preset_hbc->set_v_size_flags(SIZE_SHRINK_BEGIN);
2357
recent_preset_hbc->hide();
2358
2359
recent_preset_group.instantiate();
2360
2361
btn_recent_preset = memnew(Button(ETR("Recent Colors")));
2362
btn_recent_preset->set_flat(true);
2363
btn_recent_preset->set_toggle_mode(true);
2364
btn_recent_preset->set_focus_mode(FOCUS_ALL);
2365
btn_recent_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
2366
btn_recent_preset->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_recent_preset, recent_preset_hbc));
2367
swatches_vbc->add_child(btn_recent_preset);
2368
2369
swatches_vbc->add_child(recent_preset_hbc);
2370
2371
set_pick_color(Color(1, 1, 1));
2372
2373
btn_add_preset = memnew(Button);
2374
btn_add_preset->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
2375
btn_add_preset->set_tooltip_text(ETR("Add current color as a preset."));
2376
btn_add_preset->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_add_preset_pressed));
2377
preset_container->add_child(btn_add_preset);
2378
2379
perm_hb = memnew(HBoxContainer);
2380
perm_hb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
2381
2382
LinkButton *perm_link = memnew(LinkButton);
2383
perm_link->set_text(ETR("Screen Recording permission missing!"));
2384
perm_link->set_tooltip_text(ETR("Screen Recording permission is required to pick colors from the other application windows.\nClick here to request access..."));
2385
perm_link->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_req_permission));
2386
perm_hb->add_child(perm_link);
2387
real_vbox->add_child(perm_hb);
2388
perm_hb->set_visible(false);
2389
}
2390
2391
void ColorPicker::_req_permission() {
2392
#ifdef MACOS_ENABLED
2393
OS::get_singleton()->request_permission("macos.permission.RECORD_SCREEN");
2394
#endif
2395
}
2396
2397
ColorPicker::~ColorPicker() {
2398
for (ColorMode *mode : modes) {
2399
memdelete(mode);
2400
}
2401
for (ColorPickerShape *shape : shapes) {
2402
memdelete(shape);
2403
}
2404
}
2405
2406
/////////////////
2407
2408
void ColorPickerPopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
2409
if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
2410
_close_pressed();
2411
}
2412
PopupPanel::_input_from_window(p_event);
2413
}
2414
2415
/////////////////
2416
2417
void ColorPickerButton::_about_to_popup() {
2418
if (!get_tree()->get_root()->is_embedding_subwindows()) {
2419
get_viewport()->set_disable_input(true);
2420
}
2421
set_pressed(true);
2422
if (picker) {
2423
picker->set_old_color(color);
2424
}
2425
}
2426
2427
void ColorPickerButton::_color_changed(const Color &p_color) {
2428
color = p_color;
2429
queue_accessibility_update();
2430
queue_redraw();
2431
emit_signal(SNAME("color_changed"), color);
2432
}
2433
2434
void ColorPickerButton::_modal_closed() {
2435
if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
2436
set_pick_color(picker->get_old_color());
2437
emit_signal(SNAME("color_changed"), color);
2438
}
2439
emit_signal(SNAME("popup_closed"));
2440
set_pressed(false);
2441
if (!get_tree()->get_root()->is_embedding_subwindows()) {
2442
get_viewport()->set_disable_input(false);
2443
}
2444
}
2445
2446
void ColorPickerButton::pressed() {
2447
_update_picker();
2448
2449
Size2 minsize = popup->get_contents_minimum_size();
2450
float viewport_height = get_viewport_rect().size.y;
2451
2452
popup->reset_size();
2453
picker->_update_presets();
2454
picker->_update_recent_presets();
2455
2456
// Determine in which direction to show the popup. By default popup horizontally centered below the button.
2457
// But if the popup doesn't fit below and the button is in the bottom half of the viewport, show above.
2458
bool show_above = false;
2459
if (get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height) {
2460
show_above = true;
2461
}
2462
2463
float h_offset = (get_size().x - minsize.x) / 2;
2464
float v_offset = show_above ? -minsize.y : get_size().y;
2465
popup->set_position(get_screen_position() + Vector2(h_offset, v_offset));
2466
popup->popup();
2467
if (!picker->is_hex_visible() && picker->get_picker_shape() != ColorPicker::SHAPE_NONE) {
2468
callable_mp(picker, &ColorPicker::set_focus_on_picker_shape).call_deferred();
2469
} else if (DisplayServer::get_singleton()->has_hardware_keyboard()) {
2470
picker->set_focus_on_line_edit();
2471
}
2472
}
2473
2474
void ColorPickerButton::_notification(int p_what) {
2475
switch (p_what) {
2476
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
2477
RID ae = get_accessibility_element();
2478
ERR_FAIL_COND(ae.is_null());
2479
2480
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
2481
DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_DIALOG);
2482
DisplayServer::get_singleton()->accessibility_update_set_color_value(ae, color);
2483
} break;
2484
2485
case NOTIFICATION_DRAW: {
2486
const Rect2 r = Rect2(theme_cache.normal_style->get_offset(), get_size() - theme_cache.normal_style->get_minimum_size());
2487
draw_texture_rect(theme_cache.background_icon, r, true);
2488
draw_rect(r, color);
2489
2490
if (color.r > 1 || color.g > 1 || color.b > 1) {
2491
// Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
2492
draw_texture(theme_cache.overbright_indicator, theme_cache.normal_style->get_offset());
2493
}
2494
} break;
2495
2496
case NOTIFICATION_WM_CLOSE_REQUEST: {
2497
if (popup) {
2498
popup->hide();
2499
}
2500
} break;
2501
2502
case NOTIFICATION_VISIBILITY_CHANGED: {
2503
if (popup && !is_visible_in_tree()) {
2504
popup->hide();
2505
}
2506
} break;
2507
}
2508
}
2509
2510
void ColorPickerButton::set_pick_color(const Color &p_color) {
2511
if (color == p_color) {
2512
return;
2513
}
2514
color = p_color;
2515
if (picker) {
2516
picker->set_pick_color(p_color);
2517
}
2518
queue_accessibility_update();
2519
queue_redraw();
2520
}
2521
2522
Color ColorPickerButton::get_pick_color() const {
2523
return color;
2524
}
2525
2526
void ColorPickerButton::set_edit_alpha(bool p_show) {
2527
if (edit_alpha == p_show) {
2528
return;
2529
}
2530
edit_alpha = p_show;
2531
if (picker) {
2532
picker->set_edit_alpha(p_show);
2533
}
2534
}
2535
2536
bool ColorPickerButton::is_editing_alpha() const {
2537
return edit_alpha;
2538
}
2539
2540
void ColorPickerButton::set_edit_intensity(bool p_show) {
2541
if (edit_intensity == p_show) {
2542
return;
2543
}
2544
edit_intensity = p_show;
2545
if (picker) {
2546
picker->set_edit_intensity(p_show);
2547
}
2548
}
2549
2550
bool ColorPickerButton::is_editing_intensity() const {
2551
return edit_intensity;
2552
}
2553
2554
ColorPicker *ColorPickerButton::get_picker() {
2555
_update_picker();
2556
return picker;
2557
}
2558
2559
PopupPanel *ColorPickerButton::get_popup() {
2560
_update_picker();
2561
return popup;
2562
}
2563
2564
void ColorPickerButton::_update_picker() {
2565
if (!picker) {
2566
popup = memnew(ColorPickerPopupPanel);
2567
popup->set_wrap_controls(true);
2568
picker = memnew(ColorPicker);
2569
picker->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
2570
popup->add_child(picker);
2571
add_child(popup, false, INTERNAL_MODE_FRONT);
2572
picker->connect("color_changed", callable_mp(this, &ColorPickerButton::_color_changed));
2573
popup->connect("about_to_popup", callable_mp(this, &ColorPickerButton::_about_to_popup));
2574
popup->connect("popup_hide", callable_mp(this, &ColorPickerButton::_modal_closed));
2575
popup->connect("tree_exiting", callable_mp(this, &ColorPickerButton::_modal_closed));
2576
picker->connect(SceneStringName(minimum_size_changed), callable_mp((Window *)popup, &Window::reset_size));
2577
picker->set_pick_color(color);
2578
picker->set_edit_alpha(edit_alpha);
2579
picker->set_edit_intensity(edit_intensity);
2580
picker->set_display_old_color(true);
2581
emit_signal(SNAME("picker_created"));
2582
}
2583
}
2584
2585
void ColorPickerButton::_bind_methods() {
2586
ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
2587
ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
2588
ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
2589
ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
2590
ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
2591
ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
2592
ClassDB::bind_method(D_METHOD("set_edit_intensity", "show"), &ColorPickerButton::set_edit_intensity);
2593
ClassDB::bind_method(D_METHOD("is_editing_intensity"), &ColorPickerButton::is_editing_intensity);
2594
ClassDB::bind_method(D_METHOD("_about_to_popup"), &ColorPickerButton::_about_to_popup);
2595
2596
ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
2597
ADD_SIGNAL(MethodInfo("popup_closed"));
2598
ADD_SIGNAL(MethodInfo("picker_created"));
2599
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
2600
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
2601
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_intensity"), "set_edit_intensity", "is_editing_intensity");
2602
2603
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPickerButton, normal_style, "normal");
2604
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ColorPickerButton, background_icon, "bg");
2605
BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, ColorPickerButton, overbright_indicator, "overbright_indicator", "ColorPicker");
2606
}
2607
2608
ColorPickerButton::ColorPickerButton(const String &p_text) :
2609
Button(p_text) {
2610
set_toggle_mode(true);
2611
}
2612
2613
/////////////////
2614
2615
void ColorPresetButton::_notification(int p_what) {
2616
switch (p_what) {
2617
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
2618
RID ae = get_accessibility_element();
2619
ERR_FAIL_COND(ae.is_null());
2620
2621
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
2622
DisplayServer::get_singleton()->accessibility_update_set_color_value(ae, preset_color);
2623
} break;
2624
2625
case NOTIFICATION_DRAW: {
2626
const Rect2 r = Rect2(Point2(0, 0), get_size());
2627
Ref<StyleBox> sb_raw = theme_cache.foreground_style->duplicate();
2628
Ref<StyleBoxFlat> sb_flat = sb_raw;
2629
Ref<StyleBoxTexture> sb_texture = sb_raw;
2630
2631
if (sb_flat.is_valid()) {
2632
sb_flat->set_border_width(SIDE_BOTTOM, 2);
2633
if (get_draw_mode() == DRAW_PRESSED || get_draw_mode() == DRAW_HOVER_PRESSED) {
2634
sb_flat->set_border_color(Color(1, 1, 1, 1));
2635
} else {
2636
sb_flat->set_border_color(Color(0, 0, 0, 1));
2637
}
2638
2639
if (preset_color.a < 1) {
2640
// Draw a background pattern when the color is transparent.
2641
sb_flat->set_bg_color(Color(1, 1, 1));
2642
sb_flat->draw(get_canvas_item(), r);
2643
2644
Rect2 bg_texture_rect = r.grow_side(SIDE_LEFT, -sb_flat->get_margin(SIDE_LEFT));
2645
bg_texture_rect = bg_texture_rect.grow_side(SIDE_RIGHT, -sb_flat->get_margin(SIDE_RIGHT));
2646
bg_texture_rect = bg_texture_rect.grow_side(SIDE_TOP, -sb_flat->get_margin(SIDE_TOP));
2647
bg_texture_rect = bg_texture_rect.grow_side(SIDE_BOTTOM, -sb_flat->get_margin(SIDE_BOTTOM));
2648
2649
draw_texture_rect(theme_cache.background_icon, bg_texture_rect, true);
2650
sb_flat->set_bg_color(preset_color);
2651
}
2652
sb_flat->set_bg_color(preset_color);
2653
sb_flat->draw(get_canvas_item(), r);
2654
} else if (sb_texture.is_valid()) {
2655
if (preset_color.a < 1) {
2656
// Draw a background pattern when the color is transparent.
2657
bool use_tile_texture = (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE) || (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE_FIT);
2658
draw_texture_rect(theme_cache.background_icon, r, use_tile_texture);
2659
}
2660
sb_texture->set_modulate(preset_color);
2661
sb_texture->draw(get_canvas_item(), r);
2662
} else {
2663
WARN_PRINT("Unsupported StyleBox used for ColorPresetButton. Use StyleBoxFlat or StyleBoxTexture instead.");
2664
}
2665
2666
if (has_focus()) {
2667
RID ci = get_canvas_item();
2668
theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size()));
2669
}
2670
2671
if (is_color_overbright(preset_color)) {
2672
// Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
2673
draw_texture(theme_cache.overbright_indicator, Vector2(0, 0));
2674
}
2675
2676
} break;
2677
}
2678
}
2679
2680
void ColorPresetButton::set_preset_color(const Color &p_color) {
2681
preset_color = p_color;
2682
queue_accessibility_update();
2683
}
2684
2685
Color ColorPresetButton::get_preset_color() const {
2686
return preset_color;
2687
}
2688
2689
String ColorPresetButton::get_tooltip(const Point2 &p_pos) const {
2690
Color color = get_preset_color();
2691
if (recent) {
2692
return vformat(atr(ETR("Color: %s\nLMB: Apply color")), color_to_string(color, color.a < 1));
2693
}
2694
return vformat(atr(ETR("Color: %s\nLMB: Apply color\nRMB: Remove preset")), color_to_string(color, color.a < 1));
2695
}
2696
2697
void ColorPresetButton::_bind_methods() {
2698
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, foreground_style, "preset_fg");
2699
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, focus_style, "preset_focus");
2700
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ColorPresetButton, background_icon, "preset_bg");
2701
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPresetButton, overbright_indicator);
2702
}
2703
2704
ColorPresetButton::ColorPresetButton(Color p_color, int p_size, bool p_recent) {
2705
preset_color = p_color;
2706
recent = p_recent;
2707
set_toggle_mode(true);
2708
set_custom_minimum_size(Size2(p_size, p_size));
2709
set_accessibility_name(vformat(atr(ETR("Color: %s")), color_to_string(p_color, p_color.a < 1)));
2710
set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2711
}
2712
2713
ColorPresetButton::~ColorPresetButton() {
2714
}
2715
2716