color_picker.h 13 KB

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