color_picker.h 16 KB

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