1 #ifndef DALI_CONSTRAINTS_H
2 #define DALI_CONSTRAINTS_H
5 * Copyright (c) 2020 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/animation/constraint.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/matrix3.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/vector3.h>
27 #include <dali/public-api/math/vector4.h>
28 #include <dali/public-api/object/property-input.h>
33 * @addtogroup dali_core_animation
38 * @brief The constraint function that updates the target property with the value of the first source.
40 * @e current = <em>input[0]</em>. @e current and <em>input[0]</em> indicate the target property
41 * and the first constraint source (the one added by the first Constraint::AddSource call), respectively.
44 struct EqualToConstraint
50 EqualToConstraint() = default;
53 * @brief Overrides functor for float properties.
56 * @param[in,out] current The current property value, the constrained value is set
57 * @param[in] inputs Contains the property to copy
59 void operator()(float& current, const PropertyInputContainer& inputs)
61 current = inputs[0]->GetFloat();
65 * @brief Overrides functor for Vector2 properties.
68 * @param[in,out] current The current property value, the constrained value is set
69 * @param[in] inputs Contains the property to copy
71 void operator()(Vector2& current, const PropertyInputContainer& inputs)
73 current = inputs[0]->GetVector2();
77 * @brief Overrides functor for Vector3 properties.
80 * @param[in,out] current The current property value, the constrained value is set
81 * @param[in] inputs Contains the property to copy
83 void operator()(Vector3& current, const PropertyInputContainer& inputs)
85 current = inputs[0]->GetVector3();
89 * @brief Overrides functor for Vector4 properties.
92 * @param[in,out] current The current property value, the constrained value is set
93 * @param[in] inputs Contains the property to copy
95 void operator()(Vector4& current, const PropertyInputContainer& inputs)
97 current = inputs[0]->GetVector4();
101 * @brief Overrides functor for Quaternion properties.
104 * @param[in,out] current The current property value, the constrained value is set
105 * @param[in] inputs Contains the property to copy
107 void operator()(Quaternion& current, const PropertyInputContainer& inputs)
109 current = inputs[0]->GetQuaternion();
113 * @brief Overrides functor for Matrix3 properties.
116 * @param[in,out] current The current property value
117 * @param[in] inputs Contains the property to copy
119 void operator()(Matrix3& current, const PropertyInputContainer& inputs)
121 current = inputs[0]->GetMatrix3();
125 * @brief Overrides functor for Matrix properties.
128 * @param[in,out] current The current property value, the constrained value is set
129 * @param[in] inputs Contains the property to copy
131 void operator()(Matrix& current, const PropertyInputContainer& inputs)
133 current = inputs[0]->GetMatrix();
138 * @brief The constraint function that updates the target property with the value of the first source
139 * multiplied by scale parameter (for Vector3 properties).
141 * @e current = <em>input[0]</em> * @e scale. @e current, <em>input[0]</em>, and @e scale
142 * indicates the target property, the first constraint source, and the scale parameter, respectively.
143 * * implies element-wise multiplication.
146 struct RelativeToConstraint
149 * @brief Constructor.
151 * @param[in] scale Scale factor
153 RelativeToConstraint(float scale)
154 : mScale(scale, scale, scale)
159 * @brief Constructor.
161 * @param[in] scale Scale factor
163 RelativeToConstraint(const Vector3& scale)
171 * @param[in,out] current The current property value (vector3 property * scale factor)
172 * @param[in] inputs Property container for current property calculation
174 void operator()(Vector3& current, const PropertyInputContainer& inputs)
176 current = inputs[0]->GetVector3() * mScale;
179 Vector3 mScale; ///< Component-wise scale factor
183 * @brief The constraint function that updates the target property with the value of the first source
184 * multiplied by scale parameter (for float properties).
186 * @e current = <em>input[0]</em> * @e scale. @e current, <em>input[0]</em>, and @e scale
187 * indicates the target property, the first constraint source, and the scale parameter, respectively.
190 struct RelativeToConstraintFloat
193 * @brief Constructor.
195 * @param[in] scale Scale factor
197 RelativeToConstraintFloat(float scale)
205 * @param[in,out] current The current property value (float property * scale factor)
206 * @param[in] inputs Property container for current property calculation
208 void operator()(float& current, const PropertyInputContainer& inputs)
210 current = inputs[0]->GetFloat() * mScale;
213 float mScale; ///< Scale factor
217 * @brief Constraint function to aim a camera at a target.
219 * Constraint which sets camera's orientation given camera world position
220 * and a target world position. Uses target's up vector to orient the
221 * constrained actor along the vector between camera position and
225 * @param[in,out] current The current orientation property value, the constrained value is set
226 * @param[in] inputs Contains the world position of the target, the world position of the camera, and the world orientation of the target
228 inline void LookAt(Dali::Quaternion& current, const Dali::PropertyInputContainer& inputs)
230 const PropertyInput& targetPosition(*inputs[0]);
231 const PropertyInput& cameraPosition(*inputs[1]);
232 const PropertyInput& targetOrientation(*inputs[2]);
234 Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
235 vForward.Normalize();
237 const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
239 Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
242 // Camera Right vector is perpendicular to forward & target up
243 Vector3 vX = targetY.Cross(vForward);
246 // Camera Up vector is perpendicular to forward and right
247 Vector3 vY = vForward.Cross(vX);
250 current = Quaternion(vX, vY, vForward);
258 #endif // DALI_CONSTRAINTS_H