2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#ifndef UBUNTU_GESTURES_DAMPER_H
18
#define UBUNTU_GESTURES_DAMPER_H
20
#include <QtCore/QPointF>
23
Decreases the oscillations of a value along an axis.
25
template <class Type> class Damper {
27
Damper() : m_value(0), m_maxDelta(0) { }
29
// Maximum delta between the raw value and its dampened counterpart.
30
void setMaxDelta(Type maxDelta) {
31
if (maxDelta < 0) qFatal("Damper::maxDelta must be a positive number.");
32
m_maxDelta = maxDelta;
34
Type maxDelta() const { return m_maxDelta; }
36
void reset(Type value) {
40
Type update(Type value) {
41
Type delta = value - m_value;
42
if (delta > 0 && delta > m_maxDelta) {
43
m_value += delta - m_maxDelta;
44
} else if (delta < 0 && delta < -m_maxDelta) {
45
m_value += delta + m_maxDelta;
51
Type value() const { return m_value; }
59
A point that has its movement dampened.
63
void setMaxDelta(qreal maxDelta) {
64
m_x.setMaxDelta(maxDelta);
65
m_y.setMaxDelta(maxDelta);
68
qreal maxDelta() const { return m_x.maxDelta(); }
70
void reset(const QPointF &point) {
75
void update(const QPointF &point) {
76
m_x.update(point.x());
77
m_y.update(point.y());
80
qreal x() const { return m_x.value(); }
81
qreal y() const { return m_y.value(); }
87
QDebug operator<<(QDebug dbg, const DampedPointF &p);
89
#endif // UBUNTU_GESTURES_DAMPER_H