1 #ifndef __DALI_CONSTRAINTS_H__
2 #define __DALI_CONSTRAINTS_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/math/quaternion.h>
25 #include <dali/public-api/math/matrix.h>
26 #include <dali/public-api/math/matrix3.h>
27 #include <dali/public-api/object/property-input.h>
33 * @brief Scale To Fit constraint.
35 * Scales an Actor, such that it fits within it's Parent's Size.
36 * f(current, size, parentSize) = parentSize / size
38 struct ScaleToFitConstraint
43 ScaleToFitConstraint()
47 * @brief Functor operator
49 * @param[in] current The actor's current scale
50 * @param[in] sizeProperty The actor's current size
51 * @param[in] parentSizeProperty The parent's current size.
52 * @return The actor's new scale
54 Vector3 operator()(const Vector3& current,
55 const PropertyInput& sizeProperty,
56 const PropertyInput& parentSizeProperty)
58 const Vector3 size = sizeProperty.GetVector3();
59 const Vector3 parentSize = parentSizeProperty.GetVector3();
61 return Vector3( fabsf(size.x) > Math::MACHINE_EPSILON_1 ? (parentSize.x / size.x) : 0.0f,
62 fabsf(size.y) > Math::MACHINE_EPSILON_1 ? (parentSize.y / size.y) : 0.0f,
63 fabsf(size.z) > Math::MACHINE_EPSILON_1 ? (parentSize.z / size.z) : 0.0f );
68 * @brief Scale To Fit Keep Aspect Ratio constraint.
70 * Scales an Actor, such that it fits within its Parent's Size Keeping the aspect ratio.
71 * f(current, size, parentSize) = Vector3( min( parentSizeX / sizeX, min( parentSizeY / sizeY, parentSizeZ / sizeZ ) )
73 struct ScaleToFitKeepAspectRatioConstraint
78 ScaleToFitKeepAspectRatioConstraint()
82 * @brief Functor operator
84 * @param[in] current The actor's current scale
85 * @param[in] sizeProperty The actor's current size
86 * @param[in] parentSizeProperty The parent's current size.
87 * @return The actor's new scale
89 Vector3 operator()(const Vector3& current,
90 const PropertyInput& sizeProperty,
91 const PropertyInput& parentSizeProperty)
93 return FitKeepAspectRatio( parentSizeProperty.GetVector3(), sizeProperty.GetVector3() );
98 * @brief Scale To Fill XY Keep Aspect Ratio constraint.
100 * Scales an Actor, such that it fill its Parent's Size in the X and Y coordinates Keeping the aspect ratio.
101 * f(current, size, parentSize) = Vector3( max( parentSizeX / sizeX, max( parentSizeY / sizeY, parentSizeZ / sizeZ ) )
103 struct ScaleToFillXYKeepAspectRatioConstraint
106 * @brief Constructor.
108 ScaleToFillXYKeepAspectRatioConstraint()
112 * @param[in] current The actor's current scale
113 * @param[in] sizeProperty The actor's current size
114 * @param[in] parentSizeProperty The parent's current size.
115 * @return The actor's new scale
117 Vector3 operator()(const Vector3& current,
118 const PropertyInput& sizeProperty,
119 const PropertyInput& parentSizeProperty)
121 return FillXYKeepAspectRatio( parentSizeProperty.GetVector3(), sizeProperty.GetVector3() );
126 * @brief EqualToConstraint
128 * f(current, property) = property
130 struct EqualToConstraint
133 * @brief Constructor.
139 * @brief override functor for float properties
141 * @param[in] current The current property value
142 * @param[in] property The property to copy
143 * @return The copy of the input property
145 float operator()(const float current, const PropertyInput& property)
147 return property.GetFloat();
151 * @brief override functor for float properties
153 * @param[in] current The current property value
154 * @param[in] property The property to copy
155 * @return The copy of the input property
157 Vector3 operator()(const Vector3& current, const PropertyInput& property)
159 return property.GetVector3();
163 * @brief override functor for float properties
165 * @param[in] current The current property value
166 * @param[in] property The property to copy
167 * @return The copy of the input property
169 Vector4 operator()(const Vector4& current, const PropertyInput& property)
171 return property.GetVector4();
175 * @brief override functor for float properties
177 * @param[in] current The current property value
178 * @param[in] property The property to copy
179 * @return The copy of the input property
181 Quaternion operator()(const Quaternion& current, const PropertyInput& property)
183 return property.GetQuaternion();
187 * @brief override functor for float properties
189 * @param[in] current The current property value
190 * @param[in] property The property to copy
191 * @return The copy of the input property
193 Matrix3 operator()(const Matrix3& current, const PropertyInput& property)
195 return property.GetMatrix3();
199 * @brief override functor for float properties
201 * @param[in] current The current property value
202 * @param[in] property The property to copy
203 * @return The copy of the input property
205 Matrix operator()(const Matrix& current, const PropertyInput& property)
207 return property.GetMatrix();
213 * @brief RelativeToConstraint for Vector3 properties
215 * f(current, property, scale) = property * scale
217 struct RelativeToConstraint
220 * @brief Constructor.
222 RelativeToConstraint( float scale )
223 : mScale( scale, scale, scale ) { }
226 * @brief Constructor.
228 RelativeToConstraint( const Vector3& scale )
229 : mScale( scale ) { }
234 Vector3 operator()(const Vector3& current, const PropertyInput& property)
236 return property.GetVector3() * mScale;
239 Vector3 mScale; ///< Component-wise scale factor
243 * @brief RelativeToConstraint for float properties
245 struct RelativeToConstraintFloat
248 * @brief Constructor.
250 RelativeToConstraintFloat( float scale )
251 : mScale( scale ) { }
256 float operator()(const float current, const PropertyInput& property)
258 return property.GetFloat() * mScale;
261 float mScale; ///< Scale factor
265 * @brief Constraint which sets width to be another actor's width,
266 * and the height to a fixed height.
268 struct SourceWidthFixedHeight
271 * @brief Constructor.
273 SourceWidthFixedHeight( float height )
274 : mFixedHeight( height ) { }
279 Vector3 operator()(const Vector3& current,
280 const PropertyInput& sourceSize)
282 return Vector3( sourceSize.GetVector3().width, mFixedHeight, current.depth );
285 float mFixedHeight; ///< the fixed height
289 * @brief Constraint function to aim a camera at a target.
291 * Constraint which sets camera's rotation given camera world position
292 * and a target world position. Uses target's up vector to orient the
293 * constrained actor along the vector between camera position and
296 * @param[in] current The current rotation property value
297 * @param[in] targetPosition World position of target
298 * @param[in] cameraPosition World position of camera
299 * @param[in] targetRotation World rotation of the target
300 * @return The orientation of the camera
302 inline Quaternion LookAt( const Quaternion& current,
303 const PropertyInput& targetPosition,
304 const PropertyInput& cameraPosition,
305 const PropertyInput& targetRotation )
307 Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
308 vForward.Normalize();
310 const Quaternion& targetRotationQ = targetRotation.GetQuaternion();
312 Vector3 targetY(targetRotationQ.Rotate(Vector3::YAXIS));
315 // Camera Right vector is perpendicular to forward & target up
316 Vector3 vX = targetY.Cross(vForward);
319 // Camera Up vector is perpendicular to forward and right
320 Vector3 vY = vForward.Cross(vX);
323 return Quaternion( vX, vY, vForward );
328 #endif // __DALI_CONSTRAINTS_H__