xref: /AOO41X/main/drawinglayer/source/primitive2d/animatedprimitive2d.cxx (revision 464702f4578bd67db020a330afd07883930c5e07)
1*464702f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*464702f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*464702f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*464702f4SAndrew Rist  * distributed with this work for additional information
6*464702f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*464702f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*464702f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*464702f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*464702f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*464702f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*464702f4SAndrew Rist  * software distributed under the License is distributed on an
15*464702f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*464702f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*464702f4SAndrew Rist  * specific language governing permissions and limitations
18*464702f4SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*464702f4SAndrew Rist  *************************************************************/
21*464702f4SAndrew Rist 
22*464702f4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <drawinglayer/primitive2d/animatedprimitive2d.hxx>
28cdf0e10cSrcweir #include <drawinglayer/animation/animationtiming.hxx>
29cdf0e10cSrcweir #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
30cdf0e10cSrcweir #include <drawinglayer/geometry/viewinformation2d.hxx>
31cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace drawinglayer
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     namespace primitive2d
42cdf0e10cSrcweir     {
AnimatedSwitchPrimitive2D(const animation::AnimationEntry & rAnimationEntry,const Primitive2DSequence & rChildren,bool bIsTextAnimation)43cdf0e10cSrcweir         AnimatedSwitchPrimitive2D::AnimatedSwitchPrimitive2D(
44cdf0e10cSrcweir             const animation::AnimationEntry& rAnimationEntry,
45cdf0e10cSrcweir             const Primitive2DSequence& rChildren,
46cdf0e10cSrcweir             bool bIsTextAnimation)
47cdf0e10cSrcweir         :   GroupPrimitive2D(rChildren),
48cdf0e10cSrcweir             mpAnimationEntry(0),
49cdf0e10cSrcweir             mbIsTextAnimation(bIsTextAnimation)
50cdf0e10cSrcweir         {
51cdf0e10cSrcweir             // clone given animation description
52cdf0e10cSrcweir             mpAnimationEntry = rAnimationEntry.clone();
53cdf0e10cSrcweir         }
54cdf0e10cSrcweir 
~AnimatedSwitchPrimitive2D()55cdf0e10cSrcweir         AnimatedSwitchPrimitive2D::~AnimatedSwitchPrimitive2D()
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             // delete cloned animation description
58cdf0e10cSrcweir             delete mpAnimationEntry;
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir 
operator ==(const BasePrimitive2D & rPrimitive) const61cdf0e10cSrcweir         bool AnimatedSwitchPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             if(GroupPrimitive2D::operator==(rPrimitive))
64cdf0e10cSrcweir             {
65cdf0e10cSrcweir                 const AnimatedSwitchPrimitive2D& rCompare = static_cast< const AnimatedSwitchPrimitive2D& >(rPrimitive);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir                 return (getAnimationEntry() == rCompare.getAnimationEntry());
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir             return false;
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir 
get2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const73cdf0e10cSrcweir         Primitive2DSequence AnimatedSwitchPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir             if(getChildren().hasElements())
76cdf0e10cSrcweir             {
77cdf0e10cSrcweir                 const double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime()));
78cdf0e10cSrcweir                 const sal_uInt32 nLen(getChildren().getLength());
79cdf0e10cSrcweir                 sal_uInt32 nIndex(basegfx::fround(fState * (double)nLen));
80cdf0e10cSrcweir 
81cdf0e10cSrcweir                 if(nIndex >= nLen)
82cdf0e10cSrcweir                 {
83cdf0e10cSrcweir                     nIndex = nLen - 1L;
84cdf0e10cSrcweir                 }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir                 const Primitive2DReference xRef(getChildren()[nIndex], uno::UNO_QUERY_THROW);
87cdf0e10cSrcweir                 return Primitive2DSequence(&xRef, 1L);
88cdf0e10cSrcweir             }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             return Primitive2DSequence();
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         // provide unique ID
94cdf0e10cSrcweir         ImplPrimitrive2DIDBlock(AnimatedSwitchPrimitive2D, PRIMITIVE2D_ID_ANIMATEDSWITCHPRIMITIVE2D)
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     } // end of namespace primitive2d
97cdf0e10cSrcweir } // end of namespace drawinglayer
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
100cdf0e10cSrcweir 
101cdf0e10cSrcweir namespace drawinglayer
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     namespace primitive2d
104cdf0e10cSrcweir     {
AnimatedBlinkPrimitive2D(const animation::AnimationEntry & rAnimationEntry,const Primitive2DSequence & rChildren,bool bIsTextAnimation)105cdf0e10cSrcweir         AnimatedBlinkPrimitive2D::AnimatedBlinkPrimitive2D(
106cdf0e10cSrcweir             const animation::AnimationEntry& rAnimationEntry,
107cdf0e10cSrcweir             const Primitive2DSequence& rChildren,
108cdf0e10cSrcweir             bool bIsTextAnimation)
109cdf0e10cSrcweir         :   AnimatedSwitchPrimitive2D(rAnimationEntry, rChildren, bIsTextAnimation)
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
get2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const113cdf0e10cSrcweir         Primitive2DSequence AnimatedBlinkPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             if(getChildren().hasElements())
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 const double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime()));
118cdf0e10cSrcweir 
119cdf0e10cSrcweir                 if(fState < 0.5)
120cdf0e10cSrcweir                 {
121cdf0e10cSrcweir                     return getChildren();
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             return Primitive2DSequence();
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         // provide unique ID
129cdf0e10cSrcweir         ImplPrimitrive2DIDBlock(AnimatedBlinkPrimitive2D, PRIMITIVE2D_ID_ANIMATEDBLINKPRIMITIVE2D)
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     } // end of namespace primitive2d
132cdf0e10cSrcweir } // end of namespace drawinglayer
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
135cdf0e10cSrcweir 
136cdf0e10cSrcweir namespace drawinglayer
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     namespace primitive2d
139cdf0e10cSrcweir     {
AnimatedInterpolatePrimitive2D(const std::vector<basegfx::B2DHomMatrix> & rmMatrixStack,const animation::AnimationEntry & rAnimationEntry,const Primitive2DSequence & rChildren,bool bIsTextAnimation)140cdf0e10cSrcweir         AnimatedInterpolatePrimitive2D::AnimatedInterpolatePrimitive2D(
141cdf0e10cSrcweir             const std::vector< basegfx::B2DHomMatrix >& rmMatrixStack,
142cdf0e10cSrcweir             const animation::AnimationEntry& rAnimationEntry,
143cdf0e10cSrcweir             const Primitive2DSequence& rChildren,
144cdf0e10cSrcweir             bool bIsTextAnimation)
145cdf0e10cSrcweir         :   AnimatedSwitchPrimitive2D(rAnimationEntry, rChildren, bIsTextAnimation),
146cdf0e10cSrcweir             maMatrixStack()
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             // copy matrices to locally pre-decomposed matrix stack
149cdf0e10cSrcweir             const sal_uInt32 nCount(rmMatrixStack.size());
150cdf0e10cSrcweir             maMatrixStack.reserve(nCount);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < nCount; a++)
153cdf0e10cSrcweir             {
154cdf0e10cSrcweir                 maMatrixStack.push_back(basegfx::tools::B2DHomMatrixBufferedDecompose(rmMatrixStack[a]));
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir 
get2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const158cdf0e10cSrcweir         Primitive2DSequence AnimatedInterpolatePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             const sal_uInt32 nSize(maMatrixStack.size());
161cdf0e10cSrcweir 
162cdf0e10cSrcweir             if(nSize)
163cdf0e10cSrcweir             {
164cdf0e10cSrcweir                 double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime()));
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                 if(fState < 0.0)
167cdf0e10cSrcweir                 {
168cdf0e10cSrcweir                     fState = 0.0;
169cdf0e10cSrcweir                 }
170cdf0e10cSrcweir                 else if(fState > 1.0)
171cdf0e10cSrcweir                 {
172cdf0e10cSrcweir                     fState = 1.0;
173cdf0e10cSrcweir                 }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir                 const double fIndex(fState * (double)(nSize - 1L));
176cdf0e10cSrcweir                 const sal_uInt32 nIndA(sal_uInt32(floor(fIndex)));
177cdf0e10cSrcweir                 const double fOffset(fIndex - (double)nIndA);
178cdf0e10cSrcweir                 basegfx::B2DHomMatrix aTargetTransform;
179cdf0e10cSrcweir                 std::vector< basegfx::tools::B2DHomMatrixBufferedDecompose >::const_iterator aMatA(maMatrixStack.begin() + nIndA);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                 if(basegfx::fTools::equalZero(fOffset))
182cdf0e10cSrcweir                 {
183cdf0e10cSrcweir                     // use matrix from nIndA directly
184cdf0e10cSrcweir                     aTargetTransform = aMatA->getB2DHomMatrix();
185cdf0e10cSrcweir                 }
186cdf0e10cSrcweir                 else
187cdf0e10cSrcweir                 {
188cdf0e10cSrcweir                     // interpolate. Get involved buffered decomposed matrices
189cdf0e10cSrcweir                     const sal_uInt32 nIndB((nIndA + 1L) % nSize);
190cdf0e10cSrcweir                     std::vector< basegfx::tools::B2DHomMatrixBufferedDecompose >::const_iterator aMatB(maMatrixStack.begin() + nIndB);
191cdf0e10cSrcweir 
192cdf0e10cSrcweir                     // interpolate for fOffset [0.0 .. 1.0[
193cdf0e10cSrcweir                     const basegfx::B2DVector aScale(basegfx::interpolate(aMatA->getScale(), aMatB->getScale(), fOffset));
194cdf0e10cSrcweir                     const basegfx::B2DVector aTranslate(basegfx::interpolate(aMatA->getTranslate(), aMatB->getTranslate(), fOffset));
195cdf0e10cSrcweir                     const double fRotate(((aMatB->getRotate() - aMatA->getRotate()) * fOffset) + aMatA->getRotate());
196cdf0e10cSrcweir                     const double fShearX(((aMatB->getShearX() - aMatA->getShearX()) * fOffset) + aMatA->getShearX());
197cdf0e10cSrcweir 
198cdf0e10cSrcweir                     // build matrix for state
199cdf0e10cSrcweir                     aTargetTransform = basegfx::tools::createScaleShearXRotateTranslateB2DHomMatrix(
200cdf0e10cSrcweir                         aScale, fShearX, fRotate, aTranslate);
201cdf0e10cSrcweir                 }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir                 // create new transform primitive reference, return new sequence
204cdf0e10cSrcweir                 const Primitive2DReference xRef(new TransformPrimitive2D(aTargetTransform, getChildren()));
205cdf0e10cSrcweir                 return Primitive2DSequence(&xRef, 1L);
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir             else
208cdf0e10cSrcweir             {
209cdf0e10cSrcweir                 return getChildren();
210cdf0e10cSrcweir             }
211cdf0e10cSrcweir         }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         // provide unique ID
214cdf0e10cSrcweir         ImplPrimitrive2DIDBlock(AnimatedInterpolatePrimitive2D, PRIMITIVE2D_ID_ANIMATEDINTERPOLATEPRIMITIVE2D)
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     } // end of namespace primitive2d
217cdf0e10cSrcweir } // end of namespace drawinglayer
218cdf0e10cSrcweir 
219cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
220cdf0e10cSrcweir // eof
221