gradient_editor.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**************************************************************************/
  2. /* gradient_editor.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 GRADIENT_EDITOR_H
  31. #define GRADIENT_EDITOR_H
  32. #include "scene/gui/color_picker.h"
  33. #include "scene/gui/popup.h"
  34. #include "scene/resources/gradient.h"
  35. class GradientEditor : public Control {
  36. GDCLASS(GradientEditor, Control);
  37. PopupPanel *popup = nullptr;
  38. ColorPicker *picker = nullptr;
  39. bool grabbing = false;
  40. int grabbed = -1;
  41. Vector<Gradient::Point> points;
  42. Gradient::InterpolationMode interpolation_mode = Gradient::GRADIENT_INTERPOLATE_LINEAR;
  43. bool editing = false;
  44. Ref<Gradient> gradient;
  45. Ref<Gradient> gradient_cache;
  46. Ref<GradientTexture1D> preview_texture;
  47. // Make sure to use the scaled value below.
  48. const int BASE_SPACING = 3;
  49. const int BASE_HANDLE_WIDTH = 8;
  50. int draw_spacing = BASE_SPACING;
  51. int handle_width = BASE_HANDLE_WIDTH;
  52. void _gradient_changed();
  53. void _ramp_changed();
  54. void _color_changed(const Color &p_color);
  55. int _get_point_from_pos(int x);
  56. void _show_color_picker();
  57. protected:
  58. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  59. void _notification(int p_what);
  60. static void _bind_methods();
  61. public:
  62. void set_gradient(const Ref<Gradient> &p_gradient);
  63. void reverse_gradient();
  64. void set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors);
  65. Vector<float> get_offsets() const;
  66. Vector<Color> get_colors() const;
  67. void set_points(Vector<Gradient::Point> &p_points);
  68. Vector<Gradient::Point> &get_points();
  69. void set_interpolation_mode(Gradient::InterpolationMode p_interp_mode);
  70. Gradient::InterpolationMode get_interpolation_mode();
  71. ColorPicker *get_picker();
  72. PopupPanel *get_popup();
  73. virtual Size2 get_minimum_size() const override;
  74. GradientEditor();
  75. virtual ~GradientEditor();
  76. };
  77. #endif // GRADIENT_EDITOR_H