aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/managers/AnimationManager.cpp
blob: 9117203f8052d34f48750f23cfda07020687f949 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "AnimationManager.hpp"
#include "../Compositor.hpp"

CAnimationManager::CAnimationManager() {
    std::vector<Vector2D> points = {Vector2D(0, 0.75f), Vector2D(0.15f, 1.f)};
    m_mBezierCurves["default"].setup(&points);
}

void CAnimationManager::removeAllBeziers() {
    m_mBezierCurves.clear();

    // add the default one
    std::vector<Vector2D> points = {Vector2D(0, 0.75f), Vector2D(0.15f, 1.f)};
    m_mBezierCurves["default"].setup(&points);
}

void CAnimationManager::addBezierWithName(std::string name, const Vector2D& p1, const Vector2D& p2) {
    std::vector points = {p1, p2};
    m_mBezierCurves[name].setup(&points);
}

void CAnimationManager::tick() {

    bool animationsDisabled = false;

    if (!g_pConfigManager->getInt("animations:enabled"))
        animationsDisabled = true;

    const float ANIMSPEED       = g_pConfigManager->getFloat("animations:speed");
    const auto BORDERSIZE       = g_pConfigManager->getInt("general:border_size");
    const auto BEZIERSTR        = g_pConfigManager->getString("animations:curve");

    auto DEFAULTBEZIER = m_mBezierCurves.find(BEZIERSTR);
    if (DEFAULTBEZIER == m_mBezierCurves.end())
        DEFAULTBEZIER = m_mBezierCurves.find("default");

    for (auto& av : m_lAnimatedVariables) {
        // get speed
        const auto SPEED = *av->m_pSpeed == 0 ? ANIMSPEED : *av->m_pSpeed;

        // window stuff
        const auto PWINDOW = (CWindow*)av->m_pWindow;
        const auto PWORKSPACE = (CWorkspace*)av->m_pWorkspace;
        const auto PLAYER = (SLayerSurface*)av->m_pLayer;

        wlr_box WLRBOXPREV = {0,0,0,0};
        if (PWINDOW) {
            WLRBOXPREV = {(int)PWINDOW->m_vRealPosition.vec().x - BORDERSIZE - 1, (int)PWINDOW->m_vRealPosition.vec().y - BORDERSIZE - 1, (int)PWINDOW->m_vRealSize.vec().x + 2 * BORDERSIZE + 2, (int)PWINDOW->m_vRealSize.vec().y + 2 * BORDERSIZE + 2};
        } else if (PWORKSPACE) {
            const auto PMONITOR = g_pCompositor->getMonitorFromID(PWORKSPACE->m_iMonitorID);
            WLRBOXPREV = {(int)PMONITOR->vecPosition.x, (int)PMONITOR->vecPosition.y, (int)PMONITOR->vecSize.x, (int)PMONITOR->vecSize.y};
        } else if (PLAYER) {
            WLRBOXPREV = PLAYER->geometry;
        }
        
        // check if it's disabled, if so, warp
        if (av->m_pEnabled == 0 || animationsDisabled) {
            av->warp();
            g_pHyprRenderer->damageBox(&WLRBOXPREV);

            if (PWINDOW) {
                g_pHyprRenderer->damageWindow(PWINDOW);
                // set size and pos if valid
                if (g_pCompositor->windowValidMapped(PWINDOW))
                    g_pXWaylandManager->setWindowSize(PWINDOW, PWINDOW->m_vRealSize.goalv());
            }

            continue;
        }

        // beziers are with a switch unforto
        // TODO: maybe do something cleaner

        // get the spent % (0 - 1)
        const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - av->animationBegin).count();
        const float SPENT = std::clamp((DURATIONPASSED / 100.f) / SPEED, 0.f, 1.f);

        switch (av->m_eVarType) {
            case AVARTYPE_FLOAT: {
                if (!deltazero(av->m_fValue, av->m_fGoal)) {
                    const auto DELTA = av->m_fGoal - av->m_fBegun;
                    const auto BEZIER = m_mBezierCurves.find(*av->m_pBezier);

                    if (BEZIER != m_mBezierCurves.end())
                        av->m_fValue = av->m_fBegun + BEZIER->second.getYForPoint(SPENT) * DELTA;
                    else
                        av->m_fValue = av->m_fBegun + DEFAULTBEZIER->second.getYForPoint(SPENT) * DELTA;

                    if (SPENT >= 1.f) {
                        av->warp();
                    }
                } else {
                    continue; // dont process
                }
                break;
            }
            case AVARTYPE_VECTOR: {
                if (!deltazero(av->m_vValue, av->m_vGoal)) {
                    const auto DELTA = av->m_vGoal - av->m_vBegun;
                    const auto BEZIER = m_mBezierCurves.find(*av->m_pBezier);

                    if (BEZIER != m_mBezierCurves.end())
                        av->m_vValue = av->m_vBegun + DELTA * BEZIER->second.getYForPoint(SPENT);
                    else
                        av->m_vValue = av->m_vBegun + DELTA * DEFAULTBEZIER->second.getYForPoint(SPENT);

                    if (SPENT >= 1.f) {
                        av->warp();
                    }
                } else {
                    continue;  // dont process
                }
                break;
            }
            case AVARTYPE_COLOR: {
                if (!deltazero(av->m_cValue, av->m_cGoal)) {
                    const auto DELTA = av->m_cGoal - av->m_cBegun;
                    const auto BEZIER = m_mBezierCurves.find(*av->m_pBezier);

                    if (BEZIER != m_mBezierCurves.end())
                        av->m_cValue = av->m_cBegun + DELTA * BEZIER->second.getYForPoint(SPENT);
                    else
                        av->m_cValue = av->m_cBegun + DELTA * DEFAULTBEZIER->second.getYForPoint(SPENT);

                    if (SPENT >= 1.f) {
                        av->warp();
                    }
                } else {
                    continue;  // dont process
                }
                break;
            }
            default: {
                ;
            }
        }

        // damage the window with the damage policy
        switch (av->m_eDamagePolicy) {
            case AVARDAMAGE_ENTIRE: {
                g_pHyprRenderer->damageBox(&WLRBOXPREV);

                if (PWINDOW)
                    g_pHyprRenderer->damageWindow(PWINDOW);
                break;
            }
            case AVARDAMAGE_BORDER: {
                RASSERT(PWINDOW, "Tried to AVARDAMAGE_BORDER a non-window AVAR!");
                
                // damage only the border.
                const auto BORDERSIZE = g_pConfigManager->getInt("general:border_size") + 1; // +1 for padding and shit
                const auto ROUNDINGSIZE = g_pConfigManager->getInt("decoration:rounding") + 1;

                // damage for old box
                g_pHyprRenderer->damageBox(WLRBOXPREV.x - BORDERSIZE, WLRBOXPREV.y - BORDERSIZE, WLRBOXPREV.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE);                              // top
                g_pHyprRenderer->damageBox(WLRBOXPREV.x - BORDERSIZE, WLRBOXPREV.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXPREV.height + 2 * BORDERSIZE);                             // left
                g_pHyprRenderer->damageBox(WLRBOXPREV.x + WLRBOXPREV.width - ROUNDINGSIZE, WLRBOXPREV.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXPREV.height + 2 * BORDERSIZE);        // right
                g_pHyprRenderer->damageBox(WLRBOXPREV.x, WLRBOXPREV.y + WLRBOXPREV.height - ROUNDINGSIZE, WLRBOXPREV.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE);                     // bottom

                // damage for new box
                const wlr_box WLRBOXNEW = {PWINDOW->m_vRealPosition.vec().x, PWINDOW->m_vRealPosition.vec().y, PWINDOW->m_vRealSize.vec().x, PWINDOW->m_vRealSize.vec().y};
                g_pHyprRenderer->damageBox(WLRBOXNEW.x - BORDERSIZE, WLRBOXNEW.y - BORDERSIZE, WLRBOXNEW.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE);                                // top
                g_pHyprRenderer->damageBox(WLRBOXNEW.x - BORDERSIZE, WLRBOXNEW.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXNEW.height + 2 * BORDERSIZE);                               // left
                g_pHyprRenderer->damageBox(WLRBOXNEW.x + WLRBOXNEW.width - ROUNDINGSIZE, WLRBOXNEW.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXNEW.height + 2 * BORDERSIZE);           // right
                g_pHyprRenderer->damageBox(WLRBOXNEW.x, WLRBOXNEW.y + WLRBOXNEW.height - ROUNDINGSIZE, WLRBOXNEW.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE);                        // bottom

                break;
            }
            default: {
                Debug::log(ERR, "av has damage policy INVALID???");
                break;
            }
        }
        

        // set size and pos if valid, but only if damage policy entire (dont if border for example)
        if (g_pCompositor->windowValidMapped(PWINDOW) && av->m_eDamagePolicy == AVARDAMAGE_ENTIRE)
            g_pXWaylandManager->setWindowSize(PWINDOW, PWINDOW->m_vRealSize.goalv());
    }
}

