1 #ifndef DALI_INTERNAL_PATH_CONSTRAINER_H
2 #define DALI_INTERNAL_PATH_CONSTRAINER_H
5 * Copyright (c) 2021 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/devel-api/animation/path-constrainer.h>
23 #include <dali/internal/event/animation/constrainer.h>
24 #include <dali/internal/event/animation/path-impl.h>
25 #include <dali/public-api/math/math-utils.h>
31 typedef IntrusivePtr<PathConstrainer> PathConstrainerPtr;
34 * @brief Constraint functor to constraint properties to paths.
36 * Vector3 properties will be constrained to the position of the path and
37 * Rotation properties will be constrained to follow the tangent of the path
38 * given a forward vector in object's local space.
40 struct PathConstraintFunctor
45 * @param[in] path The path used in the constraint
46 * @param[in] range The range of values in the input property which will be mapped to [0,1]
47 * @param[in] wrap Wrapping domain. Input property value will be wrapped in the domain [wrap.x,wrap.y] before mapping to [0,1]
49 PathConstraintFunctor(PathPtr path, const Vector2& range, const Vector2& wrap)
59 * @param[in] path The path used in the constraint
60 * @param[in] range The range of values in the input property which will be mapped to 0..1
61 * @param[in] forward Vector in object space which will be aligned with the tangent of the path
62 * @param[in] wrap Wrapping domain. Input property value will be wrapped in the domain [wrap.x,wrap.y] before mapping to [0,1]
64 PathConstraintFunctor(PathPtr path, const Vector2& range, const Vector3& forward, const Vector2& wrap)
73 * @brief Functor operator for Vector3 properties
75 * @param[in,out] position Current value of the property
76 * @param[in] inputs Contains the input property used as the parameter for the path
78 * @return The position of the path at the given parameter.
80 void operator()(Vector3& position,
81 const PropertyInputContainer& inputs)
83 float inputWrapped = inputs[0]->GetFloat();
84 if(inputWrapped < mWrap.x || inputWrapped > mWrap.y)
86 inputWrapped = WrapInDomain(inputWrapped, mWrap.x, mWrap.y);
89 float t = (inputWrapped - mRange.x) / (mRange.y - mRange.x);
92 mPath->Sample(t, position, tangent);
96 * @brief Functor operator for Quaternion properties
98 * @param[in,out] current Current value of the property
99 * @param[in] inputs Contains the input property used as the parameter for the path
101 * @return The rotation which will align the forward vector and the tangent of the path at the given parameter.
103 void operator()(Quaternion& current,
104 const PropertyInputContainer& inputs)
106 float inputWrapped = inputs[0]->GetFloat();
107 if(inputWrapped < mWrap.x || inputWrapped > mWrap.y)
109 inputWrapped = WrapInDomain(inputWrapped, mWrap.x, mWrap.y);
112 float t = (inputWrapped - mRange.x) / (mRange.y - mRange.x);
114 Vector3 position, tangent;
115 mPath->Sample(t, position, tangent);
116 current = Quaternion(mForward, tangent);
119 PathPtr mPath; ///< The path used
120 Vector3 mForward; ///< Vector in object space which will be aligned with the tangent of the path
121 Vector2 mRange; ///< The range of values in the input property which will be mapped to 0..1
122 Vector2 mWrap; ///< Wrapping domain. Input property will be wrapped in this domain before being mapped to [0,1]
126 * @brief A PathConstrainer used to constraint properties to a path
128 class PathConstrainer : public Constrainer
132 * Create a new PathConstrainer
133 * @return A smart-pointer to the newly allocated PathConstrainer.
135 static PathConstrainer* New();
141 ~PathConstrainer() override;
145 * @copydoc Dali::Internal::Object::SetDefaultProperty()
147 void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue) override;
150 * @copydoc Dali::Internal::Object::GetDefaultProperty()
152 Property::Value GetDefaultProperty(Property::Index index) const override;
155 * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
157 Property::Value GetDefaultPropertyCurrentValue(Property::Index index) const override;
161 * @copydoc Dali::PathConstrainer::Apply
163 void Apply(Property target, Property source, const Vector2& range, const Vector2& wrap) override;
170 PathConstrainer(const PathConstrainer&);
173 PathConstrainer& operator=(const PathConstrainer& rhs);
175 PathPtr mPath; ///< The path used in the constraints
176 Vector3 mForward; ///< Vector in object space which will be aligned with the tangent of the path
179 } // namespace Internal
181 // Get impl of handle
182 inline Internal::PathConstrainer& GetImplementation(Dali::PathConstrainer& pathConstrainer)
184 DALI_ASSERT_ALWAYS(pathConstrainer && "PathConstrainer handle is empty");
185 Dali::RefObject& object = pathConstrainer.GetBaseObject();
186 return static_cast<Internal::PathConstrainer&>(object);
189 inline const Internal::PathConstrainer& GetImplementation(const Dali::PathConstrainer& pathConstrainer)
191 DALI_ASSERT_ALWAYS(pathConstrainer && "PathConstrainer handle is empty");
192 const Dali::RefObject& object = pathConstrainer.GetBaseObject();
193 return static_cast<const Internal::PathConstrainer&>(object);
198 #endif // DALI_INTERNAL_PATH_CONSTRAINER_H