color_picker.h 13 KB

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