Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/color_picker.h
9896 views
1
/**************************************************************************/
2
/* color_picker.h */
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
#pragma once
32
33
#include "scene/gui/box_container.h"
34
#include "scene/gui/button.h"
35
#include "scene/gui/popup.h"
36
37
class AspectRatioContainer;
38
class ColorMode;
39
class ColorPickerShape;
40
class FileDialog;
41
class GridContainer;
42
class HSlider;
43
class Label;
44
class LineEdit;
45
class MarginContainer;
46
class MenuButton;
47
class OptionButton;
48
class PopupMenu;
49
class SpinBox;
50
class StyleBoxFlat;
51
class TextureRect;
52
53
class ColorPresetButton : public BaseButton {
54
GDCLASS(ColorPresetButton, BaseButton);
55
56
Color preset_color;
57
bool recent = false;
58
59
struct ThemeCache {
60
Ref<StyleBox> foreground_style;
61
Ref<StyleBox> focus_style;
62
63
Ref<Texture2D> background_icon;
64
Ref<Texture2D> overbright_indicator;
65
} theme_cache;
66
67
protected:
68
void _notification(int);
69
static void _bind_methods();
70
71
public:
72
void set_preset_color(const Color &p_color);
73
Color get_preset_color() const;
74
75
virtual String get_tooltip(const Point2 &p_pos) const override;
76
77
ColorPresetButton(Color p_color, int p_size, bool p_recent);
78
~ColorPresetButton();
79
};
80
81
class ColorPicker : public VBoxContainer {
82
GDCLASS(ColorPicker, VBoxContainer);
83
84
// These classes poke into theme items for their internal logic.
85
friend class ColorPickerShape;
86
friend class ColorPickerShapeRectangle;
87
friend class ColorPickerShapeWheel;
88
friend class ColorPickerShapeCircle;
89
friend class ColorPickerShapeVHSCircle;
90
friend class ColorPickerShapeOKHSLCircle;
91
friend class ColorPickerShapeOKHSRectangle;
92
friend class ColorPickerShapeOKHLRectangle;
93
94
friend class ColorModeRGB;
95
friend class ColorModeHSV;
96
friend class ColorModeLinear;
97
friend class ColorModeOKHSL;
98
99
public:
100
enum ColorModeType {
101
MODE_RGB,
102
MODE_HSV,
103
#ifndef DISABLE_DEPRECATED
104
MODE_RAW = 2,
105
#endif
106
MODE_LINEAR = 2,
107
MODE_OKHSL,
108
109
MODE_MAX
110
};
111
112
enum PickerShapeType {
113
SHAPE_HSV_RECTANGLE,
114
SHAPE_HSV_WHEEL,
115
SHAPE_VHS_CIRCLE,
116
SHAPE_OKHSL_CIRCLE,
117
SHAPE_NONE,
118
SHAPE_OK_HS_RECTANGLE,
119
SHAPE_OK_HL_RECTANGLE,
120
121
SHAPE_MAX
122
};
123
124
private:
125
// Ideally, `SHAPE_NONE` should be -1 so that we don't need to convert shape type to index.
126
// In order to avoid breaking compatibility, we have to use these methods for conversion.
127
inline int get_current_shape_index() {
128
return shape_to_index(current_shape);
129
}
130
131
static inline int shape_to_index(PickerShapeType p_shape) {
132
if (p_shape == SHAPE_NONE) {
133
return -1;
134
}
135
if (p_shape > SHAPE_NONE) {
136
return p_shape - 1;
137
}
138
return p_shape;
139
}
140
141
static inline PickerShapeType index_to_shape(int p_index) {
142
if (p_index == -1) {
143
return SHAPE_NONE;
144
}
145
if (p_index >= SHAPE_NONE) {
146
return (PickerShapeType)(p_index + 1);
147
}
148
return (PickerShapeType)p_index;
149
}
150
151
public:
152
static const int MODE_SLIDER_COUNT = 3;
153
154
enum SLIDER_EXTRA {
155
SLIDER_INTENSITY = MODE_SLIDER_COUNT,
156
SLIDER_ALPHA,
157
158
SLIDER_MAX
159
};
160
161
enum class MenuOption {
162
MENU_SAVE,
163
MENU_SAVE_AS,
164
MENU_LOAD,
165
MENU_QUICKLOAD,
166
MENU_CLEAR,
167
};
168
169
private:
170
static inline Ref<Shader> wheel_shader;
171
static inline Ref<Shader> circle_shader;
172
static inline Ref<Shader> circle_ok_color_shader;
173
static inline Ref<Shader> rectangle_ok_color_hs_shader;
174
static inline Ref<Shader> rectangle_ok_color_hl_shader;
175
static inline List<Color> preset_cache;
176
static inline List<Color> recent_preset_cache;
177
178
#ifdef TOOLS_ENABLED
179
Object *editor_settings = nullptr;
180
#endif
181
182
int current_slider_count = MODE_SLIDER_COUNT;
183
184
const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 1.0 / 2;
185
const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 30;
186
float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
187
188
static constexpr int MODE_BUTTON_COUNT = 3;
189
190
bool slider_theme_modified = true;
191
192
LocalVector<ColorMode *> modes;
193
LocalVector<ColorPickerShape *> shapes;
194
195
Popup *picker_window = nullptr;
196
TextureRect *picker_texture_zoom = nullptr;
197
Panel *picker_preview = nullptr;
198
Panel *picker_preview_color = nullptr;
199
Ref<StyleBoxFlat> picker_preview_style_box;
200
Ref<StyleBoxFlat> picker_preview_style_box_color;
201
202
// Legacy color picking.
203
TextureRect *picker_texture_rect = nullptr;
204
Color picker_color;
205
FileDialog *file_dialog = nullptr;
206
MenuButton *menu_btn = nullptr;
207
PopupMenu *options_menu = nullptr;
208
209
MarginContainer *internal_margin = nullptr;
210
HBoxContainer *shape_container = nullptr;
211
TextureRect *sample = nullptr;
212
VBoxContainer *swatches_vbc = nullptr;
213
GridContainer *preset_container = nullptr;
214
HBoxContainer *recent_preset_hbc = nullptr;
215
Button *btn_add_preset = nullptr;
216
Button *btn_pick = nullptr;
217
Label *palette_name = nullptr;
218
String palette_path;
219
Button *btn_preset = nullptr;
220
Button *btn_recent_preset = nullptr;
221
PopupMenu *shape_popup = nullptr;
222
PopupMenu *mode_popup = nullptr;
223
MenuButton *btn_shape = nullptr;
224
HBoxContainer *mode_hbc = nullptr;
225
HBoxContainer *sample_hbc = nullptr;
226
GridContainer *slider_gc = nullptr;
227
HBoxContainer *hex_hbc = nullptr;
228
Label *hex_label = nullptr;
229
MenuButton *btn_mode = nullptr;
230
Button *mode_btns[MODE_BUTTON_COUNT];
231
Ref<ButtonGroup> mode_group;
232
ColorPresetButton *selected_recent_preset = nullptr;
233
Ref<ButtonGroup> preset_group;
234
Ref<ButtonGroup> recent_preset_group;
235
236
HBoxContainer *perm_hb = nullptr;
237
void _req_permission();
238
239
#ifdef TOOLS_ENABLED
240
Callable quick_open_callback;
241
Callable palette_saved_callback;
242
#endif // TOOLS_ENABLED
243
244
OptionButton *mode_option_button = nullptr;
245
246
HSlider *sliders[SLIDER_MAX];
247
SpinBox *values[SLIDER_MAX];
248
Label *labels[SLIDER_MAX];
249
250
Button *text_type = nullptr;
251
LineEdit *c_text = nullptr;
252
253
HSlider *alpha_slider = nullptr;
254
SpinBox *alpha_value = nullptr;
255
Label *alpha_label = nullptr;
256
257
bool edit_alpha = true;
258
259
HSlider *intensity_slider = nullptr;
260
SpinBox *intensity_value = nullptr;
261
Label *intensity_label = nullptr;
262
263
bool edit_intensity = true;
264
265
Size2i ms;
266
bool text_is_constructor = false;
267
PickerShapeType current_shape = SHAPE_HSV_RECTANGLE;
268
ColorModeType current_mode = MODE_RGB;
269
bool colorize_sliders = true;
270
271
const int PRESET_COLUMN_COUNT = 9;
272
int prev_preset_size = 0;
273
int prev_rencet_preset_size = 0;
274
List<Color> presets;
275
List<Color> recent_presets;
276
277
Color color;
278
Color color_normalized;
279
Color old_color;
280
Color pre_picking_color;
281
bool is_picking_color = false;
282
283
bool display_old_color = false;
284
bool deferred_mode_enabled = false;
285
bool updating = true;
286
bool changing_color = false;
287
bool spinning = false;
288
bool can_add_swatches = true;
289
bool presets_visible = true;
290
bool color_modes_visible = true;
291
bool sampler_visible = true;
292
bool sliders_visible = true;
293
bool hex_visible = true;
294
bool line_edit_mouse_release = false;
295
bool text_changed = false;
296
bool currently_dragging = false;
297
298
float h = 0.0;
299
float s = 0.0;
300
float v = 0.0;
301
302
float ok_hsl_h = 0.0;
303
float ok_hsl_s = 0.0;
304
float ok_hsl_l = 0.0;
305
306
bool hsv_cached = false;
307
bool okhsl_cached = false;
308
309
float intensity = 0.0;
310
311
struct ThemeCache {
312
float base_scale = 1.0;
313
314
int content_margin = 0;
315
int label_width = 0;
316
317
int sv_height = 0;
318
int sv_width = 0;
319
int h_width = 0;
320
321
bool center_slider_grabbers = true;
322
323
Ref<StyleBox> picker_focus_rectangle;
324
Ref<StyleBox> picker_focus_circle;
325
Color focused_not_editing_cursor_color;
326
Ref<Texture2D> menu_option;
327
Ref<Texture2D> screen_picker;
328
Ref<Texture2D> expanded_arrow;
329
Ref<Texture2D> folded_arrow;
330
Ref<Texture2D> add_preset;
331
332
Ref<Texture2D> shape_rect;
333
Ref<Texture2D> shape_rect_wheel;
334
Ref<Texture2D> shape_circle;
335
336
Ref<Texture2D> bar_arrow;
337
Ref<Texture2D> sample_bg;
338
Ref<Texture2D> sample_revert;
339
Ref<StyleBox> sample_focus;
340
Ref<Texture2D> overbright_indicator;
341
Ref<Texture2D> picker_cursor;
342
Ref<Texture2D> picker_cursor_bg;
343
Ref<Texture2D> color_hue;
344
345
Ref<Texture2D> color_script;
346
347
/* Mode buttons */
348
Ref<StyleBox> mode_button_normal;
349
Ref<StyleBox> mode_button_pressed;
350
Ref<StyleBox> mode_button_hover;
351
} theme_cache;
352
353
void _copy_normalized_to_hsv_okhsl();
354
void _copy_hsv_okhsl_to_normalized();
355
356
Color _color_apply_intensity(const Color &col) const;
357
void _normalized_apply_intensity_to_color();
358
void _copy_color_to_normalized_and_intensity();
359
360
void create_slider(GridContainer *gc, int idx);
361
void _reset_sliders_theme();
362
void _html_submitted(const String &p_html);
363
void _slider_drag_started();
364
void _slider_value_changed();
365
void _slider_drag_ended();
366
void _update_controls();
367
void _update_color(bool p_update_sliders = true);
368
void _update_text_value();
369
#ifdef TOOLS_ENABLED
370
void _text_type_toggled();
371
#endif // TOOLS_ENABLED
372
void _sample_input(const Ref<InputEvent> &p_event);
373
void _sample_draw();
374
void _slider_draw(int p_which);
375
void _alpha_slider_draw();
376
377
void _slider_or_spin_input(const Ref<InputEvent> &p_event);
378
void _line_edit_input(const Ref<InputEvent> &p_event);
379
void _preset_input(const Ref<InputEvent> &p_event, const Color &p_color);
380
void _recent_preset_pressed(const bool pressed, ColorPresetButton *p_preset);
381
void _text_changed(const String &p_new_text);
382
void _add_preset_pressed();
383
void _html_focus_exit();
384
void _pick_button_pressed();
385
void _target_gui_input(const Ref<InputEvent> &p_event);
386
void _pick_finished();
387
void _update_menu_items();
388
void _options_menu_cbk(int p_which);
389
void _block_input_on_popup_show();
390
void _enable_input_on_popup_hide();
391
392
// Native color picking.
393
void _pick_button_pressed_native();
394
void _native_cb(bool p_status, const Color &p_color);
395
396
// Legacy color picking.
397
void _pick_button_pressed_legacy();
398
void _picker_texture_input(const Ref<InputEvent> &p_event);
399
400
inline int _get_preset_size();
401
void _add_preset_button(int p_size, const Color &p_color);
402
void _add_recent_preset_button(int p_size, const Color &p_color);
403
void _save_palette(bool p_is_save_as);
404
void _load_palette();
405
406
void _show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container);
407
void _update_drop_down_arrow(const bool &p_is_btn_pressed, Button *p_btn_preset);
408
409
void _set_mode_popup_value(ColorModeType p_mode);
410
411
Variant _get_drag_data_fw(const Point2 &p_point, Control *p_from_control);
412
bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const;
413
void _drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control);
414
415
void _ensure_file_dialog();
416
417
protected:
418
virtual void _update_theme_item_cache() override;
419
420
void _notification(int);
421
static void _bind_methods();
422
423
public:
424
#ifdef TOOLS_ENABLED
425
void set_editor_settings(Object *p_editor_settings);
426
void set_quick_open_callback(const Callable &p_file_selected);
427
void set_palette_saved_callback(const Callable &p_palette_saved);
428
429
void _quick_open_palette_file_selected(const String &p_path);
430
#endif
431
432
GridContainer *get_slider_container();
433
HSlider *get_slider(int idx);
434
Vector<float> get_active_slider_values();
435
436
static void init_shaders();
437
static void finish_shaders();
438
439
void add_mode(ColorMode *p_mode);
440
void add_shape(ColorPickerShape *p_shape);
441
442
void set_edit_alpha(bool p_show);
443
bool is_editing_alpha() const;
444
445
void set_edit_intensity(bool p_show);
446
bool is_editing_intensity() const;
447
448
void _set_pick_color(const Color &p_color, bool p_update_sliders, bool p_calc_intensity);
449
void set_pick_color(const Color &p_color);
450
Color get_pick_color() const;
451
void set_old_color(const Color &p_color);
452
Color get_old_color() const;
453
454
void _palette_file_selected(const String &p_path);
455
456
void set_display_old_color(bool p_enabled);
457
bool is_displaying_old_color() const;
458
459
void set_picker_shape(PickerShapeType p_shape);
460
PickerShapeType get_picker_shape() const;
461
462
void add_preset(const Color &p_color);
463
void add_recent_preset(const Color &p_color);
464
void erase_preset(const Color &p_color);
465
void erase_recent_preset(const Color &p_color);
466
PackedColorArray get_presets() const;
467
PackedColorArray get_recent_presets() const;
468
void _update_presets();
469
void _update_recent_presets();
470
471
void _select_from_preset_container(const Color &p_color);
472
bool _select_from_recent_preset_hbc(const Color &p_color);
473
474
void set_color_mode(ColorModeType p_mode);
475
ColorModeType get_color_mode() const;
476
477
void set_colorize_sliders(bool p_colorize_sliders);
478
bool is_colorizing_sliders() const;
479
480
void set_deferred_mode(bool p_enabled);
481
bool is_deferred_mode() const;
482
483
void set_can_add_swatches(bool p_enabled);
484
bool are_swatches_enabled() const;
485
486
void set_presets_visible(bool p_visible);
487
bool are_presets_visible() const;
488
489
void set_modes_visible(bool p_visible);
490
bool are_modes_visible() const;
491
492
void set_sampler_visible(bool p_visible);
493
bool is_sampler_visible() const;
494
495
void set_sliders_visible(bool p_visible);
496
bool are_sliders_visible() const;
497
498
void set_hex_visible(bool p_visible);
499
bool is_hex_visible() const;
500
501
void set_focus_on_line_edit();
502
void set_focus_on_picker_shape();
503
504
ColorPicker();
505
~ColorPicker();
506
};
507
508
class ColorPickerPopupPanel : public PopupPanel {
509
virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
510
};
511
512
class ColorPickerButton : public Button {
513
GDCLASS(ColorPickerButton, Button);
514
515
// Initialization is now done deferred,
516
// this improves performance in the inspector as the color picker
517
// can be expensive to initialize.
518
519
PopupPanel *popup = nullptr;
520
ColorPicker *picker = nullptr;
521
Color color;
522
bool edit_alpha = true;
523
bool edit_intensity = true;
524
525
struct ThemeCache {
526
Ref<StyleBox> normal_style;
527
Ref<Texture2D> background_icon;
528
529
Ref<Texture2D> overbright_indicator;
530
} theme_cache;
531
532
void _about_to_popup();
533
void _color_changed(const Color &p_color);
534
void _modal_closed();
535
536
virtual void pressed() override;
537
538
void _update_picker();
539
540
protected:
541
void _notification(int);
542
static void _bind_methods();
543
544
public:
545
void set_pick_color(const Color &p_color);
546
Color get_pick_color() const;
547
548
void set_edit_alpha(bool p_show);
549
bool is_editing_alpha() const;
550
551
void set_edit_intensity(bool p_show);
552
bool is_editing_intensity() const;
553
554
ColorPicker *get_picker();
555
PopupPanel *get_popup();
556
557
ColorPickerButton(const String &p_text = String());
558
};
559
560
VARIANT_ENUM_CAST(ColorPicker::PickerShapeType);
561
VARIANT_ENUM_CAST(ColorPicker::ColorModeType);
562
563