[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / transition-effects / cube-transition-fold-effect-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "cube-transition-fold-effect-impl.h"
20
21 namespace Dali
22 {
23 namespace Toolkit
24 {
25 namespace Internal
26 {
27 CubeTransitionFoldEffect::CubeTransitionFoldEffect(unsigned int numRows, unsigned int numColumns)
28 : CubeTransitionEffect(numRows, numColumns)
29 {
30 }
31
32 Toolkit::CubeTransitionFoldEffect CubeTransitionFoldEffect::New(unsigned int numRows, unsigned int numColumns)
33 {
34   // Create the implementation
35   IntrusivePtr<CubeTransitionFoldEffect> internalCubeTransEffect = new CubeTransitionFoldEffect(numRows, numColumns);
36
37   // Pass ownership to CustomActor handle
38   Toolkit::CubeTransitionFoldEffect cubeTransEffect(*internalCubeTransEffect);
39
40   //Initialization
41   internalCubeTransEffect->Initialize();
42
43   return cubeTransEffect;
44 }
45
46 void CubeTransitionFoldEffect::OnInitialize()
47 {
48   unsigned int idx;
49   for(unsigned int y = 0; y < mRows; y++)
50   {
51     idx = y * mColumns;
52     for(unsigned int x = y % 2; x < mColumns; x = x + 2)
53     {
54       SetTargetLeft(idx + x);
55     }
56     for(unsigned int x = (y + 1) % 2; x < mColumns; x = x + 2)
57     {
58       SetTargetRight(idx + x);
59     }
60   }
61 }
62
63 void CubeTransitionFoldEffect::OnStartTransition(Vector2 panPosition, Vector2 panDisplacement)
64 {
65   float angle = Math::PI_2;
66
67   unsigned int idx;
68   if(panDisplacement.x < 0)
69   {
70     for(unsigned int y = 0; y < mRows; y++)
71     {
72       idx = y * mColumns;
73       for(unsigned int x = y % 2; x < mColumns; x = x + 2)
74       {
75         SetTargetLeft(idx + x);
76       }
77       for(unsigned int x = (y + 1) % 2; x < mColumns; x = x + 2)
78       {
79         SetTargetRight(idx + x);
80       }
81     }
82   }
83   else
84   {
85     angle = -angle;
86
87     for(unsigned int y = 0; y < mRows; y++)
88     {
89       idx = y * mColumns;
90       for(unsigned int x = y % 2; x < mColumns; x = x + 2)
91       {
92         SetTargetRight(idx + x);
93       }
94       for(unsigned int x = (y + 1) % 2; x < mColumns; x = x + 2)
95       {
96         SetTargetLeft(idx + x);
97       }
98     }
99   }
100
101   for(unsigned int y = 0; y < mRows; y++)
102   {
103     idx = y * mColumns;
104     for(unsigned int x = y % 2; x < mColumns; x = x + 2)
105     {
106       SetupAnimation(idx + x, x, angle);
107     }
108     for(unsigned int x = (y + 1) % 2; x < mColumns; x = x + 2)
109     {
110       SetupAnimation(idx + x, x, -angle);
111     }
112   }
113
114   mAnimation.Play();
115   mIsAnimating = true;
116 }
117
118 void CubeTransitionFoldEffect::SetupAnimation(unsigned int actorIndex, unsigned int x, float angle)
119 {
120   //rotate and translate the cube such that the edges remain in constant contact
121   //calculate the maximum distance the cube has to move when it the box has rotated 45 degrees
122   //ie distance from of centre of square to a vertex is given by:
123   //  distance = width / sqrt(2)
124   //therefore the delta distance the cube should move is given by:
125   //  delta_distance = ( width / 2 ) - distance
126   //re-arranging we get:
127   //  delta_distance = ( width / 2 ) * ( sqrt(2) - 1 )
128   //accumulating over the length of the row we get:
129   //  delta_distance_at_x = x * delta_distance;
130
131   float delta = (float)x * mTileSize.x * (1.4142f - 1.0f);
132
133   Vector3 position(mBoxes[actorIndex].GetCurrentProperty<Vector3>(Actor::Property::POSITION));
134   mAnimation.AnimateTo(Property(mBoxes[actorIndex], Actor::Property::ORIENTATION), Quaternion(Radian(angle), Vector3::YAXIS), AlphaFunction::LINEAR);
135   mAnimation.AnimateTo(Property(mBoxes[actorIndex], Actor::Property::POSITION_X), position.x + delta, AlphaFunction::BOUNCE);
136
137   mAnimation.AnimateTo(Property(mCurrentTiles[actorIndex], Actor::Property::COLOR), HALF_BRIGHTNESS, AlphaFunction::EASE_OUT);
138   mAnimation.AnimateTo(Property(mTargetTiles[actorIndex], Actor::Property::COLOR), FULL_BRIGHTNESS, AlphaFunction::EASE_IN);
139 }
140
141 } // namespace Internal
142
143 } // namespace Toolkit
144
145 } // namespace Dali