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 #ifndef SD_SLIDESORTER_CONTROLLER_ANIMATION_FUNCTION_HXX
25 #define SD_SLIDESORTER_CONTROLLER_ANIMATION_FUNCTION_HXX
26 
27 #include "model/SlsSharedPageDescriptor.hxx"
28 #include <basegfx/point/b2dpoint.hxx>
29 #include <boost/noncopyable.hpp>
30 #include <boost/function.hpp>
31 #include <tools/gen.hxx>
32 #include <vector>
33 
34 namespace sd { namespace slidesorter { namespace view {
35 class SlideSorterView;
36 } } }
37 
38 
39 
40 namespace sd { namespace slidesorter { namespace controller {
41 
42 /** A collection of functions that are useful when creating animations.
43     They are collected here until a better place is found.
44 */
45 class AnimationFunction
46     : private ::boost::noncopyable
47 {
48 public:
49     /** Acceleration function that maps [0,1] to [0,1] linearly, ie it
50         returns the given time value unaltered.
51     */
52     static double Linear (const double nTime);
53 
54     /** Acceleration function that maps [0,1] to [0,1].  Speed starts fast
55         and ends slow following the sine function.
56     */
57     static double FastInSlowOut_Sine (const double nTime);
58 
59     /** Acceleration function that maps [0,1] to [0,1].  Speed starts fast
60         and ends slow following the square root function.
61     */
62     static double FastInSlowOut_Root (const double nTime);
63 
64     /** Acceleration function that maps [0,1] to [0,0].  Speed starts slow,
65         rises, drops and ends slow following the sine function.
66     */
67     static double SlowInSlowOut_0to0_Sine (const double nTime);
68 
69     /** Acceleration function that maps [0,1] to [0,0].  Speed starts slow,
70         rises and drops several times and ends slow following multiple
71         cycles of the sine function.
72     */
73     static double Vibrate_Sine (const double nTime);
74 
75     /** Scale point linearly.
76     */
77     static Point ScalePoint (const Point& rPoint, const double nTime);
78 
79     /** Blend two points together according to the given weight.
80     */
81     static double Blend (const double nStartValue, const double nEndValue, const double nWeight);
82 
83     /** Apply a gradual visual state change.  The kind of change, i.e. the
84         previous and the new states are expected to be already set.  This
85         method only adjusts the blending of the visual representation from
86         one state to the other.
87     */
88     static void ApplyVisualStateChange (
89         const model::SharedPageDescriptor& rpDescriptor,
90         view::SlideSorterView& rView,
91         const double nTime);
92 
93     /** Apply a gradual change of a previously set offset to the location of
94         a page object.
95     */
96     static void ApplyLocationOffsetChange (
97         const model::SharedPageDescriptor& rpDescriptor,
98         view::SlideSorterView& rView,
99         const Point aLocationOffset);
100 
101     /** Apply a gradual change the alpha value from the old value to a
102         new value (set prior to this call.)
103     */
104     static void ApplyButtonAlphaChange(
105         const model::SharedPageDescriptor& rpDescriptor,
106         view::SlideSorterView& rView,
107         const double nButtonAlpha,
108         const double nButtonBarAlpha);
109 };
110 
111 
112 
113 
114 class AnimationBezierFunction
115 {
116 public:
117     /** Create a cubic bezier curve whose start and end points are given
118         implicitly as P0=(0,0) and P3=(1,1).
119     */
120     AnimationBezierFunction (
121         const double nX1,
122         const double nY1,
123         const double nX2,
124         const double nY2);
125 
126     /** Create a cubic bezier curve whose start and end points are given
127         implicitly as P0=(0,0) and P3=(1,1).  The second control point is
128         implicitly given as P2=(1-nY1,1-nX1).
129     */
130     AnimationBezierFunction (
131         const double nX1,
132         const double nY1);
133 
134     ::basegfx::B2DPoint operator() (const double nT);
135 
136 private:
137     const double mnX1;
138     const double mnY1;
139     const double mnX2;
140     const double mnY2;
141 
142     double EvaluateComponent (
143         const double nT,
144         const double nV1,
145         const double nV2);
146 };
147 
148 
149 
150 
151 /** Turn a parametric function into one whose y-Values depend on its
152     x-Values.  Note a lot of interpolation takes place.  The resulting
153     accuracy should be good enough for the purpose of acceleration
154     function for animations.
155 */
156 class AnimationParametricFunction
157 {
158 public:
159     typedef ::boost::function<basegfx::B2DPoint(double)> ParametricFunction;
160     AnimationParametricFunction (const ParametricFunction& rFunction);
161 
162     double operator() (const double nX);
163 
164 private:
165     /** y-Values of the parametric function given to the constructor
166         evaluated (and interpolated) for evenly spaced x-Values.
167     */
168     ::std::vector<double> maY;
169 };
170 
171 
172 
173 
174 } } } // end of namespace ::sd::slidesorter::controller
175 
176 #endif
177