KWin
Loading...
Searching...
No Matches
animationeffect.h
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2011 Thomas Lübking <thomas.luebking@web.de>
6 SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#pragma once
12
13#include "kwin_export.h"
14
16#include <QEasingCurve>
17#include <QElapsedTimer>
18#include <QtMath>
19
20namespace KWin
21{
22
23class KWIN_EXPORT FPx2
24{
25public:
27 {
28 f[0] = f[1] = 0.0;
29 valid = false;
30 }
31 explicit FPx2(float v)
32 {
33 f[0] = f[1] = v;
34 valid = true;
35 }
36 FPx2(float v1, float v2)
37 {
38 f[0] = v1;
39 f[1] = v2;
40 valid = true;
41 }
42 FPx2(const FPx2 &other)
43 {
44 f[0] = other.f[0];
45 f[1] = other.f[1];
46 valid = other.valid;
47 }
48 explicit FPx2(const QPoint &other)
49 {
50 f[0] = other.x();
51 f[1] = other.y();
52 valid = true;
53 }
54 explicit FPx2(const QPointF &other)
55 {
56 f[0] = other.x();
57 f[1] = other.y();
58 valid = true;
59 }
60 explicit FPx2(const QSize &other)
61 {
62 f[0] = other.width();
63 f[1] = other.height();
64 valid = true;
65 }
66 explicit FPx2(const QSizeF &other)
67 {
68 f[0] = other.width();
69 f[1] = other.height();
70 valid = true;
71 }
72 inline void invalidate()
73 {
74 valid = false;
75 }
76 inline bool isValid() const
77 {
78 return valid;
79 }
80 inline float operator[](int n) const
81 {
82 return f[n];
83 }
84 inline QString toString() const
85 {
86 QString ret;
87 if (valid) {
88 ret = QString::number(f[0]) + QLatin1Char(',') + QString::number(f[1]);
89 } else {
90 ret = QString();
91 }
92 return ret;
93 }
94
95 inline FPx2 &operator=(const FPx2 &other)
96 {
97 f[0] = other.f[0];
98 f[1] = other.f[1];
99 valid = other.valid;
100 return *this;
101 }
102 inline FPx2 &operator+=(const FPx2 &other)
103 {
104 f[0] += other[0];
105 f[1] += other[1];
106 return *this;
107 }
108 inline FPx2 &operator-=(const FPx2 &other)
109 {
110 f[0] -= other[0];
111 f[1] -= other[1];
112 return *this;
113 }
114 inline FPx2 &operator*=(float fl)
115 {
116 f[0] *= fl;
117 f[1] *= fl;
118 return *this;
119 }
120 inline FPx2 &operator/=(float fl)
121 {
122 f[0] /= fl;
123 f[1] /= fl;
124 return *this;
125 }
126
127 friend inline bool operator==(const FPx2 &f1, const FPx2 &f2)
128 {
129 return f1[0] == f2[0] && f1[1] == f2[1];
130 }
131 friend inline bool operator!=(const FPx2 &f1, const FPx2 &f2)
132 {
133 return f1[0] != f2[0] || f1[1] != f2[1];
134 }
135 friend inline const FPx2 operator+(const FPx2 &f1, const FPx2 &f2)
136 {
137 return FPx2(f1[0] + f2[0], f1[1] + f2[1]);
138 }
139 friend inline const FPx2 operator-(const FPx2 &f1, const FPx2 &f2)
140 {
141 return FPx2(f1[0] - f2[0], f1[1] - f2[1]);
142 }
143 friend inline const FPx2 operator*(const FPx2 &f, float fl)
144 {
145 return FPx2(f[0] * fl, f[1] * fl);
146 }
147 friend inline const FPx2 operator*(float fl, const FPx2 &f)
148 {
149 return FPx2(f[0] * fl, f[1] * fl);
150 }
151 friend inline const FPx2 operator-(const FPx2 &f)
152 {
153 return FPx2(-f[0], -f[1]);
154 }
155 friend inline const FPx2 operator/(const FPx2 &f, float fl)
156 {
157 return FPx2(f[0] / fl, f[1] / fl);
158 }
159
160 inline void set(float v)
161 {
162 f[0] = v;
163 valid = true;
164 }
165 inline void set(float v1, float v2)
166 {
167 f[0] = v1;
168 f[1] = v2;
169 valid = true;
170 }
171
172private:
173 float f[2];
174 bool valid;
175};
176
177class AniData;
178class AnimationEffectPrivate;
179
194class KWIN_EXPORT AnimationEffect : public CrossFadeEffect
195{
196 Q_OBJECT
197
198public:
199 enum Anchor { Left = 1 << 0,
200 Top = 1 << 1,
201 Right = 1 << 2,
202 Bottom = 1 << 3,
203 Horizontal = Left | Right,
205 Mouse = 1 << 4 };
206 Q_ENUM(Anchor)
207
232 Q_ENUM(Attribute)
233
241 Q_ENUM(MetaType)
242
243
250 Backward
251 };
252 Q_ENUM(Direction)
253
254
263 DontTerminate = 0x00,
269 TerminateAtSource = 0x01,
274 TerminateAtTarget = 0x02
275 };
276 Q_DECLARE_FLAGS(TerminationFlags, TerminationFlag)
277 Q_FLAG(TerminationFlags)
278
279
294 ~AnimationEffect() override;
295
296 bool isActive() const override;
297
312 static int metaData(MetaType type, uint meta);
313
322 static void setMetaData(MetaType type, uint value, uint &meta);
323
324 // Reimplemented from KWin::Effect.
325 QString debug(const QString &parameter) const override;
326 void prePaintWindow(EffectWindow *w, WindowPrePaintData &data, std::chrono::milliseconds presentTime) override;
327 void paintWindow(const RenderTarget &renderTarget, const RenderViewport &viewport, EffectWindow *w, int mask, QRegion region, WindowPaintData &data) override;
328 void postPaintScreen() override;
329
335 static qreal qecGaussian(qreal progress)
336 {
337 progress = 2 * progress - 1;
338 progress *= -5 * progress;
339 return qExp(progress);
340 }
341
345 static inline qint64 clock()
346 {
347 return s_clock.elapsed();
348 }
349
350protected:
376 quint64 animate(EffectWindow *w, Attribute a, uint meta, int ms, const FPx2 &to, const QEasingCurve &curve = QEasingCurve(), int delay = 0, const FPx2 &from = FPx2(), bool fullScreen = false, bool keepAlive = true, GLShader *shader = nullptr)
377 {
378 return p_animate(w, a, meta, ms, to, curve, delay, from, false, fullScreen, keepAlive, shader);
379 }
380
409 quint64 set(EffectWindow *w, Attribute a, uint meta, int ms, const FPx2 &to, const QEasingCurve &curve = QEasingCurve(), int delay = 0, const FPx2 &from = FPx2(), bool fullScreen = false, bool keepAlive = true, GLShader *shader = nullptr)
410 {
411 return p_animate(w, a, meta, ms, to, curve, delay, from, true, fullScreen, keepAlive, shader);
412 }
413
427 bool retarget(quint64 animationId, FPx2 newTarget, int newRemainingTime = -1);
428
429 bool freezeInTime(quint64 animationId, qint64 frozenTime);
430
443 bool redirect(quint64 animationId,
444 Direction direction,
445 TerminationFlags terminationFlags = TerminateAtSource);
446
455 bool complete(quint64 animationId);
456
468 virtual void animationEnded(EffectWindow *w, Attribute a, uint meta);
469
482 bool cancel(quint64 animationId);
483
496 virtual void genericAnimation(EffectWindow *w, WindowPaintData &data, float progress, uint meta);
497
501 typedef QMap<EffectWindow *, QPair<QList<AniData>, QRect>> AniMap;
502
506 AniMap state() const;
507
508private:
509 quint64 p_animate(EffectWindow *w, Attribute a, uint meta, int ms, FPx2 to, const QEasingCurve &curve, int delay, FPx2 from, bool keepAtTarget, bool fullScreenEffect, bool keepAlive, GLShader *shader);
510 QRect clipRect(const QRect &windowRect, const AniData &) const;
511 float interpolated(const AniData &, int i = 0) const;
512 float progress(const AniData &) const;
513 void updateLayerRepaints();
514 void validate(Attribute a, uint &meta, FPx2 *from, FPx2 *to, const EffectWindow *w) const;
515
516private Q_SLOTS:
517 void init();
518 void triggerRepaint();
519 void _windowClosed(KWin::EffectWindow *w);
520 void _windowDeleted(KWin::EffectWindow *w);
521 void _windowExpandedGeometryChanged(KWin::EffectWindow *w);
522
523private:
524 static QElapsedTimer s_clock;
525 const std::unique_ptr<AnimationEffectPrivate> d_ptr;
526 Q_DECLARE_PRIVATE(AnimationEffect)
527 Q_DISABLE_COPY(AnimationEffect)
528};
529
530} // namespace
531
533Q_DECLARE_OPERATORS_FOR_FLAGS(KWin::AnimationEffect::TerminationFlags)
@ Forward
The animation goes from source to target.
quint64 set(EffectWindow *w, Attribute a, uint meta, int ms, const FPx2 &to, const QEasingCurve &curve=QEasingCurve(), int delay=0, const FPx2 &from=FPx2(), bool fullScreen=false, bool keepAlive=true, GLShader *shader=nullptr)
QMap< EffectWindow *, QPair< QList< AniData >, QRect > > AniMap
quint64 animate(EffectWindow *w, Attribute a, uint meta, int ms, const FPx2 &to, const QEasingCurve &curve=QEasingCurve(), int delay=0, const FPx2 &from=FPx2(), bool fullScreen=false, bool keepAlive=true, GLShader *shader=nullptr)
static qreal qecGaussian(qreal progress)
Representation of a window used by/for Effect classes.
FPx2(const QPointF &other)
FPx2 & operator=(const FPx2 &other)
FPx2(const QPoint &other)
FPx2(float v)
friend bool operator==(const FPx2 &f1, const FPx2 &f2)
FPx2 & operator*=(float fl)
FPx2 & operator/=(float fl)
void set(float v1, float v2)
FPx2 & operator-=(const FPx2 &other)
bool isValid() const
friend bool operator!=(const FPx2 &f1, const FPx2 &f2)
FPx2(const QSize &other)
friend const FPx2 operator-(const FPx2 &f1, const FPx2 &f2)
friend const FPx2 operator*(const FPx2 &f, float fl)
friend const FPx2 operator+(const FPx2 &f1, const FPx2 &f2)
void set(float v)
FPx2(float v1, float v2)
FPx2(const FPx2 &other)
float operator[](int n) const
FPx2 & operator+=(const FPx2 &other)
friend const FPx2 operator-(const FPx2 &f)
friend const FPx2 operator*(float fl, const FPx2 &f)
friend const FPx2 operator/(const FPx2 &f, float fl)
QString toString() const
void invalidate()
FPx2(const QSizeF &other)
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)