bool CAnimationManager::deltaSmallToFlip(const Vector2D& a, const Vector2D& b) {
    return std::abs(a.x - b.x) < 0.5f && std::abs(a.y - b.y) < 0.5f;
}

bool CAnimationManager::deltaSmallToFlip(const CColor& a, const CColor& b) {
    return std::abs(a.r - b.r) < 0.5f && std::abs(a.g - b.g) < 0.5f && std::abs(a.b - b.b) < 0.5f && std::abs(a.a - b.a) < 0.5f;
}

bool CAnimationManager::deltaSmallToFlip(const float& a, const float& b) {
    return std::abs(a - b) < 0.5f;
}

bool CAnimationManager::deltazero(const Vector2D& a, const Vector2D& b) {
    return a.x == b.x && a.y == b.y;
}

bool CAnimationManager::deltazero(const float& a, const float& b) {
    return a == b;
}

bool CAnimationManager::deltazero(const CColor& a, const CColor& b) {
    return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}

//
// Anims
//
//

void CAnimationManager::animationPopin(CWindow* pWindow) {
    const auto GOALPOS = pWindow->m_vRealPosition.goalv();
    const auto GOALSIZE = pWindow->m_vRealSize.goalv();

    pWindow->m_vRealPosition.setValue(GOALPOS + GOALSIZE / 2.f);
    pWindow->m_vRealSize.setValue(Vector2D(5, 5));
}

