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 .
21 #include <comphelper/diagnose_ex.hxx>
23 #include <animatedsprite.hxx>
25 #include <cppcanvas/canvas.hxx>
26 #include <cppcanvas/customsprite.hxx>
27 #include <canvas/canvastools.hxx>
29 #include <basegfx/vector/b2dvector.hxx>
30 #include <basegfx/point/b2dpoint.hxx>
31 #include <basegfx/matrix/b2dhommatrix.hxx>
32 #include <basegfx/numeric/ftools.hxx>
36 using namespace ::com::sun::star
;
38 namespace slideshow::internal
40 AnimatedSprite::AnimatedSprite( ViewLayerSharedPtr xViewLayer
,
41 const ::basegfx::B2DSize
& rSpriteSizePixel
,
42 double nSpritePrio
) :
43 mpViewLayer(std::move( xViewLayer
)),
45 maEffectiveSpriteSizePixel( rSpriteSizePixel
),
46 maContentPixelOffset(),
47 mnSpritePrio(nSpritePrio
),
51 mbSpriteVisible( false )
53 ENSURE_OR_THROW( mpViewLayer
, "AnimatedSprite::AnimatedSprite(): Invalid view layer" );
55 // Add half a pixel tolerance to sprite size, since we later on compare
56 // against it in resize(). And view transformations will almost never yield
57 // the same data bits when transforming to device coordinates
58 maEffectiveSpriteSizePixel
+= ::basegfx::B2DSize(0.5, 0.5);
60 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
63 ENSURE_OR_THROW( mpSprite
, "AnimatedSprite::AnimatedSprite(): Could not create sprite" );
66 ::cppcanvas::CanvasSharedPtr
AnimatedSprite::getContentCanvas() const
68 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "AnimatedSprite::getContentCanvas(): No view layer canvas" );
70 const ::cppcanvas::CanvasSharedPtr
pContentCanvas( mpSprite
->getContentCanvas() );
71 pContentCanvas
->clear();
73 // extract linear part of canvas view transformation
74 // (linear means: without translational components). The
75 // only translation that is imposed at the view transform
76 // is the local content pixel offset.
78 // We can apply that directly here, no need to call
79 // aLinearTransform.translate(), since, as said above, the
80 // last column of aLinearTransform is assumed [0 0 1]
81 ::basegfx::B2DHomMatrix
aLinearTransform( mpViewLayer
->getTransformation() );
82 aLinearTransform
.set( 0, 2, maContentPixelOffset
.getWidth() );
83 aLinearTransform
.set( 1, 2, maContentPixelOffset
.getHeight() );
85 // apply linear part of canvas view transformation to sprite canvas
86 pContentCanvas
->setTransformation( aLinearTransform
);
88 return pContentCanvas
;
91 void AnimatedSprite::resize( const ::basegfx::B2DSize
& rSpriteSizePixel
)
93 // Enlarge or reduce the sprite size, if necessary. This
94 // method employs a strategy similar to container, when
95 // allocating memory: size is doubled or halved every time
96 // the limit is reached. This makes for amortized constant
97 // time in runtime complexity. Note that we take exact
98 // powers of two here, since several HW-accelerated canvas
99 // implementations are limited to such sprite sizes
100 // (otherwise, those implementations would internally
101 // round up, too, wasting precious mem).
102 ::basegfx::B2DSize
aNewSize( maEffectiveSpriteSizePixel
);
103 bool bNeedResize( false );
105 if( rSpriteSizePixel
.getWidth() > maEffectiveSpriteSizePixel
.getWidth() ||
106 rSpriteSizePixel
.getWidth() < 0.5 * maEffectiveSpriteSizePixel
.getWidth() )
108 // enlarge or shrink width
109 aNewSize
.setWidth( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getWidth()) ) );
113 if( rSpriteSizePixel
.getHeight() > maEffectiveSpriteSizePixel
.getHeight() ||
114 rSpriteSizePixel
.getHeight() < 0.5*maEffectiveSpriteSizePixel
.getHeight() )
116 // enlarge or shrink height, by doubling it
117 aNewSize
.setHeight( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getHeight()) ) );
124 // as the old sprite might have already been altered
125 // (and therefore been placed in the update list of
126 // the spritecanvas for this frame), must hide it
127 // here, to ensure it's not visible on screen any
131 maEffectiveSpriteSizePixel
= aNewSize
;
132 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
135 ENSURE_OR_THROW( mpSprite
,
136 "AnimatedSprite::resize(): Could not create new sprite" );
138 // set attributes similar to previous sprite
142 mpSprite
->setAlpha( mnAlpha
);
145 mpSprite
->movePixel( *maPosPixel
);
148 mpSprite
->setClip( *maClip
);
152 void AnimatedSprite::setPixelOffset( const ::basegfx::B2DSize
& rPixelOffset
)
154 maContentPixelOffset
= rPixelOffset
;
157 void AnimatedSprite::movePixel( const ::basegfx::B2DPoint
& rNewPos
)
159 maPosPixel
= rNewPos
;
160 mpSprite
->movePixel( rNewPos
);
163 void AnimatedSprite::setAlpha( double nAlpha
)
166 mpSprite
->setAlpha( nAlpha
);
169 void AnimatedSprite::clip( const ::basegfx::B2DPolyPolygon
& rClip
)
172 mpSprite
->setClipPixel( rClip
);
175 void AnimatedSprite::clip()
181 void AnimatedSprite::transform( const ::basegfx::B2DHomMatrix
& rTransform
)
183 mpSprite
->transform( rTransform
);
186 void AnimatedSprite::hide()
189 mbSpriteVisible
= false;
192 void AnimatedSprite::show()
194 mbSpriteVisible
= true;
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */