gradient.cpp 7.9 KB

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