javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/M2GSVGAnimator.java
changeset 87 1627c337e51e
parent 80 d6dafc5d983f
equal deleted inserted replaced
80:d6dafc5d983f 87:1627c337e51e
     1 /*
     1 /*
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    24 import java.lang.ref.WeakReference;
    24 import java.lang.ref.WeakReference;
    25 import com.nokia.mj.impl.rt.support.Finalizer;
    25 import com.nokia.mj.impl.rt.support.Finalizer;
    26 import com.nokia.mj.impl.utils.Logger;
    26 import com.nokia.mj.impl.utils.Logger;
    27 
    27 
    28 
    28 
    29 public class M2GSVGAnimator extends SVGAnimator 
    29 public class M2GSVGAnimator extends SVGAnimator
    30 {
    30 {
    31     //--------------------------------------------------
    31     //--------------------------------------------------
    32     // STATIC CONSTANTS
    32     // STATIC CONSTANTS
    33     //--------------------------------------------------
    33     //--------------------------------------------------
    34     private static final String ANIMATOR_CANVAS_BASE_CLASS =
    34     private static final String ANIMATOR_CANVAS_BASE_CLASS =
    35         "javax.microedition.lcdui.Canvas";
    35         "javax.microedition.lcdui.Canvas";
    36     // Exception text
       
    37     /* Optimization: static finals changed to local variables
       
    38     private static final String COMPONENT_BASE_CLASS_NOT_SUPPORTED_ESTR =
       
    39     "The requested componentBaseClass is not supported by the implementation.";
       
    40     private static final String ILLEGAL_TIME_INCREMENT_ESTR =
       
    41     "The timeIncrement is less than or equal to zero.";
       
    42     private static final String ANIMATOR_PLAY_ESTR =
       
    43     "The animator is not currently in the stopped or paused state.";
       
    44     private static final String ANIMATOR_PAUSE_ESTR =
       
    45     "The animator is not in the playing  state.";
       
    46     private static final String ANIMATOR_STOP_ESTR =
       
    47     "The animator is not in the playing or paused state.";
       
    48     private static final String INVALID_RUNNABLE_ESTR =
       
    49     "The runnable is null.";
       
    50     private static final String ANIMATOR_IS_STOPPED_ESTR =
       
    51     "The animator is in the stopped state.";
       
    52     private static final String RUNNABLE_IS_NULL_ESTR =
       
    53     "The runnable is null.";
       
    54     private static final String ANIMATOR_INVOKE_ESTR =
       
    55     "The animator is in the stopped state.";
       
    56     */
       
    57 
    36 
    58     //--------------------------------------------------
    37     //--------------------------------------------------
    59     // VARIABLES
    38     // VARIABLES
    60     //--------------------------------------------------
    39     //--------------------------------------------------
    61     private M2GSVGCanvas    iSVGCanvas      = null;
    40     private M2GSVGCanvas    iSVGCanvas      = null;
    68      * Constructor
    47      * Constructor
    69      * @param aImage
    48      * @param aImage
    70      */
    49      */
    71     protected M2GSVGAnimator(SVGImage aImage)
    50     protected M2GSVGAnimator(SVGImage aImage)
    72     {
    51     {
    73 				
    52 
    74         iSVGCanvas = new M2GSVGCanvas(false, aImage);
    53         iSVGCanvas = new M2GSVGCanvas(false, aImage);
    75         mFinalizer = new Finalizer()
    54         mFinalizer = new Finalizer()
    76         {	
    55         {
    77             public void finalizeImpl()
    56             public void finalizeImpl()
    78             {
    57             {
    79             		
    58 
    80                 doFinalize();
    59                 doFinalize();
    81             }
    60             }
    82         };
    61         };
    83 				
    62 
    84     }
    63     }
    85 
    64 
    86     /**
    65     /**
    87      * @see javax.microedition.m2g.SVGAnimator#getTargetComponent()
    66      * @see javax.microedition.m2g.SVGAnimator#getTargetComponent()
    88      */
    67      */
   155     /**
   134     /**
   156     * @see javax.microedition.m2g.SVGAnimator#play()
   135     * @see javax.microedition.m2g.SVGAnimator#play()
   157     */
   136     */
   158     public void play()
   137     public void play()
   159     {
   138     {
   160 				
   139 
   161         if (iSVGCanvas.isPlaying())
   140         if (iSVGCanvas.isPlaying())
   162         {
   141         {
   163             throw new IllegalStateException(
   142             throw new IllegalStateException(
   164                 /*SF*/"The animator is not currently in the stopped or paused state."/*SF*/);
   143                 /*SF*/"The animator is not currently in the stopped or paused state."/*SF*/);
   165         }
   144         }
   166         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "play()");
   145         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "play()");
   167         
   146 
   168         iSVGCanvas.play();
   147         iSVGCanvas.play();
   169     }
   148     }
   170 
   149 
   171     private void doFinalize()
   150     private void doFinalize()
   172     {
   151     {
   173         if (mFinalizer != null)
   152         if (mFinalizer != null)
   174         {
   153         {
   175 						
   154 
   176             registeredFinalize();
   155             registeredFinalize();
   177             mFinalizer = null;
   156             mFinalizer = null;
   178         }
   157         }
   179     }
   158     }
   180 
   159 
   199     /**
   178     /**
   200      * @see javax.microedition.m2g.SVGAnimator#setTimeIncrement()
   179      * @see javax.microedition.m2g.SVGAnimator#setTimeIncrement()
   201      */
   180      */
   202     public void setTimeIncrement(float timeIncrement)
   181     public void setTimeIncrement(float timeIncrement)
   203     {
   182     {
   204     		
   183 
   205         if (timeIncrement <= 0)
   184         if (timeIncrement <= 0)
   206         {
   185         {
   207             throw new IllegalArgumentException(
   186             throw new IllegalArgumentException(
   208                 /*SF*/"The time increment is less than or equal to zero."/*SF*/);
   187                 /*SF*/"The time increment is less than or equal to zero."/*SF*/);
   209         }
   188         }
   210         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "setTimeIncrement() - "
   189         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "setTimeIncrement() - "
   211                    + timeIncrement);
   190                    + timeIncrement);
   212         iSVGCanvas.setTimeIncrement(timeIncrement);
   191         iSVGCanvas.setTimeIncrement(timeIncrement);
   213         
   192 
   214     }
   193     }
   215 
   194 
   216     /**
   195     /**
   217      * @see javax.microedition.m2g.SVGAnimator#stop()
   196      * @see javax.microedition.m2g.SVGAnimator#stop()
   218      */
   197      */
   219     public void stop()
   198     public void stop()
   220     {
   199     {
   221     		
   200 
   222         if (iSVGCanvas.isStopped())
   201         if (iSVGCanvas.isStopped())
   223         {
   202         {
   224             throw new IllegalStateException(
   203             throw new IllegalStateException(
   225                 /*SF*/"The animator is not in the playing or paused state."/*SF*/);
   204                 /*SF*/"The animator is not in the playing or paused state."/*SF*/);
   226         }
   205         }
   227         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "stop()");
   206         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "stop()");
   228 
   207 
   229         iSVGCanvas.stop();
   208         iSVGCanvas.stop();
   230         
   209 
   231     }
   210     }
   232 
   211 
   233     //--------------------------------------------------
   212     //--------------------------------------------------
   234     // STATIC METHODS
   213     // STATIC METHODS
   235     //--------------------------------------------------
   214     //--------------------------------------------------
   239      * @return SVGAnimator
   218      * @return SVGAnimator
   240      * @see javax.microedition.m2g.SVGAnimator#createAnimator()
   219      * @see javax.microedition.m2g.SVGAnimator#createAnimator()
   241      */
   220      */
   242     public static SVGAnimator buildAnimator(SVGImage svgImage)
   221     public static SVGAnimator buildAnimator(SVGImage svgImage)
   243     {
   222     {
   244 	   		
   223 
   245         if (svgImage == null)
   224         if (svgImage == null)
   246         {
   225         {
   247         		
   226 
   248             throw new NullPointerException();
   227             throw new NullPointerException();
   249         }
   228         }
   250         
   229 
   251         return new M2GSVGAnimator(svgImage);
   230         return new M2GSVGAnimator(svgImage);
   252     }
   231     }
   253 
   232 
   254     /**
   233     /**
   255      * Builds animator
   234      * Builds animator
   317     //--------------------------------------------------
   296     //--------------------------------------------------
   318     /**
   297     /**
   319      * @see javax.microedition.lcdui.game.GameCanvas#GameCanvas()
   298      * @see javax.microedition.lcdui.game.GameCanvas#GameCanvas()
   320      */
   299      */
   321     public M2GSVGCanvas(boolean aSuppressKeyEvents, SVGImage aSVGImage)
   300     public M2GSVGCanvas(boolean aSuppressKeyEvents, SVGImage aSVGImage)
   322     {					
   301     {
   323 
   302 
   324         super(aSuppressKeyEvents);
   303         super(aSuppressKeyEvents);
   325 				
   304 
   326         // get the instance to the Graphics of the offscreen buffer
   305         // get the instance to the Graphics of the offscreen buffer
   327         iOffscreen = getGraphics();
   306         iOffscreen = getGraphics();
   328 
   307 
   329         iSVGImage     = aSVGImage;
   308         iSVGImage     = aSVGImage;
   330 
   309 
   331         // down-casting to M2GDocument/M2GSVGSVGElement to have access to internal methods
   310         // down-casting to M2GDocument/M2GSVGSVGElement to have access to internal methods
   332         M2GDocument doc = (M2GDocument)iSVGImage.getDocument();
   311         M2GDocument doc = (M2GDocument)iSVGImage.getDocument();
   333         iRootElement  = (M2GSVGSVGElement)iSVGImage.getDocument().getDocumentElement();
   312         iRootElement  = (M2GSVGSVGElement)iSVGImage.getDocument().getDocumentElement();
   334         
   313 
   335 				
   314 
   336         iState = STATE_STOPPED;
   315         iState = STATE_STOPPED;
   337         // Create render context
   316         // Create render context
   338         iSg = ScalableGraphics.createInstance();
   317         iSg = ScalableGraphics.createInstance();
   339         iSg.setRenderingQuality(ScalableGraphics.RENDERING_QUALITY_HIGH);
   318         iSg.setRenderingQuality(ScalableGraphics.RENDERING_QUALITY_HIGH);
   340         iDeltaTime = DEFAULT_DELTA_TIME;
   319         iDeltaTime = DEFAULT_DELTA_TIME;
   341 
   320 
   342         doc.registerDOMChangeObserver(this);
   321         doc.registerDOMChangeObserver(this);
   343         
   322 
   344         
   323 
   345         
   324 
   346         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "Ctor - delta time:"
   325         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "Ctor - delta time:"
   347                    + iDeltaTime + ", state:" + iState);
   326                    + iDeltaTime + ", state:" + iState);
   348     }
   327     }
   349 
   328 
   350     /**
   329     /**
   498      * @see javax.microedition.lcdui.game.GameCanvas#paint()
   477      * @see javax.microedition.lcdui.game.GameCanvas#paint()
   499      */
   478      */
   500     public void paint(Graphics g)
   479     public void paint(Graphics g)
   501     {
   480     {
   502         // Clears bitmap
   481         // Clears bitmap
   503         
   482 
   504         g.setColor(255, 255, 255);
   483         g.setColor(255, 255, 255);
   505         g.fillRect(0, 0, getWidth(), getHeight());
   484         g.fillRect(0, 0, getWidth(), getHeight());
   506 
   485 
   507         try
   486         try
   508         {
   487         {
   509         		
   488 
   510             iSg.bindTarget(g);
   489             iSg.bindTarget(g);
   511 
   490 
   512 						
   491 
   513             // NOTE: Source is defaultly fully opaque
   492             // NOTE: Source is defaultly fully opaque
   514             iSg.render(0, 0, iSVGImage);
   493             iSg.render(0, 0, iSVGImage);
   515             
   494 
   516         }
   495         }
   517         finally
   496         finally
   518         {
   497         {
   519         		
   498 
   520             iSg.releaseTarget();
   499             iSg.releaseTarget();
   521             
   500 
   522         }
   501         }
   523     }
   502     }
   524 
   503 
   525     /**
   504     /**
   526      * Paints a frame to the offscreen of this GameCanvas
   505      * Paints a frame to the offscreen of this GameCanvas
   635      * @see javax.microedition.m2g.SVGAnimator#play()
   614      * @see javax.microedition.m2g.SVGAnimator#play()
   636      */
   615      */
   637     public synchronized void play()
   616     public synchronized void play()
   638     {
   617     {
   639         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "play()");
   618         Logger.LOG(Logger.EJavaUI, Logger.EInfo, "play()");
   640 				
   619 
   641         if (iState == STATE_PLAYING)
   620         if (iState == STATE_PLAYING)
   642         {
   621         {
   643             // don't do anything if animation is already playing
   622             // don't do anything if animation is already playing
   644             return;
   623             return;
   645         }
   624         }