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 <tools/diagnose_ex.h>
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>
35 using namespace ::com::sun::star
;
37 namespace slideshow::internal
39 AnimatedSprite::AnimatedSprite( const ViewLayerSharedPtr
& rViewLayer
,
40 const ::basegfx::B2DSize
& rSpriteSizePixel
,
41 double nSpritePrio
) :
42 mpViewLayer( rViewLayer
),
44 maEffectiveSpriteSizePixel( rSpriteSizePixel
),
45 maContentPixelOffset(),
46 mnSpritePrio(nSpritePrio
),
50 mbSpriteVisible( false )
52 ENSURE_OR_THROW( mpViewLayer
, "AnimatedSprite::AnimatedSprite(): Invalid view layer" );
54 // Add half a pixel tolerance to sprite size, since we later on compare
55 // against it in resize(). And view transformations will almost never yield
56 // the same data bits when transforming to device coordinates
57 maEffectiveSpriteSizePixel
+= ::basegfx::B2DSize(0.5, 0.5);
59 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
62 ENSURE_OR_THROW( mpSprite
, "AnimatedSprite::AnimatedSprite(): Could not create sprite" );
65 ::cppcanvas::CanvasSharedPtr
AnimatedSprite::getContentCanvas() const
67 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "AnimatedSprite::getContentCanvas(): No view layer canvas" );
69 const ::cppcanvas::CanvasSharedPtr
pContentCanvas( mpSprite
->getContentCanvas() );
70 pContentCanvas
->clear();
72 // extract linear part of canvas view transformation
73 // (linear means: without translational components). The
74 // only translation that is imposed at the view transform
75 // is the local content pixel offset.
77 // We can apply that directly here, no need to call
78 // aLinearTransform.translate(), since, as said above, the
79 // last column of aLinearTransform is assumed [0 0 1]
80 ::basegfx::B2DHomMatrix
aLinearTransform( mpViewLayer
->getTransformation() );
81 aLinearTransform
.set( 0, 2, maContentPixelOffset
.getX() );
82 aLinearTransform
.set( 1, 2, maContentPixelOffset
.getY() );
84 // apply linear part of canvas view transformation to sprite canvas
85 pContentCanvas
->setTransformation( aLinearTransform
);
87 return pContentCanvas
;
90 void AnimatedSprite::resize( const ::basegfx::B2DSize
& rSpriteSizePixel
)
92 // Enlarge or reduce the sprite size, if necessary. This
93 // method employs a strategy similar to container, when
94 // allocating memory: size is doubled or halved every time
95 // the limit is reached. This makes for amortized constant
96 // time in runtime complexity. Note that we take exact
97 // powers of two here, since several HW-accelerated canvas
98 // implementations are limited to such sprite sizes
99 // (otherwise, those implementations would internally
100 // round up, too, wasting precious mem).
101 ::basegfx::B2DSize
aNewSize( maEffectiveSpriteSizePixel
);
102 bool bNeedResize( false );
104 if( rSpriteSizePixel
.getX() > maEffectiveSpriteSizePixel
.getX() ||
105 rSpriteSizePixel
.getX() < 0.5*maEffectiveSpriteSizePixel
.getX() )
107 // enlarge or shrink width
108 aNewSize
.setX( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getX()) ) );
112 if( rSpriteSizePixel
.getY() > maEffectiveSpriteSizePixel
.getY() ||
113 rSpriteSizePixel
.getY() < 0.5*maEffectiveSpriteSizePixel
.getY() )
115 // enlarge or shrink height, by doubling it
116 aNewSize
.setY( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getY()) ) );
123 // as the old sprite might have already been altered
124 // (and therefore been placed in the update list of
125 // the spritecanvas for this frame), must hide it
126 // here, to ensure it's not visible on screen any
130 maEffectiveSpriteSizePixel
= aNewSize
;
131 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
134 ENSURE_OR_THROW( mpSprite
,
135 "AnimatedSprite::resize(): Could not create new sprite" );
137 // set attributes similar to previous sprite
141 mpSprite
->setAlpha( mnAlpha
);
144 mpSprite
->movePixel( *maPosPixel
);
147 mpSprite
->setClip( *maClip
);
151 void AnimatedSprite::setPixelOffset( const ::basegfx::B2DSize
& rPixelOffset
)
153 maContentPixelOffset
= rPixelOffset
;
156 void AnimatedSprite::movePixel( const ::basegfx::B2DPoint
& rNewPos
)
158 maPosPixel
= rNewPos
;
159 mpSprite
->movePixel( rNewPos
);
162 void AnimatedSprite::setAlpha( double nAlpha
)
165 mpSprite
->setAlpha( nAlpha
);
168 void AnimatedSprite::clip( const ::basegfx::B2DPolyPolygon
& rClip
)
171 mpSprite
->setClipPixel( rClip
);
174 void AnimatedSprite::clip()
180 void AnimatedSprite::transform( const ::basegfx::B2DHomMatrix
& rTransform
)
182 mpSprite
->transform( rTransform
);
185 void AnimatedSprite::hide()
188 mbSpriteVisible
= false;
191 void AnimatedSprite::show()
193 mbSpriteVisible
= true;
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */