1 #ifndef ITEM_VIEW_ORIENTATION_CONSTRAINT_H
2 #define ITEM_VIEW_ORIENTATION_CONSTRAINT_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/degree.h>
24 #include <dali/public-api/math/math-utils.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/radian.h>
27 #include <dali/public-api/math/vector3.h>
30 * @brief Constraint used to constrain the orientation of the item-view depending on the position within the layout.
32 class ItemViewOrientationConstraint
37 * @param[in] maximumRotationInDegrees The maximum rotation (in degrees) that we should rotate the item-view by.
38 * @param[in] layoutPositionChangeMultiplier This value is used to multiply the change in layout position
39 * (in order to exaggerate the amount moved so it's more visible).
41 ItemViewOrientationConstraint(float maximumRotationInDegrees, float layoutPositionChangeMultiplier)
42 : mMaximumRotationInDegrees(maximumRotationInDegrees),
43 mLayoutPositionChangeMultiplier(layoutPositionChangeMultiplier),
44 mStartingLayoutPosition(0.0f),
51 * @brief Will be called by the Constraint.
53 * The first time this operator is called, it uses the values as it's base reference.
54 * Thereafter, the position in the layout is used to determine the rotation around the X-Axis.
56 * @param[in] rotation The rotation of the item-view.
57 * @param[in] inputs The constraint inputs:
58 * [0] ItemView::Property::LAYOUT_POSITION, float
60 void operator()(Dali::Quaternion& rotation, const Dali::PropertyInputContainer& inputs)
62 const float& layoutPosition = inputs[0]->GetFloat();
64 // Store values for base reference when called the first call.
67 mStartingLayoutPosition = layoutPosition;
70 Dali::Radian angleInRadians;
71 rotation.ToAxisAngle(axis, angleInRadians);
72 Dali::Degree angleInDegrees(angleInRadians); // Convert to Degrees
74 mStartingAngle = angleInDegrees.degree;
75 if(axis.x < 0.0f) // We only rotate round the X-Axis. So if the X-Axis is negative, then the angle is also a negative angle.
77 mStartingAngle = -mStartingAngle;
84 // All subsequent calls should tilt the orientation of the item-view around the X-Axis depending on how much our position has changed in the layout.
86 Dali::Degree angle(mStartingAngle + mLayoutPositionChangeMultiplier * (mStartingLayoutPosition - layoutPosition));
87 Dali::ClampInPlace(angle.degree, -mMaximumRotationInDegrees, mMaximumRotationInDegrees); // Ensure the angle does not exceed maximum specified (in both directions).
88 rotation = Dali::Quaternion(angle, Dali::Vector3::XAXIS);
93 const float mMaximumRotationInDegrees; ///< The maximum allowable rotation of the item-view.
94 const float mLayoutPositionChangeMultiplier; ///< This value is used to multiply the change in layout position.
95 float mStartingLayoutPosition; ///< The starting layout position.
96 float mStartingAngle; ///< The starting angle (in degrees) of the item-view.
97 bool mFirstCall; ///< A boolean to state whether this is the first time the operator() is called. Allows us to set the starting values.
100 #endif // ITEM_VIEW_ORIENTATION_CONSTRAINT_H