uifw/AvKon/aknphysics/src/aknphysicsrestrictor.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:  Restrictor of AknPhysics
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32debug.h>
       
    20 
       
    21 #include "aknphysicsrestrictor.h"
       
    22 #include "aknphysicsparameterprovider.h"
       
    23 
       
    24 const TReal KNotRestrictedSegment( 1.0 );
       
    25 const TInt KEmptySpaceFactor( 100 );
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CAknPhysicsRestrictor::NewL
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CAknPhysicsRestrictor* CAknPhysicsRestrictor::NewL(
       
    34     CAknPhysicsParameterProvider* aProvider )
       
    35     {
       
    36     CAknPhysicsRestrictor* self = CAknPhysicsRestrictor::NewLC( aProvider );
       
    37     CleanupStack::Pop( self );
       
    38     return self;
       
    39     }
       
    40 
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CAknPhysicsRestrictor::NewLC
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CAknPhysicsRestrictor* CAknPhysicsRestrictor::NewLC( 
       
    47     CAknPhysicsParameterProvider* aProvider )
       
    48     {
       
    49     CAknPhysicsRestrictor* self = 
       
    50         new ( ELeave ) CAknPhysicsRestrictor( aProvider );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     return self;
       
    54     }
       
    55 
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CAknPhysicsRestrictor::~CAknPhysicsRestrictor
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CAknPhysicsRestrictor::~CAknPhysicsRestrictor()
       
    62     {
       
    63     }
       
    64 
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CAknPhysicsRestrictor::UpdatePhysicsEnvironment
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 void CAknPhysicsRestrictor::UpdatePhysicsEnvironment( const TSize& aWorldSize,
       
    71                                                       const TSize& aViewSize,
       
    72                                                       const TBool& aLandscape )
       
    73     {
       
    74     iWorldSize = aWorldSize;
       
    75     iViewSize = aViewSize;
       
    76     iLandscape = aLandscape;
       
    77     CalculateViewLimits();
       
    78     }
       
    79 
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CAknPhysicsRestrictor::ViewTopPosition()
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 TInt CAknPhysicsRestrictor::ViewTopPosition() const
       
    86     {
       
    87     TInt topPosition( 0 );
       
    88 
       
    89     if ( iLandscape )
       
    90         {
       
    91         topPosition = iViewSize.iWidth/2;
       
    92         }
       
    93     else
       
    94         {
       
    95         topPosition = iViewSize.iHeight/2;
       
    96         }
       
    97     return topPosition;
       
    98     }
       
    99 
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CAknPhysicsRestrictor::ViewBottomPosition()
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 TInt CAknPhysicsRestrictor::ViewBottomPosition() const
       
   106     {
       
   107     TInt bottomPosition( 0 );
       
   108 
       
   109     // If world size smaller than view size, bottom position is same as top
       
   110     // position
       
   111     if ( iLandscape && iViewSize.iWidth >= iWorldSize.iWidth
       
   112         || !iLandscape && iViewSize.iHeight >= iWorldSize.iHeight )
       
   113         {
       
   114         bottomPosition = ViewTopPosition();        
       
   115         }
       
   116         
       
   117     // Otherwise calculate bottom position according to world size
       
   118     else
       
   119         {
       
   120         if ( iLandscape )
       
   121             {
       
   122             bottomPosition = iWorldSize.iWidth - iViewSize.iWidth / 2;
       
   123             }
       
   124         else
       
   125             {
       
   126             bottomPosition = iWorldSize.iHeight - iViewSize.iHeight / 2;
       
   127             }   
       
   128         }
       
   129     return bottomPosition;
       
   130     }
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CAknPhysicsRestrictor::PositionRevealsEmptySpace()
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 TBool CAknPhysicsRestrictor::PositionRevealsEmptySpace( TPoint& aPosition )
       
   138     {
       
   139     TInt startTop( ViewTopPosition() );
       
   140     TInt endBottom( ViewBottomPosition() );
       
   141     TInt* positionCoordinate;
       
   142 
       
   143     if ( iLandscape )
       
   144         {
       
   145         positionCoordinate = &aPosition.iX;
       
   146         }
       
   147     else
       
   148         {
       
   149         positionCoordinate = &aPosition.iY;
       
   150         }
       
   151 
       
   152     if ( aPosition == TPoint()
       
   153         || *positionCoordinate >= startTop && *positionCoordinate <= endBottom )
       
   154         {
       
   155         return EFalse;
       
   156         }
       
   157     
       
   158     if ( *positionCoordinate < startTop || WorldFitsToView() )
       
   159         {
       
   160         *positionCoordinate = startTop;
       
   161         }
       
   162     else
       
   163         {
       
   164         *positionCoordinate = endBottom;
       
   165         }
       
   166     return ETrue;
       
   167     }
       
   168 
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CAknPhysicsRestrictor::AllowedViewPosition()
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 TBool CAknPhysicsRestrictor::AllowedViewPosition( TPoint& aPosition )
       
   175     {
       
   176     // No restrictions to empty space, view position always allowed
       
   177     if ( !iEmptySpaceRestricted )
       
   178         {
       
   179         return ETrue;
       
   180         }
       
   181     
       
   182     TBool allow( EFalse );
       
   183 
       
   184     TInt *coord;
       
   185     if ( iLandscape )
       
   186         {
       
   187         coord = &aPosition.iX;
       
   188         }
       
   189     else
       
   190         {
       
   191         coord = &aPosition.iY;
       
   192         }
       
   193 
       
   194     // View position is allowed
       
   195     if ( *coord >= iPositionTopLimit && *coord <= iPositionBottomLimit )
       
   196         {
       
   197         allow = ETrue;
       
   198         iViewReachedLimit = EFalse;
       
   199         }
       
   200 
       
   201     // View has reached the limit for the first time - adjust the position
       
   202     // exactly to limit position
       
   203     else if ( !iViewReachedLimit )
       
   204         {
       
   205         allow = ETrue;
       
   206         iViewReachedLimit = ETrue;
       
   207         if ( *coord < iPositionTopLimit )
       
   208             {
       
   209             *coord = iPositionTopLimit;
       
   210             }
       
   211         else
       
   212             {
       
   213             *coord = iPositionBottomLimit;
       
   214             }
       
   215         }
       
   216 
       
   217     // Else view has already reached the limit, view position is not allowed
       
   218     // and no need to notify physics observer
       
   219 
       
   220     return allow;
       
   221     }
       
   222 
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CAknPhysicsRestrictor::AdjustDragPoint()
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 void CAknPhysicsRestrictor::AdjustDragPoint( TPoint& aDragPoint )
       
   229     {
       
   230     if ( iViewReachedLimit )
       
   231         {
       
   232         iViewReachedLimit = EFalse;
       
   233 
       
   234         TInt *coord;
       
   235         if ( iLandscape )
       
   236             {
       
   237             coord = &aDragPoint.iX;
       
   238             }
       
   239         else
       
   240             {
       
   241             coord = &aDragPoint.iY;
       
   242             }
       
   243 
       
   244         if ( *coord < 0 )
       
   245             {
       
   246             *coord = -iEmptySpaceArea;
       
   247             }
       
   248         else
       
   249             {
       
   250             *coord = iEmptySpaceArea;
       
   251             }
       
   252         }
       
   253     }
       
   254 
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CAknPhysicsRestrictor::WorldSize()
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 TSize CAknPhysicsRestrictor::WorldSize() const
       
   261     {
       
   262     return iWorldSize;
       
   263     }
       
   264 
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CAknPhysicsRestrictor::WorldSize()
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 TBool CAknPhysicsRestrictor::WorldInLandscape() const
       
   271     {
       
   272     return iLandscape;
       
   273     }
       
   274 
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CAknPhysicsRestrictor::ParameterProvider()
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 CAknPhysicsParameterProvider* CAknPhysicsRestrictor::ParameterProvider() const
       
   281     {
       
   282     return iProvider;
       
   283     }
       
   284 
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CAknPhysicsRestrictor::PositionToViewTop()
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 void CAknPhysicsRestrictor::PositionToViewTop( TPoint& aPosition ) const
       
   291     {
       
   292     TInt topPos( ViewTopPosition() );
       
   293     if ( iLandscape )
       
   294         {
       
   295         aPosition.iX = topPos;
       
   296         }
       
   297     else
       
   298         {
       
   299         aPosition.iY = topPos;
       
   300         }
       
   301     }
       
   302 
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CAknPhysicsRestrictor::PositionToViewBottom()
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CAknPhysicsRestrictor::PositionToViewBottom( TPoint& aPosition ) const
       
   309     {
       
   310     TInt bottomPos( ViewBottomPosition() );
       
   311 
       
   312     if ( iLandscape )
       
   313         {
       
   314         aPosition.iX = bottomPos;
       
   315         }
       
   316     else
       
   317         {
       
   318         aPosition.iY = bottomPos;
       
   319         }
       
   320     }
       
   321 
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // CAknPhysicsRestrictor::PositionIsOutOfBoundaries()
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 TBool CAknPhysicsRestrictor::PositionIsOutOfBoundaries( 
       
   328     const TPoint& aPosition, const TBool& aTopCollision ) const
       
   329     {
       
   330     TInt startTop( ViewTopPosition() );
       
   331     TInt endBottom( ViewBottomPosition() );
       
   332     TInt positionCoordinate;
       
   333 
       
   334     if ( iLandscape )
       
   335         {
       
   336         positionCoordinate = aPosition.iX;
       
   337         }
       
   338     else
       
   339         {
       
   340         positionCoordinate = aPosition.iY;
       
   341         }
       
   342         
       
   343 
       
   344     if ( aTopCollision && positionCoordinate > startTop )
       
   345         {
       
   346         positionCoordinate = startTop;
       
   347         return ETrue;
       
   348         }
       
   349     else if ( !aTopCollision && positionCoordinate < endBottom )
       
   350         {
       
   351         positionCoordinate = endBottom;
       
   352         return ETrue;
       
   353         }
       
   354     return EFalse;
       
   355     }
       
   356 
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // CAknPhysicsRestrictor::CAknPhysicsRestrictor
       
   360 // ---------------------------------------------------------------------------
       
   361 //
       
   362 CAknPhysicsRestrictor::CAknPhysicsRestrictor(
       
   363     CAknPhysicsParameterProvider* aProvider )
       
   364     : 
       
   365     iWorldSize( 0, 0 ),
       
   366     iViewSize( 0, 0 ),
       
   367     iLandscape( EFalse ),
       
   368     iPositionTopLimit( 0 ),
       
   369     iPositionBottomLimit( 0 ),
       
   370     iEmptySpaceArea( 0 ),
       
   371     iProvider( aProvider ),
       
   372     iViewReachedLimit( EFalse ),
       
   373     iEmptySpaceRestricted( EFalse )
       
   374     {
       
   375     }
       
   376 
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // CAknPhysicsRestrictor::ConstructL
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 void CAknPhysicsRestrictor::ConstructL()
       
   383     {
       
   384     }
       
   385 
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // CAknPhysicsRestrictor::CalculateViewLimits
       
   389 // ---------------------------------------------------------------------------
       
   390 //
       
   391 void CAknPhysicsRestrictor::CalculateViewLimits()
       
   392     {
       
   393     if ( !iProvider )
       
   394         {
       
   395         return;
       
   396         }
       
   397         
       
   398     TInt viewHeight( iViewSize.iHeight );
       
   399     TInt worldHeight( iWorldSize.iHeight );
       
   400     TReal emptySegment( KNotRestrictedSegment );
       
   401 
       
   402     if ( iLandscape )
       
   403         {
       
   404         viewHeight = iViewSize.iWidth;
       
   405         worldHeight = iWorldSize.iWidth;
       
   406         }
       
   407         
       
   408     // Calculate bottom limit
       
   409     if ( viewHeight > worldHeight )
       
   410         {
       
   411         emptySegment = 
       
   412         	( TReal ) iProvider->ShortListEmptySpace() / KEmptySpaceFactor;
       
   413         iEmptySpaceArea = 
       
   414             worldHeight * ( emptySegment );
       
   415         iPositionBottomLimit = viewHeight / 2 + iEmptySpaceArea;
       
   416         }
       
   417     else
       
   418         {
       
   419         emptySegment = 
       
   420         	( TReal ) iProvider->LongListEmptySpace() / KEmptySpaceFactor;
       
   421         iEmptySpaceArea = 
       
   422             viewHeight * ( emptySegment );
       
   423         iPositionBottomLimit = worldHeight - viewHeight / 2 + iEmptySpaceArea;
       
   424         }
       
   425 
       
   426     // Calculate top limit
       
   427     iPositionTopLimit = viewHeight / 2 - iEmptySpaceArea;
       
   428     
       
   429     // Check if empty space is really restricted
       
   430     if ( emptySegment == KNotRestrictedSegment )
       
   431         {
       
   432         iEmptySpaceRestricted = EFalse;
       
   433         }
       
   434     else
       
   435         {
       
   436         iEmptySpaceRestricted = ETrue;
       
   437         }
       
   438     }
       
   439 
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // CAknPhysicsRestrictor::WorldFitsToView
       
   443 // ---------------------------------------------------------------------------
       
   444 //
       
   445 TBool CAknPhysicsRestrictor::WorldFitsToView()
       
   446     {
       
   447     if ( iLandscape && iWorldSize.iWidth < iViewSize.iWidth )
       
   448         {
       
   449         return ETrue;
       
   450         }
       
   451     else if ( !iLandscape && iWorldSize.iHeight < iViewSize.iHeight )
       
   452         {
       
   453         return ETrue;
       
   454         }
       
   455     return EFalse;
       
   456     }
       
   457