gradient.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/core_string_names.h"
  32. //setter and getter names for property serialization
  33. #define COLOR_RAMP_GET_OFFSETS "get_offsets"
  34. #define COLOR_RAMP_GET_COLORS "get_colors"
  35. #define COLOR_RAMP_SET_OFFSETS "set_offsets"
  36. #define COLOR_RAMP_SET_COLORS "set_colors"
  37. Gradient::Gradient() {
  38. //Set initial color ramp transition from black to white
  39. points.resize(2);
  40. points.write[0].color = Color(0, 0, 0, 1);
  41. points.write[0].offset = 0;
  42. points.write[1].color = Color(1, 1, 1, 1);
  43. points.write[1].offset = 1;
  44. is_sorted = true;
  45. }
  46. Gradient::~Gradient() {
  47. }
  48. void Gradient::_bind_methods() {
  49. ClassDB::bind_method(D_METHOD("add_point", "offset", "color"), &Gradient::add_point);
  50. ClassDB::bind_method(D_METHOD("remove_point", "point"), &Gradient::remove_point);
  51. ClassDB::bind_method(D_METHOD("set_offset", "point", "offset"), &Gradient::set_offset);
  52. ClassDB::bind_method(D_METHOD("get_offset", "point"), &Gradient::get_offset);
  53. ClassDB::bind_method(D_METHOD("set_color", "point", "color"), &Gradient::set_color);
  54. ClassDB::bind_method(D_METHOD("get_color", "point"), &Gradient::get_color);
  55. ClassDB::bind_method(D_METHOD("interpolate", "offset"), &Gradient::get_color_at_offset);
  56. ClassDB::bind_method(D_METHOD("get_point_count"), &Gradient::get_points_count);
  57. ClassDB::bind_method(D_METHOD(COLOR_RAMP_SET_OFFSETS, "offsets"), &Gradient::set_offsets);
  58. ClassDB::bind_method(D_METHOD(COLOR_RAMP_GET_OFFSETS), &Gradient::get_offsets);
  59. ClassDB::bind_method(D_METHOD(COLOR_RAMP_SET_COLORS, "colors"), &Gradient::set_colors);
  60. ClassDB::bind_method(D_METHOD(COLOR_RAMP_GET_COLORS), &Gradient::get_colors);
  61. ClassDB::bind_method(D_METHOD("set_interpolation_mode", "interpolation_mode"), &Gradient::set_interpolation_mode);
  62. ClassDB::bind_method(D_METHOD("get_interpolation_mode"), &Gradient::get_interpolation_mode);
  63. ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_mode", PROPERTY_HINT_ENUM, "Linear,Constant,Cubic"), "set_interpolation_mode", "get_interpolation_mode");
  64. ADD_GROUP("Raw Data", "");
  65. ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "offsets"), COLOR_RAMP_SET_OFFSETS, COLOR_RAMP_GET_OFFSETS);
  66. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "colors"), COLOR_RAMP_SET_COLORS, COLOR_RAMP_GET_COLORS);
  67. BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_LINEAR);
  68. BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CONSTANT);
  69. BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CUBIC);
  70. }
  71. Vector<float> Gradient::get_offsets() const {
  72. Vector<float> offsets;
  73. offsets.resize(points.size());
  74. for (int i = 0; i < points.size(); i++) {
  75. offsets.write[i] = points[i].offset;
  76. }
  77. return offsets;
  78. }
  79. Vector<Color> Gradient::get_colors() const {
  80. Vector<Color> colors;
  81. colors.resize(points.size());
  82. for (int i = 0; i < points.size(); i++) {
  83. colors.write[i] = points[i].color;
  84. }
  85. return colors;
  86. }
  87. void Gradient::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
  88. interpolation_mode = p_interp_mode;
  89. emit_signal(CoreStringNames::get_singleton()->changed);
  90. }
  91. Gradient::InterpolationMode Gradient::get_interpolation_mode() {
  92. return interpolation_mode;
  93. }
  94. void Gradient::set_offsets(const Vector<float> &p_offsets) {
  95. points.resize(p_offsets.size());
  96. for (int i = 0; i < points.size(); i++) {
  97. points.write[i].offset = p_offsets[i];
  98. }
  99. is_sorted = false;
  100. emit_signal(CoreStringNames::get_singleton()->changed);
  101. }
  102. void Gradient::set_colors(const Vector<Color> &p_colors) {
  103. if (points.size() < p_colors.size()) {
  104. is_sorted = false;
  105. }
  106. points.resize(p_colors.size());
  107. for (int i = 0; i < points.size(); i++) {
  108. points.write[i].color = p_colors[i];
  109. }
  110. emit_signal(CoreStringNames::get_singleton()->changed);
  111. }
  112. Vector<Gradient::Point> &Gradient::get_points() {
  113. return points;
  114. }
  115. void Gradient::add_point(float p_offset, const Color &p_color) {
  116. Point p;
  117. p.offset = p_offset;
  118. p.color = p_color;
  119. is_sorted = false;
  120. points.push_back(p);
  121. emit_signal(CoreStringNames::get_singleton()->changed);
  122. }
  123. void Gradient::remove_point(int p_index) {
  124. ERR_FAIL_INDEX(p_index, points.size());
  125. ERR_FAIL_COND(points.size() <= 1);
  126. points.remove(p_index);
  127. emit_signal(CoreStringNames::get_singleton()->changed);
  128. }
  129. void Gradient::set_points(Vector<Gradient::Point> &p_points) {
  130. points = p_points;
  131. is_sorted = false;
  132. emit_signal(CoreStringNames::get_singleton()->changed);
  133. }
  134. void Gradient::set_offset(int pos, const float offset) {
  135. ERR_FAIL_INDEX(pos, points.size());
  136. _update_sorting();
  137. points.write[pos].offset = offset;
  138. is_sorted = false;
  139. emit_signal(CoreStringNames::get_singleton()->changed);
  140. }
  141. float Gradient::get_offset(int pos) {
  142. ERR_FAIL_INDEX_V(pos, points.size(), 0.0);
  143. _update_sorting();
  144. return points[pos].offset;
  145. }
  146. void Gradient::set_color(int pos, const Color &color) {
  147. ERR_FAIL_INDEX(pos, points.size());
  148. _update_sorting();
  149. points.write[pos].color = color;
  150. emit_signal(CoreStringNames::get_singleton()->changed);
  151. }
  152. Color Gradient::get_color(int pos) {
  153. ERR_FAIL_INDEX_V(pos, points.size(), Color());
  154. _update_sorting();
  155. return points[pos].color;
  156. }
  157. int Gradient::get_points_count() const {
  158. return points.size();
  159. }