color_picker.h 13 KB

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