Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / clipping / item-view-orientation-constraint.h
1 #ifndef ITEM_VIEW_ORIENTATION_CONSTRAINT_H
2 #define ITEM_VIEW_ORIENTATION_CONSTRAINT_H
3
4 /*
5  * Copyright (c) 2020 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 // EXTERNAL INCLUDES
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>
28
29 /**
30  * @brief Constraint used to constrain the orientation of the item-view depending on the position within the layout.
31  */
32 class ItemViewOrientationConstraint
33 {
34 public:
35   /**
36    * @brief Constructor.
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).
40    */
41   ItemViewOrientationConstraint(float maximumRotationInDegrees, float layoutPositionChangeMultiplier)
42   : mMaximumRotationInDegrees(maximumRotationInDegrees),
43     mLayoutPositionChangeMultiplier(layoutPositionChangeMultiplier),
44     mStartingLayoutPosition(0.0f),
45     mStartingAngle(0.0f),
46     mFirstCall(true)
47   {
48   }
49
50   /**
51    * @brief Will be called by the Constraint.
52    *
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.
55    *
56    * @param[in] rotation The rotation of the item-view.
57    * @param[in] inputs   The constraint inputs:
58    *                     [0] ItemView::Property::LAYOUT_POSITION, float
59    */
60   void operator()(Dali::Quaternion& rotation, const Dali::PropertyInputContainer& inputs)
61   {
62     const float& layoutPosition = inputs[0]->GetFloat();
63
64     // Store values for base reference when called the first call.
65     if(mFirstCall)
66     {
67       mStartingLayoutPosition = layoutPosition;
68
69       Dali::Vector3 axis;
70       Dali::Radian  angleInRadians;
71       rotation.ToAxisAngle(axis, angleInRadians);
72       Dali::Degree angleInDegrees(angleInRadians); // Convert to Degrees
73
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.
76       {
77         mStartingAngle = -mStartingAngle;
78       }
79
80       mFirstCall = false;
81     }
82     else
83     {
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.
85
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);
89     }
90   }
91
92 private:
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.
98 };
99
100 #endif // ITEM_VIEW_ORIENTATION_CONSTRAINT_H