color_ramp.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*************************************************************************/
  2. /* color_ramp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "color_ramp.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", "offset"), &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. ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "offsets"), COLOR_RAMP_SET_OFFSETS, COLOR_RAMP_GET_OFFSETS);
  62. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "colors"), COLOR_RAMP_SET_COLORS, COLOR_RAMP_GET_COLORS);
  63. }
  64. Vector<float> Gradient::get_offsets() const {
  65. Vector<float> offsets;
  66. offsets.resize(points.size());
  67. for (int i = 0; i < points.size(); i++) {
  68. offsets.write[i] = points[i].offset;
  69. }
  70. return offsets;
  71. }
  72. Vector<Color> Gradient::get_colors() const {
  73. Vector<Color> colors;
  74. colors.resize(points.size());
  75. for (int i = 0; i < points.size(); i++) {
  76. colors.write[i] = points[i].color;
  77. }
  78. return colors;
  79. }
  80. void Gradient::set_offsets(const Vector<float> &p_offsets) {
  81. points.resize(p_offsets.size());
  82. for (int i = 0; i < points.size(); i++) {
  83. points.write[i].offset = p_offsets[i];
  84. }
  85. is_sorted = false;
  86. emit_signal(CoreStringNames::get_singleton()->changed);
  87. }
  88. void Gradient::set_colors(const Vector<Color> &p_colors) {
  89. if (points.size() < p_colors.size())
  90. is_sorted = false;
  91. points.resize(p_colors.size());
  92. for (int i = 0; i < points.size(); i++) {
  93. points.write[i].color = p_colors[i];
  94. }
  95. emit_signal(CoreStringNames::get_singleton()->changed);
  96. }
  97. Vector<Gradient::Point> &Gradient::get_points() {
  98. return points;
  99. }
  100. void Gradient::add_point(float p_offset, const Color &p_color) {
  101. Point p;
  102. p.offset = p_offset;
  103. p.color = p_color;
  104. is_sorted = false;
  105. points.push_back(p);
  106. emit_signal(CoreStringNames::get_singleton()->changed);
  107. }
  108. void Gradient::remove_point(int p_index) {
  109. ERR_FAIL_INDEX(p_index, points.size());
  110. ERR_FAIL_COND(points.size() <= 2);
  111. points.remove(p_index);
  112. emit_signal(CoreStringNames::get_singleton()->changed);
  113. }
  114. void Gradient::set_points(Vector<Gradient::Point> &p_points) {
  115. points = p_points;
  116. is_sorted = false;
  117. emit_signal(CoreStringNames::get_singleton()->changed);
  118. }
  119. void Gradient::set_offset(int pos, const float offset) {
  120. if (points.size() <= pos)
  121. points.resize(pos + 1);
  122. points.write[pos].offset = offset;
  123. is_sorted = false;
  124. emit_signal(CoreStringNames::get_singleton()->changed);
  125. }
  126. float Gradient::get_offset(int pos) const {
  127. if (points.size() && points.size() > pos)
  128. return points[pos].offset;
  129. return 0; //TODO: Maybe throw some error instead?
  130. }
  131. void Gradient::set_color(int pos, const Color &color) {
  132. if (points.size() <= pos) {
  133. points.resize(pos + 1);
  134. is_sorted = false;
  135. }
  136. points.write[pos].color = color;
  137. emit_signal(CoreStringNames::get_singleton()->changed);
  138. }
  139. Color Gradient::get_color(int pos) const {
  140. if (points.size() && points.size() > pos)
  141. return points[pos].color;
  142. return Color(0, 0, 0, 1); //TODO: Maybe throw some error instead?
  143. }
  144. int Gradient::get_points_count() const {
  145. return points.size();
  146. }