summaryrefslogtreecommitdiff
path: root/scene/resources
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-08-12 12:38:46 +0200
committerGitHub <noreply@github.com>2020-08-12 12:38:46 +0200
commitdc90b17691b574967895546568e2966e655f9cd0 (patch)
treefbf27de5b37504a5574a0b17c655c68828ee43c3 /scene/resources
parentd3b5c0948c943b3fbcb4b5322262c59c92abfa83 (diff)
parentd5d832417ecc117ea42e6c87172941845af8f3d3 (diff)
Merge pull request #41166 from somnathsarkar/gradient-fix
Sort points in a Gradient for color and offset updates.
Diffstat (limited to 'scene/resources')
-rw-r--r--scene/resources/gradient.cpp19
-rw-r--r--scene/resources/gradient.h15
2 files changed, 17 insertions, 17 deletions
diff --git a/scene/resources/gradient.cpp b/scene/resources/gradient.cpp
index d271c906ff..4bcc52776b 100644
--- a/scene/resources/gradient.cpp
+++ b/scene/resources/gradient.cpp
@@ -141,32 +141,29 @@ void Gradient::set_points(Vector<Gradient::Point> &p_points) {
}
void Gradient::set_offset(int pos, const float offset) {
- ERR_FAIL_COND(pos < 0);
- if (points.size() <= pos) {
- points.resize(pos + 1);
- }
+ ERR_FAIL_INDEX(pos, points.size());
+ _update_sorting();
points.write[pos].offset = offset;
is_sorted = false;
emit_signal(CoreStringNames::get_singleton()->changed);
}
-float Gradient::get_offset(int pos) const {
+float Gradient::get_offset(int pos) {
ERR_FAIL_INDEX_V(pos, points.size(), 0.0);
+ _update_sorting();
return points[pos].offset;
}
void Gradient::set_color(int pos, const Color &color) {
- ERR_FAIL_COND(pos < 0);
- if (points.size() <= pos) {
- points.resize(pos + 1);
- is_sorted = false;
- }
+ ERR_FAIL_INDEX(pos, points.size());
+ _update_sorting();
points.write[pos].color = color;
emit_signal(CoreStringNames::get_singleton()->changed);
}
-Color Gradient::get_color(int pos) const {
+Color Gradient::get_color(int pos) {
ERR_FAIL_INDEX_V(pos, points.size(), Color());
+ _update_sorting();
return points[pos].color;
}
diff --git a/scene/resources/gradient.h b/scene/resources/gradient.h
index d40dcc8d44..6518b13ee8 100644
--- a/scene/resources/gradient.h
+++ b/scene/resources/gradient.h
@@ -49,6 +49,12 @@ public:
private:
Vector<Point> points;
bool is_sorted;
+ _FORCE_INLINE_ void _update_sorting() {
+ if (!is_sorted) {
+ points.sort();
+ is_sorted = true;
+ }
+ }
protected:
static void _bind_methods();
@@ -64,10 +70,10 @@ public:
Vector<Point> &get_points();
void set_offset(int pos, const float offset);
- float get_offset(int pos) const;
+ float get_offset(int pos);
void set_color(int pos, const Color &color);
- Color get_color(int pos) const;
+ Color get_color(int pos);
void set_offsets(const Vector<float> &p_offsets);
Vector<float> get_offsets() const;
@@ -80,10 +86,7 @@ public:
return Color(0, 0, 0, 1);
}
- if (!is_sorted) {
- points.sort();
- is_sorted = true;
- }
+ _update_sorting();
//binary search
int low = 0;