Debian Codegraph @

scene/gui/color_picker.h

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