2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "cube-transition-effect-impl.h"
30 const Vector4 CubeTransitionEffect::FULL_BRIGHTNESS( 1.0f, 1.0f, 1.0f, 1.0f );
31 const Vector4 CubeTransitionEffect::HALF_BRIGHTNESS( 0.5f, 0.5f, 0.5f, 1.0f );
33 CubeTransitionEffect::CubeTransitionEffect( unsigned int numRows, unsigned int numColumns, Size viewAreaSize )
34 : mNumRows( numRows ),
35 mNumColumns( numColumns ),
36 mViewAreaSize( viewAreaSize ),
39 mChangeTurningDirection( false ),
40 mIsToNextImage( true ),
41 mIsImageLoading( false ),
42 mAnimationDuration( 1.f ),
43 mIsAnimating( false ),
45 mCubeDisplacement( 0.f ),
46 mFirstTransition( true ),
51 CubeTransitionEffect::~CubeTransitionEffect()
55 void CubeTransitionEffect::Initialize()
57 //create root actor for the cube transition effect, only visible during the transition
59 mRoot.SetParentOrigin( ParentOrigin::CENTER );
60 mRoot.SetAnchorPoint( AnchorPoint::CENTER );
61 mRoot.SetVisible(false);
63 // create two groups of tiles,
64 // and one group of actors (cubes) serving as parents of every two tiles (one from each image).
65 unsigned int totalNum = mNumColumns* mNumRows;
66 mBoxes.resize( totalNum );
67 mTiles[0].resize( totalNum );
68 mTiles[1].resize( totalNum );
69 mTileSize = Vector2( mViewAreaSize.width / mNumColumns, mViewAreaSize.height / mNumRows );
70 const Vector3 basePosition( (-mViewAreaSize.width + mTileSize.width) * 0.5f,
71 (-mViewAreaSize.height + mTileSize.height) * 0.5f,
72 -mTileSize.width * 0.5f );
74 Image placeHolder = BitmapImage::WHITE();
75 for( unsigned int y = 0; y < mNumRows; y++ )
77 float positionY = y * mTileSize.height + basePosition.y;
78 for( unsigned int x = 0; x < mNumColumns; x++)
80 unsigned int idx = y*mNumColumns + x;
81 Actor actor( Actor::New() );
83 actor.SetParentOrigin( ParentOrigin::CENTER );
84 actor.SetAnchorPoint( AnchorPoint::CENTER );
85 actor.SetPosition( x * mTileSize.width + basePosition.x,
90 mTiles[ 0 ][idx] = CreateTile( placeHolder, FULL_BRIGHTNESS );
91 actor.Add( mTiles[ 0 ][idx] );
93 mTiles[ 1 ][idx] = CreateTile( placeHolder, HALF_BRIGHTNESS );
94 actor.Add( mTiles[ 1 ][idx] );
98 // helper actor to create a off-screen image using shader effect
99 mEmptyImage = ImageActor::New( placeHolder );
100 mEmptyImage.SetSize(Stage::GetCurrent().GetSize());
101 mEmptyImage.SetParentOrigin( ParentOrigin::CENTER );
102 mEmptyImage.SetAnchorPoint( AnchorPoint::CENTER );
103 mFullImageCreator = FullAreaImageCreator::New();
104 mEmptyImage.SetShaderEffect( mFullImageCreator );
105 Stage::GetCurrent().Add(mEmptyImage);
107 // set up off-screen render task
108 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
109 mOffScreenTask = taskList.CreateTask();
110 mOffScreenTask.SetSourceActor(mEmptyImage);
111 mOffScreenTask.SetExclusive(true);
112 mOffScreenBuffer[0] = FrameBufferImage::New(mViewAreaSize.x, mViewAreaSize.y);
113 mOffScreenBuffer[1] = FrameBufferImage::New(mViewAreaSize.x, mViewAreaSize.y);
114 mOffScreenTask.SetTargetFrameBuffer(mOffScreenBuffer[mBufferIndex]);
115 mOffScreenTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
120 ImageActor CubeTransitionEffect::CreateTile( Image image, const Vector4& color )
122 ImageActor tile = ImageActor::New( image );
123 tile.SetParentOrigin( ParentOrigin::CENTER );
124 tile.SetAnchorPoint( AnchorPoint::CENTER );
125 tile.SetSize( mTileSize );
126 tile.SetColorMode( Dali::USE_OWN_COLOR );
127 tile.SetColor( color );
132 void CubeTransitionEffect::SetTransitionDuration( float duration )
134 mAnimationDuration = duration;
137 float CubeTransitionEffect::GetTransitionDuration( ) const
139 return mAnimationDuration;
142 void CubeTransitionEffect::SetCubeDisplacement( float displacement )
144 mCubeDisplacement = displacement;
147 float CubeTransitionEffect::GetCubeDisplacement() const
149 return mCubeDisplacement;
152 Actor CubeTransitionEffect::GetRoot()
157 bool CubeTransitionEffect::IsTransiting()
159 return mIsImageLoading || mIsAnimating;
162 void CubeTransitionEffect::SetCurrentImage( ImageActor imageActor )
164 mContainerIndex = std::abs(mRotateIndex) % 2;
165 SetImage( imageActor );
168 void CubeTransitionEffect::SetTargetImage( ImageActor imageActor )
170 mContainerIndex = std::abs( mRotateIndex+1 ) % 2;
171 SetImage( imageActor );
174 void CubeTransitionEffect::SetImage( ImageActor imageActor )
176 mCurrentImage = imageActor;
177 mIsImageLoading = true;
179 Image image = imageActor.GetImage();
180 mBufferIndex = mBufferIndex^1;
182 //must make sure the image is already loaded before using its attributes
183 if( image.GetLoadingState() == ResourceLoadingSucceeded )
185 OnImageLoaded( image );
189 image.LoadingFinishedSignal().Connect( this, &CubeTransitionEffect::OnImageLoaded );
193 void CubeTransitionEffect::StartTransition( bool toNextImage )
197 StartTransition( Vector2( mViewAreaSize.width, mViewAreaSize.height*0.5f ), Vector2( -10.f, 0.f ) );
201 StartTransition( Vector2( 0, mViewAreaSize.height*0.5f ), Vector2( 10.f, 0.f ));
205 void CubeTransitionEffect::StartTransition( Vector2 panPosition, Vector2 panDisplacement )
207 mRoot.SetVisible( true );
208 mCurrentImage.SetVisible( false );
209 bool toNextImage = ( panDisplacement.x < 0 ) ? true : false;
210 if( mIsToNextImage != toNextImage )
212 mChangeTurningDirection = true;
216 mChangeTurningDirection = false;
218 mIsToNextImage = toNextImage;
234 mAnimation = Animation::New( mAnimationDuration );
235 mAnimation.FinishedSignal().Connect(this, &CubeTransitionEffect::OnTransitionFinished);
237 OnStartTransition( panPosition, panDisplacement );
240 void CubeTransitionEffect::PauseTransition()
242 if( mIsAnimating && !mIsPaused )
249 void CubeTransitionEffect::ResumeTransition()
251 if( mIsAnimating && mIsPaused)
258 void CubeTransitionEffect::StopTransition()
266 //reset the position of the cubes
267 //reset the color of the tiles
268 //all these status should be the same as the final state when the transition animation is finished completely
269 const Vector3 basePosition( (-mViewAreaSize.width + mTileSize.width) * 0.5f,
270 (-mViewAreaSize.height + mTileSize.height) * 0.5f,
271 -mTileSize.width * 0.5f );
272 unsigned int anotherIndex = mContainerIndex^1;
273 for( unsigned int y = 0; y < mNumRows; y++ )
275 float positionY = y * mTileSize.height + basePosition.y;
276 for( unsigned int x = 0; x < mNumColumns; x++)
278 unsigned int idx = y*mNumColumns + x;
279 mBoxes[idx].SetPosition( x * mTileSize.width + basePosition.x,
282 mTiles[mContainerIndex][idx].SetColor( FULL_BRIGHTNESS );
283 mTiles[anotherIndex][idx].SetColor( HALF_BRIGHTNESS);
287 // reset the rotation of the cubes, which is different process for different derived classes
290 mRoot.SetVisible(false);
291 mCurrentImage.SetVisible(true);
292 mIsAnimating = false;
293 mFirstTransition = false;
297 void CubeTransitionEffect::OnImageLoaded(Image image)
299 // Fit the image to view area, while keeping the aspect; FitKeepAspectRatio(imageSize, viewAreaSize)
300 ImageAttributes attributes( image.GetAttributes() );
301 float scale = std::min( mViewAreaSize.width / attributes.GetWidth(), mViewAreaSize.height / attributes.GetHeight() );
302 Vector2 imageSize(attributes.GetWidth()*scale, attributes.GetHeight()*scale);
304 mFullImageCreator.SetEffectImage(image);
305 mFullImageCreator.SetRegionSize(mViewAreaSize, imageSize);
307 mOffScreenTask.SetTargetFrameBuffer(mOffScreenBuffer[mBufferIndex]);
308 mOffScreenTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
310 ImageActor::PixelArea pixelArea( 0, 0, mViewAreaSize.x / mNumColumns, mViewAreaSize.y / mNumRows);
312 for( unsigned int y = 0; y < mNumRows; y++ )
314 pixelArea.y = y * pixelArea.height;
315 for( unsigned int x = 0; x < mNumColumns; x++)
317 pixelArea.x = x * pixelArea.width;
318 unsigned int idx = y*mNumColumns + x;
319 mTiles[mContainerIndex][idx].SetImage( mOffScreenBuffer[mBufferIndex]);
320 mTiles[mContainerIndex][idx].SetPixelArea( pixelArea );
323 mIsImageLoading = false;
326 void CubeTransitionEffect::OnTransitionFinished(Animation& source)
328 mRoot.SetVisible(false);
329 mCurrentImage.SetVisible(true);
330 mIsAnimating = false;
331 mFirstTransition = false;
334 Toolkit::CubeTransitionEffect handle( this );
335 mTransitionCompletedSignalV2.Emit( handle, mCurrentImage );
338 Toolkit::CubeTransitionEffect::TransitionCompletedSignalV2& CubeTransitionEffect::TransitionCompletedSignal()
340 return mTransitionCompletedSignalV2;
343 } // namespace Internal
345 } // namespace Toolkit