1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <drawinglayer/primitive2d/animatedprimitive2d.hxx>
32*cdf0e10cSrcweir #include <drawinglayer/animation/animationtiming.hxx>
33*cdf0e10cSrcweir #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
34*cdf0e10cSrcweir #include <drawinglayer/geometry/viewinformation2d.hxx>
35*cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace com::sun::star;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir namespace drawinglayer
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir 	namespace primitive2d
46*cdf0e10cSrcweir 	{
47*cdf0e10cSrcweir 		AnimatedSwitchPrimitive2D::AnimatedSwitchPrimitive2D(
48*cdf0e10cSrcweir 			const animation::AnimationEntry& rAnimationEntry,
49*cdf0e10cSrcweir 			const Primitive2DSequence& rChildren,
50*cdf0e10cSrcweir 			bool bIsTextAnimation)
51*cdf0e10cSrcweir 		:	GroupPrimitive2D(rChildren),
52*cdf0e10cSrcweir 			mpAnimationEntry(0),
53*cdf0e10cSrcweir 			mbIsTextAnimation(bIsTextAnimation)
54*cdf0e10cSrcweir 		{
55*cdf0e10cSrcweir 			// clone given animation description
56*cdf0e10cSrcweir 			mpAnimationEntry = rAnimationEntry.clone();
57*cdf0e10cSrcweir 		}
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 		AnimatedSwitchPrimitive2D::~AnimatedSwitchPrimitive2D()
60*cdf0e10cSrcweir 		{
61*cdf0e10cSrcweir 			// delete cloned animation description
62*cdf0e10cSrcweir 			delete mpAnimationEntry;
63*cdf0e10cSrcweir 		}
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 		bool AnimatedSwitchPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
66*cdf0e10cSrcweir 		{
67*cdf0e10cSrcweir 			if(GroupPrimitive2D::operator==(rPrimitive))
68*cdf0e10cSrcweir 			{
69*cdf0e10cSrcweir 				const AnimatedSwitchPrimitive2D& rCompare = static_cast< const AnimatedSwitchPrimitive2D& >(rPrimitive);
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 				return (getAnimationEntry() == rCompare.getAnimationEntry());
72*cdf0e10cSrcweir 			}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 			return false;
75*cdf0e10cSrcweir 		}
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 		Primitive2DSequence AnimatedSwitchPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
78*cdf0e10cSrcweir 		{
79*cdf0e10cSrcweir 			if(getChildren().hasElements())
80*cdf0e10cSrcweir 			{
81*cdf0e10cSrcweir 				const double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime()));
82*cdf0e10cSrcweir 				const sal_uInt32 nLen(getChildren().getLength());
83*cdf0e10cSrcweir 				sal_uInt32 nIndex(basegfx::fround(fState * (double)nLen));
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 				if(nIndex >= nLen)
86*cdf0e10cSrcweir 				{
87*cdf0e10cSrcweir 					nIndex = nLen - 1L;
88*cdf0e10cSrcweir 				}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 				const Primitive2DReference xRef(getChildren()[nIndex], uno::UNO_QUERY_THROW);
91*cdf0e10cSrcweir 				return Primitive2DSequence(&xRef, 1L);
92*cdf0e10cSrcweir 			}
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 			return Primitive2DSequence();
95*cdf0e10cSrcweir 		}
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 		// provide unique ID
98*cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(AnimatedSwitchPrimitive2D, PRIMITIVE2D_ID_ANIMATEDSWITCHPRIMITIVE2D)
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	} // end of namespace primitive2d
101*cdf0e10cSrcweir } // end of namespace drawinglayer
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir namespace drawinglayer
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	namespace primitive2d
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		AnimatedBlinkPrimitive2D::AnimatedBlinkPrimitive2D(
110*cdf0e10cSrcweir 			const animation::AnimationEntry& rAnimationEntry,
111*cdf0e10cSrcweir 			const Primitive2DSequence& rChildren,
112*cdf0e10cSrcweir 			bool bIsTextAnimation)
113*cdf0e10cSrcweir 		:	AnimatedSwitchPrimitive2D(rAnimationEntry, rChildren, bIsTextAnimation)
114*cdf0e10cSrcweir 		{
115*cdf0e10cSrcweir 		}
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 		Primitive2DSequence AnimatedBlinkPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
118*cdf0e10cSrcweir 		{
119*cdf0e10cSrcweir 			if(getChildren().hasElements())
120*cdf0e10cSrcweir 			{
121*cdf0e10cSrcweir 				const double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime()));
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 				if(fState < 0.5)
124*cdf0e10cSrcweir 				{
125*cdf0e10cSrcweir 					return getChildren();
126*cdf0e10cSrcweir 				}
127*cdf0e10cSrcweir 			}
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 			return Primitive2DSequence();
130*cdf0e10cSrcweir 		}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		// provide unique ID
133*cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(AnimatedBlinkPrimitive2D, PRIMITIVE2D_ID_ANIMATEDBLINKPRIMITIVE2D)
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	} // end of namespace primitive2d
136*cdf0e10cSrcweir } // end of namespace drawinglayer
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir namespace drawinglayer
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	namespace primitive2d
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir 		AnimatedInterpolatePrimitive2D::AnimatedInterpolatePrimitive2D(
145*cdf0e10cSrcweir 			const std::vector< basegfx::B2DHomMatrix >& rmMatrixStack,
146*cdf0e10cSrcweir 			const animation::AnimationEntry& rAnimationEntry,
147*cdf0e10cSrcweir 			const Primitive2DSequence& rChildren,
148*cdf0e10cSrcweir 			bool bIsTextAnimation)
149*cdf0e10cSrcweir 		:	AnimatedSwitchPrimitive2D(rAnimationEntry, rChildren, bIsTextAnimation),
150*cdf0e10cSrcweir 			maMatrixStack()
151*cdf0e10cSrcweir 		{
152*cdf0e10cSrcweir 			// copy matrices to locally pre-decomposed matrix stack
153*cdf0e10cSrcweir 			const sal_uInt32 nCount(rmMatrixStack.size());
154*cdf0e10cSrcweir             maMatrixStack.reserve(nCount);
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nCount; a++)
157*cdf0e10cSrcweir 			{
158*cdf0e10cSrcweir 				maMatrixStack.push_back(basegfx::tools::B2DHomMatrixBufferedDecompose(rmMatrixStack[a]));
159*cdf0e10cSrcweir 			}
160*cdf0e10cSrcweir 		}
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 		Primitive2DSequence AnimatedInterpolatePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
163*cdf0e10cSrcweir 		{
164*cdf0e10cSrcweir 			const sal_uInt32 nSize(maMatrixStack.size());
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 			if(nSize)
167*cdf0e10cSrcweir 			{
168*cdf0e10cSrcweir 				double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime()));
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 				if(fState < 0.0)
171*cdf0e10cSrcweir 				{
172*cdf0e10cSrcweir 					fState = 0.0;
173*cdf0e10cSrcweir 				}
174*cdf0e10cSrcweir 				else if(fState > 1.0)
175*cdf0e10cSrcweir 				{
176*cdf0e10cSrcweir 					fState = 1.0;
177*cdf0e10cSrcweir 				}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 				const double fIndex(fState * (double)(nSize - 1L));
180*cdf0e10cSrcweir 				const sal_uInt32 nIndA(sal_uInt32(floor(fIndex)));
181*cdf0e10cSrcweir 				const double fOffset(fIndex - (double)nIndA);
182*cdf0e10cSrcweir 				basegfx::B2DHomMatrix aTargetTransform;
183*cdf0e10cSrcweir 				std::vector< basegfx::tools::B2DHomMatrixBufferedDecompose >::const_iterator aMatA(maMatrixStack.begin() + nIndA);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 				if(basegfx::fTools::equalZero(fOffset))
186*cdf0e10cSrcweir 				{
187*cdf0e10cSrcweir 					// use matrix from nIndA directly
188*cdf0e10cSrcweir 					aTargetTransform = aMatA->getB2DHomMatrix();
189*cdf0e10cSrcweir 				}
190*cdf0e10cSrcweir 				else
191*cdf0e10cSrcweir 				{
192*cdf0e10cSrcweir 					// interpolate. Get involved buffered decomposed matrices
193*cdf0e10cSrcweir 					const sal_uInt32 nIndB((nIndA + 1L) % nSize);
194*cdf0e10cSrcweir 					std::vector< basegfx::tools::B2DHomMatrixBufferedDecompose >::const_iterator aMatB(maMatrixStack.begin() + nIndB);
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 					// interpolate for fOffset [0.0 .. 1.0[
197*cdf0e10cSrcweir 					const basegfx::B2DVector aScale(basegfx::interpolate(aMatA->getScale(), aMatB->getScale(), fOffset));
198*cdf0e10cSrcweir 					const basegfx::B2DVector aTranslate(basegfx::interpolate(aMatA->getTranslate(), aMatB->getTranslate(), fOffset));
199*cdf0e10cSrcweir 					const double fRotate(((aMatB->getRotate() - aMatA->getRotate()) * fOffset) + aMatA->getRotate());
200*cdf0e10cSrcweir 					const double fShearX(((aMatB->getShearX() - aMatA->getShearX()) * fOffset) + aMatA->getShearX());
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 					// build matrix for state
203*cdf0e10cSrcweir 					aTargetTransform = basegfx::tools::createScaleShearXRotateTranslateB2DHomMatrix(
204*cdf0e10cSrcweir 						aScale, fShearX, fRotate, aTranslate);
205*cdf0e10cSrcweir 				}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 				// create new transform primitive reference, return new sequence
208*cdf0e10cSrcweir 				const Primitive2DReference xRef(new TransformPrimitive2D(aTargetTransform, getChildren()));
209*cdf0e10cSrcweir 				return Primitive2DSequence(&xRef, 1L);
210*cdf0e10cSrcweir 			}
211*cdf0e10cSrcweir 			else
212*cdf0e10cSrcweir 			{
213*cdf0e10cSrcweir 				return getChildren();
214*cdf0e10cSrcweir 			}
215*cdf0e10cSrcweir 		}
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 		// provide unique ID
218*cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(AnimatedInterpolatePrimitive2D, PRIMITIVE2D_ID_ANIMATEDINTERPOLATEPRIMITIVE2D)
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	} // end of namespace primitive2d
221*cdf0e10cSrcweir } // end of namespace drawinglayer
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
224*cdf0e10cSrcweir // eof
225