1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CanvasSprite.java,v $
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 ************************************************************************/
32 import com
.sun
.star
.rendering
.*;
33 import com
.sun
.star
.geometry
.*;
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
,
45 private XAnimation spriteAnimation
;
46 private JavaCanvas canvas
;
47 private Graphics2D referenceGraphics
;
48 private SpriteRunner runner
;
49 private ViewState viewState
;
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
;
62 referenceGraphics
= _referenceGraphics
;
64 outputPosition
= new java
.awt
.geom
.Point2D
.Double(0.0,0.0);
66 runner
= new SpriteRunner( this, spriteAnimation
, canvas
);
69 public synchronized ViewState
getViewState()
74 //----------------------------------------------------------------------------------
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
97 //----------------------------------------------------------------------------------
103 public void dispose()
105 // end the animation thread
111 runner
.join(0); // and wait until it's really over
113 catch( java
.lang
.InterruptedException e
) {}
116 if( spriteRep
!= null )
120 spriteAnimation
= null;
122 referenceGraphics
= null;
128 //----------------------------------------------------------------------------------
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
155 if( spriteRep
!= null &&
156 !runner
.isAnimationActive() )
158 spriteRep
.renderAnimation( spriteAnimation
, getViewState(), runner
.getCurrentT() );
162 public synchronized void setPriority( double nPriority
)
167 public synchronized void setAlpha( double _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
,
187 if( spriteRep
!= null )
189 spriteRep
.moveSprite( outputPosition
);
193 public synchronized void transform( AffineMatrix2D aTransformation
) throws com
.sun
.star
.lang
.IllegalArgumentException
198 public synchronized void clip( XPolyPolygon2D aClip
)
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 )
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
) );
237 public synchronized AnimationAttributes
getAnimationAttributes()
239 return spriteAnimation
.getAnimationAttributes();
242 public synchronized void setAll( RealPoint2D _aNewPos
,
243 ViewState _viewState
,
244 RenderState _renderState
,
246 boolean bUpdateAnimation
)
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
,
256 if( spriteRep
!= null )
258 spriteRep
.setSpriteAlpha( alpha
);
259 spriteRep
.moveSprite( outputPosition
);
261 if( bUpdateAnimation
)
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,
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 //----------------------------------------------------------------------------------
297 public String
getImplementationName()
302 public String
[] getSupportedServiceNames()
304 return new String
[] { s_serviceName
};
307 public boolean supportsService( String serviceName
)
309 return serviceName
.equals( s_serviceName
);