color_picker.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/check_button.h"
  35. #include "scene/gui/grid_container.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/line_edit.h"
  38. #include "scene/gui/popup.h"
  39. #include "scene/gui/separator.h"
  40. #include "scene/gui/slider.h"
  41. #include "scene/gui/spin_box.h"
  42. #include "scene/gui/texture_rect.h"
  43. #include "scene/gui/tool_button.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. private:
  58. static List<Color> preset_cache;
  59. Control *screen;
  60. Control *uv_edit;
  61. Control *w_edit;
  62. TextureRect *sample;
  63. GridContainer *preset_container = memnew(GridContainer);
  64. HSeparator *preset_separator;
  65. Button *btn_add_preset;
  66. ToolButton *btn_pick;
  67. CheckButton *btn_hsv;
  68. CheckButton *btn_raw;
  69. HSlider *scroll[4];
  70. SpinBox *values[4];
  71. Label *labels[4];
  72. Button *text_type;
  73. LineEdit *c_text;
  74. bool edit_alpha;
  75. Size2i ms;
  76. bool text_is_constructor;
  77. const int preset_column_count = 10;
  78. List<Color> presets;
  79. Color color;
  80. Color old_color;
  81. bool display_old_color = false;
  82. bool raw_mode_enabled;
  83. bool hsv_mode_enabled;
  84. bool deferred_mode_enabled;
  85. bool updating;
  86. bool changing_color;
  87. bool presets_enabled;
  88. bool presets_visible;
  89. float h, s, v;
  90. Color last_hsv;
  91. void _html_entered(const String &p_html);
  92. void _value_changed(double);
  93. void _update_controls();
  94. void _update_color(bool p_update_sliders = true);
  95. void _update_text_value();
  96. void _text_type_toggled();
  97. void _sample_input(const Ref<InputEvent> &p_event);
  98. void _sample_draw();
  99. void _hsv_draw(int p_which, Control *c);
  100. void _uv_input(const Ref<InputEvent> &p_event);
  101. void _w_input(const Ref<InputEvent> &p_event);
  102. void _preset_input(const Ref<InputEvent> &p_event, const Color &p_color);
  103. void _screen_input(const Ref<InputEvent> &p_event);
  104. void _add_preset_pressed();
  105. void _screen_pick_pressed();
  106. void _focus_enter();
  107. void _focus_exit();
  108. void _html_focus_exit();
  109. inline int _get_preset_size();
  110. void _add_preset_button(int p_size, const Color &p_color);
  111. protected:
  112. void _notification(int);
  113. static void _bind_methods();
  114. public:
  115. void set_edit_alpha(bool p_show);
  116. bool is_editing_alpha() const;
  117. void _set_pick_color(const Color &p_color, bool p_update_sliders);
  118. void set_pick_color(const Color &p_color);
  119. Color get_pick_color() const;
  120. void set_old_color(const Color &p_color);
  121. void set_display_old_color(bool p_enabled);
  122. bool is_displaying_old_color() const;
  123. void add_preset(const Color &p_color);
  124. void erase_preset(const Color &p_color);
  125. PoolColorArray get_presets() const;
  126. void _update_presets();
  127. void set_hsv_mode(bool p_enabled);
  128. bool is_hsv_mode() const;
  129. void set_raw_mode(bool p_enabled);
  130. bool is_raw_mode() const;
  131. void set_deferred_mode(bool p_enabled);
  132. bool is_deferred_mode() const;
  133. void set_presets_enabled(bool p_enabled);
  134. bool are_presets_enabled() const;
  135. void set_presets_visible(bool p_visible);
  136. bool are_presets_visible() const;
  137. void set_focus_on_line_edit();
  138. ColorPicker();
  139. };
  140. class ColorPickerButton : public Button {
  141. GDCLASS(ColorPickerButton, Button);
  142. PopupPanel *popup;
  143. ColorPicker *picker;
  144. Color color;
  145. bool edit_alpha;
  146. void _about_to_show();
  147. void _color_changed(const Color &p_color);
  148. void _modal_closed();
  149. virtual void pressed();
  150. void _update_picker();
  151. protected:
  152. void _notification(int);
  153. static void _bind_methods();
  154. public:
  155. void set_pick_color(const Color &p_color);
  156. Color get_pick_color() const;
  157. void set_edit_alpha(bool p_show);
  158. bool is_editing_alpha() const;
  159. ColorPicker *get_picker();
  160. PopupPanel *get_popup();
  161. ColorPickerButton();
  162. };
  163. #endif // COLOR_PICKER_H