diff options
author | Tomas Junnonen <tomas.junnonen@nokia.com> | 2010-05-27 15:13:32 +0300 |
---|---|---|
committer | Tomas Junnonen <tomas.junnonen@nokia.com> | 2010-05-29 11:40:03 +0300 |
commit | 0febafcb773a518eaf895de23c1992ca0f395ad2 (patch) | |
tree | e8054eb607bf6af1851409898e2ee1dc55d3b7b6 /src/corelib/widgets/mphysics2dpanning.cpp | |
parent | 9c9b1763cf179392d94e3920eccb06729bcebc51 (diff) |
Fixes: 170526 - MPhysics2DPanning does not go above 33 fps
RevBy: MichaĆ Guminiak
Details:
Original QVariantAnimation in MPhysics2DPanning patch by Dominik Kapusta.
The fixed interval QTimeLine is replaced by a QVariantAnimation that has the advantage
of being driven by the Qt global timer that is capped at 60Hz. The physics integrator
also no longer depends on the frame count for its calculations.
Diffstat (limited to 'src/corelib/widgets/mphysics2dpanning.cpp')
-rw-r--r-- | src/corelib/widgets/mphysics2dpanning.cpp | 137 |
1 files changed, 63 insertions, 74 deletions
diff --git a/src/corelib/widgets/mphysics2dpanning.cpp b/src/corelib/widgets/mphysics2dpanning.cpp index d3d3bfc8..08ef21e8 100644 --- a/src/corelib/widgets/mphysics2dpanning.cpp +++ b/src/corelib/widgets/mphysics2dpanning.cpp @@ -23,9 +23,7 @@ #include "mphysics2dpanning.h" #include "mphysics2dpanning_p.h" -static const int PanningTimelineDuration = 1000000; /* in ms */ -static const int PanningTimelineInterval = 20; /* in ms */ -static const int PositionNoiseDampingDelta = 2; /* in px */ +static const int PositionNoiseDampingDelta = 2; /* in px */ MPhysics2DPanningPrivate::MPhysics2DPanningPrivate(MPhysics2DPanning *publicObject) : enabled(true), @@ -37,8 +35,7 @@ MPhysics2DPanningPrivate::MPhysics2DPanningPrivate(MPhysics2DPanning *publicObje pointerSpringX(0.0), pointerSpringY(0.0), sceneLastPos(QPointF()), - timeLine(new QTimeLine()), - currFrame(0), + panningAnimation(new PanningAnimation), pointerPressed(false), pointerSpringK(0.0), frictionC(0.0), @@ -53,75 +50,68 @@ MPhysics2DPanningPrivate::MPhysics2DPanningPrivate(MPhysics2DPanning *publicObje MPhysics2DPanningPrivate::~MPhysics2DPanningPrivate() { - delete timeLine; + delete panningAnimation; } -void MPhysics2DPanningPrivate::_q_integrator(int frame) +void MPhysics2DPanningPrivate::_q_integrator(const QVariant &value) { Q_Q(MPhysics2DPanning); + Q_UNUSED(value); qreal accX, accY; qreal tempPosX; qreal tempPosY; - int i = 0; tempPosX = posX; tempPosY = posY; - while (frame > currFrame) { - if (panDirection.testFlag(Qt::Horizontal)) { - q->integrateAxis(Qt::Horizontal, - posX, - velX, - accX, - pointerSpringX, - pointerPressed - ); - } else { - posX = 0.0; - velX = 0.0; - accX = 0.0; - } + if (panDirection.testFlag(Qt::Horizontal)) { + q->integrateAxis(Qt::Horizontal, + posX, + velX, + accX, + pointerSpringX, + pointerPressed + ); + } else { + posX = 0.0f; + velX = 0.0f; + accX = 0.0f; + } - if (panDirection.testFlag(Qt::Vertical)) { - q->integrateAxis(Qt::Vertical, - posY, - velY, - accY, - pointerSpringY, - pointerPressed - ); + if (panDirection.testFlag(Qt::Vertical)) { + q->integrateAxis(Qt::Vertical, + posY, + velY, + accY, + pointerSpringY, + pointerPressed + ); - } else { - posY = 0.0; - velY = 0.0; - accY = 0.0; - } + } else { + posY = 0.0f; + velY = 0.0f; + accY = 0.0f; + } - // Checking if the viewport is currently dragged beyond it's borders and the integration should - // continue even though the speed is low. - bool inRangeX = (panDirection.testFlag(Qt::Horizontal) == false) || - (posX >= range.left() && posX <= range.right()); + // Checking if the viewport is currently dragged beyond it's borders and the integration should + // continue even though the speed is low. + bool inRangeX = (panDirection.testFlag(Qt::Horizontal) == false) || + (posX >= range.left() && posX <= range.right()); - bool inRangeY = (panDirection.testFlag(Qt::Vertical) == false) || - (posY >= range.top() && posY <= range.bottom()); + bool inRangeY = (panDirection.testFlag(Qt::Vertical) == false) || + (posY >= range.top() && posY <= range.bottom()); // Integration stop condition. - if (inRangeX && inRangeY && - qAbs(accX) < 1 && - qAbs(accY) < 1 && - qAbs(velX) < 1 && - qAbs(velY) < 1 && - !pointerPressed) { - timeLine->stop(); - - emit q->panningStopped(); - - break; - } - - currFrame++; - i++; + if (inRangeX && inRangeY && + qAbs(accX) < 1 && + qAbs(accY) < 1 && + qAbs(velX) < 1 && + qAbs(velY) < 1 && + !pointerPressed) { + panningAnimation->stop(); + + emit q->panningStopped(); } if (tempPosX != posX || tempPosY != posY) { @@ -134,8 +124,7 @@ MPhysics2DPanning::MPhysics2DPanning(QObject *parent) d_ptr(new MPhysics2DPanningPrivate(this)) { Q_D(MPhysics2DPanning); - connect(d->timeLine, SIGNAL(frameChanged(int)), - this, SLOT(_q_integrator(int))); + connect(d->panningAnimation, SIGNAL(valueChanged(QVariant)), SLOT(_q_integrator(QVariant))); } @@ -221,20 +210,20 @@ void MPhysics2DPanning::start() { Q_D(MPhysics2DPanning); if (!inMotion()) { - d->velX = 0.0; - d->velY = 0.0; - - d->timeLine->setDuration(PanningTimelineDuration); - d->timeLine->setUpdateInterval(PanningTimelineInterval); - d->timeLine->setFrameRange(0, 29999); - d->timeLine->setCurrentTime(0); - d->timeLine->setCurveShape(QTimeLine::LinearCurve); - d->currFrame = 0; - d->timeLine->start(); + d->velX = 0.0f; + d->velY = 0.0f; + + // Duration does not matter as we loop until the physics termination condition is hit + d->panningAnimation->setDuration(1000000); + d->panningAnimation->setLoopCount(-1); + + d->panningAnimation->setStartValue(0.0f); + d->panningAnimation->setEndValue(1.0f); + + d->panningAnimation->start(); } } - void MPhysics2DPanning::stop() { Q_D(MPhysics2DPanning); @@ -248,7 +237,7 @@ void MPhysics2DPanning::stop() (d->posY >= d->range.top() && d->posY <= d->range.bottom()); if (inRangeX && inRangeY) { - d->timeLine->stop(); + d->panningAnimation->stop(); emit panningStopped(); } } @@ -320,7 +309,7 @@ bool MPhysics2DPanning::inMotion() const { Q_D(const MPhysics2DPanning); - return (d->timeLine->state() == QTimeLine::Running); + return (d->panningAnimation->state() == QAbstractAnimation::Running); } @@ -334,8 +323,8 @@ void MPhysics2DPanning::pointerPress(const QPointF &pos) d->pointerPressed = true; d->sceneLastPos = pos; - d->pointerSpringX = 0.0; - d->pointerSpringY = 0.0; + d->pointerSpringX = 0.0f; + d->pointerSpringY = 0.0f; } @@ -470,7 +459,7 @@ void MPhysics2DPanning::integrateAxis(Qt::Orientation orientation, velocity += acceleration; position += velocity; - pointerDifference += velocity; + pointerDifference += velocity; } } |