Merge pull request #41166 from somnathsarkar/gradient-fix

Sort points in a Gradient for color and offset updates.
This commit is contained in:
Rémi Verschelde 2020-08-12 12:38:46 +02:00 committed by GitHub
commit dc90b17691
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 17 deletions

View file

@ -141,32 +141,29 @@ void Gradient::set_points(Vector<Gradient::Point> &p_points) {
} }
void Gradient::set_offset(int pos, const float offset) { void Gradient::set_offset(int pos, const float offset) {
ERR_FAIL_COND(pos < 0); ERR_FAIL_INDEX(pos, points.size());
if (points.size() <= pos) { _update_sorting();
points.resize(pos + 1);
}
points.write[pos].offset = offset; points.write[pos].offset = offset;
is_sorted = false; is_sorted = false;
emit_signal(CoreStringNames::get_singleton()->changed); 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); ERR_FAIL_INDEX_V(pos, points.size(), 0.0);
_update_sorting();
return points[pos].offset; return points[pos].offset;
} }
void Gradient::set_color(int pos, const Color &color) { void Gradient::set_color(int pos, const Color &color) {
ERR_FAIL_COND(pos < 0); ERR_FAIL_INDEX(pos, points.size());
if (points.size() <= pos) { _update_sorting();
points.resize(pos + 1);
is_sorted = false;
}
points.write[pos].color = color; points.write[pos].color = color;
emit_signal(CoreStringNames::get_singleton()->changed); 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()); ERR_FAIL_INDEX_V(pos, points.size(), Color());
_update_sorting();
return points[pos].color; return points[pos].color;
} }

View file

@ -49,6 +49,12 @@ public:
private: private:
Vector<Point> points; Vector<Point> points;
bool is_sorted; bool is_sorted;
_FORCE_INLINE_ void _update_sorting() {
if (!is_sorted) {
points.sort();
is_sorted = true;
}
}
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -64,10 +70,10 @@ public:
Vector<Point> &get_points(); Vector<Point> &get_points();
void set_offset(int pos, const float offset); 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); 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); void set_offsets(const Vector<float> &p_offsets);
Vector<float> get_offsets() const; Vector<float> get_offsets() const;
@ -80,10 +86,7 @@ public:
return Color(0, 0, 0, 1); return Color(0, 0, 0, 1);
} }
if (!is_sorted) { _update_sorting();
points.sort();
is_sorted = true;
}
//binary search //binary search
int low = 0; int low = 0;