menufw/menufwui/mmwidgets/src/mmdraweranimator.cpp
changeset 0 f72a12da539e
child 1 5315654608de
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2007 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:  
       
    15 *  Version     : %version: MM_40 % << Don't touch! Updated by Synergy at check-out.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <AknUtils.h>
       
    20 
       
    21 #include "mmdraweranimator.h"
       
    22 #include "mmlistboxitemdrawer.h"
       
    23 #include "mmfloatingitem.h"
       
    24 #include "mmwidgetcontainer.h"
       
    25 
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 // -----------------------------------------------------------------------------
       
    30 // 
       
    31 CMmDrawerAnimator::CMmDrawerAnimator(CMmListBoxItemDrawer& aDrawer) :
       
    32     CActive( AknLayoutUtils::PenEnabled() ?
       
    33     		EPriorityRealTime : 
       
    34 			EPriorityAbsoluteRealTime8 ), iDrawer(aDrawer), 
       
    35     iLastNotedHighlight( KErrNotFound ), iLastNotedTopItem( KErrNotFound ),
       
    36     iLastNotedVerticalOffset( 0 ),
       
    37     iUsualAnimationFramesCount( AknLayoutUtils::PenEnabled() ?
       
    38     		MmEffects::KUsualAnimationFramesCount : 
       
    39 			MmEffects::KUsualAnimationFramesCountNonTouch )
       
    40     {
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 // -----------------------------------------------------------------------------
       
    46 // 
       
    47 CMmDrawerAnimator* CMmDrawerAnimator::NewLC(CMmListBoxItemDrawer& aDrawer)
       
    48     {
       
    49     CMmDrawerAnimator* self = new ( ELeave ) CMmDrawerAnimator(aDrawer);
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     return self;
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 // -----------------------------------------------------------------------------
       
    58 // 
       
    59 CMmDrawerAnimator* CMmDrawerAnimator::NewL(CMmListBoxItemDrawer& aDrawer)
       
    60     {
       
    61     CMmDrawerAnimator* self = CMmDrawerAnimator::NewLC(aDrawer);
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 // -----------------------------------------------------------------------------
       
    69 // 
       
    70 void CMmDrawerAnimator::ConstructL()
       
    71     {
       
    72     User::LeaveIfError( iTimer.CreateLocal() ); // Initialize timer
       
    73     CActiveScheduler::Add( this); // Add to scheduler
       
    74     iPreparedForGarbage = EFalse;
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 // -----------------------------------------------------------------------------
       
    80 // 
       
    81 CMmDrawerAnimator::~CMmDrawerAnimator()
       
    82     {
       
    83     Cancel(); // Cancel any request, if outstanding
       
    84     iTimer.Close(); // Destroy the RTimer object
       
    85     // Delete instance variables if any
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 // -----------------------------------------------------------------------------
       
    91 // 
       
    92 void CMmDrawerAnimator::DoCancel()
       
    93     {
       
    94     iTimer.Cancel();
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 // -----------------------------------------------------------------------------
       
   100 // 
       
   101 TBool CMmDrawerAnimator::CancelNextRedrawL()
       
   102 	{
       
   103 	RunL();
       
   104     TTime currentTime;
       
   105     currentTime.HomeTime();
       
   106     return (currentTime.MicroSecondsFrom(iLastRedrawTime) 
       
   107     		<= MmEffects::KAnimationFrameDelay);
       
   108 	}
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 // -----------------------------------------------------------------------------
       
   112 // 
       
   113 void CMmDrawerAnimator::RunL()
       
   114     {      
       
   115 	if ( iPreparedForGarbage )
       
   116 		{
       
   117 		return;
       
   118 		}
       
   119 	
       
   120     TTime currentTime;
       
   121     currentTime.HomeTime();
       
   122 	
       
   123    if (currentTime.MicroSecondsFrom(iLastRedrawTime) 
       
   124     		>= MmEffects::KAnimationFrameDelay && iDrawer.GetFloatingItemCount() > 0)
       
   125     	{
       
   126     	Cancel();
       
   127     	
       
   128     	TRect refreshRect;
       
   129     	for(int i=0; i< iDrawer.GetFloatingItemCount(); i++)
       
   130 			{
       
   131 			TMmFloatingItem& current = iDrawer.GetFloatingItemAtIndex(i);
       
   132 			TSize itemSize = iDrawer.GetItemSize(current.GetDrawnItemIndex(), EFalse);
       
   133 
       
   134 			TRect beforeRect(current.GetItemPosition(), itemSize);
       
   135 			current.MakeStep();
       
   136 			TRect afterRect(current.GetItemPosition(), itemSize);
       
   137 			
       
   138 			if (current.GetFloatingItemType() == EDrag )
       
   139 				{
       
   140 				TInt dragTrail = iDrawer.GetFloatingItemIndex( EPostDragRefreshItem );
       
   141 				if (dragTrail != KErrNotFound)
       
   142 					{
       
   143 					refreshRect = (refreshRect == TRect(0,0,0,0)) ? beforeRect : refreshRect;
       
   144 					refreshRect.BoundingRect( afterRect );
       
   145 					}
       
   146 				}
       
   147 			else
       
   148 				{
       
   149 				refreshRect = (refreshRect == TRect(0,0,0,0)) ? beforeRect : refreshRect;
       
   150 				refreshRect.BoundingRect( beforeRect );
       
   151 				refreshRect.BoundingRect( afterRect );
       
   152 				}
       
   153 			}
       
   154     	const TInt highlightedItemIndex = iDrawer.Widget()->View()->CurrentItemIndex();
       
   155     	if ( iLastNotedHighlight != highlightedItemIndex )
       
   156     	    {
       
   157             TRect highlightedItemRect(
       
   158                     iDrawer.Widget()->View()->ItemPos( highlightedItemIndex ),
       
   159                     iDrawer.Widget()->View()->ItemSize( highlightedItemIndex ) ); 
       
   160             refreshRect.BoundingRect( highlightedItemRect );
       
   161             iLastNotedHighlight = highlightedItemIndex;
       
   162     	    }
       
   163 
       
   164     	iDrawer.Widget()->View()->SetDisableRedraw(EFalse);
       
   165     	
       
   166     	TInt currentVerticalOffset = static_cast<CMmWidgetContainer*>(
       
   167     	        iDrawer.Widget()->Parent() )->VerticalItemOffset();
       
   168 
       
   169     	if (iLastNotedTopItem != iDrawer.Widget()->TopItemIndex() ||
       
   170     	        iLastNotedVerticalOffset != currentVerticalOffset )
       
   171     		{
       
   172     		iDrawer.Widget()->DrawNow();
       
   173         	iLastNotedTopItem = iDrawer.Widget()->TopItemIndex();
       
   174         	iLastNotedVerticalOffset = currentVerticalOffset;
       
   175     		}
       
   176     	else
       
   177     		{
       
   178 			if ( refreshRect != TRect(0,0,0,0))
       
   179 				{
       
   180 				TRect prev(iPreviousRefreshRect);
       
   181 				iPreviousRefreshRect = refreshRect;
       
   182 				refreshRect.BoundingRect(prev);
       
   183 				
       
   184 	            // expand rect if non-touch
       
   185 	            refreshRect = TRect( AdjustRefreshRectToNonTouch( refreshRect ) );
       
   186 	            
       
   187 	            // never draw on the outside of widget's view rectangle
       
   188 	            refreshRect.Intersection( iDrawer.Widget()->View()->ViewRect() );
       
   189 	            
       
   190 				iDrawer.Widget()->View()->Draw( &refreshRect );
       
   191 				}
       
   192     		}
       
   193 
       
   194     	iDrawer.Widget()->View()->SetDisableRedraw(ETrue);
       
   195     	
       
   196 		iLastRedrawTime.HomeTime();
       
   197     	
       
   198     	}
       
   199     
       
   200 		if (iDrawer.GetFloatingItemCount() > 0)
       
   201 			{
       
   202 			Trigger();
       
   203 			}
       
   204 		else
       
   205 			{
       
   206 			Cancel();
       
   207 			iDrawer.Widget()->View()->SetDisableRedraw( EFalse );
       
   208 			
       
   209 #ifdef RD_UI_TRANSITION_EFFECTS_LIST
       
   210 			if ( iTransTfx && iTransTfxInternal->EffectsDisabled() )
       
   211 				{
       
   212 				iTransTfx->EnableEffects( ETrue );
       
   213 	
       
   214 				if ( iTransTfx )
       
   215 					{
       
   216 					iTransTfxInternal->Remove( MAknListBoxTfxInternal::EListEverything );
       
   217 					iDrawer.Widget()->DrawNow();
       
   218 					iTransTfxInternal->Draw( iDrawer.Widget()->Rect() );
       
   219 					}
       
   220 				}
       
   221 #endif
       
   222 			}
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 // -----------------------------------------------------------------------------
       
   228 // 
       
   229 TRect CMmDrawerAnimator::AdjustRefreshRectToNonTouch( const TRect& aRefreshRect )
       
   230     {
       
   231     TRect rect( aRefreshRect );
       
   232     if( !AknLayoutUtils::PenEnabled() )
       
   233         {
       
   234         TRect indicatorRect( iDrawer.GetIndicatorRect() );
       
   235         TSize itemSize( iDrawer.GetItemSize(
       
   236                 iDrawer.Widget()->CurrentItemIndex(), ETrue ) );
       
   237         
       
   238         TInt horizontalMargin( ( indicatorRect.Width() - itemSize.iWidth ) / 2 );
       
   239         TInt verticalMargin( ( indicatorRect.Height() - itemSize.iHeight ) / 2 );
       
   240         rect.iBr.iX += horizontalMargin;
       
   241         rect.iBr.iY += verticalMargin;
       
   242         rect.iTl.iX -= horizontalMargin;
       
   243         rect.iTl.iY -= verticalMargin;
       
   244         }
       
   245     return rect;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 // -----------------------------------------------------------------------------
       
   251 // 
       
   252 TInt CMmDrawerAnimator::Trigger()
       
   253     {
       
   254 	if ( iPreparedForGarbage )
       
   255 		{
       
   256 		return KErrCancel;
       
   257 		}
       
   258 	
       
   259     if (!IsActive() && iDrawer.GetFloatingItemCount() > 0 )
       
   260         {
       
   261 #ifdef RD_UI_TRANSITION_EFFECTS_LIST
       
   262 		iTransTfx = CAknListLoader::TfxApi( iDrawer.Gc() );
       
   263 		iTransTfxInternal = CAknListLoader::TfxApiInternal( 
       
   264 				iDrawer.Gc() );
       
   265 	
       
   266 		if ( iTransTfx && !iTransTfxInternal->EffectsDisabled() )
       
   267 			{
       
   268 			iTransTfx->EnableEffects( EFalse );
       
   269 			
       
   270 			if ( iTransTfxInternal )
       
   271 				{
       
   272 				iTransTfxInternal->Remove( MAknListBoxTfxInternal::EListEverything );
       
   273 				iTransTfxInternal->Draw( iDrawer.Widget()->Rect() );
       
   274 				}
       
   275 			}
       
   276 #endif
       
   277     
       
   278         iDrawer.Widget()->View()->SetDisableRedraw(ETrue);
       
   279         iTimer.After(iStatus, TTimeIntervalMicroSeconds32( 
       
   280         		MmEffects::KAnimationFrameDelay ) ); 
       
   281         SetActive();
       
   282         }
       
   283     return KErrNone;
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 // -----------------------------------------------------------------------------
       
   289 // 
       
   290 TInt CMmDrawerAnimator::RunError(TInt aError)
       
   291     {
       
   292     return aError;
       
   293     }
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 // -----------------------------------------------------------------------------
       
   298 // 
       
   299 void CMmDrawerAnimator::AnimateDragItemTransitionL( )
       
   300     {     
       
   301     if (KErrNotFound != iDrawer.GetFloatingItemIndex(EDrag))
       
   302         {
       
   303         TMmFloatingItem floatingItem(
       
   304                 iDrawer.GetFloatingItemL(EDrag).GetDrawnItemIndex(), 
       
   305                 iDrawer.GetFloatingItemL(EDrag).GetItemPosition(), 
       
   306                 EDragTransition, iUsualAnimationFramesCount,
       
   307                 iDrawer.Widget()->View());
       
   308         TPoint pointEnd = iDrawer.Widget()->View()->ItemPos( floatingItem.GetDrawnItemIndex() );
       
   309         floatingItem.SetPositionStep( pointEnd - floatingItem.GetItemPosition() );
       
   310         iDrawer.AddFloatingItemL( floatingItem, 0 );
       
   311         iDrawer.RemoveFloatingItem( iDrawer.GetFloatingItemIndex(EDrag) );
       
   312         }
       
   313     }
       
   314   
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 TBool CMmDrawerAnimator::IsReadyForNewAnimation()
       
   320     {
       
   321 	if ( iPreparedForGarbage )
       
   322 		{
       
   323 		return EFalse;
       
   324 		}
       
   325 	
       
   326     for(int i=0; i< iDrawer.GetFloatingItemCount(); i++)
       
   327         {
       
   328         TMmFloatingItemType type 
       
   329 			= iDrawer.GetFloatingItemAtIndex(i).GetFloatingItemType();
       
   330         if ( type != EDrag && type != EZoomTransition )
       
   331             {
       
   332             return EFalse;
       
   333             }
       
   334         }
       
   335     return ETrue;
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 // -----------------------------------------------------------------------------
       
   341 //
       
   342 void CMmDrawerAnimator::CancelAnimationsL()
       
   343     {
       
   344     for(int i=0; i< iDrawer.GetFloatingItemCount(); i++)
       
   345         {
       
   346         TMmFloatingItem& current = iDrawer.GetFloatingItemAtIndex(i);
       
   347         if (current.GetFloatingItemType() != EDrag)
       
   348             {
       
   349             current.InvalidateFloatingItem();
       
   350             }
       
   351         }
       
   352     }
       
   353 
       
   354  // -----------------------------------------------------------------------------
       
   355  //
       
   356  // -----------------------------------------------------------------------------
       
   357  // 
       
   358 void CMmDrawerAnimator::AnimateItemSwapL( TInt aItemFrom, TInt aItemTo )
       
   359     {
       
   360     TMmFloatingItem floatingItem( aItemTo, 
       
   361             iDrawer.Widget()->View()->ItemPos( aItemFrom ), 
       
   362             ESwapTransition, iUsualAnimationFramesCount,
       
   363             iDrawer.Widget()->View() );
       
   364       
       
   365     for(int i=0; i< iDrawer.GetFloatingItemCount(); i++)
       
   366         {
       
   367         TMmFloatingItem& current = iDrawer.GetFloatingItemAtIndex(i);
       
   368 
       
   369         if (current.GetFloatingItemType() == EZoomTransition 
       
   370                 && current.GetDrawnItemIndex() == aItemFrom )
       
   371             {
       
   372             current.InvalidateFloatingItem();
       
   373             floatingItem.SetSizeStep( current.GetCurrentZoomRatio(), 1.0f );
       
   374             break;
       
   375             }
       
   376         }
       
   377 
       
   378     floatingItem.SetPositionStep( iDrawer.Widget()->View()->ItemPos(aItemTo)- 
       
   379             iDrawer.Widget()->View()->ItemPos(aItemFrom) );
       
   380     iDrawer.AddFloatingItemL(floatingItem);
       
   381     }
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 // -----------------------------------------------------------------------------
       
   386 // 
       
   387 void CMmDrawerAnimator::AnimateItemZoomL( TInt aItemIndex, TBool aZoomIn )
       
   388     {
       
   389     TMmFloatingItem floatingItem( aItemIndex,
       
   390             iDrawer.Widget()->View()->ItemPos( aItemIndex ),
       
   391             EZoomTransition, iUsualAnimationFramesCount,
       
   392             iDrawer.Widget()->View() );
       
   393     floatingItem.SetManualDelete( aZoomIn );
       
   394     floatingItem.SetSizeStep(
       
   395             (aZoomIn) ? KNormalStateZoomRatio : KZoomStateZoomRatio,
       
   396             (aZoomIn) ? KZoomStateZoomRatio : KNormalStateZoomRatio );
       
   397 
       
   398     TInt animationFound = EFalse;
       
   399     for(int i=0; i< iDrawer.GetFloatingItemCount(); i++)
       
   400         {
       
   401         TMmFloatingItem& current = iDrawer.GetFloatingItemAtIndex(i);
       
   402         if (current.GetFloatingItemType() == EZoomTransition &&
       
   403                 current.GetDrawnItemIndex() == aItemIndex )
       
   404             {
       
   405             current.InvalidateFloatingItem();
       
   406             floatingItem.SetSizeStep( current.GetCurrentZoomRatio(),
       
   407                     (aZoomIn) ? KZoomStateZoomRatio : KNormalStateZoomRatio);
       
   408             animationFound = ETrue;
       
   409             break;
       
   410             }
       
   411         }
       
   412     
       
   413     if ( (aZoomIn != EFalse || animationFound != EFalse) )
       
   414         {
       
   415         // This covers the situation, when a zoom out animation is requested
       
   416         // whilst no zoom in occured earlier. In this case the request is 
       
   417         // neglected.
       
   418         iDrawer.AddFloatingItemL( floatingItem );
       
   419         }
       
   420     }
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 //
       
   424 // -----------------------------------------------------------------------------
       
   425 // 
       
   426 void CMmDrawerAnimator::SetNextRedrawToWholeScreen()
       
   427 	{
       
   428 	iPreviousRefreshRect = iDrawer.Widget()->View()->ViewRect();
       
   429 	}
       
   430 
       
   431 // -----------------------------------------------------------------------------
       
   432 //
       
   433 // -----------------------------------------------------------------------------
       
   434 // 
       
   435 void CMmDrawerAnimator::PrepareForGarbage()
       
   436 	{
       
   437 //	Cancel any outstanding requests
       
   438 	Cancel();
       
   439 	iPreparedForGarbage = ETrue;
       
   440 	}
       
   441 //End of file