idlehomescreen/xmluirendering/uiengine/src/xngesturerecogniser.cpp
branchRCL_3
changeset 20 899e4666ea9a
parent 16 1526727a5b85
parent 19 79311d856354
child 21 45cc9ca502a9
equal deleted inserted replaced
16:1526727a5b85 20:899e4666ea9a
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Gesture helper implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32math.h>
       
    19 
       
    20 #include "xngesturerecogniser.h"
       
    21 #include "xngesturedefs.h"
       
    22 #include "xnpointarray.h"
       
    23 
       
    24 using namespace XnGestureHelper;
       
    25 
       
    26 const TInt minPointCount = 5;
       
    27 
       
    28 /**
       
    29  * Vector class (math)
       
    30  */
       
    31 NONSHARABLE_CLASS( TVector )
       
    32     {
       
    33 public:
       
    34     /**
       
    35      * Constructor
       
    36      * @param aFrom starting point of the vector
       
    37      * @param aTo ending point of the vector
       
    38      */
       
    39     TVector( const TPoint& aFrom, const TPoint& aTo )
       
    40             : iX( aTo.iX - aFrom.iX ),
       
    41               iY( aTo.iY - aFrom.iY )
       
    42         {
       
    43         }
       
    44 
       
    45     /** @return angle of the vector */
       
    46     TReal Angle() const
       
    47         {
       
    48         TReal angle = 0;
       
    49         TReal length = Length();
       
    50         if ( length != 0 )
       
    51             {
       
    52             Math::ACos( angle, iX / Length() );
       
    53             if ( iY < 0 )
       
    54                 {
       
    55                 angle = 2 * KPi - angle;
       
    56                 }
       
    57             }
       
    58         return Degrees( angle );
       
    59         }
       
    60 
       
    61     /** @return length of the vector */
       
    62     TReal Length() const
       
    63         {
       
    64         TReal length = 0;
       
    65         Math::Sqrt( length, iX * iX + iY * iY );
       
    66         return length;
       
    67         }
       
    68 
       
    69 private:
       
    70     /** @return radians in degrees */
       
    71     inline TReal Degrees( TReal aRadians ) const
       
    72         {
       
    73         return aRadians * 180 / KPi;
       
    74         }
       
    75 
       
    76 public:
       
    77     /// x coordinate that represent the vector
       
    78     TReal iX;
       
    79     /// y coordinate that represent the vector
       
    80     TReal iY;
       
    81     };
       
    82 
       
    83 /**
       
    84  * @return ETrue if points for a tap event
       
    85  */
       
    86 inline TBool IsTap( const TXnPointArray& aPoints )
       
    87     {
       
    88     // with tap, the pointer is not allowed to leave the tap area and come back
       
    89     // therefore, gesture length is not an acceptable test for tap, as it tests
       
    90     // only the last point. therefore, check if *any* point is outside tap area.
       
    91     TInt i = aPoints.Count(); // latest point if most likely to be outside tap area
       
    92     while ( --i >= 0 )
       
    93         {
       
    94         // use the raw point (from which no axis has been filtered out)
       
    95         // because tap should consider both axes even when Code() ignores one axis
       
    96         if ( KSamePointTolerance < Abs( aPoints.Raw( i ).iX - aPoints.Raw( 0 ).iX ) ||
       
    97              KSamePointTolerance < Abs( aPoints.Raw( i ).iY - aPoints.Raw( 0 ).iY ) )
       
    98             {
       
    99             return EFalse;
       
   100             }
       
   101         }
       
   102     return ETrue;
       
   103     }
       
   104 
       
   105 /**
       
   106  * @return Length of the gesture in points
       
   107  */
       
   108 inline TReal GestureLength( const TXnPointArray& aPoints )
       
   109     {
       
   110     return TVector( aPoints[0], aPoints[aPoints.Count() - 1] ).Length();
       
   111     }
       
   112 
       
   113 /**
       
   114  * @return ETrue if aAngleUnderTest is almost aAngle
       
   115  *         Closeness of the angles is controlled by KAngleTolerance
       
   116  */
       
   117 static TBool IsNear( TReal aAngleUnderTest, TReal aAngle )
       
   118     {
       
   119     return aAngle - KAngleTolerance <= aAngleUnderTest &&
       
   120            aAngleUnderTest <= aAngle + KAngleTolerance;
       
   121     }
       
   122 
       
   123 /**
       
   124  * @return the angle as a direction flags of TGesture
       
   125  */
       
   126 inline TXnGestureCode Direction( TReal aAngle )
       
   127     {
       
   128     TXnGestureCode direction = EGestureUnknown;
       
   129 
       
   130     if ( IsNear( aAngle, 90.0 ) )
       
   131         {
       
   132         direction = EGestureSwipeDown;
       
   133         }
       
   134     else if ( IsNear( aAngle, 180.0 ) )
       
   135         {
       
   136         direction = EGestureSwipeLeft;
       
   137         }
       
   138     else if ( IsNear( aAngle, 270.0 ) )
       
   139         {
       
   140         direction = EGestureSwipeUp;
       
   141         }
       
   142     else if ( 360.0 - KAngleTolerance <= aAngle || aAngle <= KAngleTolerance )
       
   143         {
       
   144         direction = EGestureSwipeRight;
       
   145         }
       
   146     else // for lint warning
       
   147         {
       
   148         // unknown angle
       
   149         }
       
   150 
       
   151     return direction;
       
   152     }
       
   153 
       
   154 /** @return direction between points */
       
   155 inline TXnGestureCode Direction( const TPoint& aFromPoint, const TPoint& aToPoint )
       
   156     {
       
   157     return Direction( TVector( aFromPoint, aToPoint ).Angle() );
       
   158     }
       
   159 
       
   160 /** @return overall direction between points */
       
   161 static TXnGestureCode GeneralDirection( const TXnPointArray& aPoints )
       
   162     {
       
   163     return Direction( aPoints[0], aPoints[aPoints.Count() - 1]);
       
   164     }
       
   165 
       
   166 /**
       
   167 * @return the last received point that is different that the latest point,
       
   168 *         or first point if no point is different than latest
       
   169 */
       
   170 inline TPoint PreviousPoint( const TXnPointArray& aPoints )
       
   171     {
       
   172     TPoint latestPoint = aPoints[aPoints.Count() - 1];
       
   173     TInt i = aPoints.Count() - 1;
       
   174     while ( --i >= 0 )
       
   175         {
       
   176         if ( latestPoint != aPoints[i] )
       
   177             {
       
   178             return aPoints[i];
       
   179             }
       
   180         }
       
   181     return aPoints[0];
       
   182     }
       
   183 
       
   184 /** @return direction between last two points */
       
   185 TXnGestureCode TXnGestureRecogniser::LastDirection( const TXnPointArray& aPoints ) const
       
   186     {
       
   187     if ( aPoints.Count() > minPointCount )
       
   188         {
       
   189         // return direction between latest and previous points.
       
   190         // pick the previous point that is different than the last point
       
   191         // because while using an x or y filter array, more than one
       
   192         // sequential points may look like the same point because
       
   193         // the differing coordinate coordinate is filtered out. For example,
       
   194         // if dragging left and slightly up, many y coordinates will have the
       
   195         // same value, while only x differs.
       
   196         return Direction( aPoints[ aPoints.Count() - minPointCount ],
       
   197                           aPoints[ aPoints.Count() - 1 ] );
       
   198         }
       
   199     return GeneralDirection( aPoints );
       
   200     }
       
   201 
       
   202 // ----------------------------------------------------------------------------
       
   203 // Return gesture code of a gesture formed by a sequence of points
       
   204 // ----------------------------------------------------------------------------
       
   205 //
       
   206 TXnGestureCode TXnGestureRecogniser::GestureCode( const TXnPointArray& aPoints ) const
       
   207     {
       
   208     __ASSERT_DEBUG( aPoints.Count() > 0, Panic( EGesturePanicIllegalLogic ) );
       
   209 
       
   210     if (aPoints.Count() <= 0)
       
   211         {
       
   212         return EGestureUnknown;
       
   213         }
       
   214 
       
   215     if ( GestureLength( aPoints ) >= KMinSwipeLength )
       
   216         {
       
   217         return GeneralDirection( aPoints );
       
   218         }
       
   219 
       
   220     // the pointer was moved but was either not moved far enough, or was
       
   221     // brought back to close to the starting point
       
   222     return EGestureUnknown;
       
   223     }