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