From 123ebf1d957e48227b0819d8236cba57aa52a0d2 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Sat, 9 Sep 2017 13:30:51 -0400 Subject: [PATCH] Improved drawing of end of current stroke Distance between the last drawn point and the current point is calculated to be able to discard very short stroke segments (i.e we only add to the current stroke if the input device has moved more than a certain distance since the last drawn point). This commit moves this code from the stroke to the scene, which allows to calculate distance more accurately: it is now calculated as the total, absolute distance traveled since the last point, rather than simply the length of a line between the last point and current one. --- src/domain/UBGraphicsScene.cpp | 21 +++++++++++++++++---- src/domain/UBGraphicsScene.h | 1 + src/domain/UBGraphicsStroke.cpp | 8 -------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 9b6a1958..4327a495 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -561,9 +561,22 @@ bool UBGraphicsScene::inputDeviceMove(const QPointF& scenePos, const qreal& pres interpolator = UBInterpolator::Bezier; } - QList > newPoints = mCurrentStroke->addPoint(scenePos, width, interpolator); - if (newPoints.length() > 1) { - drawCurve(newPoints); + + // Don't draw segments smaller than a certain length. This can help with performance + // (less polygons to draw) but mostly with making the curve look smooth. + + qreal antiScaleRatio = 1./(UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom()); + qreal MIN_DISTANCE = 10*antiScaleRatio; // arbitrary. Move to settings if relevant. + qreal distance = QLineF(mPreviousPoint, scenePos).length(); + + mDistanceFromLastStrokePoint += distance; + + if (mDistanceFromLastStrokePoint > MIN_DISTANCE) { + QList > newPoints = mCurrentStroke->addPoint(scenePos, width, interpolator); + if (newPoints.length() > 1) + drawCurve(newPoints); + + mDistanceFromLastStrokePoint = 0; } if (interpolator == UBInterpolator::Bezier) { @@ -577,7 +590,7 @@ bool UBGraphicsScene::inputDeviceMove(const QPointF& scenePos, const qreal& pres mTempPolygon = NULL; } - QPointF lastDrawnPoint = newPoints.last().first; + QPointF lastDrawnPoint = mCurrentStroke->points().last().first; mTempPolygon = lineToPolygonItem(QLineF(lastDrawnPoint, scenePos), mPreviousWidth, width); addItem(mTempPolygon); diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 18a8c05a..84d01514 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -429,6 +429,7 @@ public slots: QPointF mPreviousPoint; qreal mPreviousWidth; + qreal mDistanceFromLastStrokePoint; QList mPreviousPolygonItems; diff --git a/src/domain/UBGraphicsStroke.cpp b/src/domain/UBGraphicsStroke.cpp index 8218624d..4456b26d 100644 --- a/src/domain/UBGraphicsStroke.cpp +++ b/src/domain/UBGraphicsStroke.cpp @@ -102,14 +102,6 @@ QList > UBGraphicsStroke::addPoint(const QPointF& point, q // The curve we are interpolating is not between two drawn points; // it is between the midway points of the second-to-last and last point, and last and current point. - // Don't draw segments smaller than a certain length. This can help with performance - // (less polygons to draw) but mostly with keeping the curve smooth. - qreal MIN_DISTANCE = 3*mAntiScaleRatio; - qreal distance = QLineF(mReceivedPoints.last().first, newPoint.first).length(); - - if (distance < MIN_DISTANCE) { - return QList() << mDrawnPoints.last(); - } // The first segment is just a straight line to the first midway point if (n == 1) {