Sort points in a Gradient for color and offset updates.

(cherry picked from commit d5d832417e)
This commit is contained in:
Somnath Sarkar 2020-08-10 04:23:12 -04:00 committed by Rémi Verschelde
parent 0d8b2d34e1
commit 1f7a3e0f8d
No known key found for this signature in database
GPG key ID: C3336907360768E1
3 changed files with 19 additions and 19 deletions

View file

@ -20,7 +20,7 @@
Adds the specified color to the end of the ramp, with the specified offset.
</description>
</method>
<method name="get_color" qualifiers="const">
<method name="get_color">
<return type="Color">
</return>
<argument index="0" name="point" type="int">
@ -29,7 +29,7 @@
Returns the color of the ramp color at index [code]point[/code].
</description>
</method>
<method name="get_offset" qualifiers="const">
<method name="get_offset">
<return type="float">
</return>
<argument index="0" name="point" type="int">

View file

@ -143,32 +143,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;
}

View file

@ -50,6 +50,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();
@ -65,10 +71,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;
@ -81,10 +87,7 @@ public:
if (points.empty())
return Color(0, 0, 0, 1);
if (!is_sorted) {
points.sort();
is_sorted = true;
}
_update_sorting();
//binary search
int low = 0;