1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <canvas/debug.hxx>
23 #include <tools/diagnose_ex.h>
25 #include <animatedsprite.hxx>
27 #include <cppcanvas/canvas.hxx>
28 #include <canvas/canvastools.hxx>
30 #include <basegfx/vector/b2dvector.hxx>
31 #include <basegfx/point/b2dpoint.hxx>
32 #include <basegfx/matrix/b2dhommatrix.hxx>
33 #include <basegfx/numeric/ftools.hxx>
36 using namespace ::com::sun::star
;
42 AnimatedSprite::AnimatedSprite( const ViewLayerSharedPtr
& rViewLayer
,
43 const ::basegfx::B2DSize
& rSpriteSizePixel
,
44 double nSpritePrio
) :
45 mpViewLayer( rViewLayer
),
47 maEffectiveSpriteSizePixel( rSpriteSizePixel
),
48 maContentPixelOffset(),
49 mnSpritePrio(nSpritePrio
),
54 mbSpriteVisible( false )
56 ENSURE_OR_THROW( mpViewLayer
, "AnimatedSprite::AnimatedSprite(): Invalid view layer" );
58 // Add half a pixel tolerance to sprite size, since we later on compare
59 // against it in resize(). And view transformations will almost never yield
60 // the same data bits when transforming to device coordinates
61 maEffectiveSpriteSizePixel
+= ::basegfx::B2DSize(0.5, 0.5);
63 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
66 ENSURE_OR_THROW( mpSprite
, "AnimatedSprite::AnimatedSprite(): Could not create sprite" );
69 ::cppcanvas::CanvasSharedPtr
AnimatedSprite::getContentCanvas() const
71 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "AnimatedSprite::getContentCanvas(): No view layer canvas" );
73 const ::cppcanvas::CanvasSharedPtr
pContentCanvas( mpSprite
->getContentCanvas() );
74 pContentCanvas
->clear();
76 // extract linear part of canvas view transformation
77 // (linear means: without translational components). The
78 // only translation that is imposed at the view transform
79 // is the local content pixel offset.
81 // We can apply that directly here, no need to call
82 // aLinearTransform.translate(), since, as said above, the
83 // last column of aLinearTransform is assumed [0 0 1]
84 ::basegfx::B2DHomMatrix
aLinearTransform( mpViewLayer
->getTransformation() );
85 aLinearTransform
.set( 0, 2, maContentPixelOffset
.getX() );
86 aLinearTransform
.set( 1, 2, maContentPixelOffset
.getY() );
88 // apply linear part of canvas view transformation to sprite canvas
89 pContentCanvas
->setTransformation( aLinearTransform
);
91 return pContentCanvas
;
94 bool AnimatedSprite::resize( const ::basegfx::B2DSize
& rSpriteSizePixel
)
96 // Enlarge or reduce the sprite size, if necessary. This
97 // method employs a strategy similar to container, when
98 // allocating memory: size is doubled or halved every time
99 // the limit is reached. This makes for amortized constant
100 // time in runtime complexity. Note that we take exact
101 // powers of two here, since several HW-accelerated canvas
102 // implementations are limited to such sprite sizes
103 // (otherwise, those implementations would internally
104 // round up, too, wasting precious mem).
105 ::basegfx::B2DSize
aNewSize( maEffectiveSpriteSizePixel
);
106 bool bNeedResize( false );
108 if( rSpriteSizePixel
.getX() > maEffectiveSpriteSizePixel
.getX() ||
109 rSpriteSizePixel
.getX() < 0.5*maEffectiveSpriteSizePixel
.getX() )
111 // enlarge or shrink width
112 aNewSize
.setX( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getX()) ) );
116 if( rSpriteSizePixel
.getY() > maEffectiveSpriteSizePixel
.getY() ||
117 rSpriteSizePixel
.getY() < 0.5*maEffectiveSpriteSizePixel
.getY() )
119 // enlarge or shrink height, by doubling it
120 aNewSize
.setY( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getY()) ) );
126 // as the old sprite might have already been altered
127 // (and therefore been placed in the update list of
128 // the spritecanvas for this frame), must hide it
129 // here, to ensure it's not visible on screen any
133 maEffectiveSpriteSizePixel
= aNewSize
;
134 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
137 ENSURE_OR_THROW( mpSprite
,
138 "AnimatedSprite::resize(): Could not create new sprite" );
140 // set attributes similar to previous sprite
141 if( mpSprite
&& mbSpriteVisible
)
144 mpSprite
->setAlpha( mnAlpha
);
147 mpSprite
->movePixel( *maPosPixel
);
150 mpSprite
->setClip( *maClip
);
154 return static_cast< bool >(mpSprite
);
157 void AnimatedSprite::setPixelOffset( const ::basegfx::B2DSize
& rPixelOffset
)
159 maContentPixelOffset
= rPixelOffset
;
162 void AnimatedSprite::movePixel( const ::basegfx::B2DPoint
& rNewPos
)
164 maPosPixel
.reset( rNewPos
);
165 mpSprite
->movePixel( rNewPos
);
168 void AnimatedSprite::setAlpha( double nAlpha
)
171 mpSprite
->setAlpha( nAlpha
);
174 void AnimatedSprite::clip( const ::basegfx::B2DPolyPolygon
& rClip
)
176 maClip
.reset( rClip
);
177 mpSprite
->setClipPixel( rClip
);
180 void AnimatedSprite::clip()
186 void AnimatedSprite::transform( const ::basegfx::B2DHomMatrix
& rTransform
)
188 maTransform
.reset( rTransform
);
189 mpSprite
->transform( rTransform
);
192 void AnimatedSprite::hide()
195 mbSpriteVisible
= false;
198 void AnimatedSprite::show()
200 mbSpriteVisible
= true;
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */