Added Animation GetState()
[platform/core/uifw/dali-core.git] / dali / public-api / animation / constraints.h
1 #ifndef __DALI_CONSTRAINTS_H__
2 #define __DALI_CONSTRAINTS_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/animation/constraint.h>
23 #include <dali/public-api/math/vector3.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/matrix.h>
27 #include <dali/public-api/math/matrix3.h>
28 #include <dali/public-api/object/property-input.h>
29
30 namespace Dali
31 {
32 /**
33  * @addtogroup dali_core_animation
34  * @{
35  */
36
37 /**
38  * @brief EqualToConstraint
39  *
40  * f(current, property) = property
41  * @SINCE_1_0.0
42  */
43 struct EqualToConstraint
44 {
45   /**
46    * @brief Constructor.
47    * @SINCE_1_0.0
48    */
49   EqualToConstraint()
50   { }
51
52   /**
53    * @brief override functor for float properties
54    *
55    * @SINCE_1_0.0
56    * @param[in, out] current The current property value, the constrained value is set
57    * @param[in] inputs Contains the property to copy
58    * @return The copy of the input property
59    */
60   void operator()( float& current, const PropertyInputContainer& inputs )
61   {
62     current = inputs[0]->GetFloat();
63   }
64
65   /**
66    * @brief override functor for float properties
67    *
68    * @SINCE_1_0.0
69    * @param[in] current The current property value, the constrained value is set
70    * @param[in] inputs Contains the property to copy
71    * @return The copy of the input property
72    */
73   void operator()( Vector2& current, const PropertyInputContainer& inputs )
74   {
75     current = inputs[0]->GetVector2();
76   }
77
78   /**
79    * @brief override functor for float properties
80    *
81    * @SINCE_1_0.0
82    * @param[in,out] current The current property value, the constrained value is set
83    * @param[in] inputs Contains the property to copy
84    * @return The copy of the input property
85    */
86   void operator()( Vector3& current, const PropertyInputContainer& inputs )
87   {
88     current = inputs[0]->GetVector3();
89   }
90
91   /**
92    * @brief override functor for float properties
93    *
94    * @SINCE_1_0.0
95    * @param[in,out] current The current property value, the constrained value is set
96    * @param[in] inputs Contains the property to copy
97    * @return The copy of the input property
98    */
99   void operator()( Vector4& current, const PropertyInputContainer& inputs )
100   {
101     current = inputs[0]->GetVector4();
102   }
103
104   /**
105    * @brief override functor for float properties
106    *
107    * @SINCE_1_0.0
108    * @param[in,out] current The current property value, the constrained value is set
109    * @param[in] inputs Contains the property to copy
110    * @return The copy of the input property
111    */
112   void operator()( Quaternion& current, const PropertyInputContainer& inputs )
113   {
114     current = inputs[0]->GetQuaternion();
115   }
116
117   /**
118    * @brief override functor for float properties
119    *
120    * @SINCE_1_0.0
121    * @param[in,out] current The current property value
122    * @param[in] inputs Contains the property to copy
123    * @return The copy of the input property
124    */
125   void operator()( Matrix3& current, const PropertyInputContainer& inputs )
126   {
127     current = inputs[0]->GetMatrix3();
128   }
129
130   /**
131    * @brief override functor for float properties
132    *
133    * @SINCE_1_0.0
134    * @param[in,out] current The current property value, the constrained value is set
135    * @param[in] inputs Contains the property to copy
136    * @return The copy of the input property
137    */
138   void operator()( Matrix& current, const PropertyInputContainer& inputs )
139   {
140     current = inputs[0]->GetMatrix();
141   }
142
143 };
144
145 /**
146  * @brief RelativeToConstraint for Vector3 properties
147  *
148  * current = property * scale
149  * @SINCE_1_0.0
150  */
151 struct RelativeToConstraint
152 {
153   /**
154    * @brief Constructor.
155    * @SINCE_1_0.0
156    */
157   RelativeToConstraint( float scale )
158   : mScale( scale, scale, scale ) { }
159
160   /**
161    * @brief Constructor.
162    * @SINCE_1_0.0
163    */
164   RelativeToConstraint( const Vector3& scale )
165   : mScale( scale ) { }
166
167   /**
168    * @brief Functor.
169    * @SINCE_1_0.0
170    */
171   void operator()( Vector3& current, const PropertyInputContainer& inputs )
172   {
173     current = inputs[0]->GetVector3() * mScale;
174   }
175
176   Vector3 mScale; ///< Component-wise scale factor
177 };
178
179 /**
180  * @brief RelativeToConstraint for float properties
181  * @SINCE_1_0.0
182  */
183 struct RelativeToConstraintFloat
184 {
185   /**
186    * @brief Constructor.
187    * @SINCE_1_0.0
188    */
189   RelativeToConstraintFloat( float scale )
190   : mScale( scale ) { }
191
192   /**
193    * @brief Functor.
194    * @SINCE_1_0.0
195    */
196   void operator()( float& current, const PropertyInputContainer& inputs )
197   {
198     current = inputs[0]->GetFloat() * mScale;
199   }
200
201   float mScale; ///< Scale factor
202 };
203
204 /**
205  * @brief Constraint function to aim a camera at a target.
206  *
207  * Constraint which sets camera's orientation given camera world position
208  * and a target world position.  Uses target's up vector to orient the
209  * constrained actor along the vector between camera position and
210  * target position.
211  *
212  * @SINCE_1_0.0
213  * @param[in,out] current The current orientation property value, the constrained value is set.
214  * @param[in] inputs Contains the World position of the target, the World position of the camera, and the world orientation of the target
215  * @return The orientation of the camera
216  */
217 inline void LookAt( Quaternion& current, const PropertyInputContainer& inputs )
218 {
219   const PropertyInput& targetPosition( *inputs[0] );
220   const PropertyInput& cameraPosition( *inputs[1] );
221   const PropertyInput& targetOrientation( *inputs[2] );
222
223   Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
224   vForward.Normalize();
225
226   const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
227
228   Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
229   targetY.Normalize();
230
231   // Camera Right vector is perpendicular to forward & target up
232   Vector3 vX = targetY.Cross(vForward);
233   vX.Normalize();
234
235   // Camera Up vector is perpendicular to forward and right
236   Vector3 vY = vForward.Cross(vX);
237   vY.Normalize();
238
239   current = Quaternion( vX, vY, vForward );
240 }
241
242 /**
243  * @}
244  */
245 } // namespace Dali
246
247 #endif // __DALI_CONSTRAINTS_H__