xref: /AOO41X/main/sd/source/ui/slidesorter/controller/SlsAnimationFunction.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_sd.hxx"
29 #include "controller/SlsAnimationFunction.hxx"
30 #include "model/SlsPageDescriptor.hxx"
31 #include "view/SlideSorterView.hxx"
32 
33 
34 #include <osl/diagnose.hxx>
35 #include <rtl/math.hxx>
36 
37 namespace sd { namespace slidesorter { namespace controller {
38 
39 
40 double AnimationFunction::Linear (const double nTime)
41 {
42     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
43     return nTime;
44 }
45 
46 
47 
48 
49 double AnimationFunction::FastInSlowOut_Sine (const double nTime)
50 {
51     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
52 
53     const double nResult (sin(nTime * M_PI/2));
54 
55     OSL_ASSERT(nResult>=0.0 && nResult<=1.0);
56     return nResult;
57 }
58 
59 
60 
61 
62 double AnimationFunction::FastInSlowOut_Root (const double nTime)
63 {
64     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
65 
66     const double nResult (sqrt(nTime));
67 
68     OSL_ASSERT(nResult>=0.0 && nResult<=1.0);
69     return nResult;
70 }
71 
72 
73 
74 
75 double AnimationFunction::SlowInSlowOut_0to0_Sine (const double nTime)
76 {
77     OSL_ASSERT(nTime>=0.0 && nTime<=1.0);
78 
79     const double nResult (sin(nTime * M_PI));
80 
81     OSL_ASSERT(nResult>=0.0 && nResult<=1.0);
82     return nResult;
83 }
84 
85 
86 
87 
88 double AnimationFunction::Vibrate_Sine (const double nTime)
89 {
90     return sin(nTime*M_PI*8);
91 }
92 
93 
94 
95 
96 Point AnimationFunction::ScalePoint (const Point& rPoint, const double nTime)
97 {
98     return Point(
99         sal_Int32(::rtl::math::round(rPoint.X() * nTime)),
100         sal_Int32(::rtl::math::round(rPoint.Y() * nTime)));
101 }
102 
103 
104 
105 
106 double AnimationFunction::Blend (
107     const double nStartValue,
108     const double nEndValue,
109     const double nTime)
110 {
111     return nStartValue*(1-nTime) + nEndValue*nTime;
112 }
113 
114 
115 
116 
117 void AnimationFunction::ApplyVisualStateChange (
118     const model::SharedPageDescriptor& rpDescriptor,
119     view::SlideSorterView& rView,
120     const double nTime)
121 {
122     if (rpDescriptor)
123     {
124         rpDescriptor->GetVisualState().SetVisualStateBlend(nTime);
125         rView.RequestRepaint(rpDescriptor);
126     }
127 }
128 
129 
130 
131 
132 void AnimationFunction::ApplyLocationOffsetChange (
133     const model::SharedPageDescriptor& rpDescriptor,
134     view::SlideSorterView& rView,
135     const Point aLocationOffset)
136 {
137     if (rpDescriptor)
138     {
139         const Rectangle aOldBoundingBox(rpDescriptor->GetBoundingBox());
140         rpDescriptor->GetVisualState().SetLocationOffset(aLocationOffset);
141         rView.RequestRepaint(aOldBoundingBox);
142         rView.RequestRepaint(rpDescriptor);
143     }
144 }
145 
146 
147 
148 
149 void AnimationFunction::ApplyButtonAlphaChange(
150     const model::SharedPageDescriptor& rpDescriptor,
151     view::SlideSorterView& rView,
152     const double nButtonAlpha,
153     const double nButtonBarAlpha)
154 {
155     if (rpDescriptor)
156     {
157         rpDescriptor->GetVisualState().SetButtonAlpha(nButtonAlpha);
158         rpDescriptor->GetVisualState().SetButtonBarAlpha(nButtonBarAlpha);
159         rView.RequestRepaint(rpDescriptor);
160     }
161 }
162 
163 
164 
165 
166 //===== AnimationBezierFunction ===============================================
167 
168 AnimationBezierFunction::AnimationBezierFunction (
169     const double nX1,
170     const double nY1,
171     const double nX2,
172     const double nY2)
173     : mnX1(nX1),
174       mnY1(nY1),
175       mnX2(nX2),
176       mnY2(nY2)
177 {
178 }
179 
180 
181 
182 
183 AnimationBezierFunction::AnimationBezierFunction (
184     const double nX1,
185     const double nY1)
186     : mnX1(nX1),
187       mnY1(nY1),
188       mnX2(1-nY1),
189       mnY2(1-nX1)
190 {
191 }
192 
193 
194 
195 
196 ::basegfx::B2DPoint AnimationBezierFunction::operator() (const double nT)
197 {
198     return ::basegfx::B2DPoint(
199         EvaluateComponent(nT, mnX1, mnX2),
200         EvaluateComponent(nT, mnY1, mnY2));
201 }
202 
203 
204 
205 
206 double AnimationBezierFunction::EvaluateComponent (
207     const double nT,
208     const double nV1,
209     const double nV2)
210 {
211     const double nS (1-nT);
212 
213     // While the control point values 1 and 2 are explicitly given the start
214     // and end values are implicitly given.
215     const double nV0 (0);
216     const double nV3 (1);
217 
218     const double nV01 (nS*nV0 + nT*nV1);
219     const double nV12 (nS*nV1 + nT*nV2);
220     const double nV23 (nS*nV2 + nT*nV3);
221 
222     const double nV012 (nS*nV01 + nT*nV12);
223     const double nV123 (nS*nV12 + nT*nV23);
224 
225     const double nV0123 (nS*nV012 + nT*nV123);
226 
227     return nV0123;
228 }
229 
230 
231 
232 
233 //===== AnimationParametricFunction ===========================================
234 
235 AnimationParametricFunction::AnimationParametricFunction (const ParametricFunction& rFunction)
236     : maY()
237 {
238     const sal_Int32 nSampleCount (64);
239 
240     // Sample the given parametric function.
241     ::std::vector<basegfx::B2DPoint> aPoints;
242     aPoints.reserve(nSampleCount);
243     for (sal_Int32 nIndex=0; nIndex<nSampleCount; ++nIndex)
244     {
245         const double nT (nIndex/double(nSampleCount-1));
246         aPoints.push_back(basegfx::B2DPoint(rFunction(nT)));
247     }
248 
249     // Interpolate at evenly spaced points.
250     maY.clear();
251     maY.reserve(nSampleCount);
252     double nX0 (aPoints[0].getX());
253     double nY0 (aPoints[0].getY());
254     double nX1 (aPoints[1].getX());
255     double nY1 (aPoints[1].getY());
256     sal_Int32 nIndex (1);
257     for (sal_Int32 nIndex2=0; nIndex2<nSampleCount; ++nIndex2)
258     {
259         const double nX (nIndex2 / double(nSampleCount-1));
260         while (nX > nX1 && nIndex<nSampleCount)
261         {
262             nX0 = nX1;
263             nY0 = nY1;
264             nX1 = aPoints[nIndex].getX();
265             nY1 = aPoints[nIndex].getY();
266             ++nIndex;
267         }
268         const double nU ((nX-nX1) / (nX0 - nX1));
269         const double nY (nY0*nU + nY1*(1-nU));
270         maY.push_back(nY);
271     }
272 }
273 
274 
275 
276 
277 double AnimationParametricFunction::operator() (const double nX)
278 {
279     const sal_Int32 nIndex0 (nX * maY.size());
280     const double nX0 (nIndex0 / double(maY.size()-1));
281     const sal_uInt32 nIndex1 (nIndex0 + 1);
282     const double nX1 (nIndex1 / double(maY.size()-1));
283 
284     if (nIndex0<=0)
285         return maY[0];
286     else if (sal_uInt32(nIndex0)>=maY.size() || nIndex1>=maY.size())
287         return maY[maY.size()-1];
288 
289     const double nU ((nX-nX1) / (nX0 - nX1));
290     return maY[nIndex0]*nU + maY[nIndex1]*(1-nU);
291 }
292 
293 
294 } } } // end of namespace ::sd::slidesorter::controller
295