| 1 | /**************************************************************************/ |
| 2 | /* gradient.cpp */ |
| 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 | #include "gradient.h" |
| 32 | |
| 33 | Gradient::Gradient() { |
| 34 | //Set initial gradient transition from black to white |
| 35 | points.resize(2); |
| 36 | points.write[0].color = Color(0, 0, 0, 1); |
| 37 | points.write[0].offset = 0; |
| 38 | points.write[1].color = Color(1, 1, 1, 1); |
| 39 | points.write[1].offset = 1; |
| 40 | } |
| 41 | |
| 42 | Gradient::~Gradient() { |
| 43 | } |
| 44 | |
| 45 | void Gradient::_bind_methods() { |
| 46 | ClassDB::bind_method(D_METHOD("add_point" , "offset" , "color" ), &Gradient::add_point); |
| 47 | ClassDB::bind_method(D_METHOD("remove_point" , "point" ), &Gradient::remove_point); |
| 48 | |
| 49 | ClassDB::bind_method(D_METHOD("set_offset" , "point" , "offset" ), &Gradient::set_offset); |
| 50 | ClassDB::bind_method(D_METHOD("get_offset" , "point" ), &Gradient::get_offset); |
| 51 | |
| 52 | ClassDB::bind_method(D_METHOD("reverse" ), &Gradient::reverse); |
| 53 | |
| 54 | ClassDB::bind_method(D_METHOD("set_color" , "point" , "color" ), &Gradient::set_color); |
| 55 | ClassDB::bind_method(D_METHOD("get_color" , "point" ), &Gradient::get_color); |
| 56 | |
| 57 | ClassDB::bind_method(D_METHOD("sample" , "offset" ), &Gradient::get_color_at_offset); |
| 58 | |
| 59 | ClassDB::bind_method(D_METHOD("get_point_count" ), &Gradient::get_point_count); |
| 60 | |
| 61 | ClassDB::bind_method(D_METHOD("set_offsets" , "offsets" ), &Gradient::set_offsets); |
| 62 | ClassDB::bind_method(D_METHOD("get_offsets" ), &Gradient::get_offsets); |
| 63 | |
| 64 | ClassDB::bind_method(D_METHOD("set_colors" , "colors" ), &Gradient::set_colors); |
| 65 | ClassDB::bind_method(D_METHOD("get_colors" ), &Gradient::get_colors); |
| 66 | |
| 67 | ClassDB::bind_method(D_METHOD("set_interpolation_mode" , "interpolation_mode" ), &Gradient::set_interpolation_mode); |
| 68 | ClassDB::bind_method(D_METHOD("get_interpolation_mode" ), &Gradient::get_interpolation_mode); |
| 69 | |
| 70 | ClassDB::bind_method(D_METHOD("set_interpolation_color_space" , "interpolation_color_space" ), &Gradient::set_interpolation_color_space); |
| 71 | ClassDB::bind_method(D_METHOD("get_interpolation_color_space" ), &Gradient::get_interpolation_color_space); |
| 72 | |
| 73 | ADD_GROUP("Interpolation" , "interpolation_" ); |
| 74 | ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_mode" , PROPERTY_HINT_ENUM, "Linear,Constant,Cubic" ), "set_interpolation_mode" , "get_interpolation_mode" ); |
| 75 | ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_color_space" , PROPERTY_HINT_ENUM, "sRGB,Linear sRGB,Oklab" ), "set_interpolation_color_space" , "get_interpolation_color_space" ); |
| 76 | |
| 77 | ADD_GROUP("Raw Data" , "" ); |
| 78 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "offsets" ), "set_offsets" , "get_offsets" ); |
| 79 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors" ), "set_colors" , "get_colors" ); |
| 80 | |
| 81 | BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_LINEAR); |
| 82 | BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CONSTANT); |
| 83 | BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CUBIC); |
| 84 | |
| 85 | BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_SRGB); |
| 86 | BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_LINEAR_SRGB); |
| 87 | BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_OKLAB); |
| 88 | } |
| 89 | |
| 90 | void Gradient::_validate_property(PropertyInfo &p_property) const { |
| 91 | if (p_property.name == "interpolation_color_space" && interpolation_mode == GRADIENT_INTERPOLATE_CONSTANT) { |
| 92 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Vector<float> Gradient::get_offsets() const { |
| 97 | Vector<float> offsets; |
| 98 | offsets.resize(points.size()); |
| 99 | for (int i = 0; i < points.size(); i++) { |
| 100 | offsets.write[i] = points[i].offset; |
| 101 | } |
| 102 | return offsets; |
| 103 | } |
| 104 | |
| 105 | Vector<Color> Gradient::get_colors() const { |
| 106 | Vector<Color> colors; |
| 107 | colors.resize(points.size()); |
| 108 | for (int i = 0; i < points.size(); i++) { |
| 109 | colors.write[i] = points[i].color; |
| 110 | } |
| 111 | return colors; |
| 112 | } |
| 113 | |
| 114 | void Gradient::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) { |
| 115 | if (p_interp_mode == interpolation_mode) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | interpolation_mode = p_interp_mode; |
| 120 | emit_changed(); |
| 121 | notify_property_list_changed(); |
| 122 | } |
| 123 | |
| 124 | Gradient::InterpolationMode Gradient::get_interpolation_mode() { |
| 125 | return interpolation_mode; |
| 126 | } |
| 127 | |
| 128 | void Gradient::set_interpolation_color_space(Gradient::ColorSpace p_color_space) { |
| 129 | if (p_color_space == interpolation_color_space) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | interpolation_color_space = p_color_space; |
| 134 | emit_changed(); |
| 135 | } |
| 136 | |
| 137 | Gradient::ColorSpace Gradient::get_interpolation_color_space() { |
| 138 | return interpolation_color_space; |
| 139 | } |
| 140 | |
| 141 | void Gradient::set_offsets(const Vector<float> &p_offsets) { |
| 142 | points.resize(p_offsets.size()); |
| 143 | for (int i = 0; i < points.size(); i++) { |
| 144 | points.write[i].offset = p_offsets[i]; |
| 145 | } |
| 146 | is_sorted = false; |
| 147 | emit_changed(); |
| 148 | } |
| 149 | |
| 150 | void Gradient::set_colors(const Vector<Color> &p_colors) { |
| 151 | if (points.size() < p_colors.size()) { |
| 152 | is_sorted = false; |
| 153 | } |
| 154 | points.resize(p_colors.size()); |
| 155 | for (int i = 0; i < points.size(); i++) { |
| 156 | points.write[i].color = p_colors[i]; |
| 157 | } |
| 158 | emit_changed(); |
| 159 | } |
| 160 | |
| 161 | Vector<Gradient::Point> &Gradient::get_points() { |
| 162 | return points; |
| 163 | } |
| 164 | |
| 165 | void Gradient::add_point(float p_offset, const Color &p_color) { |
| 166 | Point p; |
| 167 | p.offset = p_offset; |
| 168 | p.color = p_color; |
| 169 | is_sorted = false; |
| 170 | points.push_back(p); |
| 171 | |
| 172 | emit_changed(); |
| 173 | } |
| 174 | |
| 175 | void Gradient::remove_point(int p_index) { |
| 176 | ERR_FAIL_INDEX(p_index, points.size()); |
| 177 | ERR_FAIL_COND(points.size() <= 1); |
| 178 | points.remove_at(p_index); |
| 179 | emit_changed(); |
| 180 | } |
| 181 | |
| 182 | void Gradient::reverse() { |
| 183 | for (int i = 0; i < points.size(); i++) { |
| 184 | points.write[i].offset = 1.0 - points[i].offset; |
| 185 | } |
| 186 | |
| 187 | is_sorted = false; |
| 188 | _update_sorting(); |
| 189 | emit_changed(); |
| 190 | } |
| 191 | |
| 192 | void Gradient::set_points(const Vector<Gradient::Point> &p_points) { |
| 193 | points = p_points; |
| 194 | is_sorted = false; |
| 195 | emit_changed(); |
| 196 | } |
| 197 | |
| 198 | void Gradient::set_offset(int pos, const float offset) { |
| 199 | ERR_FAIL_INDEX(pos, points.size()); |
| 200 | _update_sorting(); |
| 201 | points.write[pos].offset = offset; |
| 202 | is_sorted = false; |
| 203 | emit_changed(); |
| 204 | } |
| 205 | |
| 206 | float Gradient::get_offset(int pos) { |
| 207 | ERR_FAIL_INDEX_V(pos, points.size(), 0.0); |
| 208 | _update_sorting(); |
| 209 | return points[pos].offset; |
| 210 | } |
| 211 | |
| 212 | void Gradient::set_color(int pos, const Color &color) { |
| 213 | ERR_FAIL_INDEX(pos, points.size()); |
| 214 | _update_sorting(); |
| 215 | points.write[pos].color = color; |
| 216 | emit_changed(); |
| 217 | } |
| 218 | |
| 219 | Color Gradient::get_color(int pos) { |
| 220 | ERR_FAIL_INDEX_V(pos, points.size(), Color()); |
| 221 | _update_sorting(); |
| 222 | return points[pos].color; |
| 223 | } |
| 224 | |
| 225 | int Gradient::get_point_count() const { |
| 226 | return points.size(); |
| 227 | } |
| 228 | |