color_picker.h 15 KB

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