language: C++ 4.7.2 (gcc-4.7.2)
date: 78 days 8 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//EaseIn = Start slow, accelerate to the goal.
//EaseOut = Start fast, decelerate to the goal.
//EaseInOut = Start slow, accelerate to the half-way point, decelerate to the goal.
//EaseOutIn = Start fast, decelerate to the half-way point, accelerate to the goal.
enum EaseStyle {EaseIn, EaseOut, EaseInOut, EaseOutIn};
 
 
/*
    Easer is an Easing/Tweening/Interpolating functor class.
    You can either use the EaseEquations stand-alone, or pass them to Easer if you want
    persistent data (perhaps holding Easer as a member-variable of another class).
*/
class Easer
{
public:
    Easer(EaseFunction easeFunction, EaseStyle style = EaseIn) : EaseFunc(easeFunction), Style(style) { }
    ~Easer() = default;
    
    EaseStyle Style = EaseIn;
    EaseFunction EaseFunc;
    
    //Used to constrain the return result to the desired range, such as 0.0f - 1.0f.
    //The default setting is (-FLT_MAX, FLT_MAX). Note: For some funky reason, FLT_MIN is "the minimum positive value above zero". =/
    FloatRange Constaints = FloatRange(-FLT_MAX, FLT_MAX);
    
    //The current position.
    float Position = 0.0f;
    
public:
    //Sets 'position' to 'newPosition'.
    void SetPosition(float position);
    //Resets 'position' to 0.0f. This is the same as calling SetPosition(0.0f);
    void Reset();
    
    //Returns true if 'position' is at or beyond 1.0f.
    bool AtEnd() const;
    
    //Returns the value at the current position.
    //(Note: Unless constrained, the actual result may actually drop below 0.0f or above 1.0f depending
    //on the easing function. This is intentional, and desirable for some effects)
    float Get() const;
    //Returns the value at the current position, scaled to the range of 'min' to 'max' instead of around (0.0f - 1.0f)-ish
    float GetInRange(const FloatRange &range) const;
    
    //Returns the value at 'pos'.
    float GetAt(float pos) const;
    //Returns the value at 'pos', scaled to the range of 'min' to 'max' instead of around (0.0f - 1.0f)-ish
    float GetInRangeAt(float pos, const FloatRange &range) const;
        
    //Moves the position of the ease by 'amount'.
    void Step(float amount);
    
    void operator+=(float amount); //Same as Step().
    void operator-=(float amount); //Same as Step() with a negative amount.
    void operator=(float position); //Same as SetPosition().
    
    float operator()() const; //Same as Get()
    float operator()(float value) const; //Same as GetAt()
    
private:
    //Calls the callback function 'EaseFunc' and processes the result depending on the EaseStyle,
    //then constrains the result using 'Constaints'. 
    float plot(float position) const;
};
Easer.h