[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [calligra/krita-chili-kazakov] krita/plugins/tools/tool_transform2: Grab the nearest handle instead 
From:       Dmitry Kazakov <dimula73 () gmail ! com>
Date:       2014-11-14 11:00:42
Message-ID: E1XpEcM-0002W1-Oy () scm ! kde ! org
[Download RAW message or body]

Git commit e6532508b39074116d1cf865e7ffdd91bada0a1f by Dmitry Kazakov.
Committed on 14/11/2014 at 10:58.
Pushed by dkazakov into branch 'krita-chili-kazakov'.

Grab the nearest handle instead of the fist caught

Previously the fist handle that satisfied the radius condition was chosen.
That can cause reanny annoying effects, especially in perspective mode.

M  +29   -25   krita/plugins/tools/tool_transform2/kis_free_transform_strategy.cpp
M  +13   -9    krita/plugins/tools/tool_transform2/kis_perspective_transform_strategy.cpp
 M  +37   -0    krita/plugins/tools/tool_transform2/kis_transform_utils.h
M  +7    -3    krita/plugins/tools/tool_transform2/kis_warp_transform_strategy.cpp

http://commits.kde.org/calligra/e6532508b39074116d1cf865e7ffdd91bada0a1f

diff --git a/krita/plugins/tools/tool_transform2/kis_free_transform_strategy.cpp \
b/krita/plugins/tools/tool_transform2/kis_free_transform_strategy.cpp index \
                ca0123e..9809109 100644
--- a/krita/plugins/tools/tool_transform2/kis_free_transform_strategy.cpp
+++ b/krita/plugins/tools/tool_transform2/kis_free_transform_strategy.cpp
@@ -167,32 +167,36 @@ void KisFreeTransformStrategy::setTransformFunction(const \
QPointF &mousePos, boo  }
 
     QPolygonF transformedPolygon = \
                m_d->transform.map(QPolygonF(m_d->transaction.originalRect()));
-    m_d->function =
-        transformedPolygon.containsPoint(mousePos, Qt::OddEvenFill) ? MOVE : ROTATE;
-
     qreal handleRadius = \
                KisTransformUtils::effectiveHandleGrabRadius(m_d->converter);
-    qreal handleRadiusSq = pow2(handleRadius);
-
-    qreal rotationHandleRadiusSq = \
                pow2(KisTransformUtils::effectiveHandleGrabRadius(m_d->converter));
-
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.topMiddle) <= \
                handleRadiusSq)
-        m_d->function = TOPSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.topRight) <= \
                handleRadiusSq)
-        m_d->function = TOPRIGHTSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.middleRight) <= \
                handleRadiusSq)
-        m_d->function = RIGHTSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.bottomRight) <= \
                handleRadiusSq)
-        m_d->function = BOTTOMRIGHTSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.bottomMiddle) <= \
                handleRadiusSq)
-        m_d->function = BOTTOMSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.bottomLeft) <= \
                handleRadiusSq)
-        m_d->function = BOTTOMLEFTSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.middleLeft) <= \
                handleRadiusSq)
-        m_d->function = LEFTSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.topLeft) <= \
                handleRadiusSq)
-        m_d->function = TOPLEFTSCALE;
-    if (kisSquareDistance(mousePos, m_d->transformedHandles.rotationCenter) <= \
                rotationHandleRadiusSq)
