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