DALi Version 2.1.5
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / transition-effects / cube-transition-cross-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-cross-effect-impl.h"
20
21 namespace Dali
22 {
23 namespace Toolkit
24 {
25 namespace Internal
26 {
27 CubeTransitionCrossEffect::CubeTransitionCrossEffect(unsigned int numRows, unsigned int numColumns)
28 : CubeTransitionEffect(numRows, numColumns),
29   mDisplacementSpreadFactor(0.008f)
30 {
31 }
32
33 Toolkit::CubeTransitionCrossEffect CubeTransitionCrossEffect::New(unsigned int numRows, unsigned int numColumns)
34 {
35   // Create the implementation
36   IntrusivePtr<CubeTransitionCrossEffect> internalCubeTransEffect = new CubeTransitionCrossEffect(numRows, numColumns);
37
38   // Pass ownership to CustomActor handle
39   Toolkit::CubeTransitionCrossEffect cubeTransEffect(*internalCubeTransEffect);
40
41   //Initialization
42   internalCubeTransEffect->Initialize();
43
44   return cubeTransEffect;
45 }
46
47 void CubeTransitionCrossEffect::OnInitialize()
48 {
49   unsigned int idx;
50   for(unsigned int y = 0; y < mRows; y++)
51   {
52     for(unsigned int x = y % 2; x < mColumns; x += 2)
53     {
54       idx = y * mColumns + x;
55       SetTargetTop(idx);
56     }
57     for(unsigned int x = (y + 1) % 2; x < mColumns; x += 2)
58     {
59       idx = y * mColumns + x;
60       SetTargetRight(idx);
61     }
62   }
63 }
64
65 void CubeTransitionCrossEffect::OnStartTransition(Vector2 panPosition, Vector2 panDisplacement)
66 {
67   float        angle = Math::PI_2;
68   unsigned int idx;
69
70   if(panDisplacement.x < 0)
71   {
72     for(unsigned int y = 0; y < mRows; y++)
73     {
74       for(unsigned int x = y % 2; x < mColumns; x += 2)
75       {
76         idx = y * mColumns + x;
77         SetTargetTop(idx);
78       }
79       for(unsigned int x = (y + 1) % 2; x < mColumns; x += 2)
80       {
81         idx = y * mColumns + x;
82         SetTargetRight(idx);
83       }
84     }
85   }
86   else
87   {
88     angle = -angle;
89
90     for(unsigned int y = 0; y < mRows; y++)
91     {
92       for(unsigned int x = y % 2; x < mColumns; x += 2)
93       {
94         idx = y * mColumns + x;
95         SetTargetBottom(idx);
96       }
97       for(unsigned int x = (y + 1) % 2; x < mColumns; x += 2)
98       {
99         idx = y * mColumns + x;
100         SetTargetLeft(idx);
101       }
102     }
103   }
104
105   const Vector2 halfSize = Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE).GetVectorXY() * 0.5f;
106   //the centre to "explode" the tiles outwards from
107   Vector3 centre(halfSize.x, halfSize.y, -1.0f / mDisplacementSpreadFactor);
108
109   for(unsigned int y = 0; y < mRows; y++)
110   {
111     for(unsigned int x = y % 2; x < mColumns; x = x + 2) // rotate vertically
112     {
113       idx = y * mColumns + x;
114       SetupAnimation(idx, x, y, -angle, Vector3::XAXIS, centre);
115     }
116     for(unsigned int x = (y + 1) % 2; x < mColumns; x = x + 2) // rotate horizontally
117     {
118       idx = y * mColumns + x;
119       SetupAnimation(idx, x, y, angle, Vector3::YAXIS, centre);
120     }
121   }
122
123   mAnimation.Play();
124   mIsAnimating = true;
125 }
126
127 void CubeTransitionCrossEffect::SetupAnimation(unsigned int actorIndex, unsigned int x, unsigned int y, float angle, const Vector3 axis, const Vector3& displacementCentre)
128 {
129   const Vector2 size     = Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE).GetVectorXY();
130   Vector2       halfSize = size * 0.5f;
131
132   //the position of the centre of the front face tile
133   Vector3 position(halfSize.x * (2.0f * x + 1.0f) / mColumns, halfSize.y * (2.0f * y + 1.0f) / mRows, 0.0f);
134
135   Vector3 direction = position - displacementCentre;
136   float   length    = direction.Length();
137   direction.Normalize();
138
139   float deltaLength = mCubeDisplacement / direction.z; //the length along the direction vector such that the projected direction onto the z axis is equal to mCubeDisplacement
140
141   Vector3 newPosition      = (direction * (length + deltaLength)) + displacementCentre;
142   Vector3 newLocalPosition = newPosition - position;
143
144   mAnimation.AnimateTo(Property(mBoxes[actorIndex], Actor::Property::ORIENTATION), Quaternion(Radian(-angle), axis), AlphaFunction::EASE_IN_OUT_SINE);
145   mAnimation.AnimateTo(Property(mBoxes[actorIndex], Actor::Property::POSITION), newLocalPosition, AlphaFunction::BOUNCE);
146
147   mAnimation.AnimateTo(Property(mCurrentTiles[actorIndex], Actor::Property::COLOR), HALF_BRIGHTNESS, AlphaFunction::EASE_OUT);
148   mAnimation.AnimateTo(Property(mTargetTiles[actorIndex], Actor::Property::COLOR), FULL_BRIGHTNESS, AlphaFunction::EASE_IN);
149 }
150
151 } // namespace Internal
152
153 } // namespace Toolkit
154
155 } // namespace Dali