LibreOffice Module slideshow (master) 1
animatedsprite.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
22
23#include <animatedsprite.hxx>
24
25#include <cppcanvas/canvas.hxx>
28
33#include <utility>
34
35
36using namespace ::com::sun::star;
37
38namespace slideshow::internal
39{
41 const ::basegfx::B2DSize& rSpriteSizePixel,
42 double nSpritePrio ) :
43 mpViewLayer(std::move( xViewLayer )),
44 mpSprite(),
45 maEffectiveSpriteSizePixel( rSpriteSizePixel ),
46 maContentPixelOffset(),
47 mnSpritePrio(nSpritePrio),
48 mnAlpha(0.0),
49 maPosPixel(),
50 maClip(),
51 mbSpriteVisible( false )
52 {
53 ENSURE_OR_THROW( mpViewLayer, "AnimatedSprite::AnimatedSprite(): Invalid view layer" );
54
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
59
62
63 ENSURE_OR_THROW( mpSprite, "AnimatedSprite::AnimatedSprite(): Could not create sprite" );
64 }
65
67 {
68 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "AnimatedSprite::getContentCanvas(): No view layer canvas" );
69
70 const ::cppcanvas::CanvasSharedPtr pContentCanvas( mpSprite->getContentCanvas() );
71 pContentCanvas->clear();
72
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.
77
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() );
84
85 // apply linear part of canvas view transformation to sprite canvas
86 pContentCanvas->setTransformation( aLinearTransform );
87
88 return pContentCanvas;
89 }
90
91 void AnimatedSprite::resize( const ::basegfx::B2DSize& rSpriteSizePixel )
92 {
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).
103 bool bNeedResize( false );
104
105 if( rSpriteSizePixel.getWidth() > maEffectiveSpriteSizePixel.getWidth() ||
106 rSpriteSizePixel.getWidth() < 0.5 * maEffectiveSpriteSizePixel.getWidth() )
107 {
108 // enlarge or shrink width
109 aNewSize.setWidth( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel.getWidth()) ) );
110 bNeedResize = true;
111 }
112
113 if( rSpriteSizePixel.getHeight() > maEffectiveSpriteSizePixel.getHeight() ||
114 rSpriteSizePixel.getHeight() < 0.5*maEffectiveSpriteSizePixel.getHeight() )
115 {
116 // enlarge or shrink height, by doubling it
117 aNewSize.setHeight( ::canvas::tools::nextPow2( ::basegfx::fround(rSpriteSizePixel.getHeight()) ) );
118 bNeedResize = true;
119 }
120
121 if( !bNeedResize )
122 return;
123
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
128 // longer.
129 mpSprite->hide();
130
133 mnSpritePrio );
134
136 "AnimatedSprite::resize(): Could not create new sprite" );
137
138 // set attributes similar to previous sprite
139 if (mbSpriteVisible)
140 {
141 mpSprite->show();
142 mpSprite->setAlpha( mnAlpha );
143
144 if( maPosPixel )
145 mpSprite->movePixel( *maPosPixel );
146
147 if( maClip )
148 mpSprite->setClip( *maClip );
149 }
150 }
151
152 void AnimatedSprite::setPixelOffset( const ::basegfx::B2DSize& rPixelOffset )
153 {
154 maContentPixelOffset = rPixelOffset;
155 }
156
157 void AnimatedSprite::movePixel( const ::basegfx::B2DPoint& rNewPos )
158 {
159 maPosPixel = rNewPos;
160 mpSprite->movePixel( rNewPos );
161 }
162
163 void AnimatedSprite::setAlpha( double nAlpha )
164 {
165 mnAlpha = nAlpha;
166 mpSprite->setAlpha( nAlpha );
167 }
168
169 void AnimatedSprite::clip( const ::basegfx::B2DPolyPolygon& rClip )
170 {
171 maClip = rClip;
172 mpSprite->setClipPixel( rClip );
173 }
174
176 {
177 maClip.reset();
178 mpSprite->setClip();
179 }
180
181 void AnimatedSprite::transform( const ::basegfx::B2DHomMatrix& rTransform )
182 {
183 mpSprite->transform( rTransform );
184 }
185
187 {
188 mpSprite->hide();
189 mbSpriteVisible = false;
190 }
191
193 {
194 mbSpriteVisible = true;
195 mpSprite->show();
196 }
197
198}
199
200/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
void setHeight(TYPE const &rHeight)
TYPE getWidth() const
void setWidth(TYPE const &rWidth)
TYPE getHeight() const
::cppcanvas::CustomSpriteSharedPtr mpSprite
void setPixelOffset(const ::basegfx::B2DSize &rPixelOffset)
Set an offset for the content output in pixel.
void resize(const ::basegfx::B2DSize &rSpriteSizePixel)
Resize the sprite.
void movePixel(const ::basegfx::B2DPoint &rNewPos)
Move the sprite in device pixel space.
::cppcanvas::CanvasSharedPtr getContentCanvas() const
Query the content canvas for the current sprite.
void transform(const ::basegfx::B2DHomMatrix &rTransform)
Set a sprite transformation.
void setAlpha(double rAlpha)
Set the alpha value of the sprite.
::std::optional< ::basegfx::B2DPoint > maPosPixel
AnimatedSprite(ViewLayerSharedPtr xViewLayer, const ::basegfx::B2DSize &rSpriteSizePixel, double nSpritePrio)
Create a new AnimatedSprite, for the given metafile shape.
::basegfx::B2DSize maEffectiveSpriteSizePixel
void clip()
Clears a sprite clip.
::std::optional< ::basegfx::B2DPolyPolygon > maClip
#define ENSURE_OR_THROW(c, m)
std::shared_ptr< Canvas > CanvasSharedPtr
std::shared_ptr< ViewLayer > ViewLayerSharedPtr
Definition: viewlayer.hxx:168
std::weak_ptr< cppcanvas::CustomSprite > mpSprite
output surface (necessarily a sprite, won't otherwise be able to display anything before other sprite...
Definition: slideview.cxx:85
basegfx::B2DPolyPolygon maClip
Current clip polygon in user coordinates.
Definition: slideview.cxx:385