color_picker.h 14 KB

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