| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 6 | #include "Image/BsColor.h" |
| 7 | #include "Allocators/BsPoolAlloc.h" |
| 8 | |
| 9 | namespace bs |
| 10 | { |
| 11 | /** @addtogroup Image |
| 12 | * @{ |
| 13 | */ |
| 14 | |
| 15 | /** Single key in a ColorGradient. */ |
| 16 | struct BS_SCRIPT_EXPORT(m:Image,pl:true) ColorGradientKey |
| 17 | { |
| 18 | ColorGradientKey() = default; |
| 19 | ColorGradientKey(const Color& color, float time) |
| 20 | :color(color), time(time) |
| 21 | { } |
| 22 | |
| 23 | Color color; |
| 24 | float time = 0.0f; |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * Represents a range of color values over some parameters, similar to a curve. Internally represented as a set of |
| 29 | * keys that get interpolated between. |
| 30 | */ |
| 31 | class BS_UTILITY_EXPORT BS_SCRIPT_EXPORT(m:Image) ColorGradient |
| 32 | { |
| 33 | static constexpr UINT32 MAX_KEYS = 8; |
| 34 | public: |
| 35 | BS_SCRIPT_EXPORT() |
| 36 | ColorGradient() = default; |
| 37 | |
| 38 | BS_SCRIPT_EXPORT() |
| 39 | ColorGradient(const Color& color); |
| 40 | |
| 41 | BS_SCRIPT_EXPORT() |
| 42 | ColorGradient(const Vector<ColorGradientKey>& keys); |
| 43 | |
| 44 | /** Evaluates a color at the specified @p t. */ |
| 45 | RGBA evaluate(float t) const; |
| 46 | |
| 47 | /** Keys that control the gradient, sorted by time from first to last. Key times should be in range [0, 1]. */ |
| 48 | BS_SCRIPT_EXPORT() |
| 49 | void setKeys(const Vector<ColorGradientKey>& keys, float duration = 1.0f); |
| 50 | |
| 51 | /** @copydoc setKeys */ |
| 52 | BS_SCRIPT_EXPORT() |
| 53 | Vector<ColorGradientKey> getKeys() const; |
| 54 | |
| 55 | /** Returns the number of color keys in the gradient. */ |
| 56 | BS_SCRIPT_EXPORT(pr:getter,n:NumKeys) |
| 57 | UINT32 getNumKeys() const { return mNumKeys; } |
| 58 | |
| 59 | /** Returns the color key at the specified index. If out of range an empty key is returned. */ |
| 60 | BS_SCRIPT_EXPORT() |
| 61 | ColorGradientKey getKey(UINT32 idx) const; |
| 62 | |
| 63 | /** Specify a "gradient" that represents a single color value. */ |
| 64 | BS_SCRIPT_EXPORT() |
| 65 | void setConstant(const Color& color); |
| 66 | |
| 67 | /** |
| 68 | * Returns the duration over which the gradient values are interpolated over. Corresponds to the time value of the |
| 69 | * final keyframe. |
| 70 | */ |
| 71 | float getDuration() const { return mDuration; } |
| 72 | |
| 73 | /** Returns the time of the first and last keyframe in the gradient. */ |
| 74 | std::pair<float, float> getTimeRange() const; |
| 75 | |
| 76 | bool operator== (const ColorGradient& rhs) const; |
| 77 | bool operator!= (const ColorGradient& rhs) const { return !operator==(rhs); } |
| 78 | private: |
| 79 | friend struct RTTIPlainType<ColorGradient>; |
| 80 | |
| 81 | RGBA mColors[MAX_KEYS]; |
| 82 | uint16_t mTimes[MAX_KEYS]; |
| 83 | uint32_t mNumKeys = 0; |
| 84 | float mDuration = 0.0f; |
| 85 | }; |
| 86 | |
| 87 | /* @} */ |
| 88 | |
| 89 | IMPLEMENT_GLOBAL_POOL(ColorGradient, 32) |
| 90 | } |
| 91 | |