color_picker.h 13 KB

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