void CAnimationManager::animationSlide(CWindow* pWindow, std::string force) {
    pWindow->m_vRealSize.warp();  // size we preserve in slide

    const auto GOALPOS = pWindow->m_vRealPosition.goalv();
    const auto GOALSIZE = pWindow->m_vRealSize.goalv();

    const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID);

    if (force != "") {
        if (force == "bottom") pWindow->m_vRealPosition.setValue(Vector2D(GOALPOS.x, PMONITOR->vecPosition.y + PMONITOR->vecSize.y));
        else if (force == "left") pWindow->m_vRealPosition.setValue(GOALPOS - Vector2D(GOALSIZE.x, 0));
        else if (force == "right") pWindow->m_vRealPosition.setValue(GOALPOS + Vector2D(GOALSIZE.x, 0));
        else pWindow->m_vRealPosition.setValue(Vector2D(GOALPOS.x, PMONITOR->vecPosition.y - GOALSIZE.y));

        return;
    }

    const auto MIDPOINT = GOALPOS + GOALSIZE / 2.f;

    // check sides it touches
    const bool DISPLAYLEFT = STICKS(pWindow->m_vPosition.x, PMONITOR->vecPosition.x + PMONITOR->vecReservedTopLeft.x);
    const bool DISPLAYRIGHT = STICKS(pWindow->m_vPosition.x + pWindow->m_vSize.x, PMONITOR->vecPosition.x + PMONITOR->vecSize.x - PMONITOR->vecReservedBottomRight.x);
    const bool DISPLAYTOP = STICKS(pWindow->m_vPosition.y, PMONITOR->vecPosition.y + PMONITOR->vecReservedTopLeft.y);
    const bool DISPLAYBOTTOM = STICKS(pWindow->m_vPosition.y + pWindow->m_vSize.y, PMONITOR->vecPosition.y + PMONITOR->vecSize.y - PMONITOR->vecReservedBottomRight.y);

    if (DISPLAYBOTTOM && DISPLAYTOP) {
        if (DISPLAYLEFT && DISPLAYRIGHT) {
            pWindow->m_vRealPosition.setValue(GOALPOS + Vector2D(0, GOALSIZE.y));
        } else if (DISPLAYLEFT) {
            pWindow->m_vRealPosition.setValue(GOALPOS - Vector2D(GOALSIZE.x, 0));
        } else {
            pWindow->m_vRealPosition.setValue(GOALPOS + Vector2D(GOALSIZE.x, 0));
        }
    } else if (DISPLAYTOP) {
        pWindow->m_vRealPosition.setValue(GOALPOS - Vector2D(0, GOALSIZE.y));
    } else if (DISPLAYBOTTOM) {
        pWindow->m_vRealPosition.setValue(GOALPOS + Vector2D(0, GOALSIZE.y));
    } else {
        if (MIDPOINT.y > PMONITOR->vecPosition.y + PMONITOR->vecSize.y / 2.f)
            pWindow->m_vRealPosition.setValue(Vector2D(GOALPOS.x, PMONITOR->vecPosition.y + PMONITOR->vecSize.y));
        else
            pWindow->m_vRealPosition.setValue(Vector2D(GOALPOS.x, PMONITOR->vecPosition.y - GOALSIZE.y));
    }
}

void CAnimationManager::onWindowPostCreate(CWindow* pWindow) {
    auto ANIMSTYLE = g_pConfigManager->getString("animations:windows_style");
    transform(ANIMSTYLE.begin(), ANIMSTYLE.end(), ANIMSTYLE.begin(), ::tolower);

    // if the window is not being animated, that means the layout set a fixed size for it, don't animate.
    if (!pWindow->m_vRealPosition.isBeingAnimated() && !pWindow->m_vRealSize.isBeingAnimated())
        return;

    if (pWindow->m_sAdditionalConfigData.animationStyle != "") {
        // the window has config'd special anim
        if (pWindow->m_sAdditionalConfigData.animationStyle.find("slide") == 0) {
            if (pWindow->m_sAdditionalConfigData.animationStyle.find(' ') != std::string::npos) {
                // has a direction
                animationSlide(pWindow, pWindow->m_sAdditionalConfigData.animationStyle.substr(pWindow->m_sAdditionalConfigData.animationStyle.find(' ') + 1));
            } else {
                animationSlide(pWindow);
            }
        } else {
            // anim popin, fallback
            animationPopin(pWindow);
        }
    } else {
        if (ANIMSTYLE == "slide") {
            animationSlide(pWindow);
        } else {
            // anim popin, fallback
            animationPopin(pWindow);
        }
    }
}