uifw/AvKon/aknphysics/src/aknphysicsflicktracker.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2009 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:  AknPhysics flick tracker
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32debug.h>
       
    19 
       
    20 #include "aknphysicsconstants.h"
       
    21 #include "aknphysicsflicktracker.h"
       
    22 #include "aknphysicsparameterprovider.h"
       
    23 
       
    24 const TInt KDistanceFactor( 1000000 );
       
    25 const TInt KSpeedUpIncrease( 500 );
       
    26 const TInt KIgnoreSpeedUpEvents( 3 );
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CAknPhysicsFlickTracker::NewL
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CAknPhysicsFlickTracker* CAknPhysicsFlickTracker::NewL(
       
    35         CAknPhysicsParameterProvider* aProvider )
       
    36     {
       
    37     CAknPhysicsFlickTracker* self = 
       
    38         CAknPhysicsFlickTracker::NewLC( aProvider );
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CAknPhysicsFlickTracker::NewLC
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CAknPhysicsFlickTracker* CAknPhysicsFlickTracker::NewLC(
       
    49         CAknPhysicsParameterProvider* aProvider )
       
    50     {
       
    51     CAknPhysicsFlickTracker* self 
       
    52         = new ( ELeave ) CAknPhysicsFlickTracker( aProvider );
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     return self;
       
    56     }
       
    57 
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CAknPhysicsFlickTracker::~CAknPhysicsDragObserver
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CAknPhysicsFlickTracker::~CAknPhysicsFlickTracker()
       
    64     {
       
    65     }
       
    66 
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // CAknPhysicsFlickTracker::InitTracker
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void CAknPhysicsFlickTracker::InitTracker( const TInt& aCoordinate )
       
    73     {
       
    74     iDirection = EDirectionNone;
       
    75     iCoordinate = aCoordinate;
       
    76     InitFlickData( iDirectionData, aCoordinate );
       
    77     InitFlickData( iNormalStartData, aCoordinate );
       
    78     InitFlickData( iSpeedUpData, aCoordinate );
       
    79     InitFlickData( iSpeedUpStartData, aCoordinate );
       
    80     iLastDragTime.HomeTime();
       
    81     iChangeRegistered = EFalse;
       
    82     iSpeedUpRegistered = EFalse;
       
    83     iLastDragSpeed = 0;
       
    84     iIgnoredEvents = 0;
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CAknPhysicsFlickTracker::DragChanged
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CAknPhysicsFlickTracker::DragChanged( const TInt& aCoordinate )
       
    93     {
       
    94     TBool changed( EFalse );
       
    95     TBool registered( EFalse );
       
    96 
       
    97     // First check if drag direction has changed
       
    98     TDirection direction( EDirectionNone );
       
    99     if ( aCoordinate > iCoordinate 
       
   100         && iDirection != EDirectionForward )
       
   101         {
       
   102         direction = EDirectionForward;
       
   103         }
       
   104     else if ( aCoordinate < iCoordinate 
       
   105         && iDirection != EDirectionBackward )
       
   106         {
       
   107         direction = EDirectionBackward;
       
   108         }
       
   109 
       
   110     if ( direction != EDirectionNone )
       
   111         {
       
   112         // Direction change only after first direction is saved
       
   113         if ( iDirection != EDirectionNone )
       
   114             {
       
   115             iDirectionData.iCoordinate = aCoordinate;
       
   116             iDirectionData.iTime.HomeTime();
       
   117             iChangeRegistered = EFalse;
       
   118             changed = ETrue;
       
   119             }
       
   120         iDirection = direction;
       
   121         }
       
   122 
       
   123     if ( !iChangeRegistered && iDirectionData.iCoordinate != iNormalStartData.iCoordinate )
       
   124         {
       
   125         TInt distance( iDirectionData.iCoordinate - aCoordinate );
       
   126         // Distance from direction change position has exceeded threshold
       
   127         // - save the direction changed position as new start position
       
   128         if ( Threshold() && Abs( distance ) > Threshold() )
       
   129             {
       
   130             iNormalStartData = iDirectionData;
       
   131             iChangeRegistered = ETrue;
       
   132             registered = ETrue;
       
   133             }
       
   134         }
       
   135 
       
   136     // No speed changes are registered when direction has changed
       
   137     if ( !changed && !registered )
       
   138         {
       
   139         SpeedChanged( aCoordinate );    	
       
   140         }
       
   141     // If direction was changed - reset speed change data
       
   142     else if ( registered )
       
   143         {
       
   144         iSpeedUpData = iNormalStartData;
       
   145         iSpeedUpStartData = iNormalStartData;
       
   146         iSpeedUpRegistered = EFalse;
       
   147         iIgnoredEvents = 0;
       
   148         iLastDragSpeed = 0;
       
   149         }
       
   150     
       
   151     iCoordinate = aCoordinate;
       
   152     iLastDragTime.HomeTime();
       
   153     }
       
   154 
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // CAknPhysicsFlickTracker::FlickStartCoordinate
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 TInt CAknPhysicsFlickTracker::FlickStartCoordinate() const
       
   161     {
       
   162     // If move time from flick start point has exceeded flick maximum duration,
       
   163     // check if speed has increased and use speed-up position parameters
       
   164     if ( NormalFlickMoveTime() > KFlickMaxDuration )
       
   165         {
       
   166         return iSpeedUpStartData.iCoordinate;
       
   167         }
       
   168     return iNormalStartData.iCoordinate;
       
   169     }
       
   170 
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CAknPhysicsFlickTracker::FlickMoveTime
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 TInt CAknPhysicsFlickTracker::FlickMoveTime() const
       
   177     {
       
   178     // If move time from flick start point has exceeded flick maximum duration,
       
   179     // check if speed has increased and use speed-up position parameters
       
   180     if ( NormalFlickMoveTime() > KFlickMaxDuration )
       
   181         {
       
   182         TTime now;
       
   183         now.HomeTime();
       
   184         return now.MicroSecondsFrom( iSpeedUpStartData.iTime ).Int64();
       
   185         }
       
   186     return NormalFlickMoveTime();
       
   187     }
       
   188 
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // CAknPhysicsFlickTracker::CAknPhysicsFlickTracker
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 CAknPhysicsFlickTracker::CAknPhysicsFlickTracker( 
       
   195         CAknPhysicsParameterProvider* aProvider )
       
   196     :
       
   197     iDirection( EDirectionNone ),
       
   198     iProvider( aProvider )
       
   199     {
       
   200     }
       
   201 
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CAknPhysicsFlickTracker::ConstructL
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 void CAknPhysicsFlickTracker::ConstructL()
       
   208     {
       
   209     }
       
   210 
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CAknPhysicsFlickTracker::SpeedChanged
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 void CAknPhysicsFlickTracker::SpeedChanged( const TInt& aCoordinate )
       
   217     {
       
   218     TInt newSpeed( DragSpeed( aCoordinate ) );
       
   219 
       
   220     // iIgnoreEvents keeps track of such events which speedup positions should
       
   221     // be ignored. This is done to calculate speed-up parameters from the first
       
   222     // successive speed-up positions
       
   223     if ( iIgnoredEvents > 0 )
       
   224         {
       
   225         iIgnoredEvents--;
       
   226         }
       
   227 
       
   228     // New speed exceeds the speed threshold - save speed up data
       
   229     if ( iIgnoredEvents == 0 
       
   230         && iLastDragSpeed && ( newSpeed - iLastDragSpeed > KSpeedUpIncrease )
       
   231         && ( ( iDirection == EDirectionForward && aCoordinate > iCoordinate )
       
   232         ||  ( iDirection == EDirectionBackward && aCoordinate < iCoordinate ) ) )
       
   233         {
       
   234         iSpeedUpData.iCoordinate = iCoordinate;
       
   235         iSpeedUpData.iTime = iLastDragTime;
       
   236         iSpeedUpRegistered = EFalse;
       
   237         iIgnoredEvents = KIgnoreSpeedUpEvents;
       
   238         iSpeedUpStartData = iNormalStartData;
       
   239         }
       
   240 
       
   241     // Speed-up hasn't yet been registered - save speed up start data if the
       
   242     // drag distance exceeds threshold
       
   243     if  ( !iSpeedUpRegistered  && iSpeedUpData.iCoordinate != iSpeedUpStartData.iCoordinate )
       
   244         {
       
   245         TInt distance( iSpeedUpData.iCoordinate - aCoordinate );
       
   246         // Distance from direction change position has exceeded threshold
       
   247         // - save the direction changed position as new start position
       
   248         if ( Threshold() && Abs( distance ) > Threshold() )
       
   249             {
       
   250             iSpeedUpStartData = iSpeedUpData;
       
   251             iSpeedUpRegistered = ETrue;
       
   252             }
       
   253         }
       
   254     iLastDragSpeed = newSpeed;
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // CAknPhysicsFlickTracker::Threshold
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 TInt CAknPhysicsFlickTracker::Threshold()
       
   262     {
       
   263     if ( iProvider )
       
   264         {
       
   265         return iProvider->DragThreshold();
       
   266         }
       
   267     return 0;
       
   268     }
       
   269 
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // CAknPhysicsFlickTracker::DragSpeed
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 TInt CAknPhysicsFlickTracker::DragSpeed( const TInt& aNewCoordinate )
       
   276     {
       
   277     TTime now;
       
   278     now.HomeTime();
       
   279     TInt moveTime( now.MicroSecondsFrom( iLastDragTime ).Int64() );
       
   280     TInt distance( Abs( iCoordinate - aNewCoordinate ) * KDistanceFactor );
       
   281     if ( moveTime )
       
   282         {
       
   283         return ( distance / moveTime ); 
       
   284         }
       
   285     return 0;
       
   286     }
       
   287 
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // CAknPhysicsFlickTracker::InitFlickData
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 void CAknPhysicsFlickTracker::InitFlickData( TAknPhysicsTrackerData& aData, 
       
   294         const TInt& aCoordinate )
       
   295     {
       
   296     aData.iCoordinate = aCoordinate;
       
   297     aData.iTime.HomeTime();
       
   298     }
       
   299 
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // CAknPhysicsFlickTracker::NormalFlickMoveTime
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 TInt CAknPhysicsFlickTracker::NormalFlickMoveTime() const
       
   306     {
       
   307     TTime now;
       
   308     now.HomeTime();
       
   309     return now.MicroSecondsFrom( iNormalStartData.iTime ).Int64();
       
   310     }