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