update dev300-m58
[ooovba.git] / canvas / source / java / CanvasSprite.java
blob938d1eda95893477a7bbb57e393737f2fc4f6b8b
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: CanvasSprite.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.*;
33 import com.sun.star.geometry.*;
35 // Java AWT
36 import java.awt.*;
37 import java.awt.geom.*;
39 public class CanvasSprite
40 extends com.sun.star.lib.uno.helper.ComponentBase
41 implements com.sun.star.rendering.XAnimatedSprite,
42 com.sun.star.lang.XServiceInfo,
43 SpriteBase
45 private XAnimation spriteAnimation;
46 private JavaCanvas canvas;
47 private Graphics2D referenceGraphics;
48 private SpriteRunner runner;
49 private ViewState viewState;
50 private double alpha;
51 private java.awt.geom.Point2D.Double outputPosition;
52 private SpriteRep spriteRep;
54 //----------------------------------------------------------------------------------
56 public CanvasSprite( XAnimation _animation, JavaCanvas _canvas, Graphics2D _referenceGraphics )
58 CanvasUtils.printLog( "CanvasSprite constructor called!" );
60 spriteAnimation = _animation;
61 canvas = _canvas;
62 referenceGraphics = _referenceGraphics;
63 alpha = 0.0;
64 outputPosition = new java.awt.geom.Point2D.Double(0.0,0.0);
66 runner = new SpriteRunner( this, spriteAnimation, canvas );
69 public synchronized ViewState getViewState()
71 return viewState;
74 //----------------------------------------------------------------------------------
77 // SpriteBase
78 // ==========
80 public synchronized SpriteRep getSpriteRep()
82 if( spriteRep == null )
84 spriteRep = new SpriteRep();
86 setupSpriteBuffering( CanvasUtils.makeTransform( getViewState().AffineTransform ) );
88 spriteRep.moveSprite( outputPosition );
89 spriteRep.setSpriteAlpha( alpha );
91 // render initial sprite content
92 updateAnimation();
94 return spriteRep;
97 //----------------------------------------------------------------------------------
100 // XComponent
101 // ==========
103 public void dispose()
105 // end the animation thread
106 if( runner != null )
108 runner.quit();
111 runner.join(0); // and wait until it's really over
113 catch( java.lang.InterruptedException e ) {}
116 if( spriteRep != null )
117 spriteRep.dispose();
119 canvas = null;
120 spriteAnimation = null;
121 runner = null;
122 referenceGraphics = null;
123 spriteRep = null;
125 super.dispose();
128 //----------------------------------------------------------------------------------
131 // XSprite impl
132 // ============
135 public synchronized void startAnimation( double speed )
137 runner.startAnimation( speed );
140 public synchronized void stopAnimation()
142 runner.stopAnimation();
145 public synchronized void resetAnimation()
147 runner.resetAnimation();
150 public synchronized void updateAnimation()
152 // only call render explicitely, if animation not
153 // running. Otherwise, next animation render will update
154 // anyway.
155 if( spriteRep != null &&
156 !runner.isAnimationActive() )
158 spriteRep.renderAnimation( spriteAnimation, getViewState(), runner.getCurrentT() );
162 public synchronized void setPriority( double nPriority )
164 // TODO
167 public synchronized void setAlpha( double _alpha )
169 alpha = _alpha;
171 if( spriteRep != null )
173 spriteRep.setSpriteAlpha( alpha );
177 public synchronized void move( com.sun.star.geometry.RealPoint2D _aNewPos,
178 com.sun.star.rendering.ViewState _viewState,
179 com.sun.star.rendering.RenderState _renderState )
181 // transform given point with concatenated transformation
182 AffineTransform transform = CanvasUtils.ViewConcatRenderTransform( _viewState, _renderState );
183 transform.transform( new java.awt.geom.Point2D.Double(_aNewPos.X,
184 _aNewPos.Y),
185 outputPosition );
187 if( spriteRep != null )
189 spriteRep.moveSprite( outputPosition );
193 public synchronized void transform( AffineMatrix2D aTransformation ) throws com.sun.star.lang.IllegalArgumentException
195 // TODO
198 public synchronized void clip( XPolyPolygon2D aClip )
200 // TODO
203 public synchronized void show()
205 canvas.showSprite( this );
206 canvas.updateScreen( false );
209 public synchronized void hide()
211 canvas.hideSprite( this );
213 // dispose and clear SpriteRep, animation content can be
214 // regenerated at any time
215 if( spriteRep != null )
216 spriteRep.dispose();
218 spriteRep = null;
221 public synchronized void setViewState( ViewState _viewState )
223 viewState = CanvasUtils.createAnimationViewState(_viewState,
224 getAnimationAttributes());
226 CanvasUtils.printTransform( CanvasUtils.makeTransform( viewState.AffineTransform ),
227 "CanvasSprite.setViewState" );
229 if( spriteRep != null )
231 // calculate bounds of view-transformed animation output rectangle
232 setupSpriteBuffering( CanvasUtils.makeTransform(getViewState().AffineTransform) );
233 updateAnimation();
237 public synchronized AnimationAttributes getAnimationAttributes()
239 return spriteAnimation.getAnimationAttributes();
242 public synchronized void setAll( RealPoint2D _aNewPos,
243 ViewState _viewState,
244 RenderState _renderState,
245 double _alpha,
246 boolean bUpdateAnimation )
248 alpha = _alpha;
250 // transform given point with concatenated transformation
251 AffineTransform transform = CanvasUtils.ViewConcatRenderTransform( _viewState, _renderState );
252 transform.transform( new java.awt.geom.Point2D.Double(_aNewPos.X,
253 _aNewPos.Y),
254 outputPosition );
256 if( spriteRep != null )
258 spriteRep.setSpriteAlpha( alpha );
259 spriteRep.moveSprite( outputPosition );
261 if( bUpdateAnimation )
262 updateAnimation();
266 //----------------------------------------------------------------------------------
268 private void setupSpriteBuffering( AffineTransform _viewTransform )
270 // determine bounds of view-transformed animation output rectangle
271 com.sun.star.geometry.RealSize2D animSize = getAnimationAttributes().UntransformedSize;
273 java.awt.geom.Rectangle2D.Double aTransformedBounds =
274 CanvasUtils.calcTransformedRectBounds( new java.awt.geom.Rectangle2D.Double(0.0,0.0,
275 animSize.Width,
276 animSize.Height),
277 _viewTransform );
278 CanvasUtils.printTransform( _viewTransform, "setupSpriteBuffering" );
279 CanvasUtils.printLog( "setupSpriteBuffering: bounds are (" + aTransformedBounds.width + ", " + aTransformedBounds.height + ")" );
281 // create a buffer of the appropriate size
282 spriteRep.setupBuffer(referenceGraphics, (int)(aTransformedBounds.width+.5),
283 (int)(aTransformedBounds.height+.5) );
286 //----------------------------------------------------------------------------------
288 private static final String s_implName = "XSprite.java.impl";
289 private static final String s_serviceName = "com.sun.star.rendering.Sprite";
291 //----------------------------------------------------------------------------------
294 // XServiceInfo impl
295 // =================
297 public String getImplementationName()
299 return s_implName;
302 public String [] getSupportedServiceNames()
304 return new String [] { s_serviceName };
307 public boolean supportsService( String serviceName )
309 return serviceName.equals( s_serviceName );