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