|
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: |
|
15 * |
|
16 */ |
|
17 package org.eclipse.swt.widgets; |
|
18 |
|
19 // this package is removed from Qt import org.eclipse.swt.internal.symbian. |
|
20 /*import org.eclipse.swt.internal.symbian.*;*/ |
|
21 import org.eclipse.swt.internal.*; |
|
22 import org.eclipse.swt.widgets.Control; |
|
23 import javax.microedition.m2g.*; |
|
24 import com.nokia.microedition.m2g.*; |
|
25 import java.util.*; |
|
26 import java.lang.ref.WeakReference; |
|
27 import org.eclipse.swt.graphics.GC; |
|
28 import org.eclipse.swt.graphics.Rectangle; |
|
29 import org.eclipse.swt.events.*; |
|
30 import com.nokia.mj.impl.utils.Logger; |
|
31 import org.eclipse.swt.widgets.Internal_PackageSupport; |
|
32 import org.eclipse.swt.graphics.Internal_GfxPackageSupport; |
|
33 |
|
34 |
|
35 |
|
36 /** |
|
37 * Control |
|
38 */ |
|
39 public class SVGAnimatorControl extends Canvas |
|
40 implements ControlListener, |
|
41 KeyListener, |
|
42 MouseListener, |
|
43 DisposeListener, |
|
44 ShellListener, |
|
45 PaintListener |
|
46 { |
|
47 //-------------------------------------------------- |
|
48 // STATIC CONSTANTS |
|
49 //-------------------------------------------------- |
|
50 public static final int STATE_STOPPED = 1; |
|
51 public static final int STATE_PLAYING = 2; |
|
52 public static final int STATE_PAUSED = 3; |
|
53 public static final float DEFAULT_DELTA_TIME = 0.1f; |
|
54 |
|
55 //-------------------------------------------------- |
|
56 // VARIABLES |
|
57 //-------------------------------------------------- |
|
58 private int iState; |
|
59 private SVGImage iSVGImage; |
|
60 private M2GScalableGraphics iSg; |
|
61 private float iDeltaTime; |
|
62 private SVGAnimatorRunnable iTask = null; |
|
63 private SVGEventListener iEventListener = null; |
|
64 |
|
65 //-------------------------------------------------- |
|
66 // METHODS |
|
67 //-------------------------------------------------- |
|
68 |
|
69 /** |
|
70 * @see |
|
71 */ |
|
72 public SVGAnimatorControl(SVGImage aSVGImage) |
|
73 { |
|
74 |
|
75 |
|
76 iSVGImage = aSVGImage; |
|
77 iState = STATE_STOPPED; |
|
78 // Create render context, use M2GScalableGraphics |
|
79 // directly to get access to render method with native |
|
80 // side clearing parameter |
|
81 iSg = new M2GScalableGraphics(); |
|
82 iSg.setRenderingQuality(ScalableGraphics.RENDERING_QUALITY_HIGH); |
|
83 iDeltaTime = DEFAULT_DELTA_TIME; |
|
84 iTask = new SVGAnimatorRunnable(this); |
|
85 } |
|
86 |
|
87 /** |
|
88 * @see org.eclipse.swt.widgets.Control#setParent() |
|
89 */ |
|
90 public boolean setParent(Composite aParent) |
|
91 { |
|
92 parent = aParent; |
|
93 display = aParent.display; |
|
94 |
|
95 // Init widget after parent and display are known |
|
96 //Have to create Widget. |
|
97 //Workaround for Qt changes this method is now in with one int argument.L:\sf\app\jrt\javauis\eswt_qt\org.eclipse.swt\Eclipse SWT\qt\org\eclipse\swt\widgets\Control.java |
|
98 // int i =10; |
|
99 // createWidget(i); |
|
100 // internal_createWidget(); |
|
101 |
|
102 // Add this to necessary listeners |
|
103 addControlListener(this); |
|
104 addKeyListener(this); |
|
105 addMouseListener(this); |
|
106 addDisposeListener(this); |
|
107 parent.getShell().addShellListener(this); |
|
108 addPaintListener(this); |
|
109 |
|
110 return true; |
|
111 } |
|
112 |
|
113 /** |
|
114 * @see org.eclipse.swt.widgets.Control#paint() |
|
115 */ |
|
116 public void paintControl(PaintEvent e) { |
|
117 GC gc = e.gc; |
|
118 |
|
119 // Render image |
|
120 try |
|
121 { |
|
122 iSg.bindTarget(gc); |
|
123 // Parameter true to use native side clearing |
|
124 // -> animator background is always white |
|
125 iSg.render(0, 0, iSVGImage, true); |
|
126 } |
|
127 finally |
|
128 { |
|
129 |
|
130 iSg.releaseTarget(); |
|
131 } |
|
132 } |
|
133 |
|
134 /** |
|
135 * Returns event listener |
|
136 * @return event listener |
|
137 */ |
|
138 public synchronized SVGEventListener getEventListener() |
|
139 { |
|
140 return iEventListener; |
|
141 } |
|
142 |
|
143 /** |
|
144 * @see javax.microedition.m2g.SVGAnimator#getTimeIncrement() |
|
145 */ |
|
146 public synchronized float getTimeIncrement() |
|
147 { |
|
148 return iDeltaTime; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Increases the increment time of the SVGImage. |
|
153 * Increment is done only if the playing state is active. |
|
154 * @see javax.microedition.m2g.SVGImage#incrementTime() |
|
155 */ |
|
156 public synchronized void increaseCurrentTime() |
|
157 { |
|
158 if (iState == STATE_PLAYING) |
|
159 { |
|
160 iSVGImage.incrementTime(iDeltaTime); |
|
161 } |
|
162 } |
|
163 |
|
164 /** |
|
165 * Checks if playing |
|
166 * @return true if playing |
|
167 */ |
|
168 public synchronized boolean isPlaying() |
|
169 { |
|
170 return iState == STATE_PLAYING; |
|
171 } |
|
172 |
|
173 /** |
|
174 * Checks if paused |
|
175 * @return true if paused |
|
176 */ |
|
177 public synchronized boolean isPaused() |
|
178 { |
|
179 return iState == STATE_PAUSED; |
|
180 } |
|
181 |
|
182 /** |
|
183 * Checks if stopped |
|
184 * @return true if stopped |
|
185 */ |
|
186 public synchronized boolean isStopped() |
|
187 { |
|
188 return iState == STATE_STOPPED; |
|
189 } |
|
190 |
|
191 /** |
|
192 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed() |
|
193 * @see javax.microedition.m2g.SVGEventListener#hideNotify() |
|
194 */ |
|
195 public synchronized void widgetDisposed(DisposeEvent e) |
|
196 { |
|
197 if (iEventListener != null) |
|
198 { |
|
199 iEventListener.hideNotify(); |
|
200 } |
|
201 } |
|
202 |
|
203 /** |
|
204 * @see org.eclipse.swt.events.ShellListener#shellActivated() |
|
205 * @see javax.microedition.m2g.SVGEventListener#showNotify() |
|
206 */ |
|
207 public synchronized void shellActivated(ShellEvent e) |
|
208 { |
|
209 if (iEventListener != null) |
|
210 { |
|
211 iEventListener.showNotify(); |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * @see org.eclipse.swt.events.ShellListener#shellClosed() |
|
217 * @see javax.microedition.m2g.SVGEventListener#hideNotify() |
|
218 */ |
|
219 public synchronized void shellClosed(ShellEvent e) |
|
220 { |
|
221 if (iEventListener != null) |
|
222 { |
|
223 iEventListener.hideNotify(); |
|
224 } |
|
225 } |
|
226 |
|
227 /** |
|
228 * @see org.eclipse.swt.events.ShellListener#shellDeactivated() |
|
229 * @see javax.microedition.m2g.SVGEventListener#hideNotify() |
|
230 */ |
|
231 public synchronized void shellDeactivated(ShellEvent e) |
|
232 { |
|
233 if (iEventListener != null) |
|
234 { |
|
235 iEventListener.hideNotify(); |
|
236 } |
|
237 } |
|
238 |
|
239 /** |
|
240 * @see org.eclipse.swt.events.ShellListener#shellDeiconified() |
|
241 * @see javax.microedition.m2g.SVGEventListener#hideNotify() |
|
242 */ |
|
243 public synchronized void shellDeiconified(ShellEvent e) |
|
244 { |
|
245 if (iEventListener != null) |
|
246 { |
|
247 iEventListener.hideNotify(); |
|
248 } |
|
249 } |
|
250 |
|
251 /** |
|
252 * @see org.eclipse.swt.events.ShellListener#shellIconified() |
|
253 * @see javax.microedition.m2g.SVGEventListener#showNotify() |
|
254 */ |
|
255 public synchronized void shellIconified(ShellEvent e) |
|
256 { |
|
257 if (iEventListener != null) |
|
258 { |
|
259 iEventListener.showNotify(); |
|
260 } |
|
261 } |
|
262 |
|
263 /** |
|
264 * @see org.eclipse.swt.events.KeyListener#keyPressed() |
|
265 * @see javax.microedition.m2g.SVGEventListener#keyPressed() |
|
266 */ |
|
267 public synchronized void keyPressed(KeyEvent e) |
|
268 { |
|
269 if (iEventListener != null) |
|
270 { |
|
271 iEventListener.keyPressed(e.keyCode); |
|
272 } |
|
273 } |
|
274 |
|
275 /** |
|
276 * @see org.eclipse.swt.events.KeyListener#keyReleased() |
|
277 * @see javax.microedition.m2g.SVGEventListener#keyPReleased() |
|
278 */ |
|
279 public synchronized void keyReleased(KeyEvent e) |
|
280 { |
|
281 if (iEventListener != null) |
|
282 { |
|
283 iEventListener.keyReleased(e.keyCode); |
|
284 } |
|
285 } |
|
286 |
|
287 /** |
|
288 * @see org.eclipse.swt.events.MouseListener#mouseDown() |
|
289 * @see javax.microedition.m2g.SVGEventListener#pointerPressed() |
|
290 */ |
|
291 public synchronized void mouseDown(MouseEvent e) |
|
292 { |
|
293 if (iEventListener != null) |
|
294 { |
|
295 iEventListener.pointerPressed(e.x, e.y); |
|
296 } |
|
297 } |
|
298 |
|
299 /** |
|
300 * @see org.eclipse.swt.events.MouseListener#mouseUp() |
|
301 * @see javax.microedition.m2g.SVGEventListener#pointerReleased() |
|
302 */ |
|
303 public synchronized void mouseUp(MouseEvent e) |
|
304 { |
|
305 if (iEventListener != null) |
|
306 { |
|
307 iEventListener.pointerReleased(e.x, e.y); |
|
308 } |
|
309 } |
|
310 |
|
311 /** |
|
312 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick() |
|
313 */ |
|
314 public synchronized void mouseDoubleClick(MouseEvent e) |
|
315 { |
|
316 // No implementation needed |
|
317 } |
|
318 |
|
319 /** |
|
320 * @see org.eclipse.swt.events.ControlListener#controlResized() |
|
321 * @see javax.microedition.m2g.SVGEventListener#sizeChanged() |
|
322 */ |
|
323 public synchronized void controlResized(ControlEvent e) |
|
324 { |
|
325 Rectangle bounds = getBounds(); |
|
326 if (iEventListener != null) |
|
327 { |
|
328 iEventListener.sizeChanged(bounds.width, bounds.height); |
|
329 } |
|
330 } |
|
331 |
|
332 /** |
|
333 * @see org.eclipse.swt.events.ControlListener#controlMoved() |
|
334 */ |
|
335 public synchronized void controlMoved(ControlEvent e) |
|
336 { |
|
337 // No implementation needed |
|
338 } |
|
339 |
|
340 /** |
|
341 * @see javax.microedition.m2g.SVGAnimator#pause() |
|
342 */ |
|
343 public synchronized void pause() |
|
344 { |
|
345 iState = STATE_PAUSED; |
|
346 } |
|
347 |
|
348 /** |
|
349 * @see javax.microedition.m2g.SVGAnimator#play() |
|
350 */ |
|
351 public synchronized void play() |
|
352 { |
|
353 if (iState == STATE_STOPPED) |
|
354 { |
|
355 // Set runnable to be run in UI thread |
|
356 display.asyncExec(iTask); |
|
357 } |
|
358 iState = STATE_PLAYING; |
|
359 } |
|
360 |
|
361 /** |
|
362 * @see javax.microedition.m2g.SVGAnimator#setSVGEventListener() |
|
363 */ |
|
364 public synchronized void setEventListener(SVGEventListener eventListener) |
|
365 { |
|
366 iEventListener = eventListener; |
|
367 } |
|
368 |
|
369 /** |
|
370 * @see javax.microedition.m2g.SVGAnimator#setTimeIncrement() |
|
371 */ |
|
372 public synchronized void setTimeIncrement(float aDeltaTime) |
|
373 { |
|
374 iDeltaTime = aDeltaTime; |
|
375 } |
|
376 |
|
377 /** |
|
378 * @see javax.microedition.m2g.SVGAnimator#stop() |
|
379 */ |
|
380 public synchronized void stop() |
|
381 { |
|
382 iState = STATE_STOPPED; |
|
383 } |
|
384 } |
|
385 |
|
386 /** |
|
387 * SVGAnimatorRunnable |
|
388 * Runnable class to perform control redrawing. |
|
389 */ |
|
390 class SVGAnimatorRunnable implements Runnable |
|
391 { |
|
392 //-------------------------------------------------- |
|
393 // VARIABLES |
|
394 //-------------------------------------------------- |
|
395 |
|
396 private WeakReference iWeakControl; |
|
397 |
|
398 //-------------------------------------------------- |
|
399 // METHODS |
|
400 //-------------------------------------------------- |
|
401 |
|
402 public SVGAnimatorRunnable(SVGAnimatorControl aControl) |
|
403 { |
|
404 iWeakControl = new WeakReference(aControl); |
|
405 } |
|
406 /* |
|
407 * @see java.lang.Runnable#run() |
|
408 */ |
|
409 public void run() |
|
410 { |
|
411 SVGAnimatorControl control = (SVGAnimatorControl)iWeakControl.get(); |
|
412 if (control != null && !control.isDisposed()) |
|
413 { |
|
414 try |
|
415 { |
|
416 synchronized (control) |
|
417 { |
|
418 if (control.isPlaying()) |
|
419 { |
|
420 // Playing: increase time |
|
421 control.increaseCurrentTime(); |
|
422 } |
|
423 if (!control.isStopped()) |
|
424 { |
|
425 // Playing or paused: request repainting and new run |
|
426 // If animator has been stopped no new run is requested |
|
427 control.redraw(); |
|
428 control.getDisplay().timerExec( |
|
429 (int)(control.getTimeIncrement() * 1000), |
|
430 this); |
|
431 } |
|
432 } |
|
433 } |
|
434 catch (Exception e) |
|
435 { |
|
436 Logger.ELOG(Logger.EJavaUI, "SVGAnimatorRunnable: run() - exception: " |
|
437 + e.toString()); |
|
438 } |
|
439 } |
|
440 } |
|
441 } |