diff options
author | Ralf Engels <ralf.engels@nokia.com> | 2010-04-13 13:57:48 +0200 |
---|---|---|
committer | Ralf Engels <ralf.engels@nokia.com> | 2010-04-13 13:57:48 +0200 |
commit | c67b439d2cc7b49b79bf664204bf8ccc2a654951 (patch) | |
tree | de310bad9aca6d6df44541ed5d456844d41a9857 | |
parent | 959bc6e1177c987e7dff1d811dae26d18812eb37 (diff) |
Fix overshoot, remove velocity
-rw-r--r-- | qkineticscroller.cpp | 77 | ||||
-rw-r--r-- | qkineticscroller_p.h | 8 |
2 files changed, 40 insertions, 45 deletions
diff --git a/qkineticscroller.cpp b/qkineticscroller.cpp index 7a378d1..eb6d0e9 100644 --- a/qkineticscroller.cpp +++ b/qkineticscroller.cpp @@ -479,7 +479,7 @@ void QKineticScroller::setDpiFromWidget(QWidget *widget) void QKineticScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint64 deltaTime) { - qKSDebug() << "updateVelocity(dP = " << deltaPixelRaw << " [pix], dT = " << deltaTime << " [ms]) -- velocity: " << velocity << "[m/s]"; + qKSDebug() << "updateVelocity(dP = " << deltaPixelRaw << " [pix], dT = " << deltaTime << " [ms]) -- velocity: " << releaseVelocity << "[m/s]"; QPointF deltaPixel = deltaPixelRaw; @@ -489,16 +489,16 @@ void QKineticScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint6 qreal inversSmoothingFactor = ((qreal(1) - dragVelocitySmoothingFactor) * qreal(deltaTime) / qreal(1000)); QPointF newv = -deltaPixel / qreal(deltaTime) * qreal(1000) / pixelPerMeter; - newv = newv * (qreal(1) - inversSmoothingFactor) + velocity * inversSmoothingFactor; + newv = newv * (qreal(1) - inversSmoothingFactor) + releaseVelocity * inversSmoothingFactor; // newv = newv * dragVelocitySmoothingFactor + velocity * (qreal(1) - dragVelocitySmoothingFactor); if (deltaPixel.x()) - velocity.setX(qBound(-maximumVelocity, newv.x(), maximumVelocity)); + releaseVelocity.setX(qBound(-maximumVelocity, newv.x(), maximumVelocity)); if (deltaPixel.y()) - velocity.setY(qBound(-maximumVelocity, newv.y(), maximumVelocity)); + releaseVelocity.setY(qBound(-maximumVelocity, newv.y(), maximumVelocity)); - qKSDebug() << "resulting new velocity:" << velocity; + qKSDebug() << "resulting new velocity:" << releaseVelocity; } qreal QKineticScrollerPrivate::decelerate(qreal v, qreal t) @@ -592,11 +592,11 @@ void QKineticScrollerPrivate::handleDrag(const QPointF &position, qint64 timesta if (!canScrollX) { deltaPixel.setX(0); - velocity.setX(0); + releaseVelocity.setX(0); } if (!canScrollY) { deltaPixel.setY(0); - velocity.setY(0); + releaseVelocity.setY(0); } // if (firstDrag) { @@ -716,33 +716,33 @@ bool QKineticScrollerPrivate::releaseWhileDragging(QKineticScroller::Input, cons // more than one fast swipe in a row: add fastSwipeVelocity int signX = 0, signY = 0; - if (velocity.x()) - signX = (velocity.x() > 0) == (oldVelocity.x() > 0) ? 1 : -1; - if (velocity.y()) - signY = (velocity.y() > 0) == (oldVelocity.y() > 0) ? 1 : -1; + if (releaseVelocity.x()) + signX = (releaseVelocity.x() > 0) == (oldVelocity.x() > 0) ? 1 : -1; + if (releaseVelocity.y()) + signY = (releaseVelocity.y() > 0) == (oldVelocity.y() > 0) ? 1 : -1; - velocity.setX(signX * (oldVelocity.x() + (oldVelocity.x() > 0 ? fastSwipeVelocity.x() : -fastSwipeVelocity.x()))); - velocity.setY(signY * (oldVelocity.y() + (oldVelocity.y() > 0 ? fastSwipeVelocity.y() : -fastSwipeVelocity.y()))); + releaseVelocity.setX(signX * (oldVelocity.x() + (oldVelocity.x() > 0 ? fastSwipeVelocity.x() : -fastSwipeVelocity.x()))); + releaseVelocity.setY(signY * (oldVelocity.y() + (oldVelocity.y() > 0 ? fastSwipeVelocity.y() : -fastSwipeVelocity.y()))); - } else if (velocity >= minimumVelocity) { + } else if (releaseVelocity >= minimumVelocity) { // if we have a fast swipe, accelerate it to the fastSwipe velocity - if ((qAbs(velocity.x()) > maximumNonAcceleratedVelocity) && + if ((qAbs(releaseVelocity.x()) > maximumNonAcceleratedVelocity) && (fastSwipeVelocity.x() > maximumNonAcceleratedVelocity)) { - velocity.setX(velocity.x() > 0 ? fastSwipeVelocity.x() : -fastSwipeVelocity.x()); + releaseVelocity.setX(releaseVelocity.x() > 0 ? fastSwipeVelocity.x() : -fastSwipeVelocity.x()); } - if ((qAbs(velocity.y()) > maximumNonAcceleratedVelocity) && + if ((qAbs(releaseVelocity.y()) > maximumNonAcceleratedVelocity) && (fastSwipeVelocity.y() > maximumNonAcceleratedVelocity)) { - velocity.setY(velocity.y() > 0 ? fastSwipeVelocity.y() : -fastSwipeVelocity.y()); + releaseVelocity.setY(releaseVelocity.y() > 0 ? fastSwipeVelocity.y() : -fastSwipeVelocity.y()); } } - qKSDebug() << "release While dragging, velocity: "<<velocity<<"minimum velocity"<<minimumVelocity; + qKSDebug() << "release While dragging, velocity: "<<releaseVelocity<<"minimum velocity"<<minimumVelocity; if (overshootX || overshootY) { - velocity = QPointF(0, 0); + releaseVelocity = QPointF(0, 0); setState(QKineticScroller::StateScrolling); - } else if (velocity >= minimumVelocity) + } else if (releaseVelocity >= minimumVelocity) setState(QKineticScroller::StateScrolling); else setState(QKineticScroller::StateInactive); @@ -769,8 +769,6 @@ void QKineticScrollerPrivate::timerEventWhileScrolling() bool QKineticScrollerPrivate::pressWhileScrolling(QKineticScroller::Input, const QPointF &, qint64) { - oldVelocity = velocity; - setState(QKineticScroller::StatePressed); return true; } @@ -802,7 +800,7 @@ void QKineticScrollerPrivate::setState(QKineticScroller::State newstate) qWarning() << "State change from " << stateName(state) << " to " << stateName(newstate) << ", but timer is not active."; } } - velocity = QPointF(0, 0); + releaseVelocity = QPointF(0, 0); break; case QKineticScroller::StatePressed: @@ -812,7 +810,8 @@ void QKineticScrollerPrivate::setState(QKineticScroller::State newstate) } scrollToX = false; scrollToY = false; - velocity = QPointF(0, 0); + oldVelocity = releaseVelocity; + // releaseVelocity = QPointF(0, 0); break; case QKineticScroller::StateDragging: @@ -832,19 +831,12 @@ void QKineticScrollerPrivate::setState(QKineticScroller::State newstate) } scrollRelativeTimer.start(); scrollAbsoluteTimer.start(); - releaseVelocity = velocity; if (state == QKineticScroller::StateDragging) { - QPointF oldPos = q->contentPosition(); - QPointF maxPos = q->maximumContentPosition(); - QPointF oldClampedPos; - oldClampedPos.setX(qBound(qreal(0), oldPos.x(), maxPos.x())); - oldClampedPos.setY(qBound(qreal(0), oldPos.y(), maxPos.y())); - - QPointF overshootDistance = oldPos - oldClampedPos; + overshootPosition *= overshootDragResistanceFactor; - overshootStartTimeX = overshootStartTimeY = scrollAbsoluteTimer.elapsed() - M_PI / (overshootSpringConstantRoot * 2); - overshootVelocity = overshootDistance * overshootSpringConstantRoot; + overshootStartTimeX = overshootStartTimeY = qreal(scrollAbsoluteTimer.elapsed()) / 1000 - M_PI / (overshootSpringConstantRoot * 2); + overshootVelocity = overshootPosition * overshootSpringConstantRoot; } break; @@ -916,7 +908,7 @@ void QKineticScroller::scrollTo(const QPointF &pos, int scrollTime) d->scrollToPosition = pos; d->scrollToX = true; d->scrollToY = true; - d->velocity = v; + d->releaseVelocity = v; d->setState(QKineticScroller::StateScrolling); } @@ -966,7 +958,7 @@ void QKineticScrollerPrivate::setContentPositionHelper(const QPointF &deltaPos) { Q_Q(QKineticScroller); - QPointF oldPos = q->contentPosition(); + QPointF oldPos = q->contentPosition() + overshootPosition; QPointF newPos = oldPos + deltaPos; QPointF maxPos = q->maximumContentPosition(); @@ -999,14 +991,15 @@ void QKineticScrollerPrivate::setContentPositionHelper(const QPointF &deltaPos) qDebug() << "oldOver: " << oldOvershootY << " newOver: " << newOvershootY; // -- x axis if (oldOvershootX && (qSign(oldOvershootX) != qSign(newOvershootX))) { + newClampedPos.setX((oldOvershootX < 0) ? 0 : maxPos.x()); realOvershootDistance.setX(0); releaseVelocity.setX(0); overshootX = false; } else if (newOvershootX) { if (!oldOvershootX) { - overshootStartTimeX = scrollAbsoluteTimer.elapsed(); - overshootVelocity.setX(velocity.x()); + overshootStartTimeX = qreal(scrollAbsoluteTimer.elapsed()) / 1000; + overshootVelocity.setX(calculateVelocity(overshootStartTimeX).x()); scrollToX = false; overshootX = true; } @@ -1019,14 +1012,15 @@ void QKineticScrollerPrivate::setContentPositionHelper(const QPointF &deltaPos) // -- y axis if (oldOvershootY && (qSign(oldOvershootY) != qSign(newOvershootY))) { + newClampedPos.setY((oldOvershootY < 0) ? 0 : maxPos.y()); realOvershootDistance.setY(0); releaseVelocity.setY(0); overshootY = false; } else if (newOvershootY) { if (!oldOvershootY) { - overshootStartTimeY = scrollAbsoluteTimer.elapsed(); - overshootVelocity.setY(velocity.y()); + overshootStartTimeY = qreal(scrollAbsoluteTimer.elapsed()) / 1000; + overshootVelocity.setY(calculateVelocity(overshootStartTimeY).y()); scrollToY = false; overshootY = true; } @@ -1052,6 +1046,7 @@ void QKineticScrollerPrivate::setContentPositionHelper(const QPointF &deltaPos) } q->setContentPosition(newClampedPos, realOvershootDistance); + overshootPosition = newPos - newClampedPos; qDebug() << "setContentPositionHelper" << newClampedPos << " overshoot:" << realOvershootDistance; } diff --git a/qkineticscroller_p.h b/qkineticscroller_p.h index 1f5a564..ccaee6d 100644 --- a/qkineticscroller_p.h +++ b/qkineticscroller_p.h @@ -119,7 +119,6 @@ public: bool enabled; QKineticScroller::State state; QKineticScroller::OvershootPolicy overshootPolicy; - QPointF velocity; QPointF oldVelocity; QPointF pressPosition; @@ -127,12 +126,13 @@ public: qint64 pressTimestamp; qint64 lastTimestamp; - QPointF dragDistance; + QPointF dragDistance; // the distance we should move during the next drag timer event QPointF scrollToPosition; bool scrollToX; bool scrollToY; + QPointF overshootPosition; // the number of pixels we are overshooting (before overshootDragResistanceFactor) bool overshootX; bool overshootY; @@ -140,8 +140,8 @@ public: QElapsedTimer scrollRelativeTimer; QElapsedTimer scrollAbsoluteTimer; - QPointF releaseVelocity; - QPointF overshootVelocity; + QPointF releaseVelocity; // the starting velocity of the scrolling state + QPointF overshootVelocity; // the starting velocity when going into overshoot qreal overshootStartTimeX; qreal overshootStartTimeY; int timerId; |