merge the formfield patch from ooo-build
[ooovba.git] / slideshow / source / engine / animatedsprite.cxx
blob85afde9fc43ee1628d436dc844d322d454c46a40
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: animatedsprite.cxx,v $
10 * $Revision: 1.14 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 // must be first
35 #include <canvas/debug.hxx>
36 #include <tools/diagnose_ex.h>
38 #include <animatedsprite.hxx>
40 #include <cppcanvas/canvas.hxx>
41 #include <canvas/canvastools.hxx>
43 #include <basegfx/vector/b2dvector.hxx>
44 #include <basegfx/point/b2dpoint.hxx>
45 #include <basegfx/matrix/b2dhommatrix.hxx>
46 #include <basegfx/numeric/ftools.hxx>
49 using namespace ::com::sun::star;
51 namespace slideshow
53 namespace internal
55 AnimatedSprite::AnimatedSprite( const ViewLayerSharedPtr& rViewLayer,
56 const ::basegfx::B2DSize& rSpriteSizePixel,
57 double nSpritePrio ) :
58 mpViewLayer( rViewLayer ),
59 mpSprite(),
60 maEffectiveSpriteSizePixel( rSpriteSizePixel ),
61 maContentPixelOffset(),
62 mnSpritePrio(nSpritePrio),
63 mnAlpha(0.0),
64 maPosPixel(),
65 maClip(),
66 maTransform(),
67 mbSpriteVisible( false )
69 ENSURE_OR_THROW( mpViewLayer, "AnimatedSprite::AnimatedSprite(): Invalid view layer" );
71 // Add half a pixel tolerance to sprite size, since we later on compare
72 // against it in resize(). And view transformations will almost never yield
73 // the same data bits when transforming to device coordinates
74 maEffectiveSpriteSizePixel += ::basegfx::B2DSize(0.5, 0.5);
76 mpSprite = mpViewLayer->createSprite( maEffectiveSpriteSizePixel,
77 mnSpritePrio );
79 ENSURE_OR_THROW( mpSprite, "AnimatedSprite::AnimatedSprite(): Could not create sprite" );
82 ::cppcanvas::CanvasSharedPtr AnimatedSprite::getContentCanvas() const
84 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "AnimatedSprite::getContentCanvas(): No view layer canvas" );
86 const ::cppcanvas::CanvasSharedPtr pContentCanvas( mpSprite->getContentCanvas() );
87 pContentCanvas->clear();
89 // extract linear part of canvas view transformation
90 // (linear means: without translational components). The
91 // only translation that is imposed at the view transform
92 // is the local content pixel offset.
93 //
94 // We can apply that directly here, no need to call
95 // aLinearTransform.translate(), since, as said above, the
96 // last column of aLinearTransform is assumed [0 0 1]
97 ::basegfx::B2DHomMatrix aLinearTransform( mpViewLayer->getTransformation() );
98 aLinearTransform.set( 0, 2, maContentPixelOffset.getX() );
99 aLinearTransform.set( 1, 2, maContentPixelOffset.getY() );
101 // apply linear part of canvas view transformation to sprite canvas
102 pContentCanvas->setTransformation( aLinearTransform );
104 return pContentCanvas;
107 bool AnimatedSprite::resize( const ::basegfx::B2DSize& rSpriteSizePixel )
109 // Enlarge or reduce the sprite size, if necessary. This
110 // method employs a strategy similar to container, when
111 // allocating memory: size is doubled or halved everytime
112 // the limit is reached. This makes for amortized constant
113 // time in runtime complexity. Note that we take exact
114 // powers of two here, since several HW-accelerated canvas
115 // implementations are limited to such sprite sizes
116 // (otherwise, those implementations would internally
117 // round up, too, wasting precious mem).
118 ::basegfx::B2DSize aNewSize( maEffectiveSpriteSizePixel );
119 bool bNeedResize( false );
121 if( rSpriteSizePixel.getX() > maEffectiveSpriteSizePixel.getX() ||
122 rSpriteSizePixel.getX() < 0.5*maEffectiveSpriteSizePixel.getX() )
124 // enlarge or shrink width
125 aNewSize.setX( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel.getX()) ) );
126 bNeedResize = true;
129 if( rSpriteSizePixel.getY() > maEffectiveSpriteSizePixel.getY() ||
130 rSpriteSizePixel.getY() < 0.5*maEffectiveSpriteSizePixel.getY() )
132 // enlarge or shrink height, by doubling it
133 aNewSize.setY( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel.getY()) ) );
134 bNeedResize = true;
137 if( bNeedResize )
139 // as the old sprite might have already been altered
140 // (and therefore been placed in the update list of
141 // the spritecanvas for this frame), must hide it
142 // here, to ensure it's not visible on screen any
143 // longer.
144 mpSprite->hide();
146 maEffectiveSpriteSizePixel = aNewSize;
147 mpSprite = mpViewLayer->createSprite( maEffectiveSpriteSizePixel,
148 mnSpritePrio );
150 ENSURE_OR_THROW( mpSprite,
151 "AnimatedSprite::resize(): Could not create new sprite" );
153 // set attributes similar to previous sprite
154 if( mpSprite && mbSpriteVisible )
156 mpSprite->show();
157 mpSprite->setAlpha( mnAlpha );
159 if( maPosPixel )
160 mpSprite->movePixel( *maPosPixel );
162 if( maClip )
163 mpSprite->setClip( *maClip );
167 return mpSprite;
170 void AnimatedSprite::setPixelOffset( const ::basegfx::B2DSize& rPixelOffset )
172 maContentPixelOffset = rPixelOffset;
175 ::basegfx::B2DSize AnimatedSprite::getPixelOffset() const
177 return maContentPixelOffset;
180 void AnimatedSprite::movePixel( const ::basegfx::B2DPoint& rNewPos )
182 maPosPixel.reset( rNewPos );
183 mpSprite->movePixel( rNewPos );
186 void AnimatedSprite::setAlpha( double nAlpha )
188 mnAlpha = nAlpha;
189 mpSprite->setAlpha( nAlpha );
192 void AnimatedSprite::clip( const ::basegfx::B2DPolyPolygon& rClip )
194 maClip.reset( rClip );
195 mpSprite->setClipPixel( rClip );
198 void AnimatedSprite::clip()
200 maClip.reset();
201 mpSprite->setClip();
204 void AnimatedSprite::transform( const ::basegfx::B2DHomMatrix& rTransform )
206 maTransform.reset( rTransform );
207 mpSprite->transform( rTransform );
210 void AnimatedSprite::setPriority( double nPrio )
212 mpSprite->setPriority( nPrio );
215 void AnimatedSprite::hide()
217 mpSprite->hide();
218 mbSpriteVisible = false;
221 void AnimatedSprite::show()
223 mbSpriteVisible = true;
224 mpSprite->show();