(Clipping Example) Add a rotation effect around the X-axis as we scroll through the...
[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) 2017 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   /**
37    * @brief Constructor.
38    * @param[in] maximumRotationInDegrees       The maximum rotation (in degrees) that we should rotate the item-view by.
39    * @param[in] layoutPositionChangeMultiplier This value is used to multiply the change in layout position
40    *                                           (in order to exaggerate the amount moved so it's more visible).
41    */
42   ItemViewOrientationConstraint( float maximumRotationInDegrees, float layoutPositionChangeMultiplier )
43   : mMaximumRotationInDegrees( maximumRotationInDegrees ),
44     mLayoutPositionChangeMultiplier( layoutPositionChangeMultiplier ),
45     mStartingLayoutPosition( 0.0f ),
46     mStartingAngle( 0.0f ),
47     mFirstCall( true )
48   {
49   }
50
51   /**
52    * @brief Will be called by the Constraint.
53    *
54    * The first time this operator is called, it uses the values as it's base reference.
55    * Thereafter, the position in the layout is used to determine the rotation around the X-Axis.
56    *
57    * @param[in] rotation The rotation of the item-view.
58    * @param[in] inputs   The constraint inputs:
59    *                     [0] ItemView::Property::LAYOUT_POSITION, float
60    */
61   void operator()( Dali::Quaternion& rotation, const Dali::PropertyInputContainer& inputs )
62   {
63     const float& layoutPosition = inputs[ 0 ]->GetFloat();
64
65     // Store values for base reference when called the first call.
66     if( mFirstCall )
67     {
68       mStartingLayoutPosition = layoutPosition;
69
70       Dali::Vector3 axis;
71       Dali::Radian angleInRadians;
72       rotation.ToAxisAngle( axis, angleInRadians );
73       Dali::Degree angleInDegrees( angleInRadians ); // Convert to Degrees
74
75       mStartingAngle = angleInDegrees.degree;
76       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       {
78         mStartingAngle = -mStartingAngle;
79       }
80
81       mFirstCall = false;
82     }
83     else
84     {
85       // 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
87       Dali::Degree angle( mStartingAngle + mLayoutPositionChangeMultiplier * ( mStartingLayoutPosition - layoutPosition ) );
88       Dali::ClampInPlace( angle.degree, -mMaximumRotationInDegrees, mMaximumRotationInDegrees ); // Ensure the angle does not exceed maximum specified (in both directions).
89       rotation = Dali::Quaternion( angle, Dali::Vector3::XAXIS );
90     }
91   }
92
93 private:
94
95   const float mMaximumRotationInDegrees; ///< The maximum allowable rotation of the item-view.
96   const float mLayoutPositionChangeMultiplier; ///< This value is used to multiply the change in layout position.
97   float mStartingLayoutPosition; ///< The starting layout position.
98   float mStartingAngle; ///< The starting angle (in degrees) of the item-view.
99   bool mFirstCall; ///< A boolean to state whether this is the first time the operator() is called. Allows us to set the starting values.
100 };
101
102 #endif // ITEM_VIEW_ORIENTATION_CONSTRAINT_H