80
|
1 |
/*
|
87
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
80
|
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 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.microedition.m2g;
|
|
19 |
|
|
20 |
import javax.microedition.m2g.*;
|
|
21 |
import java.util.*;
|
|
22 |
import javax.microedition.lcdui.*;
|
|
23 |
import javax.microedition.lcdui.game.GameCanvas;
|
|
24 |
import java.lang.ref.WeakReference;
|
|
25 |
import com.nokia.mj.impl.rt.support.Finalizer;
|
|
26 |
import com.nokia.mj.impl.utils.Logger;
|
|
27 |
|
|
28 |
|
87
|
29 |
public class M2GSVGAnimator extends SVGAnimator
|
80
|
30 |
{
|
|
31 |
//--------------------------------------------------
|
|
32 |
// STATIC CONSTANTS
|
|
33 |
//--------------------------------------------------
|
|
34 |
private static final String ANIMATOR_CANVAS_BASE_CLASS =
|
|
35 |
"javax.microedition.lcdui.Canvas";
|
|
36 |
|
|
37 |
//--------------------------------------------------
|
|
38 |
// VARIABLES
|
|
39 |
//--------------------------------------------------
|
|
40 |
private M2GSVGCanvas iSVGCanvas = null;
|
|
41 |
private Finalizer mFinalizer;
|
|
42 |
|
|
43 |
//--------------------------------------------------
|
|
44 |
// METHODS
|
|
45 |
//--------------------------------------------------
|
|
46 |
/**
|
|
47 |
* Constructor
|
|
48 |
* @param aImage
|
|
49 |
*/
|
|
50 |
protected M2GSVGAnimator(SVGImage aImage)
|
|
51 |
{
|
87
|
52 |
|
80
|
53 |
iSVGCanvas = new M2GSVGCanvas(false, aImage);
|
|
54 |
mFinalizer = new Finalizer()
|
87
|
55 |
{
|
80
|
56 |
public void finalizeImpl()
|
|
57 |
{
|
87
|
58 |
|
80
|
59 |
doFinalize();
|
|
60 |
}
|
|
61 |
};
|
87
|
62 |
|
80
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* @see javax.microedition.m2g.SVGAnimator#getTargetComponent()
|
|
67 |
*/
|
|
68 |
public Object getTargetComponent()
|
|
69 |
{
|
|
70 |
return iSVGCanvas;
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* @see javax.microedition.m2g.SVGAnimator#getTimeIncrement()
|
|
75 |
*/
|
|
76 |
public float getTimeIncrement()
|
|
77 |
{
|
|
78 |
return iSVGCanvas.getTimeIncrement();
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* @see javax.microedition.m2g.SVGAnimator#invokeAndWait()
|
|
83 |
*/
|
|
84 |
public void invokeAndWait(java.lang.Runnable runnable)
|
|
85 |
{
|
|
86 |
if (runnable == null)
|
|
87 |
{
|
|
88 |
throw new NullPointerException(
|
|
89 |
/*SF*/"The runnable is null."/*SF*/);
|
|
90 |
}
|
|
91 |
if (iSVGCanvas.isStopped())
|
|
92 |
{
|
|
93 |
throw new IllegalStateException(
|
|
94 |
/*SF*/"The animator is in the stopped state."/*SF*/);
|
|
95 |
}
|
|
96 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "invokeAndWait()");
|
|
97 |
runnable.run();
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* @see javax.microedition.m2g.SVGAnimator#invokeLater()
|
|
102 |
*/
|
|
103 |
public void invokeLater(java.lang.Runnable runnable)
|
|
104 |
{
|
|
105 |
if (runnable == null)
|
|
106 |
{
|
|
107 |
throw new NullPointerException(
|
|
108 |
/*SF*/"The runnable is null."/*SF*/);
|
|
109 |
}
|
|
110 |
if (iSVGCanvas.isStopped())
|
|
111 |
{
|
|
112 |
throw new IllegalStateException(
|
|
113 |
/*SF*/"The animator is in the stopped state."/*SF*/);
|
|
114 |
}
|
|
115 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "invokeLater()");
|
|
116 |
Thread thread = new Thread(runnable);
|
|
117 |
thread.start();
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* @see javax.microedition.m2g.SVGAnimator#pause()
|
|
122 |
*/
|
|
123 |
public void pause()
|
|
124 |
{
|
|
125 |
if (!iSVGCanvas.isPlaying())
|
|
126 |
{
|
|
127 |
throw new IllegalStateException(
|
|
128 |
/*SF*/"The animator is not in the playing or paused state."/*SF*/);
|
|
129 |
}
|
|
130 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "pause()");
|
|
131 |
iSVGCanvas.pause();
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* @see javax.microedition.m2g.SVGAnimator#play()
|
|
136 |
*/
|
|
137 |
public void play()
|
|
138 |
{
|
87
|
139 |
|
80
|
140 |
if (iSVGCanvas.isPlaying())
|
|
141 |
{
|
|
142 |
throw new IllegalStateException(
|
|
143 |
/*SF*/"The animator is not currently in the stopped or paused state."/*SF*/);
|
|
144 |
}
|
|
145 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "play()");
|
87
|
146 |
|
80
|
147 |
iSVGCanvas.play();
|
|
148 |
}
|
|
149 |
|
|
150 |
private void doFinalize()
|
|
151 |
{
|
|
152 |
if (mFinalizer != null)
|
|
153 |
{
|
87
|
154 |
|
80
|
155 |
registeredFinalize();
|
|
156 |
mFinalizer = null;
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Finalize
|
|
162 |
*/
|
|
163 |
synchronized void registeredFinalize()
|
|
164 |
{
|
|
165 |
iSVGCanvas.cancel();
|
|
166 |
iSVGCanvas = null;
|
|
167 |
}
|
|
168 |
|
|
169 |
|
|
170 |
/**
|
|
171 |
* @see javax.microedition.m2g.SVGAnimator#setSVGEventListener()
|
|
172 |
*/
|
|
173 |
public void setSVGEventListener(SVGEventListener svgEventListener)
|
|
174 |
{
|
|
175 |
iSVGCanvas.setEventListener(svgEventListener);
|
|
176 |
}
|
|
177 |
|
|
178 |
/**
|
|
179 |
* @see javax.microedition.m2g.SVGAnimator#setTimeIncrement()
|
|
180 |
*/
|
|
181 |
public void setTimeIncrement(float timeIncrement)
|
|
182 |
{
|
87
|
183 |
|
80
|
184 |
if (timeIncrement <= 0)
|
|
185 |
{
|
|
186 |
throw new IllegalArgumentException(
|
|
187 |
/*SF*/"The time increment is less than or equal to zero."/*SF*/);
|
|
188 |
}
|
|
189 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "setTimeIncrement() - "
|
|
190 |
+ timeIncrement);
|
|
191 |
iSVGCanvas.setTimeIncrement(timeIncrement);
|
87
|
192 |
|
80
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* @see javax.microedition.m2g.SVGAnimator#stop()
|
|
197 |
*/
|
|
198 |
public void stop()
|
|
199 |
{
|
87
|
200 |
|
80
|
201 |
if (iSVGCanvas.isStopped())
|
|
202 |
{
|
|
203 |
throw new IllegalStateException(
|
|
204 |
/*SF*/"The animator is not in the playing or paused state."/*SF*/);
|
|
205 |
}
|
|
206 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "stop()");
|
|
207 |
|
|
208 |
iSVGCanvas.stop();
|
87
|
209 |
|
80
|
210 |
}
|
|
211 |
|
|
212 |
//--------------------------------------------------
|
|
213 |
// STATIC METHODS
|
|
214 |
//--------------------------------------------------
|
|
215 |
/**
|
|
216 |
* Builds animator
|
|
217 |
* @param svgImage -
|
|
218 |
* @return SVGAnimator
|
|
219 |
* @see javax.microedition.m2g.SVGAnimator#createAnimator()
|
|
220 |
*/
|
|
221 |
public static SVGAnimator buildAnimator(SVGImage svgImage)
|
|
222 |
{
|
87
|
223 |
|
80
|
224 |
if (svgImage == null)
|
|
225 |
{
|
87
|
226 |
|
80
|
227 |
throw new NullPointerException();
|
|
228 |
}
|
87
|
229 |
|
80
|
230 |
return new M2GSVGAnimator(svgImage);
|
|
231 |
}
|
|
232 |
|
|
233 |
/**
|
|
234 |
* Builds animator
|
|
235 |
* @param svgImage -
|
|
236 |
* @param componentBaseClass -
|
|
237 |
* @return SVGAnimator
|
|
238 |
* @see javax.microedition.m2g.SVGAnimator#createAnimator()
|
|
239 |
*/
|
|
240 |
public static SVGAnimator buildAnimator(
|
|
241 |
SVGImage svgImage, String componentBaseClass)
|
|
242 |
{
|
|
243 |
if (svgImage == null)
|
|
244 |
{
|
|
245 |
throw new NullPointerException();
|
|
246 |
}
|
|
247 |
if ((componentBaseClass != null) &&
|
|
248 |
(!componentBaseClass.equals(ANIMATOR_CANVAS_BASE_CLASS)))
|
|
249 |
{
|
|
250 |
throw new IllegalArgumentException(
|
|
251 |
/*SF*/"The requested componentBaseClass is not supported by the implementation."/*SF*/);
|
|
252 |
}
|
|
253 |
return buildAnimator(svgImage);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
//--------------------------------------------------
|
|
258 |
// OTHER CLASSES
|
|
259 |
//--------------------------------------------------
|
|
260 |
|
|
261 |
/**
|
|
262 |
* Canvas
|
|
263 |
*/
|
|
264 |
class M2GSVGCanvas extends GameCanvas implements M2GDOMChangeObserver
|
|
265 |
{
|
|
266 |
//--------------------------------------------------
|
|
267 |
// STATIC CONSTANTS
|
|
268 |
//--------------------------------------------------
|
|
269 |
public static final int STATE_STOPPED = 1;
|
|
270 |
public static final int STATE_PLAYING = 2;
|
|
271 |
public static final int STATE_PAUSED = 3;
|
|
272 |
public static final float DEFAULT_DELTA_TIME = 0.1f; // (10fps) - defined by specs
|
|
273 |
|
|
274 |
//--------------------------------------------------
|
|
275 |
// VARIABLES
|
|
276 |
//--------------------------------------------------
|
|
277 |
private int iState;
|
|
278 |
private SVGImage iSVGImage;
|
|
279 |
private ScalableGraphics iSg;
|
|
280 |
private Graphics iOffscreen = null;
|
|
281 |
private M2GSVGSVGElement iRootElement = null;
|
|
282 |
|
|
283 |
private float iDeltaTime;
|
|
284 |
private Timer iTimer = null;
|
|
285 |
private SVGCanvasTask iTask = null;
|
|
286 |
|
|
287 |
private SVGEventListener iEventListener = null;
|
|
288 |
|
|
289 |
/**
|
|
290 |
* True if the GameCanvas is in background or false otherwise
|
|
291 |
*/
|
|
292 |
private boolean iWasPlaying = false;
|
|
293 |
|
|
294 |
//--------------------------------------------------
|
|
295 |
// METHODS
|
|
296 |
//--------------------------------------------------
|
|
297 |
/**
|
|
298 |
* @see javax.microedition.lcdui.game.GameCanvas#GameCanvas()
|
|
299 |
*/
|
|
300 |
public M2GSVGCanvas(boolean aSuppressKeyEvents, SVGImage aSVGImage)
|
87
|
301 |
{
|
80
|
302 |
|
|
303 |
super(aSuppressKeyEvents);
|
87
|
304 |
|
80
|
305 |
// get the instance to the Graphics of the offscreen buffer
|
|
306 |
iOffscreen = getGraphics();
|
|
307 |
|
|
308 |
iSVGImage = aSVGImage;
|
|
309 |
|
|
310 |
// down-casting to M2GDocument/M2GSVGSVGElement to have access to internal methods
|
|
311 |
M2GDocument doc = (M2GDocument)iSVGImage.getDocument();
|
|
312 |
iRootElement = (M2GSVGSVGElement)iSVGImage.getDocument().getDocumentElement();
|
87
|
313 |
|
|
314 |
|
80
|
315 |
iState = STATE_STOPPED;
|
|
316 |
// Create render context
|
|
317 |
iSg = ScalableGraphics.createInstance();
|
|
318 |
iSg.setRenderingQuality(ScalableGraphics.RENDERING_QUALITY_HIGH);
|
|
319 |
iDeltaTime = DEFAULT_DELTA_TIME;
|
|
320 |
|
|
321 |
doc.registerDOMChangeObserver(this);
|
87
|
322 |
|
|
323 |
|
|
324 |
|
80
|
325 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "Ctor - delta time:"
|
|
326 |
+ iDeltaTime + ", state:" + iState);
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* Handles any change in DOM.
|
|
331 |
* <br>
|
|
332 |
* While in PAUSE state, SVGAnimator must repaint any changes in SVGImage
|
|
333 |
* done via the API (e.g. setTrait(), insertBefore())
|
|
334 |
* <br>
|
|
335 |
* @see M2GDOMChangeObserver.notifyDOMChange()
|
|
336 |
* @since S60 3.2
|
|
337 |
*/
|
|
338 |
public void notifyDOMChange()
|
|
339 |
{
|
|
340 |
if (isPaused())
|
|
341 |
{
|
|
342 |
repaint();
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
/**
|
|
347 |
* Cancel a timed task
|
|
348 |
*/
|
|
349 |
public void cancel()
|
|
350 |
{
|
|
351 |
if (iTimer != null)
|
|
352 |
{
|
|
353 |
iTimer.cancel();
|
|
354 |
}
|
|
355 |
if (iTask != null)
|
|
356 |
{
|
|
357 |
iTask.cancel();
|
|
358 |
}
|
|
359 |
iTask = null;
|
|
360 |
iTimer = null;
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Returns event listener
|
|
365 |
* @return event listener
|
|
366 |
*/
|
|
367 |
public synchronized SVGEventListener getEventListener()
|
|
368 |
{
|
|
369 |
return iEventListener;
|
|
370 |
}
|
|
371 |
|
|
372 |
/**
|
|
373 |
* @see javax.microedition.m2g.SVGAnimator#getTimeIncrement()
|
|
374 |
*/
|
|
375 |
public synchronized float getTimeIncrement()
|
|
376 |
{
|
|
377 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "getTimeIncrement() - "
|
|
378 |
+ iDeltaTime);
|
|
379 |
return iDeltaTime;
|
|
380 |
}
|
|
381 |
|
|
382 |
/**
|
|
383 |
* Increases the increment time of the SVGImage.
|
|
384 |
* Increment is done only if the playing state is active.
|
|
385 |
* @see javax.microedition.m2g.SVGImage#incrementTime()
|
|
386 |
*/
|
|
387 |
public synchronized void increaseCurrentTime(float time)
|
|
388 |
{
|
|
389 |
if (iState == STATE_PLAYING)
|
|
390 |
{
|
|
391 |
// update the time only in java side
|
|
392 |
// the time in engine side is updated during rendering
|
|
393 |
iRootElement.incrementTimeNoUpdate(time);
|
|
394 |
}
|
|
395 |
}
|
|
396 |
|
|
397 |
/**
|
|
398 |
* Checks if playing
|
|
399 |
* @return true if playing
|
|
400 |
*/
|
|
401 |
public synchronized boolean isPlaying()
|
|
402 |
{
|
|
403 |
return iState == STATE_PLAYING;
|
|
404 |
}
|
|
405 |
|
|
406 |
/**
|
|
407 |
* Checks if paused
|
|
408 |
* @return true if paused
|
|
409 |
*/
|
|
410 |
public synchronized boolean isPaused()
|
|
411 |
{
|
|
412 |
return iState == STATE_PAUSED;
|
|
413 |
}
|
|
414 |
|
|
415 |
/**
|
|
416 |
* Checks if stopped
|
|
417 |
* @return true if stopped
|
|
418 |
*/
|
|
419 |
public synchronized boolean isStopped()
|
|
420 |
{
|
|
421 |
return iState == STATE_STOPPED;
|
|
422 |
}
|
|
423 |
|
|
424 |
/**
|
|
425 |
* @see javax.microedition.lcdui.Canvas#keyPressed()
|
|
426 |
*/
|
|
427 |
protected synchronized void keyPressed(int keyCode)
|
|
428 |
{
|
|
429 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "keyPressed() - " + keyCode);
|
|
430 |
if (iEventListener != null)
|
|
431 |
{
|
|
432 |
iEventListener.keyPressed(keyCode);
|
|
433 |
}
|
|
434 |
}
|
|
435 |
|
|
436 |
/**
|
|
437 |
* @see javax.microedition.lcdui.Canvas#keyReleased()
|
|
438 |
*/
|
|
439 |
protected synchronized void keyReleased(int keyCode)
|
|
440 |
{
|
|
441 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "keyReleased() - " + keyCode);
|
|
442 |
if (iEventListener != null)
|
|
443 |
{
|
|
444 |
iEventListener.keyReleased(keyCode);
|
|
445 |
}
|
|
446 |
}
|
|
447 |
|
|
448 |
/**
|
|
449 |
* @see javax.microedition.lcdui.Canvas#pointerPressed()
|
|
450 |
* @see javax.microedition.m2g.SVGEventListener#pointerPressed()
|
|
451 |
*/
|
|
452 |
protected synchronized void pointerPressed(int x, int y)
|
|
453 |
{
|
|
454 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo,
|
|
455 |
"pointerPressed() - x:" + x + ", y:" + y);
|
|
456 |
if (iEventListener != null)
|
|
457 |
{
|
|
458 |
iEventListener.pointerPressed(x, y);
|
|
459 |
}
|
|
460 |
}
|
|
461 |
|
|
462 |
/**
|
|
463 |
* @see javax.microedition.lcdui.Canvas#pointerReleased()
|
|
464 |
* @see javax.microedition.m2g.SVGEventListener#pointerReleased()
|
|
465 |
*/
|
|
466 |
protected synchronized void pointerReleased(int x, int y)
|
|
467 |
{
|
|
468 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo,
|
|
469 |
"pointerReleased() - x:" + x + ", y:" + y);
|
|
470 |
if (iEventListener != null)
|
|
471 |
{
|
|
472 |
iEventListener.pointerReleased(x, y);
|
|
473 |
}
|
|
474 |
}
|
|
475 |
|
|
476 |
/**
|
|
477 |
* @see javax.microedition.lcdui.game.GameCanvas#paint()
|
|
478 |
*/
|
|
479 |
public void paint(Graphics g)
|
|
480 |
{
|
|
481 |
// Clears bitmap
|
87
|
482 |
|
80
|
483 |
g.setColor(255, 255, 255);
|
|
484 |
g.fillRect(0, 0, getWidth(), getHeight());
|
|
485 |
|
|
486 |
try
|
|
487 |
{
|
87
|
488 |
|
80
|
489 |
iSg.bindTarget(g);
|
|
490 |
|
87
|
491 |
|
80
|
492 |
// NOTE: Source is defaultly fully opaque
|
|
493 |
iSg.render(0, 0, iSVGImage);
|
87
|
494 |
|
80
|
495 |
}
|
|
496 |
finally
|
|
497 |
{
|
87
|
498 |
|
80
|
499 |
iSg.releaseTarget();
|
87
|
500 |
|
80
|
501 |
}
|
|
502 |
}
|
|
503 |
|
|
504 |
/**
|
|
505 |
* Paints a frame to the offscreen of this GameCanvas
|
|
506 |
* @note GameCanvas.getGraphics() is not used since it always creates a new instance of Graphics
|
|
507 |
*/
|
|
508 |
public void paintToOffscreen()
|
|
509 |
{
|
|
510 |
paint(iOffscreen);
|
|
511 |
}
|
|
512 |
|
|
513 |
/**
|
|
514 |
* @see javax.microedition.m2g.SVGAnimator#setSVGEventListener()
|
|
515 |
*/
|
|
516 |
public synchronized void setEventListener(SVGEventListener eventListener)
|
|
517 |
{
|
|
518 |
iEventListener = eventListener;
|
|
519 |
}
|
|
520 |
|
|
521 |
/**
|
|
522 |
* @see javax.microedition.m2g.SVGAnimator#setTimeIncrement()
|
|
523 |
*/
|
|
524 |
public synchronized void setTimeIncrement(float aDeltaTime)
|
|
525 |
{
|
|
526 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "setTimeIncrement() - "
|
|
527 |
+ aDeltaTime);
|
|
528 |
|
|
529 |
iDeltaTime = aDeltaTime;
|
|
530 |
}
|
|
531 |
|
|
532 |
|
|
533 |
/**
|
|
534 |
* @see javax.microedition.lcdui.Canvas#showNotify()
|
|
535 |
* @see javax.microedition.m2g.SVGEventListener#showNotify()
|
|
536 |
*/
|
|
537 |
protected synchronized void showNotify()
|
|
538 |
{
|
|
539 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "showNotify()");
|
|
540 |
|
|
541 |
if (iEventListener != null)
|
|
542 |
{
|
|
543 |
iEventListener.showNotify();
|
|
544 |
}
|
|
545 |
|
|
546 |
// A common use-case could be that the developer plays the animation by themselves
|
|
547 |
// in SVGEventListener.showNotify().
|
|
548 |
// Therefore we play the animation only if the developer didn't resume it already.
|
|
549 |
if (iWasPlaying)
|
|
550 |
{
|
|
551 |
iWasPlaying = false;
|
|
552 |
play();
|
|
553 |
}
|
|
554 |
}
|
|
555 |
|
|
556 |
/**
|
|
557 |
* @see javax.microedition.lcdui.Canvas#hideNotify()
|
|
558 |
* @see javax.microedition.m2g.SVGEventListener#hideNotify()
|
|
559 |
*/
|
|
560 |
protected synchronized void hideNotify()
|
|
561 |
{
|
|
562 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "hideNotify()");
|
|
563 |
|
|
564 |
if (iEventListener != null)
|
|
565 |
{
|
|
566 |
iEventListener.hideNotify();
|
|
567 |
}
|
|
568 |
|
|
569 |
// A common use-case could be that developer pause the animation by themselves
|
|
570 |
// in SVGEventListener.hideNotify().
|
|
571 |
// Therefore we pause the animation only if the developer didn't pause it already.
|
|
572 |
if (isPlaying())
|
|
573 |
{
|
|
574 |
pause();
|
|
575 |
iWasPlaying = true;
|
|
576 |
}
|
|
577 |
}
|
|
578 |
|
|
579 |
|
|
580 |
/**
|
|
581 |
* @see javax.microedition.lcdui.Canvas#sizeChanged()
|
|
582 |
* @see javax.microedition.m2g.SVGEventListener#sizeChanged()
|
|
583 |
*/
|
|
584 |
protected synchronized void sizeChanged(int w, int h)
|
|
585 |
{
|
|
586 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "sizeChanged() - w:"
|
|
587 |
+ w + ", h:" + h);
|
|
588 |
|
|
589 |
// get a new instance of offscreen Graphics since the Graphics object
|
|
590 |
// does not update its size or clipping area.
|
|
591 |
// If not updating this, the GameCanvas is clipped when it is switched
|
|
592 |
// to full-screen
|
|
593 |
if (iOffscreen != null)
|
|
594 |
{
|
|
595 |
// sizeChanged() is called twice when the Canvas is set as Displayable.
|
|
596 |
// If we try to get the Graphics object (first time), the Graphics object is not
|
|
597 |
// fully initialized, so GameCanvas.getGraphics() will throw a NullPointerException
|
|
598 |
iOffscreen = getGraphics();
|
|
599 |
}
|
|
600 |
|
|
601 |
if (iSVGImage != null)
|
|
602 |
{
|
|
603 |
iSVGImage.setViewportWidth(w);
|
|
604 |
iSVGImage.setViewportHeight(h);
|
|
605 |
}
|
|
606 |
|
|
607 |
if (iEventListener != null)
|
|
608 |
{
|
|
609 |
iEventListener.sizeChanged(w, h);
|
|
610 |
}
|
|
611 |
}
|
|
612 |
|
|
613 |
/**
|
|
614 |
* @see javax.microedition.m2g.SVGAnimator#play()
|
|
615 |
*/
|
|
616 |
public synchronized void play()
|
|
617 |
{
|
|
618 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "play()");
|
87
|
619 |
|
80
|
620 |
if (iState == STATE_PLAYING)
|
|
621 |
{
|
|
622 |
// don't do anything if animation is already playing
|
|
623 |
return;
|
|
624 |
}
|
|
625 |
|
|
626 |
if ((iState == STATE_PAUSED)||(iState == STATE_STOPPED))
|
|
627 |
{
|
|
628 |
iTask = new SVGCanvasTask(this);
|
|
629 |
iTimer = new Timer();
|
|
630 |
}
|
|
631 |
iState = STATE_PLAYING;
|
|
632 |
if (iTimer != null && iTask != null)
|
|
633 |
{
|
|
634 |
|
|
635 |
iTimer.schedule(iTask,0,(long)(iDeltaTime*1000.0f));
|
|
636 |
|
|
637 |
}
|
|
638 |
}
|
|
639 |
|
|
640 |
/**
|
|
641 |
* @see javax.microedition.m2g.SVGAnimator#pause()
|
|
642 |
*/
|
|
643 |
public synchronized void pause()
|
|
644 |
{
|
|
645 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "pause()");
|
|
646 |
|
|
647 |
if (iState == STATE_PLAYING)
|
|
648 |
{
|
|
649 |
iTask.cancel();
|
|
650 |
iTask = null;
|
|
651 |
iTimer.cancel();
|
|
652 |
iTimer = null;
|
|
653 |
}
|
|
654 |
iState = STATE_PAUSED;
|
|
655 |
|
|
656 |
}
|
|
657 |
|
|
658 |
/**
|
|
659 |
* @see javax.microedition.m2g.SVGAnimator#stop()
|
|
660 |
*/
|
|
661 |
public synchronized void stop()
|
|
662 |
{
|
|
663 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "stop()");
|
|
664 |
if (iState == STATE_PLAYING)
|
|
665 |
{
|
|
666 |
iTask.cancel();
|
|
667 |
iTask = null;
|
|
668 |
iTimer.cancel();
|
|
669 |
iTimer = null;
|
|
670 |
}
|
|
671 |
iState = STATE_STOPPED;
|
|
672 |
}
|
|
673 |
}
|
|
674 |
|
|
675 |
/**
|
|
676 |
* Timer task for rendering frames
|
|
677 |
*/
|
|
678 |
class SVGCanvasTask extends TimerTask
|
|
679 |
{
|
|
680 |
//--------------------------------------------------
|
|
681 |
// VARIABLES
|
|
682 |
//--------------------------------------------------
|
|
683 |
// private WeakReference iWeakCanvas;
|
|
684 |
|
|
685 |
private M2GSVGCanvas iCanvas = null;
|
|
686 |
private long iPrevTime = 0;
|
|
687 |
|
|
688 |
//--------------------------------------------------
|
|
689 |
// METHODS
|
|
690 |
//--------------------------------------------------
|
|
691 |
/**
|
|
692 |
* Constructor
|
|
693 |
* @param aCanvas SVG canvas
|
|
694 |
*/
|
|
695 |
public SVGCanvasTask(M2GSVGCanvas aCanvas)
|
|
696 |
{
|
|
697 |
// iWeakCanvas = new WeakReference(aCanvas);
|
|
698 |
// iCanvas = (M2GSVGCanvas)iWeakCanvas.get();
|
|
699 |
|
|
700 |
iCanvas = aCanvas;
|
|
701 |
iPrevTime = System.currentTimeMillis();
|
|
702 |
}
|
|
703 |
/**
|
|
704 |
* Updates the animation time and generates frames which get flushed to the screen
|
|
705 |
* <br>
|
|
706 |
* * @note The timing for the next frame is decided upon the max(iDeltaTime*1000, elapsedTime)
|
|
707 |
* iDeltaTime: FrameTime set by client.
|
|
708 |
* elapsedTime: Actual Time taken for rendering
|
|
709 |
the max(iDeltaTime*1000, elapsedTime) will be incremented on the SVGElement
|
|
710 |
* @note rendering will be done only in PLAY state.
|
|
711 |
* @note While in PAUSE state, SVGAnimator will repaint any changes done
|
|
712 |
* to SVGImage via <code>M2GDOMChangeObserver.notifyDOMChange()</code>
|
|
713 |
*/
|
|
714 |
public void run()
|
|
715 |
{
|
|
716 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo,
|
|
717 |
"SVGCanvasTask: run() - begin");
|
|
718 |
|
|
719 |
if (iCanvas == null)
|
|
720 |
{
|
|
721 |
return;
|
|
722 |
}
|
|
723 |
|
|
724 |
try
|
|
725 |
{
|
|
726 |
synchronized (iCanvas)
|
|
727 |
{
|
|
728 |
long elapsedTime = System.currentTimeMillis()- iPrevTime;
|
|
729 |
if (elapsedTime > 0)
|
|
730 |
iCanvas.increaseCurrentTime((float)(elapsedTime/ 1000.));
|
|
731 |
iPrevTime = System.currentTimeMillis();
|
|
732 |
iCanvas.paintToOffscreen();
|
|
733 |
iCanvas.flushGraphics();
|
|
734 |
}
|
|
735 |
}
|
|
736 |
catch (Exception e)
|
|
737 |
{
|
|
738 |
Logger.ELOG(Logger.EJavaUI,
|
|
739 |
"SVGCanvasTask: run() - exception: " + e.toString());
|
|
740 |
}
|
|
741 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo, "SVGCanvasTask: run() - end");
|
|
742 |
|
|
743 |
}
|
|
744 |
}
|