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