-        m_d->function = MOVECENTER;
+    qreal rotationHandleRadius = \
KisTransformUtils::effectiveHandleGrabRadius(m_d->converter); +
+
+    StrokeFunction defaultFunction =
+        transformedPolygon.containsPoint(mousePos, Qt::OddEvenFill) ? MOVE : ROTATE;
+    KisTransformUtils::HandleChooser<StrokeFunction>
+        handleChooser(mousePos, defaultFunction);
+
+    handleChooser.addFunction(m_d->transformedHandles.topMiddle,
+                              handleRadius, TOPSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.topRight,
+                              handleRadius, TOPRIGHTSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.middleRight,
+                              handleRadius, RIGHTSCALE);
+
+    handleChooser.addFunction(m_d->transformedHandles.bottomRight,
+                              handleRadius, BOTTOMRIGHTSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.bottomMiddle,
+                              handleRadius, BOTTOMSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.bottomLeft,
+                              handleRadius, BOTTOMLEFTSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.middleLeft,
+                              handleRadius, LEFTSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.topLeft,
+                              handleRadius, TOPLEFTSCALE);
+    handleChooser.addFunction(m_d->transformedHandles.rotationCenter,
+                              rotationHandleRadius, MOVECENTER);
+
+    m_d->function = handleChooser.function();
 
     if (m_d->function == ROTATE || m_d->function == MOVE) {
         QRectF originalRect = m_d->transaction.originalRect();
diff --git a/krita/plugins/tools/tool_transform2/kis_perspective_transform_strategy.cpp \
b/krita/plugins/tools/tool_transform2/kis_perspective_transform_strategy.cpp index \
                7e7a360..ba32e44 100644
--- a/krita/plugins/tools/tool_transform2/kis_perspective_transform_strategy.cpp
+++ b/krita/plugins/tools/tool_transform2/kis_perspective_transform_strategy.cpp
@@ -157,26 +157,30 @@ void \
                KisPerspectiveTransformStrategy::setTransformFunction(const QPointF \
                &mouseP
         transformedPolygon.containsPoint(mousePos, Qt::OddEvenFill) ? MOVE : NONE;
 
     qreal handleRadius = \
                KisTransformUtils::effectiveHandleGrabRadius(m_d->converter);
-    qreal handleRadiusSq = pow2(handleRadius);
 
-    if (!m_d->transformedHandles.xVanishing.isNull() &&
-        kisSquareDistance(mousePos, m_d->transformedHandles.xVanishing) <= \
handleRadiusSq) { +    KisTransformUtils::HandleChooser<StrokeFunction>
+        handleChooser(mousePos, NONE);
 
-        m_d->function = DRAG_X_VANISHING_POINT;
+    if (!m_d->transformedHandles.xVanishing.isNull()) {
+        handleChooser.addFunction(m_d->transformedHandles.xVanishing,
+                                  handleRadius, DRAG_X_VANISHING_POINT);
     }
 
-    if (!m_d->transformedHandles.yVanishing.isNull() &&
-        kisSquareDistance(mousePos, m_d->transformedHandles.yVanishing) <= \
                handleRadiusSq) {
-        m_d->function = DRAG_Y_VANISHING_POINT;
+    if (!m_d->transformedHandles.yVanishing.isNull()) {
+        handleChooser.addFunction(m_d->transformedHandles.yVanishing,
+                                  handleRadius, DRAG_Y_VANISHING_POINT);
     }
 
     m_d->currentDraggingCornerPoint = -1;
     for (int i = 0; i < m_d->dstCornerPoints.size(); i++) {
-        if (kisSquareDistance(mousePos, m_d->dstCornerPoints[i]) <= handleRadiusSq) \
{ +        if (handleChooser.addFunction(m_d->dstCornerPoints[i],
+                                      handleRadius, DRAG_HANDLE)) {
+
             m_d->currentDraggingCornerPoint = i;
-            m_d->function = DRAG_HANDLE;
         }
     }
+
+    m_d->function = handleChooser.function();
 }
 
 QCursor KisPerspectiveTransformStrategy::getCurrentCursor() const
diff --git a/krita/plugins/tools/tool_transform2/kis_transform_utils.h \
b/krita/plugins/tools/tool_transform2/kis_transform_utils.h index de5f9bd..697d60a \
                100644
--- a/krita/plugins/tools/tool_transform2/kis_transform_utils.h
+++ b/krita/plugins/tools/tool_transform2/kis_transform_utils.h
@@ -26,6 +26,10 @@
 #include <QTransform>
 #include <QMatrix4x4>
 #include <kis_processing_visitor.h>
+#include <limits>
+
+// for kisSquareDistance only
+#include "kis_global.h"
 
 class ToolTransformArgs;
 class KisTransformWorker;
@@ -88,6 +92,39 @@ public:
     static void transformDevice(const ToolTransformArgs &config,
                                 KisPaintDeviceSP device,
                                 KisProcessingVisitor::ProgressHelper *helper);
+
+    template<typename Function>
+    class HandleChooser {
+    public:
+        HandleChooser(const QPointF &cursorPos, Function defaultFunction)
+            : m_cursorPos(cursorPos),
+              m_minDistance(std::numeric_limits<qreal>::max()),
+              m_function(defaultFunction)
+        {
+        }
+
+        bool addFunction(const QPointF &pt, qreal radius, Function function) {
+            bool result = false;
+            qreal distance = kisSquareDistance(pt, m_cursorPos);
+
+            if (distance < pow2(radius) && distance < m_minDistance) {
+                m_minDistance = distance;
+                m_function = function;
+                result = true;
+            }
+
+            return result;
+        }
+
+        Function function() const {
+            return m_function;
+        }
+
+    private:
+        QPointF m_cursorPos;
+        qreal m_minDistance;
+        Function m_function;
+    };
 };
 
 #endif /* __KIS_TRANSFORM_UTILS_H */
diff --git a/krita/plugins/tools/tool_transform2/kis_warp_transform_strategy.cpp \
b/krita/plugins/tools/tool_transform2/kis_warp_transform_strategy.cpp index \
                bb3dbbb..7bbabe9 100644
--- a/krita/plugins/tools/tool_transform2/kis_warp_transform_strategy.cpp
+++ b/krita/plugins/tools/tool_transform2/kis_warp_transform_strategy.cpp
@@ -118,17 +118,21 @@ KisWarpTransformStrategy::~KisWarpTransformStrategy()
 
 void KisWarpTransformStrategy::setTransformFunction(const QPointF &mousePos, bool \
perspectiveModifierActive)  {
-    double handleRadiusSq = \
pow2(KisTransformUtils::effectiveHandleGrabRadius(m_d->converter)); +    double \
handleRadius = KisTransformUtils::effectiveHandleGrabRadius(m_d->converter);  
     bool cursorOverPoint = false;
     m_d->pointIndexUnderCursor = -1;
 
+    KisTransformUtils::HandleChooser<Private::Mode>
+        handleChooser(mousePos, Private::NOTHING);
+
     const QVector<QPointF> &points = m_d->currentArgs.transfPoints();
     for (int i = 0; i < points.size(); ++i) {
-        if (kisSquareDistance(mousePos, points[i]) <= handleRadiusSq) {
+        if (handleChooser.addFunction(points[i],
+                                      handleRadius, Private::NOTHING)) {
+
             cursorOverPoint = true;
             m_d->pointIndexUnderCursor = i;
-            break;
         }
     }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic