|
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 <gdi.h> |
|
17 #include <fbs.h> |
|
18 |
|
19 #include "BitmapAnimator.h" |
|
20 #include "AnimationFrame.h" |
|
21 #include "AnimationConfig.h" |
|
22 #include "AnimationEvents.h" |
|
23 #include "AnimationTicker.h" |
|
24 |
|
25 /** |
|
26 Two stage constructor. |
|
27 |
|
28 Animators should not be constructed directly. They are loaded as required |
|
29 by the ECOM plugin framework, using this interface. |
|
30 |
|
31 @see CAnimator |
|
32 @param aRenderer A pointer to an MAnimationDrawer |
|
33 @return The newly constructed animator |
|
34 */ |
|
35 CBitmapAnimator* CBitmapAnimator::NewL(TAny* aRenderer) |
|
36 { |
|
37 MAnimationDrawer* renderer = static_cast<MAnimationDrawer*>(aRenderer); |
|
38 CBitmapAnimator* self = new (ELeave) CBitmapAnimator(renderer); |
|
39 CleanupStack::PushL(self); |
|
40 self->ConstructL(); |
|
41 CleanupStack::Pop(self); |
|
42 |
|
43 return self; |
|
44 } |
|
45 |
|
46 /** Destructor.*/ |
|
47 CBitmapAnimator::~CBitmapAnimator() |
|
48 { |
|
49 Stop(); |
|
50 iFrameArray.ResetAndDestroy(); |
|
51 iFrameArray.Close(); |
|
52 } |
|
53 |
|
54 void CBitmapAnimator::Start(const TAnimationConfig& aConfig) |
|
55 /** |
|
56 Implements CAnimator::Start. |
|
57 |
|
58 This function ignores the EStartImmediately flag in the config argument, and |
|
59 always delays starting untill all frames of the animation have been loaded. |
|
60 */ |
|
61 { |
|
62 Stop(); |
|
63 |
|
64 if (aConfig.iFlags & TAnimationConfig::ECountFrames) |
|
65 iFlags |= ECountFrames; |
|
66 else |
|
67 iFlags &= ~ECountFrames; |
|
68 |
|
69 if (aConfig.iFlags & TAnimationConfig::EEndOnLastFrame) |
|
70 iFlags |= EEndOnLastFrame; |
|
71 else |
|
72 iFlags &= ~EEndOnLastFrame; |
|
73 |
|
74 if (aConfig.iFlags & TAnimationConfig::ELoop) |
|
75 iLoop = aConfig.iData; |
|
76 else |
|
77 iLoop = 1; |
|
78 |
|
79 iFlags &= ~ERunMask; |
|
80 |
|
81 if (iFrameArray.Count() > 0 && (iFlags & ECompleteData || aConfig.iFlags & TAnimationConfig::EStartImmediately)) |
|
82 { |
|
83 iFlags |= ERunning; |
|
84 iTicksLeft = 0; |
|
85 // Add can fail, but if it does there isn't much we can do about it, we just won't animate. |
|
86 iRenderer->AnimatorTicker().Add(this); |
|
87 } |
|
88 else |
|
89 { |
|
90 iFlags |= EPending; |
|
91 } |
|
92 } |
|
93 |
|
94 /** Implements CAnimator::Stop.*/ |
|
95 void CBitmapAnimator::Stop() |
|
96 { |
|
97 if (iFlags & ERunning) |
|
98 { |
|
99 if (!(iFlags & EPaused)) |
|
100 iRenderer->AnimatorTicker().Remove(this); |
|
101 iFlags &= ~ERunMask; |
|
102 iNextFrame = 0; |
|
103 iCurrentFrame = 0; |
|
104 } |
|
105 } |
|
106 |
|
107 /** Implements CAnimator::Pause.*/ |
|
108 void CBitmapAnimator::Pause() |
|
109 { |
|
110 if (iFlags & ERunning && !(iFlags & EPaused)) |
|
111 { |
|
112 iFlags |= EPaused; |
|
113 iRenderer->AnimatorTicker().Remove(this); |
|
114 } |
|
115 } |
|
116 |
|
117 /** Implements CAnimator::Resume.*/ |
|
118 void CBitmapAnimator::Resume() |
|
119 { |
|
120 if (iFlags & EPaused) |
|
121 { |
|
122 iFlags &= ~EPaused; |
|
123 // Add can fail, but if it does there isn't much we can do about it, we just won't animate. |
|
124 iRenderer->AnimatorTicker().Add(this); |
|
125 } |
|
126 } |
|
127 |
|
128 /** Implements CAnimator::Hold.*/ |
|
129 void CBitmapAnimator::Hold() |
|
130 { |
|
131 if (!(iFlags & EHeld)) |
|
132 { |
|
133 iFlags |= EHeld; |
|
134 } |
|
135 } |
|
136 |
|
137 /** Implements CAnimator::Unhold.*/ |
|
138 void CBitmapAnimator::Unhold() |
|
139 { |
|
140 // It isn't possible to jump straight to the current frame, because it will depend on the ones |
|
141 // before. |
|
142 // It may be possible, some of the time, to use the frame we drew last to skip some of the while |
|
143 // loop - this is a possible optimization for the future. |
|
144 if (iFlags & EHeld) |
|
145 { |
|
146 iFlags &= ~EHeld; |
|
147 TInt oldCurrent = iCurrentFrame; |
|
148 iCurrentFrame = 0; |
|
149 iNextFrame = 0; |
|
150 while (iCurrentFrame < oldCurrent) |
|
151 { |
|
152 iRenderer->AnimatorDraw(); |
|
153 iCurrentFrame = iNextFrame; |
|
154 iNextFrame = (iNextFrame + 1) % iFrameArray.Count(); |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 void CBitmapAnimator::InitialisedL() |
|
160 { |
|
161 iCurrentFrame = iNextFrame = 0; |
|
162 iFlags |= ECompleteData; |
|
163 iRenderer->AnimatorInitialisedL(iMaxSize); |
|
164 if (!(iFlags & EHeld)) |
|
165 iRenderer->AnimatorDraw(); |
|
166 if (iFlags & EPending) |
|
167 { |
|
168 iFlags |= ERunning; |
|
169 iFlags &= ~EPending; |
|
170 iTicksLeft = 0; |
|
171 // Add can fail, but if it does there isn't much we can do about it, we just won't animate. |
|
172 iRenderer->AnimatorTicker().Add(this); |
|
173 } |
|
174 } |
|
175 |
|
176 void CBitmapAnimator::ResetL() |
|
177 { |
|
178 Stop(); |
|
179 iFlags = 0; |
|
180 iMaxSize.iWidth = iMaxSize.iHeight = 0; |
|
181 iFrameArray.ResetAndDestroy(); |
|
182 iRenderer->AnimatorResetL(); |
|
183 } |
|
184 |
|
185 /** Implements CAnimator::DataEventL.*/ |
|
186 void CBitmapAnimator::DataEventL(TInt aEvent, TAny* aData, TInt aDataLength) |
|
187 { |
|
188 switch (aEvent) |
|
189 { |
|
190 case EBitmapAnimationNewFrame: |
|
191 __ASSERT_ALWAYS(aDataLength==sizeof(CAnimationFrame::THandles),User::Leave(KErrArgument)); |
|
192 AppendFrameL(*static_cast<CAnimationFrame::THandles*>(aData)); |
|
193 break; |
|
194 case EBitmapAnimationComplete: |
|
195 InitialisedL(); |
|
196 break; |
|
197 case EAnimationDataChanged: |
|
198 ResetL(); |
|
199 break; |
|
200 default: |
|
201 break; |
|
202 } |
|
203 } |
|
204 |
|
205 /** Implements CAnimator::Draw.*/ |
|
206 void CBitmapAnimator::Draw(CBitmapContext& aBitmapContext) const |
|
207 { |
|
208 const TUint32 frameFlags = iFrameArray[iCurrentFrame]->FrameInfo().iFlags; |
|
209 |
|
210 if (frameFlags&TFrameInfo::ERestoreToPrevious) |
|
211 RestoreToPrevious(aBitmapContext, EFalse); |
|
212 Render(aBitmapContext, iNextFrame); |
|
213 } |
|
214 |
|
215 /** Implements CAnimator::DrawMask.*/ |
|
216 void CBitmapAnimator::DrawMask(CBitmapContext& aBitmapContext) const |
|
217 { |
|
218 const TUint32 frameFlags = iFrameArray[iCurrentFrame]->FrameInfo().iFlags; |
|
219 |
|
220 if (frameFlags&TFrameInfo::ERestoreToPrevious) |
|
221 RestoreToPrevious(aBitmapContext, ETrue); |
|
222 RenderMask(aBitmapContext, iNextFrame); |
|
223 } |
|
224 |
|
225 void CBitmapAnimator::AppendFrameL(CAnimationFrame::THandles& aAnimationFrame) |
|
226 { |
|
227 CAnimationFrame* frame = CAnimationFrame::NewL(aAnimationFrame); |
|
228 CleanupStack::PushL(frame); |
|
229 iFrameArray.AppendL(frame); |
|
230 CleanupStack::Pop(frame); |
|
231 |
|
232 const TSize& size(frame->FrameInfo().iOverallSizeInPixels); |
|
233 if (size.iHeight > iMaxSize.iHeight) |
|
234 { |
|
235 iMaxSize.iHeight = size.iHeight; |
|
236 } |
|
237 if (size.iWidth > iMaxSize.iWidth) |
|
238 { |
|
239 iMaxSize.iWidth = size.iWidth; |
|
240 } |
|
241 } |
|
242 |
|
243 CBitmapAnimator::CBitmapAnimator(MAnimationDrawer* aRenderer) : |
|
244 iRenderer(aRenderer) |
|
245 { |
|
246 } |
|
247 |
|
248 void CBitmapAnimator::ConstructL() |
|
249 { // nothing to do |
|
250 } |
|
251 |
|
252 void CBitmapAnimator::Tick() |
|
253 { |
|
254 --iTicksLeft; |
|
255 if (iTicksLeft < 1) |
|
256 { |
|
257 iTicksLeft = iFrameArray[iNextFrame]->FrameInfo().iDelay.Int64() / iRenderer->AnimatorTicker().TickLength().Int(); |
|
258 DoUpdateFrame(); |
|
259 } |
|
260 } |
|
261 |
|
262 void CBitmapAnimator::DoUpdateFrame() |
|
263 { |
|
264 if (!((iLoop == 0 && iFlags & EEndOnLastFrame) || iFlags & EHeld)) |
|
265 iRenderer->AnimatorDraw(); |
|
266 |
|
267 if (iLoop != 0) |
|
268 { |
|
269 iCurrentFrame = iNextFrame; |
|
270 ++iNextFrame; |
|
271 |
|
272 if (iLoop > 0 && iFlags & ECountFrames) |
|
273 --iLoop; |
|
274 |
|
275 if (iNextFrame >= iFrameArray.Count()) |
|
276 { |
|
277 iNextFrame = 0; |
|
278 |
|
279 if (iLoop > 0 && !(iFlags & ECountFrames)) |
|
280 --iLoop; |
|
281 } |
|
282 } |
|
283 else |
|
284 { |
|
285 Stop(); |
|
286 } |
|
287 } |
|
288 |
|
289 void CBitmapAnimator::Render(CBitmapContext& aBitmapContext, TInt aFrame) const |
|
290 { |
|
291 aBitmapContext.Reset(); |
|
292 |
|
293 const CAnimationFrame& nextFrame = *iFrameArray[aFrame]; |
|
294 const TBool useMask = nextFrame.FrameInfo().iFlags&TFrameInfo::ETransparencyPossible || nextFrame.FrameInfo().iFlags&TFrameInfo::EAlphaChannel; |
|
295 |
|
296 if (useMask) |
|
297 { |
|
298 aBitmapContext.BitBltMasked(nextFrame.FrameInfo().iFrameCoordsInPixels.iTl, nextFrame.Bitmap(), nextFrame.FrameInfo().iOverallSizeInPixels, nextFrame.Mask(), EFalse); |
|
299 } |
|
300 else |
|
301 { |
|
302 aBitmapContext.BitBlt(nextFrame.FrameInfo().iFrameCoordsInPixels.iTl, nextFrame.Bitmap(), nextFrame.FrameInfo().iOverallSizeInPixels); |
|
303 } |
|
304 } |
|
305 |
|
306 void CBitmapAnimator::RenderMask(CBitmapContext& aBitmapContext, TInt aFrame) const |
|
307 { |
|
308 aBitmapContext.Reset(); |
|
309 |
|
310 const CAnimationFrame& nextFrame = *iFrameArray[aFrame]; |
|
311 |
|
312 if (aFrame == 0) |
|
313 { |
|
314 aBitmapContext.SetBrushColor(KRgbBlack); |
|
315 aBitmapContext.Clear(iMaxSize); |
|
316 } |
|
317 else |
|
318 { |
|
319 const CAnimationFrame& currentFrame = *iFrameArray[aFrame - 1]; |
|
320 if (currentFrame.FrameInfo().iFlags & TFrameInfo::ERestoreToBackground) |
|
321 { |
|
322 aBitmapContext.SetBrushColor(KRgbBlack); |
|
323 aBitmapContext.Clear(currentFrame.FrameInfo().iFrameCoordsInPixels); |
|
324 } |
|
325 } |
|
326 const TBool useMask = nextFrame.FrameInfo().iFlags&TFrameInfo::ETransparencyPossible || nextFrame.FrameInfo().iFlags&TFrameInfo::EAlphaChannel; |
|
327 if (useMask) |
|
328 { |
|
329 aBitmapContext.SetDrawMode(CGraphicsContext::EDrawModeOR); |
|
330 aBitmapContext.BitBlt(nextFrame.FrameInfo().iFrameCoordsInPixels.iTl, nextFrame.Mask(), nextFrame.FrameInfo().iOverallSizeInPixels); |
|
331 } |
|
332 else |
|
333 { |
|
334 aBitmapContext.SetBrushColor(KRgbWhite); |
|
335 if (nextFrame.FrameInfo().iOverallSizeInPixels != nextFrame.FrameInfo().iFrameSizeInPixels) |
|
336 { |
|
337 // If the frame size smaller than the overall size, the mask shall not take effect later in CBasicAnimation::Draw(). |
|
338 aBitmapContext.Clear(nextFrame.FrameInfo().iOverallSizeInPixels); |
|
339 } |
|
340 else |
|
341 { |
|
342 aBitmapContext.Clear(nextFrame.FrameInfo().iFrameCoordsInPixels); |
|
343 } |
|
344 } |
|
345 } |
|
346 |
|
347 void CBitmapAnimator::RestoreToPrevious(CBitmapContext& aBitmapContext, TBool aMask) const |
|
348 { |
|
349 TInt frame = 0; |
|
350 while (frame < iCurrentFrame) |
|
351 { |
|
352 if (!(iFrameArray[frame]->FrameInfo().iFlags&TFrameInfo::ERestoreToPrevious)) |
|
353 { |
|
354 if (aMask) |
|
355 RenderMask(aBitmapContext, frame); |
|
356 else |
|
357 Render(aBitmapContext, frame); |
|
358 } |
|
359 ++frame; |
|
360 } |
|
361 } |