KWin
Loading...
Searching...
No Matches
motionmanager.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
3 SPDX-FileCopyrightText: 2009 Lucas Murray <lmurray@undefinedfire.com>
4 SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9#pragma once
10
12
13namespace KWin
14{
15
19template<typename T>
20class KWIN_EXPORT Motion
21{
22public:
29 explicit Motion(T initial, double strength, double smoothness);
34 Motion(const Motion<T> &other);
36
37 inline T value() const
38 {
39 return m_value;
40 }
41 inline void setValue(const T value)
42 {
43 m_value = value;
44 }
45 inline T target() const
46 {
47 return m_target;
48 }
49 inline void setTarget(const T target)
50 {
51 m_start = m_value;
52 m_target = target;
53 }
54 inline T velocity() const
55 {
56 return m_velocity;
57 }
58 inline void setVelocity(const T velocity)
59 {
60 m_velocity = velocity;
61 }
62
63 inline double strength() const
64 {
65 return m_strength;
66 }
67 inline void setStrength(const double strength)
68 {
69 m_strength = strength;
70 }
71 inline double smoothness() const
72 {
73 return m_smoothness;
74 }
75 inline void setSmoothness(const double smoothness)
76 {
77 m_smoothness = smoothness;
78 }
79 inline T startValue()
80 {
81 return m_start;
82 }
83
87 inline T distance() const
88 {
89 return m_target - m_value;
90 }
91
96 void calculate(const int msec);
101 void finish();
102
103private:
104 T m_value;
105 T m_start;
106 T m_target;
107 T m_velocity;
108 double m_strength;
109 double m_smoothness;
110};
111
119class KWIN_EXPORT Motion1D : public Motion<double>
120{
121public:
122 explicit Motion1D(double initial = 0.0, double strength = 0.08, double smoothness = 4.0);
123 Motion1D(const Motion1D &other);
124 ~Motion1D();
125};
126
134class KWIN_EXPORT Motion2D : public Motion<QPointF>
135{
136public:
137 explicit Motion2D(QPointF initial = QPointF(), double strength = 0.08, double smoothness = 4.0);
138 Motion2D(const Motion2D &other);
139 ~Motion2D();
140};
141
154class KWIN_EXPORT WindowMotionManager
155{
156public:
160 explicit WindowMotionManager(bool useGlobalAnimationModifier = true);
162
166 void manage(EffectWindow *w);
170 inline void manage(const QList<EffectWindow *> &list)
171 {
172 for (int i = 0; i < list.size(); i++) {
173 manage(list.at(i));
174 }
175 }
180 void unmanage(EffectWindow *w);
185 void unmanageAll();
192 void calculate(int time);
200 void apply(EffectWindow *w, WindowPaintData &data);
206 void reset();
211 void reset(EffectWindow *w);
212
219 void moveWindow(EffectWindow *w, QPoint target, double scale = 1.0, double yScale = 0.0);
226 inline void moveWindow(EffectWindow *w, QRect target)
227 {
228 // TODO: Scale might be slightly different in the comparison due to rounding
229 moveWindow(w, target.topLeft(),
230 target.width() / double(w->width()), target.height() / double(w->height()));
231 }
232
237 QRectF transformedGeometry(EffectWindow *w) const;
243 void setTransformedGeometry(EffectWindow *w, const QRectF &geometry);
248 QRectF targetGeometry(EffectWindow *w) const;
255 EffectWindow *windowAtPoint(QPoint point, bool useStackingOrder = true) const;
256
260 inline QList<EffectWindow *> managedWindows() const
261 {
262 return m_managedWindows.keys();
263 }
268 inline bool isManaging(EffectWindow *w) const
269 {
270 return m_managedWindows.contains(w);
271 }
276 inline bool managingWindows() const
277 {
278 return !m_managedWindows.empty();
279 }
285 inline bool areWindowsMoving() const
286 {
287 return !m_movingWindowsSet.isEmpty();
288 }
293 inline bool isWindowMoving(EffectWindow *w) const
294 {
295 return m_movingWindowsSet.contains(w);
296 }
297
298private:
299 bool m_useGlobalAnimationModifier;
300 struct WindowMotion
301 {
302 // TODO: Rotation, etc?
303 Motion2D translation; // Absolute position
304 Motion2D scale; // xScale and yScale
305 };
306 QHash<EffectWindow *, WindowMotion> m_managedWindows;
307 QSet<EffectWindow *> m_movingWindowsSet;
308};
309
310template<typename T>
311Motion<T>::Motion(T initial, double strength, double smoothness)
312 : m_value(initial)
313 , m_start(initial)
314 , m_target(initial)
315 , m_velocity()
316 , m_strength(strength)
317 , m_smoothness(smoothness)
318{
319}
320
321template<typename T>
323 : m_value(other.value())
324 , m_start(other.target())
325 , m_target(other.target())
326 , m_velocity(other.velocity())
327 , m_strength(other.strength())
328 , m_smoothness(other.smoothness())
329{
330}
331
332template<typename T>
336
337template<typename T>
338void Motion<T>::calculate(const int msec)
339{
340 if (m_value == m_target && m_velocity == T()) { // At target and not moving
341 return;
342 }
343
344 // Poor man's time independent calculation
345 int steps = std::max(1, msec / 5);
346 for (int i = 0; i < steps; i++) {
347 T diff = m_target - m_value;
348 T strength = diff * m_strength;
349 m_velocity = (m_smoothness * m_velocity + strength) / (m_smoothness + 1.0);
350 m_value += m_velocity;
351 }
352}
353
354template<typename T>
356{
357 m_value = m_target;
358 m_velocity = T();
359}
360
361} // namespace KWin
Representation of a window used by/for Effect classes.
A single 1D motion dynamics object.
A single 2D motion dynamics object.
double strength() const
double smoothness() const
T velocity() const
void setTarget(const T target)
void setSmoothness(const double smoothness)
void calculate(const int msec)
T target() const
Motion(T initial, double strength, double smoothness)
void setStrength(const double strength)
T value() const
void setValue(const T value)
void setVelocity(const T velocity)
Motion(const Motion< T > &other)
T distance() const
Helper class for motion dynamics in KWin effects.
void manage(const QList< EffectWindow * > &list)
bool isWindowMoving(EffectWindow *w) const
QList< EffectWindow * > managedWindows() const
void moveWindow(EffectWindow *w, QRect target)
bool isManaging(EffectWindow *w) const