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