AlphaFunction refactoring
[platform/core/uifw/dali-core.git] / dali / public-api / animation / alpha-function.h
1 #ifndef __DALI_ALPHA_FUNCTION_H__
2 #define __DALI_ALPHA_FUNCTION_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * 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, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/constants.h>
24 #include <dali/public-api/math/compile-time-math.h>
25 #include <dali/public-api/math/math-utils.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector4.h>
28
29 namespace Dali
30 {
31
32 typedef float (*AlphaFunctionPrototype)(float progress); ///< Prototype of an alpha function
33
34   /*
35    * @brief Alpha functions are used in animations to specify the rate of change of the animation parameter over time.
36    * Understanding an animation as a parametric function over time, the alpha function is applied to the parameter of
37    * the animation before computing the final animation value.
38    */
39 class DALI_IMPORT_API AlphaFunction
40 {
41 public:
42
43   /**
44    * Built-in alpha functions
45    */
46   enum BuiltinFunction
47   {
48     DEFAULT,            ///< Linear
49     LINEAR,             ///< No transformation
50     REVERSE,            ///< Reverse linear
51
52     EASE_IN_SQUARE,     ///< Speeds up and comes to a sudden stop (Square)
53     EASE_OUT_SQUARE,    ///< Sudden start and slows to a gradual stop (Square)
54
55     EASE_IN,            ///< Speeds up and comes to a sudden stop (Cubic)
56     EASE_OUT,           ///< Sudden start and slows to a gradual stop (Cubic)
57     EASE_IN_OUT,        ///< Speeds up and slows to a gradual stop (Cubic)
58
59     EASE_IN_SINE,       ///< Speeds up and comes to a sudden stop (sinusoidal)
60     EASE_OUT_SINE,      ///< Sudden start and slows to a gradual stop (sinusoidal)
61     EASE_IN_OUT_SINE,   ///< Speeds up and slows to a gradual stop (sinusoidal)
62
63     BOUNCE,             ///< Sudden start, loses momentum and returns to start position
64     SIN,                ///< Single revolution
65     EASE_OUT_BACK,      ///< Sudden start, exceed end position and return to a gradual stop
66
67     COUNT
68   };
69
70   /**
71    * All possible functioning modes for the alpha function
72    */
73   enum Mode
74   {
75     BUILTIN_FUNCTION,  //< The user has specified a built-in function
76     CUSTOM_FUNCTION,   //< The user has provided a custom function
77     BEZIER             //< The user has provided the control points of a bezier curve
78   };
79
80   /**
81    * @brief Default constructor.
82    * Creates an alpha function object with the default built-in alpha function
83    * @return The alpha function
84    */
85   AlphaFunction();
86
87   /**
88    * @brief Constructor.
89    * Creates an alpha function object with the built-in alpha function passed as a parameter
90    * to the constructor
91    * @param[in] function One of the built-in alpha functions
92    * @return The alpha function
93    */
94   AlphaFunction( BuiltinFunction function);
95
96   /**
97    * @brief Constructor.
98    * Creates an alpha function object using a pointer to an alpha function passed as a paramter
99    * to the constructor
100    * @param[in] function A pointer to an alpha function
101    * @return The alpha function
102    */
103   AlphaFunction( AlphaFunctionPrototype function);
104
105   /**
106    * @brief Constructor.
107    * Creates a bezier alpha function. The bezier will have the first point at (0,0) and
108    * the end point at (1,1).
109    * @note The x components of the control points will be clamped to the range [0,1] to prevent
110    * non monotonic curves.
111    * @param[in] controlPoint0 A Vector2 which will be used as the first control point of the curve
112    * @param[in] controlPoint1 A Vector2 which will be used as the second control point of the curve
113    * @return The alpha function
114    */
115   AlphaFunction( const Dali::Vector2& controlPoint0, const Dali::Vector2& controlPoint1 );
116
117   /**
118    * @brief Return the control points of the alpha function
119    * @return Vector4 containing the two control points of the curve.
120    * (xy for the first point and zw for the second)
121    */
122   Vector4 GetBezierControlPoints() const;
123
124   /**
125    * @brief Returns the pointer to the custom function
126    * @return A pointer to a custom alpha function or 0 if not defined
127    */
128   AlphaFunctionPrototype GetCustomFunction() const;
129
130   /**
131    * @brief Returns the built0in function used by the alpha function
132    * @return One of the built-in alpha functions. In case no built-in function
133    * has been specified, it will return AlphaFunction::DEfAULT
134    */
135   BuiltinFunction GetBuiltinFunction() const;
136
137   /**
138    * @brief Returns the functioning mode of the alpha function
139    * @return The functioning mode of the alpha function
140    */
141   Mode GetMode() const;
142
143 private:
144
145   Vector4                 mBezierControlPoints;   //< Control points for the bezier alpha function
146   AlphaFunctionPrototype  mCustom;                //< Pointer to an alpha function
147   BuiltinFunction         mBuiltin : Log<COUNT>::value+1; //< Enum indicating the built-in alpha function
148   Mode                    mMode    : 2;                   //< Enum indicating the functioning mode of the AlphaFunction
149 };
150
151 } // namespace Dali
152
153 #endif // __DALI_ALPHA_FUNCTION_H__