gradient.cpp 8.0 KB

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