diff options
Diffstat (limited to 'scene/resources/gradient.h')
-rw-r--r-- | scene/resources/gradient.h | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/scene/resources/gradient.h b/scene/resources/gradient.h index 2d98f799e2..cf5b179c45 100644 --- a/scene/resources/gradient.h +++ b/scene/resources/gradient.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,7 @@ #ifndef GRADIENT_H #define GRADIENT_H -#include "core/resource.h" +#include "core/io/resource.h" class Gradient : public Resource { GDCLASS(Gradient, Resource); @@ -39,8 +39,7 @@ class Gradient : public Resource { public: struct Point { - - float offset; + float offset = 0.0; Color color; bool operator<(const Point &p_ponit) const { return offset < p_ponit.offset; @@ -49,7 +48,13 @@ public: private: Vector<Point> points; - bool is_sorted; + bool is_sorted = true; + _FORCE_INLINE_ void _update_sorting() { + if (!is_sorted) { + points.sort(); + is_sorted = true; + } + } protected: static void _bind_methods(); @@ -65,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; @@ -77,23 +82,21 @@ public: Vector<Color> get_colors() const; _FORCE_INLINE_ Color get_color_at_offset(float p_offset) { - - if (points.empty()) + if (points.is_empty()) { return Color(0, 0, 0, 1); - - if (!is_sorted) { - points.sort(); - is_sorted = true; } + _update_sorting(); + //binary search int low = 0; int high = points.size() - 1; int middle = 0; #ifdef DEBUG_ENABLED - if (low > high) + if (low > high) { ERR_PRINT("low > high, this may be a bug"); + } #endif while (low <= high) { @@ -114,13 +117,15 @@ public: } int first = middle; int second = middle + 1; - if (second >= points.size()) + if (second >= points.size()) { return points[points.size() - 1].color; - if (first < 0) + } + if (first < 0) { return points[0].color; + } const Point &pointFirst = points[first]; const Point &pointSecond = points[second]; - return pointFirst.color.linear_interpolate(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset)); + return pointFirst.color.lerp(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset)); } int get_points_count() const; |