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 #include "precompiled_sd.hxx"
25 #include "controller/SlsAnimationFunction.hxx"
26 #include "model/SlsPageDescriptor.hxx"
27 #include "view/SlideSorterView.hxx"
28 
29 
30 #include <osl/diagnose.hxx>
31 #include <rtl/math.hxx>
32 
33 namespace sd { namespace slidesorter { namespace controller {
34 
35 
Linear(const double nTime)36 double AnimationFunction::Linear (const double nTime)
37 {
38     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
39     return nTime;
40 }
41 
42 
43 
44 
FastInSlowOut_Sine(const double nTime)45 double AnimationFunction::FastInSlowOut_Sine (const double nTime)
46 {
47     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
48 
49     const double nResult (sin(nTime * M_PI/2));
50 
51     OSL_ASSERT(nResult>=0.0 && nResult<=1.0);
52     return nResult;
53 }
54 
55 
56 
57 
FastInSlowOut_Root(const double nTime)58 double AnimationFunction::FastInSlowOut_Root (const double nTime)
59 {
60     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
61 
62     const double nResult (sqrt(nTime));
63 
64     OSL_ASSERT(nResult>=0.0 && nResult<=1.0);
65     return nResult;
66 }
67 
68 
69 
70 
SlowInSlowOut_0to0_Sine(const double nTime)71 double AnimationFunction::SlowInSlowOut_0to0_Sine (const double nTime)
72 {
73     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
74 
75     const double nResult (sin(nTime * M_PI));
76 
77     OSL_ASSERT(nResult>=0.0 && nResult<=1.0);
78     return nResult;
79 }
80 
81 
82 
83 
Vibrate_Sine(const double nTime)84 double AnimationFunction::Vibrate_Sine (const double nTime)
85 {
86     return sin(nTime*M_PI*8);
87 }
88 
89 
90 
91 
ScalePoint(const Point & rPoint,const double nTime)92 Point AnimationFunction::ScalePoint (const Point& rPoint, const double nTime)
93 {
94     return Point(
95         sal_Int32(::rtl::math::round(rPoint.X() * nTime)),
96         sal_Int32(::rtl::math::round(rPoint.Y() * nTime)));
97 }
98 
99 
100 
101 
Blend(const double nStartValue,const double nEndValue,const double nTime)102 double AnimationFunction::Blend (
103     const double nStartValue,
104     const double nEndValue,
105     const double nTime)
106 {
107     return nStartValue*(1-nTime) + nEndValue*nTime;
108 }
109 
110 
111 
112 
ApplyVisualStateChange(const model::SharedPageDescriptor & rpDescriptor,view::SlideSorterView & rView,const double nTime)113 void AnimationFunction::ApplyVisualStateChange (
114     const model::SharedPageDescriptor& rpDescriptor,
115     view::SlideSorterView& rView,
116     const double nTime)
117 {
118     if (rpDescriptor)
119     {
120         rpDescriptor->GetVisualState().SetVisualStateBlend(nTime);
121         rView.RequestRepaint(rpDescriptor);
122     }
123 }
124 
125 
126 
127 
ApplyLocationOffsetChange(const model::SharedPageDescriptor & rpDescriptor,view::SlideSorterView & rView,const Point aLocationOffset)128 void AnimationFunction::ApplyLocationOffsetChange (
129     const model::SharedPageDescriptor& rpDescriptor,
130     view::SlideSorterView& rView,
131     const Point aLocationOffset)
132 {
133     if (rpDescriptor)
134     {
135         const Rectangle aOldBoundingBox(rpDescriptor->GetBoundingBox());
136         rpDescriptor->GetVisualState().SetLocationOffset(aLocationOffset);
137         rView.RequestRepaint(aOldBoundingBox);
138         rView.RequestRepaint(rpDescriptor);
139     }
140 }
141 
142 
143 
144 
ApplyButtonAlphaChange(const model::SharedPageDescriptor & rpDescriptor,view::SlideSorterView & rView,const double nButtonAlpha,const double nButtonBarAlpha)145 void AnimationFunction::ApplyButtonAlphaChange(
146     const model::SharedPageDescriptor& rpDescriptor,
147     view::SlideSorterView& rView,
148     const double nButtonAlpha,
149     const double nButtonBarAlpha)
150 {
151     if (rpDescriptor)
152     {
153         rpDescriptor->GetVisualState().SetButtonAlpha(nButtonAlpha);
154         rpDescriptor->GetVisualState().SetButtonBarAlpha(nButtonBarAlpha);
155         rView.RequestRepaint(rpDescriptor);
156     }
157 }
158 
159 
160 
161 
162 //===== AnimationBezierFunction ===============================================
163 
AnimationBezierFunction(const double nX1,const double nY1,const double nX2,const double nY2)164 AnimationBezierFunction::AnimationBezierFunction (
165     const double nX1,
166     const double nY1,
167     const double nX2,
168     const double nY2)
169     : mnX1(nX1),
170       mnY1(nY1),
171       mnX2(nX2),
172       mnY2(nY2)
173 {
174 }
175 
176 
177 
178 
AnimationBezierFunction(const double nX1,const double nY1)179 AnimationBezierFunction::AnimationBezierFunction (
180     const double nX1,
181     const double nY1)
182     : mnX1(nX1),
183       mnY1(nY1),
184       mnX2(1-nY1),
185       mnY2(1-nX1)
186 {
187 }
188 
189 
190 
191 
operator ()(const double nT)192 ::basegfx::B2DPoint AnimationBezierFunction::operator() (const double nT)
193 {
194     return ::basegfx::B2DPoint(
195         EvaluateComponent(nT, mnX1, mnX2),
196         EvaluateComponent(nT, mnY1, mnY2));
197 }
198 
199 
200 
201 
EvaluateComponent(const double nT,const double nV1,const double nV2)202 double AnimationBezierFunction::EvaluateComponent (
203     const double nT,
204     const double nV1,
205     const double nV2)
206 {
207     const double nS (1-nT);
208 
209     // While the control point values 1 and 2 are explicitly given the start
210     // and end values are implicitly given.
211     const double nV0 (0);
212     const double nV3 (1);
213 
214     const double nV01 (nS*nV0 + nT*nV1);
215     const double nV12 (nS*nV1 + nT*nV2);
216     const double nV23 (nS*nV2 + nT*nV3);
217 
218     const double nV012 (nS*nV01 + nT*nV12);
219     const double nV123 (nS*nV12 + nT*nV23);
220 
221     const double nV0123 (nS*nV012 + nT*nV123);
222 
223     return nV0123;
224 }
225 
226 
227 
228 
229 //===== AnimationParametricFunction ===========================================
230 
AnimationParametricFunction(const ParametricFunction & rFunction)231 AnimationParametricFunction::AnimationParametricFunction (const ParametricFunction& rFunction)
232     : maY()
233 {
234     const sal_Int32 nSampleCount (64);
235 
236     // Sample the given parametric function.
237     ::std::vector<basegfx::B2DPoint> aPoints;
238     aPoints.reserve(nSampleCount);
239     for (sal_Int32 nIndex=0; nIndex<nSampleCount; ++nIndex)
240     {
241         const double nT (nIndex/double(nSampleCount-1));
242         aPoints.push_back(basegfx::B2DPoint(rFunction(nT)));
243     }
244 
245     // Interpolate at evenly spaced points.
246     maY.clear();
247     maY.reserve(nSampleCount);
248     double nX0 (aPoints[0].getX());
249     double nY0 (aPoints[0].getY());
250     double nX1 (aPoints[1].getX());
251     double nY1 (aPoints[1].getY());
252     sal_Int32 nIndex (1);
253     for (sal_Int32 nIndex2=0; nIndex2<nSampleCount; ++nIndex2)
254     {
255         const double nX (nIndex2 / double(nSampleCount-1));
256         while (nX > nX1 && nIndex<nSampleCount)
257         {
258             nX0 = nX1;
259             nY0 = nY1;
260             nX1 = aPoints[nIndex].getX();
261             nY1 = aPoints[nIndex].getY();
262             ++nIndex;
263         }
264         const double nU ((nX-nX1) / (nX0 - nX1));
265         const double nY (nY0*nU + nY1*(1-nU));
266         maY.push_back(nY);
267     }
268 }
269 
270 
271 
272 
operator ()(const double nX)273 double AnimationParametricFunction::operator() (const double nX)
274 {
275     const sal_Int32 nIndex0 (static_cast<sal_Int32>(nX * maY.size()));
276     const double nX0 (nIndex0 / double(maY.size()-1));
277     const sal_uInt32 nIndex1 (nIndex0 + 1);
278     const double nX1 (nIndex1 / double(maY.size()-1));
279 
280     if (nIndex0<=0)
281         return maY[0];
282     else if (sal_uInt32(nIndex0)>=maY.size() || nIndex1>=maY.size())
283         return maY[maY.size()-1];
284 
285     const double nU ((nX-nX1) / (nX0 - nX1));
286     return maY[nIndex0]*nU + maY[nIndex1]*(1-nU);
287 }
288 
289 
290 } } } // end of namespace ::sd::slidesorter::controller
291