color_picker.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*************************************************************************/
  2. /* color_picker.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/check_button.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/popup.h"
  40. #include "scene/gui/separator.h"
  41. #include "scene/gui/slider.h"
  42. #include "scene/gui/spin_box.h"
  43. #include "scene/gui/texture_rect.h"
  44. class ColorPresetButton : public BaseButton {
  45. GDCLASS(ColorPresetButton, BaseButton);
  46. Color preset_color;
  47. protected:
  48. void _notification(int);
  49. public:
  50. void set_preset_color(const Color &p_color);
  51. Color get_preset_color() const;
  52. ColorPresetButton(Color p_color);
  53. ~ColorPresetButton();
  54. };
  55. class ColorPicker : public BoxContainer {
  56. GDCLASS(ColorPicker, BoxContainer);
  57. public:
  58. enum PickerShapeType {
  59. SHAPE_HSV_RECTANGLE,
  60. SHAPE_HSV_WHEEL,
  61. SHAPE_VHS_CIRCLE,
  62. SHAPE_MAX
  63. };
  64. private:
  65. static Ref<Shader> wheel_shader;
  66. static Ref<Shader> circle_shader;
  67. static List<Color> preset_cache;
  68. Control *screen = nullptr;
  69. Control *uv_edit = memnew(Control);
  70. Control *w_edit = memnew(Control);
  71. AspectRatioContainer *wheel_edit = memnew(AspectRatioContainer);
  72. MarginContainer *wheel_margin = memnew(MarginContainer);
  73. Ref<ShaderMaterial> wheel_mat;
  74. Ref<ShaderMaterial> circle_mat;
  75. Control *wheel = memnew(Control);
  76. Control *wheel_uv = memnew(Control);
  77. TextureRect *sample = memnew(TextureRect);
  78. GridContainer *preset_container = memnew(GridContainer);
  79. HSeparator *preset_separator = memnew(HSeparator);
  80. Button *btn_add_preset = memnew(Button);
  81. Button *btn_pick = memnew(Button);
  82. CheckButton *btn_hsv = memnew(CheckButton);
  83. CheckButton *btn_raw = memnew(CheckButton);
  84. HSlider *scroll[4];
  85. SpinBox *values[4];
  86. Label *labels[4];
  87. Button *text_type = memnew(Button);
  88. LineEdit *c_text = memnew(LineEdit);
  89. bool edit_alpha = true;
  90. Size2i ms;
  91. bool text_is_constructor = false;
  92. PickerShapeType picker_type = SHAPE_HSV_WHEEL;
  93. const int preset_column_count = 9;
  94. int prev_preset_size = 0;
  95. List<Color> presets;
  96. Color color;
  97. Color old_color;
  98. bool display_old_color = false;
  99. bool raw_mode_enabled = false;
  100. bool hsv_mode_enabled = false;
  101. bool deferred_mode_enabled = false;
  102. bool updating = true;
  103. bool changing_color = false;
  104. bool spinning = false;
  105. bool presets_enabled = true;
  106. bool presets_visible = true;
  107. float h = 0.0;
  108. float s = 0.0;
  109. float v = 0.0;
  110. Color last_hsv;
  111. void _html_submitted(const String &p_html);
  112. void _value_changed(double);
  113. void _update_controls();
  114. void _update_color(bool p_update_sliders = true);
  115. void _update_text_value();
  116. void _text_type_toggled();
  117. void _sample_input(const Ref<InputEvent> &p_event);
  118. void _sample_draw();
  119. void _hsv_draw(int p_which, Control *c);
  120. void _slider_draw(int p_which);
  121. void _uv_input(const Ref<InputEvent> &p_event, Control *c);
  122. void _w_input(const Ref<InputEvent> &p_event);
  123. void _preset_input(const Ref<InputEvent> &p_event, const Color &p_color);
  124. void _screen_input(const Ref<InputEvent> &p_event);
  125. void _add_preset_pressed();
  126. void _screen_pick_pressed();
  127. void _focus_enter();
  128. void _focus_exit();
  129. void _html_focus_exit();
  130. inline int _get_preset_size();
  131. void _add_preset_button(int p_size, const Color &p_color);
  132. protected:
  133. void _notification(int);
  134. static void _bind_methods();
  135. public:
  136. static void init_shaders();
  137. static void finish_shaders();
  138. void set_edit_alpha(bool p_show);
  139. bool is_editing_alpha() const;
  140. void _set_pick_color(const Color &p_color, bool p_update_sliders);
  141. void set_pick_color(const Color &p_color);
  142. Color get_pick_color() const;
  143. void set_old_color(const Color &p_color);
  144. void set_display_old_color(bool p_enabled);
  145. bool is_displaying_old_color() const;
  146. void set_picker_shape(PickerShapeType p_picker_type);
  147. PickerShapeType get_picker_shape() const;
  148. void add_preset(const Color &p_color);
  149. void erase_preset(const Color &p_color);
  150. PackedColorArray get_presets() const;
  151. void _update_presets();
  152. void set_hsv_mode(bool p_enabled);
  153. bool is_hsv_mode() const;
  154. void set_raw_mode(bool p_enabled);
  155. bool is_raw_mode() const;
  156. void set_deferred_mode(bool p_enabled);
  157. bool is_deferred_mode() const;
  158. void set_presets_enabled(bool p_enabled);
  159. bool are_presets_enabled() const;
  160. void set_presets_visible(bool p_visible);
  161. bool are_presets_visible() const;
  162. void set_focus_on_line_edit();
  163. ColorPicker();
  164. };
  165. class ColorPickerButton : public Button {
  166. GDCLASS(ColorPickerButton, Button);
  167. // Initialization is now done deferred,
  168. // this improves performance in the inspector as the color picker
  169. // can be expensive to initialize.
  170. PopupPanel *popup = nullptr;
  171. ColorPicker *picker = nullptr;
  172. Color color;
  173. bool edit_alpha = true;
  174. void _about_to_popup();
  175. void _color_changed(const Color &p_color);
  176. void _modal_closed();
  177. virtual void pressed() override;
  178. void _update_picker();
  179. protected:
  180. void _notification(int);
  181. static void _bind_methods();
  182. public:
  183. void set_pick_color(const Color &p_color);
  184. Color get_pick_color() const;
  185. void set_edit_alpha(bool p_show);
  186. bool is_editing_alpha() const;
  187. ColorPicker *get_picker();
  188. PopupPanel *get_popup();
  189. ColorPickerButton();
  190. };
  191. VARIANT_ENUM_CAST(ColorPicker::PickerShapeType);
  192. #endif // COLOR_PICKER_H