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