color_picker.h 11 KB

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