taskswitcher/taskswitcherui/taskswitcherapp/src/tsphysics.cpp
changeset 4 4d54b72983ae
equal deleted inserted replaced
3:fb3763350a08 4:4d54b72983ae
       
     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:  Application class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <aknphysicsobserveriface.h>
       
    20 
       
    21 #include "tsphysics.h"
       
    22 
       
    23 const TInt KSingleItemChangeAnimTime = 1000000; // 1 second
       
    24 const TInt KAnimationFrameCount = 15; // 15 frames per second
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CTsPhysics::CTsPhysics
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CTsPhysics::CTsPhysics( MAknPhysicsObserver& aObserver ) :
       
    31     CActive( EPriorityStandard ), iObserver( aObserver )
       
    32     {
       
    33     }
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CTsPhysics::NewLC
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CTsPhysics* CTsPhysics::NewLC( MAknPhysicsObserver& aObserver )
       
    40     {
       
    41     CTsPhysics* self = new ( ELeave ) CTsPhysics( aObserver );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     return self;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CTsPhysics::NewL
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CTsPhysics* CTsPhysics::NewL( MAknPhysicsObserver& aObserver )
       
    52     {
       
    53     CTsPhysics* self = CTsPhysics::NewLC( aObserver );
       
    54     CleanupStack::Pop();
       
    55     return self;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CTsPhysics::ConstructL
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void CTsPhysics::ConstructL()
       
    63     {
       
    64     User::LeaveIfError( iTimer.CreateLocal() );
       
    65     CActiveScheduler::Add( this );
       
    66     // Animation parameters
       
    67     const TInt KInitValue = 0;
       
    68     iAnimationTickTime = KSingleItemChangeAnimTime / KSingleItemChangeAnimTime;
       
    69     iPhysicsState = EStopped;
       
    70     iAnimationTicks = KInitValue;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CTsPhysics::~CTsPhysics
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 CTsPhysics::~CTsPhysics()
       
    78     {
       
    79     Cancel();
       
    80     iTimer.Close();
       
    81     iAnimationSteps.Close();
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CTsPhysics::DoCancel
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 void CTsPhysics::DoCancel()
       
    89     {
       
    90     iTimer.Cancel();
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CTsPhysics::StartPhysics
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CTsPhysics::StartPhysics( const TPoint& aTarget )
       
    98     {
       
    99     Cancel();
       
   100     // Setup animation
       
   101     TPoint currentPoint = iObserver.ViewPosition();
       
   102     if ( currentPoint.iX != aTarget.iX )
       
   103         {
       
   104         iAnimationTicks = 0;
       
   105         if ( CalculateAnimationSteps( aTarget ) == KErrNone )
       
   106             {
       
   107             // Request
       
   108             iTimer.After( iStatus, 0 );
       
   109             SetActive();
       
   110             }
       
   111         else
       
   112             {
       
   113             // If calculation failes (no memory) or there is
       
   114             // only a small step, set view to target
       
   115             iObserver.ViewPositionChanged( aTarget, ETrue, 0);
       
   116             iObserver.PhysicEmulationEnded();
       
   117             }
       
   118         }
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CTsPhysics::StopPhysics
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CTsPhysics::StopPhysics()
       
   126     {
       
   127     Cancel();
       
   128     if ( iPhysicsState == ERunning || iPhysicsState == EFinished )
       
   129         {
       
   130         iObserver.PhysicEmulationEnded();
       
   131         }
       
   132     iPhysicsState = EStopped;
       
   133     iAnimationTicks = 0;
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CTsPhysics::RunL
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 void CTsPhysics::RunL()
       
   141     {
       
   142     if ( iPhysicsState == EStopped )
       
   143         {
       
   144         // Start animation
       
   145         iPhysicsState = ERunning;
       
   146         iTimer.After( iStatus, iAnimationTickTime );
       
   147         SetActive();
       
   148         }
       
   149     else if ( iPhysicsState == ERunning )
       
   150         {
       
   151         // Ongoing animation
       
   152         if ( iAnimationTicks >= 0 && iAnimationTicks < KAnimationFrameCount )
       
   153             {
       
   154             iObserver.ViewPositionChanged( iObserver.ViewPosition()+iAnimationSteps[iAnimationTicks], ETrue, 0 );
       
   155             }
       
   156         iAnimationTicks++;
       
   157         if ( iAnimationTicks >= KAnimationFrameCount )
       
   158             {
       
   159             iPhysicsState = EFinished;
       
   160             }
       
   161         iTimer.After( iStatus, iAnimationTickTime );
       
   162         SetActive();
       
   163         }
       
   164     else if ( iPhysicsState == EFinished )
       
   165         {
       
   166         iAnimationTicks = 0;
       
   167         iPhysicsState = EStopped;
       
   168         iObserver.PhysicEmulationEnded();
       
   169         }
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CTsPhysics::RunError
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 TInt CTsPhysics::RunError( TInt /*aError*/ )
       
   177     {
       
   178     return KErrNone;
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CTsPhysics::CalculateAnimationSteps
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TInt CTsPhysics::CalculateAnimationSteps( const TPoint& aTarget )
       
   186     {
       
   187     TInt retVal( KErrNone );
       
   188     iAnimationSteps.Reset();
       
   189     retVal = iAnimationSteps.Reserve( KAnimationFrameCount );
       
   190     if ( retVal == KErrNone )
       
   191         {
       
   192         TInt yValue = aTarget.iY;
       
   193         TPoint currentPos = iObserver.ViewPosition();
       
   194         TInt moveLen = aTarget.iX - currentPos.iX;
       
   195         if ( moveLen > KAnimationFrameCount || moveLen < -KAnimationFrameCount )
       
   196             {
       
   197             TInt singleStep = moveLen / KAnimationFrameCount;
       
   198             for ( TInt i = 0; i < KAnimationFrameCount - 1; i++ )
       
   199                 {
       
   200                 iAnimationSteps.Append( TPoint( singleStep, yValue ) );
       
   201                 }
       
   202             TInt lastStep = moveLen - ( ( KAnimationFrameCount - 1 ) * singleStep );
       
   203             iAnimationSteps.Append( TPoint( lastStep, yValue ) );
       
   204             }
       
   205         else
       
   206             {
       
   207             retVal = KErrArgument;
       
   208             }
       
   209         }
       
   210     return retVal;
       
   211     }