gradient.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*************************************************************************/
  2. /* gradient.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/core_string_names.h"
  32. Gradient::Gradient() {
  33. //Set initial gradient transition from black to white
  34. points.resize(2);
  35. points.write[0].color = Color(0, 0, 0, 1);
  36. points.write[0].offset = 0;
  37. points.write[1].color = Color(1, 1, 1, 1);
  38. points.write[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_points_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. ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_mode", PROPERTY_HINT_ENUM, "Linear,Constant,Cubic"), "set_interpolation_mode", "get_interpolation_mode");
  59. ADD_GROUP("Raw Data", "");
  60. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "offsets"), "set_offsets", "get_offsets");
  61. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors"), "set_colors", "get_colors");
  62. BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_LINEAR);
  63. BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CONSTANT);
  64. BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CUBIC);
  65. }
  66. Vector<float> Gradient::get_offsets() const {
  67. Vector<float> offsets;
  68. offsets.resize(points.size());
  69. for (int i = 0; i < points.size(); i++) {
  70. offsets.write[i] = points[i].offset;
  71. }
  72. return offsets;
  73. }
  74. Vector<Color> Gradient::get_colors() const {
  75. Vector<Color> colors;
  76. colors.resize(points.size());
  77. for (int i = 0; i < points.size(); i++) {
  78. colors.write[i] = points[i].color;
  79. }
  80. return colors;
  81. }
  82. void Gradient::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
  83. interpolation_mode = p_interp_mode;
  84. emit_signal(CoreStringNames::get_singleton()->changed);
  85. }
  86. Gradient::InterpolationMode Gradient::get_interpolation_mode() {
  87. return interpolation_mode;
  88. }
  89. void Gradient::set_offsets(const Vector<float> &p_offsets) {
  90. points.resize(p_offsets.size());
  91. for (int i = 0; i < points.size(); i++) {
  92. points.write[i].offset = p_offsets[i];
  93. }
  94. is_sorted = false;
  95. emit_signal(CoreStringNames::get_singleton()->changed);
  96. }
  97. void Gradient::set_colors(const Vector<Color> &p_colors) {
  98. if (points.size() < p_colors.size()) {
  99. is_sorted = false;
  100. }
  101. points.resize(p_colors.size());
  102. for (int i = 0; i < points.size(); i++) {
  103. points.write[i].color = p_colors[i];
  104. }
  105. emit_signal(CoreStringNames::get_singleton()->changed);
  106. }
  107. Vector<Gradient::Point> &Gradient::get_points() {
  108. return points;
  109. }
  110. void Gradient::add_point(float p_offset, const Color &p_color) {
  111. Point p;
  112. p.offset = p_offset;
  113. p.color = p_color;
  114. is_sorted = false;
  115. points.push_back(p);
  116. emit_signal(CoreStringNames::get_singleton()->changed);
  117. }
  118. void Gradient::remove_point(int p_index) {
  119. ERR_FAIL_INDEX(p_index, points.size());
  120. ERR_FAIL_COND(points.size() <= 1);
  121. points.remove_at(p_index);
  122. emit_signal(CoreStringNames::get_singleton()->changed);
  123. }
  124. void Gradient::reverse() {
  125. for (int i = 0; i < points.size(); i++) {
  126. points.write[i].offset = 1.0 - points[i].offset;
  127. }
  128. _update_sorting();
  129. emit_signal(CoreStringNames::get_singleton()->changed);
  130. }
  131. void Gradient::set_points(const Vector<Gradient::Point> &p_points) {
  132. points = p_points;
  133. is_sorted = false;
  134. emit_signal(CoreStringNames::get_singleton()->changed);
  135. }
  136. void Gradient::set_offset(int pos, const float offset) {
  137. ERR_FAIL_INDEX(pos, points.size());
  138. _update_sorting();
  139. points.write[pos].offset = offset;
  140. is_sorted = false;
  141. emit_signal(CoreStringNames::get_singleton()->changed);
  142. }
  143. float Gradient::get_offset(int pos) {
  144. ERR_FAIL_INDEX_V(pos, points.size(), 0.0);
  145. _update_sorting();
  146. return points[pos].offset;
  147. }
  148. void Gradient::set_color(int pos, const Color &color) {
  149. ERR_FAIL_INDEX(pos, points.size());
  150. _update_sorting();
  151. points.write[pos].color = color;
  152. emit_signal(CoreStringNames::get_singleton()->changed);
  153. }
  154. Color Gradient::get_color(int pos) {
  155. ERR_FAIL_INDEX_V(pos, points.size(), Color());
  156. _update_sorting();
  157. return points[pos].color;
  158. }
  159. int Gradient::get_points_count() const {
  160. return points.size();
  161. }