aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/helpers/AnimatedVariable.hpp
blob: a484958b1534014e18256c598fd6f4fa1d4a5179 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#pragma once

#include "../defines.hpp"
#include <any>

enum ANIMATEDVARTYPE {
    AVARTYPE_INVALID = -1,
    AVARTYPE_FLOAT,
    AVARTYPE_VECTOR,
    AVARTYPE_COLOR
};

enum AVARDAMAGEPOLICY {
    AVARDAMAGE_INVALID = -1,
    AVARDAMAGE_ENTIRE = 0,
    AVARDAMAGE_BORDER
};

class CAnimationManager;
class CWorkspace;

class CAnimatedVariable {
public:
    CAnimatedVariable(); // dummy var

    void create(ANIMATEDVARTYPE, float* speed, int64_t* enabled, std::string* pBezier, void* pWindow, AVARDAMAGEPOLICY);
    void create(ANIMATEDVARTYPE, std::any val, float* speed, int64_t* enabled, std::string* pBezier, void* pWindow, AVARDAMAGEPOLICY);

    ~CAnimatedVariable();

    void unregister();

    // gets the current vector value (real time)
    const Vector2D& vec() const {
        RASSERT(m_eVarType == AVARTYPE_VECTOR, "Tried to access vec() of AVARTYPE %i!", m_eVarType);
        return m_vValue;
    }

    // gets the current float value (real time)
    const float& fl() const {
        RASSERT(m_eVarType == AVARTYPE_FLOAT, "Tried to access fl() of AVARTYPE %i!", m_eVarType);
        return m_fValue;
    }

    // gets the current color value (real time)
    const CColor& col() const {
        RASSERT(m_eVarType == AVARTYPE_COLOR, "Tried to access col() of AVARTYPE %i!", m_eVarType);
        return m_cValue;
    }

    // gets the goal vector value
    const Vector2D& goalv() const {
        RASSERT(m_eVarType == AVARTYPE_VECTOR, "Tried to access goalv() of AVARTYPE %i!", m_eVarType);
        return m_vGoal;
    }

    // gets the goal float value
    const float& goalf() const {
        RASSERT(m_eVarType == AVARTYPE_FLOAT, "Tried to access goalf() of AVARTYPE %i!", m_eVarType);
        return m_fGoal;
    }

    // gets the goal color value
    const CColor& goalc() const {
        RASSERT(m_eVarType == AVARTYPE_COLOR, "Tried to access goalc() of AVARTYPE %i!", m_eVarType);
        return m_cGoal;
    }

    void operator=(const Vector2D& v) {
        RASSERT(m_eVarType == AVARTYPE_VECTOR, "Tried to access =v of AVARTYPE %i!", m_eVarType);
        m_vGoal = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;
    }

    void operator=(const float& v) {
        RASSERT(m_eVarType == AVARTYPE_FLOAT, "Tried to access =f of AVARTYPE %i!", m_eVarType);
        m_fGoal = v;
        animationBegin = std::chrono::system_clock::now();
        m_fBegun = m_fValue;
    }

    void operator=(const CColor& v) {
        RASSERT(m_eVarType == AVARTYPE_COLOR, "Tried to access =c of AVARTYPE %i!", m_eVarType);
        m_cGoal = v;
        animationBegin = std::chrono::system_clock::now();
        m_cBegun = m_cValue;
    }

    // Sets the actual stored value, without affecting the goal, but resets the timer
    void setValue(const Vector2D& v) {
        RASSERT(m_eVarType == AVARTYPE_VECTOR, "Tried to access setValue(v) of AVARTYPE %i!", m_eVarType);
        m_vValue = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;
    }

    // Sets the actual stored value, without affecting the goal, but resets the timer
    void setValue(const float& v) {
        RASSERT(m_eVarType == AVARTYPE_FLOAT, "Tried to access setValue(f) of AVARTYPE %i!", m_eVarType);
        m_fValue = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;
    }

    // Sets the actual stored value, without affecting the goal, but resets the timer
    void setValue(const CColor& v) {
        RASSERT(m_eVarType == AVARTYPE_COLOR, "Tried to access setValue(c) of AVARTYPE %i!", m_eVarType);
        m_cValue = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;
    }

    // Sets the actual value and goal
    void setValueAndWarp(const Vector2D& v) {
        RASSERT(m_eVarType == AVARTYPE_VECTOR, "Tried to access setValueAndWarp(v) of AVARTYPE %i!", m_eVarType);
        m_vGoal = v;
        warp();
    }

    // Sets the actual value and goal
    void setValueAndWarp(const float& v) {
        RASSERT(m_eVarType == AVARTYPE_FLOAT, "Tried to access setValueAndWarp(f) of AVARTYPE %i!", m_eVarType);
        m_fGoal = v;
        warp();
    }

    // Sets the actual value and goal
    void setValueAndWarp(const CColor& v) {
        RASSERT(m_eVarType == AVARTYPE_COLOR, "Tried to access setValueAndWarp(c) of AVARTYPE %i!", m_eVarType);
        m_cGoal = v;
        warp();
    }

    // checks if an animation is in progress
    bool isBeingAnimated() {
        switch (m_eVarType) {
            case AVARTYPE_FLOAT:
                return m_fValue != m_fGoal;
            case AVARTYPE_VECTOR: 
                return m_vValue != m_vGoal;
            case AVARTYPE_COLOR:
                return m_cValue != m_cGoal;
            default:
                return false;
        }

        return false; // unreachable
    }

private:

    void warp() {
        switch (m_eVarType) {
            case AVARTYPE_FLOAT: {
                m_fValue = m_fGoal;
                break;
            }
            case AVARTYPE_VECTOR: {
                m_vValue = m_vGoal;
                break;
            }
            case AVARTYPE_COLOR: {
                m_cValue = m_cGoal;
                break;
            }
            default:
                break;
        }
    }

    Vector2D        m_vValue = Vector2D(0,0);
    float           m_fValue = 0;
    CColor          m_cValue;

    Vector2D        m_vGoal = Vector2D(0,0);
    float           m_fGoal = 0;
    CColor          m_cGoal;

    Vector2D        m_vBegun = Vector2D(0,0);
    float           m_fBegun = 0;
    CColor          m_cBegun;

    float*          m_pSpeed = nullptr;
    int64_t*        m_pEnabled = nullptr;
    void*           m_pWindow = nullptr;
    void*           m_pWorkspace = nullptr;
    std::string*    m_pBezier = nullptr;

    bool            m_bDummy = true;

    std::chrono::system_clock::time_point animationBegin;

    ANIMATEDVARTYPE     m_eVarType      = AVARTYPE_INVALID;
    AVARDAMAGEPOLICY    m_eDamagePolicy = AVARDAMAGE_INVALID;

    friend class CAnimationManager;
    friend class CWorkspace;
};