color_picker.h 12 KB

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