lafagnosticuifoundation/animation/src/AnimationTicker.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "AnimationTicker.h"
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 #include "AnimationMixins.h"
       
    21 
       
    22 /**
       
    23 Two stage constructor.
       
    24 
       
    25 After construction, the ticker is already active.
       
    26 
       
    27 Creates a new CAnimationTicker with the specified time period in microseconds.
       
    28 The time period aTickLength must be greater than 0 otherwise the caller will be panicked. See CPeriodic for the panic code raised.
       
    29 
       
    30 @see MAnimationTickee
       
    31 @see CPeriodic
       
    32 @param aTickLength The resolution of the timer.
       
    33 @return The new object
       
    34 */
       
    35 EXPORT_C CAnimationTicker* CAnimationTicker::NewL(TTimeIntervalMicroSeconds32 aTickLength)
       
    36 	{
       
    37 	CAnimationTicker* self = new (ELeave) CAnimationTicker;
       
    38 	CleanupStack::PushL(self);
       
    39 	self->ConstructL(aTickLength);
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 /** Destructor.*/
       
    45 EXPORT_C CAnimationTicker::~CAnimationTicker()
       
    46 	{
       
    47 	delete iPeriodic;
       
    48 	iTickees.Reset();
       
    49 	}
       
    50 	
       
    51 CAnimationTicker::CAnimationTicker()
       
    52 	{
       
    53 	}
       
    54 
       
    55 static TInt OnTickCb(TAny* self)
       
    56 	{
       
    57 	TRAPD(err, static_cast<CAnimationTicker*>(self)->OnTick());
       
    58 	return err;
       
    59 	}
       
    60 
       
    61 void CAnimationTicker::ConstructL(TTimeIntervalMicroSeconds32 aTickLength)
       
    62 	{
       
    63 	iTickLength = aTickLength;
       
    64 	}
       
    65 	
       
    66 void CAnimationTicker::StartTickingL()
       
    67 	{
       
    68 	if(!iPeriodic)
       
    69 		{
       
    70 		iPeriodic = CPeriodic::NewL(EPriorityLess);
       
    71 		TCallBack callback(OnTickCb, this);
       
    72 		iPeriodic->Start(iTickLength, iTickLength, callback);
       
    73 		}
       
    74 	}
       
    75 	
       
    76 void CAnimationTicker::StopTicking()
       
    77 	{
       
    78 	delete iPeriodic;
       
    79 	iPeriodic = 0;
       
    80 	}
       
    81 	
       
    82 void CAnimationTicker::OnTick()
       
    83 	{
       
    84 	if (iFreeze == 0)
       
    85 		{
       
    86 		for (TInt tt = iTickees.Count() - 1; tt >= 0; --tt)
       
    87 			{
       
    88 			iTickees[tt]->Tick();
       
    89 			}
       
    90 		}
       
    91 	}
       
    92 
       
    93 /**
       
    94 Adds a new tickee to the internal list.  All added tickees will be called
       
    95 once every tick.
       
    96 @param aTickee An object derived from MAnimationTickee
       
    97 @return KErrNone or one of the other system wide error codes
       
    98 */
       
    99 EXPORT_C TInt CAnimationTicker::Add(MAnimationTickee* aTickee)
       
   100 	{
       
   101 	TInt err = iTickees.Append(aTickee);
       
   102 	if (err == KErrNone)
       
   103 		{
       
   104 		TRAP(err,StartTickingL())
       
   105 		};
       
   106 	return err;
       
   107 	}
       
   108 
       
   109 /**
       
   110 Removes a tickee from the internal list.  Attempting to remove a tickee
       
   111 that has not been added is not an error, and has no effect.
       
   112 @param aTickee An object added using Add()
       
   113 @return KErrNone or KErrNotFound
       
   114 */
       
   115 EXPORT_C TInt CAnimationTicker::Remove(MAnimationTickee* aTickee)
       
   116 	{
       
   117 	TInt entry = iTickees.Find(aTickee);
       
   118 	if (entry >= 0)
       
   119 		{
       
   120 		iTickees.Remove(entry);
       
   121 		if (iTickees.Count() < 1)
       
   122 			StopTicking();
       
   123 		return KErrNone;
       
   124 		}
       
   125 	return KErrNotFound;
       
   126 	}
       
   127 
       
   128 /**
       
   129 Freezes this ticker 
       
   130 
       
   131 This allows asynchronous actions to be taken for more than one animation without the
       
   132 animations getting out of synch with each other.
       
   133 
       
   134 It is possible to freeze multiple times.  Doing so will increment a reference counter
       
   135 and the ticker will not be unfrozen until every freeze has been matched by a call
       
   136 to unfreeze.
       
   137 
       
   138 You should not need to call this function unless you are writing a new animation type.
       
   139 @see CAnimationTicker::Unfreeze()
       
   140 */
       
   141 EXPORT_C void CAnimationTicker::Freeze()
       
   142 	{
       
   143 	++iFreeze;
       
   144 	}
       
   145 
       
   146 /**
       
   147 Under some circumstances it may be necessary to handle unfreezing directly,
       
   148 instead of through the item on the cleanupstack.  In this case the cleanup item
       
   149 should be popped and then Unfreeze called when appropriate.
       
   150 
       
   151 You should not need to call this function unless you are writing a new animation type.
       
   152 @see CAnimationTicker::Freeze()
       
   153 */
       
   154 EXPORT_C void CAnimationTicker::Unfreeze()
       
   155 	{
       
   156 	if(iFreeze > 0)
       
   157 		--iFreeze;
       
   158 	}
       
   159 
       
   160 /** Reserved for future use */
       
   161 EXPORT_C void CAnimationTicker::CAnimationTicker_Reserved1() {}
       
   162 
       
   163 /** Reserved for future use */
       
   164 EXPORT_C void CAnimationTicker::CAnimationTicker_Reserved2() {}