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_svx.hxx"
26 #include <svx/sdr/animation/animationstate.hxx>
27 #include <tools/debug.hxx>
28 #include <svx/sdr/contact/viewobjectcontact.hxx>
29 #include <svx/sdr/animation/objectanimator.hxx>
30 #include <svx/sdr/contact/objectcontact.hxx>
31 #include <svx/sdr/contact/viewcontact.hxx>
32 #include <drawinglayer/primitive2d/animatedprimitive2d.hxx>
33 #include <drawinglayer/animation/animationtiming.hxx>
34 
35 //////////////////////////////////////////////////////////////////////////////
36 
37 namespace sdr
38 {
39 	namespace animation
40 	{
getSmallestNextTime(double fCurrentTime)41 		double PrimitiveAnimation::getSmallestNextTime(double fCurrentTime)
42 		{
43 			double fRetval(0.0);
44 
45 			if(maAnimatedPrimitives.hasElements())
46 			{
47 				const sal_Int32 nCount(maAnimatedPrimitives.getLength());
48 
49 				for(sal_Int32 a(0L); a < nCount; a++)
50 				{
51 					const drawinglayer::primitive2d::Primitive2DReference xRef(maAnimatedPrimitives[a]);
52 					const drawinglayer::primitive2d::AnimatedSwitchPrimitive2D* pCandidate = dynamic_cast< const drawinglayer::primitive2d::AnimatedSwitchPrimitive2D* >(xRef.get());
53 					OSL_ENSURE(pCandidate, "PrimitiveAnimation::getSmallestNextTime: wrong primitive in animated list (!)");
54 
55 					if(pCandidate)
56 					{
57 						const drawinglayer::animation::AnimationEntry& rAnimEntry = pCandidate->getAnimationEntry();
58 						const double fNextTime(rAnimEntry.getNextEventTime(fCurrentTime));
59 
60 						if(!::basegfx::fTools::equalZero(fNextTime))
61 						{
62 							if(::basegfx::fTools::equalZero(fRetval))
63 							{
64 								fRetval = fNextTime;
65 							}
66 							else if(::basegfx::fTools::less(fNextTime, fRetval))
67 							{
68 								fRetval = fNextTime;
69 							}
70 						}
71 					}
72 				}
73 			}
74 
75 			return fRetval;
76 		}
77 
prepareNextEvent()78 		void PrimitiveAnimation::prepareNextEvent()
79 		{
80 			const double fCurrentTime(mrVOContact.GetObjectContact().getPrimitiveAnimator().GetTime());
81 			const double fNextTime(getSmallestNextTime(fCurrentTime));
82 
83 			// getSmallestNextTime will be zero when animation ended. If not zero, a next step
84 			// exists
85 			if(!::basegfx::fTools::equalZero(fNextTime))
86 			{
87 				// next time point exists, use it
88 				sal_uInt32 nNextTime;
89 
90 				if(fNextTime >= (double)0xffffff00)
91 				{
92 					// take care for very late points in time, e.g. when a text animation stops
93 					// in a defined AnimationEntryFixed with endless (0xffffffff) duration
94 					nNextTime = GetTime() + (1000 * 60 * 60); // one hour, works with vcl timers, 0xffffff00 was too much...
95 				}
96 				else
97 				{
98 					nNextTime = (sal_uInt32)fNextTime;
99 				}
100 
101 				// ensure step forward in integer timing, the floating step difference maybe smaller than 1.0. Use
102 				// at least 25ms for next step
103 				const sal_uInt32 nMinimumStepTime((sal_uInt32)fCurrentTime + 25L);
104 
105 				if(nNextTime <= nMinimumStepTime)
106 				{
107 					nNextTime = nMinimumStepTime;
108 				}
109 
110 				// set time and reactivate by re-adding to the scheduler
111 				SetTime(nNextTime);
112 				mrVOContact.GetObjectContact().getPrimitiveAnimator().InsertEvent(this);
113 			}
114 		}
115 
PrimitiveAnimation(sdr::contact::ViewObjectContact & rVOContact,const drawinglayer::primitive2d::Primitive2DSequence & rAnimatedPrimitives)116 		PrimitiveAnimation::PrimitiveAnimation(sdr::contact::ViewObjectContact& rVOContact, const drawinglayer::primitive2d::Primitive2DSequence& rAnimatedPrimitives)
117 		:	Event(0L),
118 			mrVOContact(rVOContact),
119 			maAnimatedPrimitives(rAnimatedPrimitives)
120 		{
121 			// setup initially
122 			prepareNextEvent();
123 		}
124 
~PrimitiveAnimation()125 		PrimitiveAnimation::~PrimitiveAnimation()
126 		{
127 			// ensure that Event member is removed from PrimitiveAnimator
128 			mrVOContact.GetObjectContact().getPrimitiveAnimator().RemoveEvent(this);
129 		}
130 
131 		// execute event, from base class Event
Trigger(sal_uInt32)132 		void PrimitiveAnimation::Trigger(sal_uInt32 /*nTime*/)
133 		{
134 			// schedule a repaint of associated object
135 			mrVOContact.ActionChanged();
136 
137 			// re-setup
138 			prepareNextEvent();
139 		}
140 	} // end of namespace animation
141 } // end of namespace sdr
142 
143 //////////////////////////////////////////////////////////////////////////////
144 // eof
145