summaryrefslogtreecommitdiff
path: root/src/helpers/AnimatedVariable.hpp
blob: 2c2c3ccf670217d6a94a3368df1903b2c059a283 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#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,
    AVARDAMAGE_SHADOW
};

class CAnimationManager;
class CWorkspace;
struct SLayerSurface;
struct SAnimationPropertyConfig;
class CHyprRenderer;

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

    void create(ANIMATEDVARTYPE, SAnimationPropertyConfig*, void* pWindow, AVARDAMAGEPOLICY);
    void create(ANIMATEDVARTYPE, std::any val, SAnimationPropertyConfig*, void* pWindow, AVARDAMAGEPOLICY);

    ~CAnimatedVariable();

    void unregister();
    void registerVar();

    // gets the current vector value (real time)
    const Vector2D& vec() const {
        return m_vValue;
    }

    // gets the current float value (real time)
    const float& fl() const {
        return m_fValue;
    }

    // gets the current color value (real time)
    const CColor& col() const {
        return m_cValue;
    }

    // gets the goal vector value
    const Vector2D& goalv() const {
        return m_vGoal;
    }

    // gets the goal float value
    const float& goalf() const {
        return m_fGoal;
    }

    // gets the goal color value
    const CColor& goalc() const {
        return m_cGoal;
    }

    void operator=(const Vector2D& v) {
        m_vGoal = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;

        onAnimationBegin();
    }

    void operator=(const float& v) {
        m_fGoal = v;
        animationBegin = std::chrono::system_clock::now();
        m_fBegun = m_fValue;

        onAnimationBegin();
    }

    void operator=(const CColor& v) {
        m_cGoal = v;
        animationBegin = std::chrono::system_clock::now();
        m_cBegun = m_cValue;

        onAnimationBegin();
    }

    // Sets the actual stored value, without affecting the goal, but resets the timer
    void setValue(const Vector2D& v) {
        m_vValue = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;

        onAnimationBegin();
    }

    // Sets the actual stored value, without affecting the goal, but resets the timer
    void setValue(const float& v) {
        m_fValue = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;

        onAnimationBegin();
    }

    // Sets the actual stored value, without affecting the goal, but resets the timer
    void setValue(const CColor& v) {
        m_cValue = v;
        animationBegin = std::chrono::system_clock::now();
        m_vBegun = m_vValue;

        onAnimationBegin();
    }

    // Sets the actual value and goal
    void setValueAndWarp(const Vector2D& v) {
        m_vGoal = v;
        warp();
    }

    // Sets the actual value and goal
    void setValueAndWarp(const float& v) {
        m_fGoal = v;
        warp();
    }

    // Sets the actual value and goal
    void setValueAndWarp(const CColor& v) {
        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:
                UNREACHABLE();
        }

        UNREACHABLE();

        return false; // just so that the warning is suppressed
    }

    void warp(bool endCallback = true) {
        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:
                UNREACHABLE();
        }

        if (endCallback)
            onAnimationEnd();
    }

    void setConfig(SAnimationPropertyConfig* pConfig) {
        m_pConfig = pConfig;
    }

    SAnimationPropertyConfig* getConfig() {
        return m_pConfig;
    }

    int getDurationLeftMs();

    /* returns the spent (completion) % */
    float getPercent();

    /*  sets a function to be ran when the animation finishes.
        if an animation is not running, runs instantly.
        if "remove" is set to true, will remove the callback when ran. */
    void setCallbackOnEnd(std::function<void(void* thisptr)> func, bool remove = true) {
        m_fEndCallback = func;
        m_bRemoveEndAfterRan = remove;

        if (!isBeingAnimated())
            onAnimationEnd();
    }

    /*  sets a function to be ran when an animation is started.
        if "remove" is set to true, will remove the callback when ran. */
    void setCallbackOnBegin(std::function<void(void* thisptr)> func, bool remove = true) {
        m_fBeginCallback = func;
        m_bRemoveBeginAfterRan = remove;
    }

private:

    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;

    // owners
    void*           m_pWindow = nullptr;
    void*           m_pWorkspace = nullptr;
    void*           m_pLayer = nullptr;

    SAnimationPropertyConfig* m_pConfig = nullptr;

    bool            m_bDummy = true;
    bool            m_bIsRegistered = false;

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

    ANIMATEDVARTYPE     m_eVarType      = AVARTYPE_INVALID;
    AVARDAMAGEPOLICY    m_eDamagePolicy = AVARDAMAGE_INVALID;

    bool            m_bRemoveEndAfterRan = true;
    bool            m_bRemoveBeginAfterRan = true;
    std::function<void(void* thisptr)> m_fEndCallback;
    std::function<void(void* thisptr)> m_fBeginCallback;

    // methods
    void onAnimationEnd() {
        if (m_fEndCallback) {
            m_fEndCallback(this);
            if (m_bRemoveEndAfterRan)
                m_fEndCallback = nullptr;  // reset
        }
    }

    void onAnimationBegin() {
        if (m_fBeginCallback) {
            m_fBeginCallback(this);
            if (m_bRemoveBeginAfterRan)
                m_fBeginCallback = nullptr;  // reset
        }
    }

    friend class CAnimationManager;
    friend class CWorkspace;
    friend struct SLayerSurface;
    friend class CHyprRenderer;
};