Update ooo320-m1
[ooovba.git] / canvas / source / java / SpriteRunner.java
blob80b3da23b0de6cf03274443b96077b05637d7f24
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SpriteRunner.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // Canvas
32 import com.sun.star.rendering.*;
35 // HOWTO get a Graphics2D from a window
38 // import javax.swing.*;
39 // import java.awt.*;
40 // import java.awt.geom.*;
42 // public class Stroke1 extends JFrame {
43 // Stroke drawingStroke = new BasicStroke(5);
44 // Rectangle2D rect =
45 // new Rectangle2D.Double(20, 40, 100, 40);
47 // public void paint(Graphics g) {
48 // Graphics2D g2d = (Graphics2D)g;
49 // g2d.setStroke(drawingStroke);
50 // g2d.draw(rect);
51 // }
53 // public static void main(String args[]) {
54 // JFrame frame = new Stroke1();
55 // frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
56 // frame.setSize(200, 100);
57 // frame.show();
58 // }
59 // }
62 public class SpriteRunner
63 extends Thread
65 private CanvasSprite sprite;
66 private XAnimation spriteAnimation;
67 private JavaCanvas canvas;
68 private double startTime;
69 private double currentSpeed;
70 private double currentT;
71 private boolean animationActive;
72 private boolean stayAlive;
74 //----------------------------------------------------------------------------------
76 public SpriteRunner( CanvasSprite _sprite, XAnimation _animation, JavaCanvas _canvas )
78 CanvasUtils.printLog( "SpriteRunner constructor called!" );
80 sprite = _sprite;
81 spriteAnimation = _animation;
82 canvas = _canvas;
83 startTime = 0.0;
84 currentSpeed = 0.0;
85 currentT = 0.0;
86 animationActive = false;
87 stayAlive = true;
89 // Set priority to lower-than-normal, as this thread runs the
90 // animation in a busy loop.
91 setPriority( MIN_PRIORITY );
94 //----------------------------------------------------------------------------------
97 // Thread
98 // ======
100 // Overriding
102 public void run()
104 // perform the animation rendering (as fast as possible, for now)
106 while( stayAlive )
108 while( animationActive )
110 // to determine the current animation step to render, calc the
111 // elapsed time since this animation was started
112 double elapsedTime = getCurrentTime() - startTime;
114 // the frame to render is determined by mapping the cycle
115 // duration (currentSpeed) to the [0,1] range of the animation.
116 currentT = (elapsedTime % currentSpeed) / currentSpeed;
118 // delegate animation rendering to SpriteCanvas, as
119 // only this instance can enforce consistency (on-time
120 // saving of background etc.)
121 canvas.renderAnimation( sprite, spriteAnimation, currentT );
123 // TODO: Consolidate _all_ sprites per canvas into one
124 // thread, call updateScreen() only after all sprites
125 // have been processed.
127 //Make changes visible
128 canvas.updateScreen( false );
130 // TODO: Evaluate vs. setPriority and other means
131 // try
132 // {
133 // // give other tasks some time to breathe (10ms - this is only meant symbolically)
134 // sleep(10);
135 // } catch( InterruptedException e ) { }
138 // wait until animation is activated again
141 wait();
143 catch( InterruptedException e ) { }
144 catch( IllegalMonitorStateException e ) { }
148 public synchronized void startAnimation( double speed )
150 resetAnimation();
151 currentSpeed = speed;
153 // start us, if not done already
154 if( !isAlive() )
155 start();
157 // enable animation thread
158 animationActive = true;
162 notify();
163 } catch( IllegalMonitorStateException e ) { }
166 public synchronized void stopAnimation()
168 // stop animation after the current frame has been completely
169 // rendered
170 animationActive = false;
173 public synchronized void resetAnimation()
175 startTime = getCurrentTime();
178 public synchronized boolean isAnimationActive()
180 return animationActive;
183 public synchronized void quit()
185 stayAlive = false;
186 stopAnimation();
189 public synchronized double getCurrentT()
191 return currentT;
194 //----------------------------------------------------------------------------------
196 // helper
197 static double getCurrentTime()
199 // determine current time in seconds
200 java.util.Calendar cal = new java.util.GregorianCalendar();
201 return cal.getTimeInMillis() / 1000.0;