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 <canvas/canvastools.hxx>
28 #include <basegfx/vector/b2dvector.hxx>
29 #include <basegfx/point/b2dpoint.hxx>
30 #include <basegfx/matrix/b2dhommatrix.hxx>
31 #include <basegfx/numeric/ftools.hxx>
34 using namespace ::com::sun::star
;
40 AnimatedSprite::AnimatedSprite( const ViewLayerSharedPtr
& rViewLayer
,
41 const ::basegfx::B2DSize
& rSpriteSizePixel
,
42 double nSpritePrio
) :
43 mpViewLayer( rViewLayer
),
45 maEffectiveSpriteSizePixel( rSpriteSizePixel
),
46 maContentPixelOffset(),
47 mnSpritePrio(nSpritePrio
),
52 mbSpriteVisible( false )
54 ENSURE_OR_THROW( mpViewLayer
, "AnimatedSprite::AnimatedSprite(): Invalid view layer" );
56 // Add half a pixel tolerance to sprite size, since we later on compare
57 // against it in resize(). And view transformations will almost never yield
58 // the same data bits when transforming to device coordinates
59 maEffectiveSpriteSizePixel
+= ::basegfx::B2DSize(0.5, 0.5);
61 mpSprite
= mpViewLayer
->createSprite( maEffectiveSpriteSizePixel
,
64 ENSURE_OR_THROW( mpSprite
, "AnimatedSprite::AnimatedSprite(): Could not create sprite" );
67 ::cppcanvas::CanvasSharedPtr
AnimatedSprite::getContentCanvas() const
69 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "AnimatedSprite::getContentCanvas(): No view layer canvas" );
71 const ::cppcanvas::CanvasSharedPtr
pContentCanvas( mpSprite
->getContentCanvas() );
72 pContentCanvas
->clear();
74 // extract linear part of canvas view transformation
75 // (linear means: without translational components). The
76 // only translation that is imposed at the view transform
77 // is the local content pixel offset.
79 // We can apply that directly here, no need to call
80 // aLinearTransform.translate(), since, as said above, the
81 // last column of aLinearTransform is assumed [0 0 1]
82 ::basegfx::B2DHomMatrix
aLinearTransform( mpViewLayer
->getTransformation() );
83 aLinearTransform
.set( 0, 2, maContentPixelOffset
.getX() );
84 aLinearTransform
.set( 1, 2, maContentPixelOffset
.getY() );
86 // apply linear part of canvas view transformation to sprite canvas
87 pContentCanvas
->setTransformation( aLinearTransform
);
89 return pContentCanvas
;
92 void AnimatedSprite::resize( const ::basegfx::B2DSize
& rSpriteSizePixel
)
94 // Enlarge or reduce the sprite size, if necessary. This
95 // method employs a strategy similar to container, when
96 // allocating memory: size is doubled or halved every time
97 // the limit is reached. This makes for amortized constant
98 // time in runtime complexity. Note that we take exact
99 // powers of two here, since several HW-accelerated canvas
100 // implementations are limited to such sprite sizes
101 // (otherwise, those implementations would internally
102 // round up, too, wasting precious mem).
103 ::basegfx::B2DSize
aNewSize( maEffectiveSpriteSizePixel
);
104 bool bNeedResize( false );
106 if( rSpriteSizePixel
.getX() > maEffectiveSpriteSizePixel
.getX() ||
107 rSpriteSizePixel
.getX() < 0.5*maEffectiveSpriteSizePixel
.getX() )
109 // enlarge or shrink width
110 aNewSize
.setX( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getX()) ) );
114 if( rSpriteSizePixel
.getY() > maEffectiveSpriteSizePixel
.getY() ||
115 rSpriteSizePixel
.getY() < 0.5*maEffectiveSpriteSizePixel
.getY() )
117 // enlarge or shrink height, by doubling it
118 aNewSize
.setY( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel
.getY()) ) );
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
139 if( mpSprite
&& mbSpriteVisible
)
142 mpSprite
->setAlpha( mnAlpha
);
145 mpSprite
->movePixel( *maPosPixel
);
148 mpSprite
->setClip( *maClip
);
153 void AnimatedSprite::setPixelOffset( const ::basegfx::B2DSize
& rPixelOffset
)
155 maContentPixelOffset
= rPixelOffset
;
158 void AnimatedSprite::movePixel( const ::basegfx::B2DPoint
& rNewPos
)
160 maPosPixel
.reset( rNewPos
);
161 mpSprite
->movePixel( rNewPos
);
164 void AnimatedSprite::setAlpha( double nAlpha
)
167 mpSprite
->setAlpha( nAlpha
);
170 void AnimatedSprite::clip( const ::basegfx::B2DPolyPolygon
& rClip
)
172 maClip
.reset( rClip
);
173 mpSprite
->setClipPixel( rClip
);
176 void AnimatedSprite::clip()
182 void AnimatedSprite::transform( const ::basegfx::B2DHomMatrix
& rTransform
)
184 maTransform
.reset( rTransform
);
185 mpSprite
->transform( rTransform
);
188 void AnimatedSprite::hide()
191 mbSpriteVisible
= false;
194 void AnimatedSprite::show()
196 mbSpriteVisible
= true;
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */