Dali-toolkit: Replace constraints with SizeMode
[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
24 namespace Toolkit
25 {
26
27 namespace Internal
28 {
29 const float CubeTransitionFoldEffect::mDisplacementRatio = 1.4142f; //sqrt(2)
30
31 CubeTransitionFoldEffect::CubeTransitionFoldEffect( unsigned int numRows, unsigned int numColumns, Size viewAreaSize )
32 : CubeTransitionEffect( numRows, numColumns, viewAreaSize)
33 {
34 }
35
36 Toolkit::CubeTransitionFoldEffect CubeTransitionFoldEffect::New(unsigned int numRows, unsigned int numColumns, Size viewAreaSize)
37 {
38   // Create the implementation
39   CubeTransitionFoldEffect* internalCubeTransEffect = new CubeTransitionFoldEffect( numRows, numColumns, viewAreaSize );
40
41   // Pass ownership to CustomActor handle
42   Toolkit::CubeTransitionFoldEffect cubeTransEffect( internalCubeTransEffect );
43
44   //Initialization
45   internalCubeTransEffect->Initialize();
46
47   return cubeTransEffect;
48 }
49
50 void CubeTransitionFoldEffect::OnInitialize()
51 {
52   float offset = mTileSize.width*0.5f;
53   unsigned int idx;
54   for( unsigned int y = 0; y < mNumRows; y++ )
55   {
56     idx = y*mNumColumns;
57     for( unsigned int x = y%2; x < mNumColumns; x=x+2)
58     {
59       mTiles[0][idx+x].SetZ( offset );
60       mTiles[1][idx+x].SetX( offset );
61     }
62     for( unsigned int x = (y+1)%2; x < mNumColumns; x=x+2)
63     {
64       mTiles[0][idx+x].SetZ( offset );
65       mTiles[1][idx+x].SetX( -offset );
66     }
67   }
68 }
69
70 void CubeTransitionFoldEffect::OnStartTransition( Vector2 panPosition, Vector2 panDisplacement )
71 {
72   float angle = mRotateIndex * Math::PI_2 ;
73   Vector3 translation0 = mTiles[mContainerIndex][ 0 ].GetCurrentPosition()*(-2.f);
74   Vector3 translation1 = mTiles[mContainerIndex][ mNumColumns ].GetCurrentPosition()*(-2.f);
75
76   unsigned int idx;
77
78   for( unsigned int y = 0; y < mNumRows; y++ )
79   {
80     for( unsigned int x = y%2; x < mNumColumns; x=x+2)
81     {
82       idx = y*mNumColumns + x;
83       SetupAnimation( idx, -angle, translation0 );
84     }
85     for( unsigned int x = (y+1)%2; x < mNumColumns; x=x+2)
86     {
87       idx = y*mNumColumns + x;
88       SetupAnimation( idx, angle, translation1 );
89     }
90   }
91
92   mAnimation.Play();
93   mIsAnimating = true;
94 }
95
96 void CubeTransitionFoldEffect::OnStopTransition()
97 {
98   float angle = mRotateIndex * Math::PI_2 ;
99   unsigned int idx;
100   for( unsigned int y = 0; y < mNumRows; y++ )
101   {
102     idx = y*mNumColumns;
103     for( unsigned int x = y%2; x < mNumColumns; x=x+2)
104     {
105       mBoxes[idx+x].SetRotation( Radian(angle), Vector3::YAXIS );
106     }
107     for( unsigned int x = (y+1)%2; x < mNumColumns; x=x+2)
108     {
109       mBoxes[idx+x].SetRotation( Radian(-angle), Vector3::YAXIS );
110     }
111   }
112 }
113
114 void CubeTransitionFoldEffect::SetupAnimation(unsigned int actorIndex, float angle, Vector3 resetTranslation)
115 {
116   Actor currentCube = mBoxes[actorIndex];
117   ImageActor sideTile = mTiles[mContainerIndex][actorIndex];
118   ImageActor frontTile = mTiles[mContainerIndex^1][actorIndex];
119   if ( mFirstTransition && (!mIsToNextImage) ) // for the first transition, it is going to previous image
120   {
121     sideTile.SetRotation( Radian( angle),   Vector3::YAXIS );
122   }
123   else if( !mChangeTurningDirection )   // reset rotation, translation and color
124   {
125     sideTile.MoveBy( resetTranslation );
126     sideTile.SetRotation( Radian( angle),   Vector3::YAXIS );
127   }
128   mAnimation.RotateTo( currentCube, Radian( -angle ), Vector3::YAXIS, AlphaFunctions::Linear );
129   Vector3 position(currentCube.GetCurrentPosition());
130   mAnimation.MoveTo(currentCube, Vector3( position.x*mDisplacementRatio, position.y, position.z ), AlphaFunctions::Bounce);
131   mAnimation.ColorTo( frontTile, HALF_BRIGHTNESS, AlphaFunctions::EaseOut );
132   mAnimation.ColorTo( sideTile, FULL_BRIGHTNESS, AlphaFunctions::EaseIn );
133 }
134
135 } // namespace Internal
136
137 } // namespace Toolkit
138
139 } // namespace Dali