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.
22 #include "shared/view.h"
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/devel-api/transition-effects/cube-transition-effect.h>
27 #include <dali-toolkit/devel-api/transition-effects/cube-transition-cross-effect.h>
28 #include <dali-toolkit/devel-api/transition-effects/cube-transition-fold-effect.h>
29 #include <dali-toolkit/devel-api/transition-effects/cube-transition-wave-effect.h>
34 using Dali::Toolkit::TextLabel;
40 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
41 const char * const APPLICATION_TITLE_WAVE( "Cube Transition: Wave" );
42 const char * const APPLICATION_TITLE_CROSS( "Cube Transition: Cross" );
43 const char * const APPLICATION_TITLE_FOLD( "Cube Transition: Fold" );
44 const char * const EFFECT_WAVE_IMAGE( DALI_IMAGE_DIR "icon-effect-wave.png" );
45 const char * const EFFECT_CROSS_IMAGE( DALI_IMAGE_DIR "icon-effect-cross.png" );
46 const char * const EFFECT_FOLD_IMAGE( DALI_IMAGE_DIR "icon-effect-fold.png" );
47 const char * const SLIDE_SHOW_START_ICON( DALI_IMAGE_DIR "icon-play.png" );
48 const char * const SLIDE_SHOW_STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
50 const char* IMAGES[] =
52 DALI_IMAGE_DIR "gallery-large-1.jpg",
53 DALI_IMAGE_DIR "gallery-large-2.jpg",
54 DALI_IMAGE_DIR "gallery-large-3.jpg",
55 DALI_IMAGE_DIR "gallery-large-4.jpg",
56 DALI_IMAGE_DIR "gallery-large-5.jpg",
57 DALI_IMAGE_DIR "gallery-large-6.jpg",
58 DALI_IMAGE_DIR "gallery-large-7.jpg",
59 DALI_IMAGE_DIR "gallery-large-8.jpg",
60 DALI_IMAGE_DIR "gallery-large-9.jpg",
61 DALI_IMAGE_DIR "gallery-large-10.jpg",
62 DALI_IMAGE_DIR "gallery-large-11.jpg",
63 DALI_IMAGE_DIR "gallery-large-12.jpg",
64 DALI_IMAGE_DIR "gallery-large-13.jpg",
65 DALI_IMAGE_DIR "gallery-large-14.jpg",
66 DALI_IMAGE_DIR "gallery-large-15.jpg",
67 DALI_IMAGE_DIR "gallery-large-16.jpg",
68 DALI_IMAGE_DIR "gallery-large-17.jpg",
69 DALI_IMAGE_DIR "gallery-large-18.jpg",
70 DALI_IMAGE_DIR "gallery-large-19.jpg",
71 DALI_IMAGE_DIR "gallery-large-20.jpg",
72 DALI_IMAGE_DIR "gallery-large-21.jpg",
74 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
76 // the number of cubes: NUM_COLUMNS*NUM_ROWS
77 // better choose the numbers that can divide viewAreaSize.x
78 const int NUM_COLUMNS_WAVE(16);
79 const int NUM_COLUMNS_CROSS(8);
80 const int NUM_COLUMNS_FOLD(8);
81 // better choose the numbers that can divide viewAreaSize.y
82 const int NUM_ROWS_WAVE(20);
83 const int NUM_ROWS_CROSS(10);
84 const int NUM_ROWS_FOLD(10);
85 //transition effect duration
86 const float ANIMATION_DURATION_WAVE(1.5f);
87 const float ANIMATION_DURATION_CROSS(1.f);
88 const float ANIMATION_DURATION_FOLD(1.f);
89 //transition effect displacement
90 const float CUBE_DISPLACEMENT_WAVE(70.f);
91 const float CUBE_DISPLACEMENT_CROSS(30.f);
93 // The duration of the current image staying on screen when slideshow is on
94 const int VIEWINGTIME = 2000; // 2 seconds
97 * @brief Load an image, scaled-down to no more than the stage dimensions.
99 * Uses image scaling mode SCALE_TO_FILL to resize the image at
100 * load time to cover the entire stage with pixels with no borders,
101 * and filter mode BOX_THEN_LINEAR to sample the image with
104 ResourceImage LoadStageFillingImage( const char * const imagePath )
106 Size stageSize = Stage::GetCurrent().GetSize();
107 return ResourceImage::New( imagePath, ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
112 class CubeTransitionApp : public ConnectionTracker
118 * @param application class, stored as reference
120 CubeTransitionApp( Application& application );
122 ~CubeTransitionApp();
127 * This method gets called once the main loop of application is up and running
129 void OnInit( Application& application );
131 * PanGesture callback. This method gets called when the pan gesture is detected.
132 * @param[in] actor The actor receiving the pan gesture.
133 * @param[in] gesture The detected pan gesture.
135 void OnPanGesture( Actor actor, const PanGesture& gesture );
137 * Load the next image and start the transition;
139 void GoToNextImage();
141 * Callback function of image resource loading succeed
142 * Start the transition
143 * @param[in] image The image content of the imageActor for transition
145 void OnImageLoaded(ResourceImage image);
147 * Main key event handler
149 void OnKeyEvent(const KeyEvent& event);
151 * Callback function of effect-switch button
152 * Change the effect when the effect button is clicked
153 * @param[in] button The handle of the clicked button
155 bool OnEffectButtonClicked( Toolkit::Button button );
157 * Callback function of slideshow button
158 * Start or stop the automatical image display when the slideshow button is clicked
159 * @param[in] button The handle of the clicked button
161 bool OnSildeshowButtonClicked( Toolkit::Button button );
163 * Callback function of cube transition completed signal
164 * @param[in] effect The cube effect used for the transition
165 * @param[in] imageActor The target imageActor of the completed transition
167 void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor);
169 * Callback function of timer tick
170 * The timer is used to count the image display duration in slideshow,
175 Application& mApplication;
176 Toolkit::Control mView;
177 Toolkit::ToolBar mToolBar;
179 Toolkit::TextLabel mTitleActor;
184 ImageActor mCurrentImage;
185 ImageActor mNextImage;
187 bool mIsImageLoading;
189 PanGestureDetector mPanGestureDetector;
191 Toolkit::CubeTransitionEffect mCubeWaveEffect;
192 Toolkit::CubeTransitionEffect mCubeCrossEffect;
193 Toolkit::CubeTransitionEffect mCubeFoldEffect;
194 Toolkit::CubeTransitionEffect mCurrentEffect;
198 Toolkit::PushButton mSlideshowButton;
199 Image mIconSlideshowStart;
200 Image mIconSlideshowStop;
202 Vector2 mPanPosition;
203 Vector2 mPanDisplacement;
208 Toolkit::PushButton mEffectChangeButton;
211 CubeTransitionApp::CubeTransitionApp( Application& application )
212 : mApplication( application ),
214 mIsImageLoading( false ),
217 mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
220 CubeTransitionApp::~CubeTransitionApp()
225 void CubeTransitionApp::OnInit( Application& application )
227 DemoHelper::RequestThemeChange();
229 Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
231 // Creates a default view with a default tool bar, the view is added to the stage.
232 mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
234 // Add an effect-changing button on the right of the tool bar.
235 mImageWave = ResourceImage::New( EFFECT_WAVE_IMAGE );
236 mImageCross = ResourceImage::New( EFFECT_CROSS_IMAGE );
237 mImageFold = ResourceImage::New( EFFECT_FOLD_IMAGE );
238 mEffectChangeButton = Toolkit::PushButton::New();
239 mEffectChangeButton.SetBackgroundImage(mImageWave);
240 mEffectChangeButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
241 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
243 // Add title to the tool bar.
244 mTitleActor = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_WAVE );
245 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
247 //Add an slideshow icon on the right of the title
248 mIconSlideshowStart = ResourceImage::New( SLIDE_SHOW_START_ICON );
249 mIconSlideshowStop = ResourceImage::New( SLIDE_SHOW_STOP_ICON );
250 mSlideshowButton = Toolkit::PushButton::New();
251 mSlideshowButton.SetBackgroundImage( mIconSlideshowStart );
252 mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
253 mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
255 // Set size to stage size to avoid seeing a black border on transition
256 mViewSize = Stage::GetCurrent().GetSize();
258 mParent = Actor::New();
259 mParent.SetSize( mViewSize );
260 mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
261 mContent.Add( mParent );
263 // use pan gesture to detect the cursor or finger movement
264 mPanGestureDetector = PanGestureDetector::New();
265 mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
266 mPanGestureDetector.Attach( mParent );
269 mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New(NUM_ROWS_WAVE, NUM_COLUMNS_WAVE, mViewSize);
270 mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
271 mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
272 mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
273 mParent.Add(mCubeWaveEffect.GetRoot());
275 mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS, mViewSize);
276 mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS);
277 mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
278 mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
279 mParent.Add(mCubeCrossEffect.GetRoot());
281 mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New(NUM_ROWS_FOLD, NUM_COLUMNS_FOLD, mViewSize);
282 mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD);
283 mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
284 mParent.Add(mCubeFoldEffect.GetRoot());
286 mViewTimer = Timer::New( VIEWINGTIME );
287 mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
289 // show the first image
290 mCurrentImage = ImageActor::New( LoadStageFillingImage( IMAGES[mIndex] ) );
291 mCurrentImage.SetPositionInheritanceMode( USE_PARENT_POSITION );
292 mCurrentImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
293 mCurrentImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
294 mParent.Add( mCurrentImage );
296 mCurrentEffect = mCubeWaveEffect;
297 mCurrentEffect.SetCurrentImage( mCurrentImage );
300 // signal handler, called when the pan gesture is detected
301 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
303 // does not response when the transition has not finished
304 if( mIsImageLoading || mCubeWaveEffect.IsTransiting() || mCubeCrossEffect.IsTransiting() || mCubeFoldEffect.IsTransiting() || mSlideshow )
309 if( gesture.state == Gesture::Continuing )
311 if( gesture.displacement.x < 0)
313 mIndex = (mIndex + 1)%NUM_IMAGES;
317 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
320 mPanPosition = gesture.position;
321 mPanDisplacement = gesture.displacement;
326 void CubeTransitionApp::GoToNextImage()
328 ResourceImage image = LoadStageFillingImage( IMAGES[ mIndex ] );
329 mNextImage = ImageActor::New( image );
331 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION);
332 mNextImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
333 mNextImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
334 mCurrentEffect.SetTargetImage(mNextImage);
335 if( image.GetLoadingState() == ResourceLoadingSucceeded )
337 mIsImageLoading = false;
338 OnImageLoaded( image );
342 mIsImageLoading = true;
343 image.LoadingFinishedSignal().Connect( this, &CubeTransitionApp::OnImageLoaded );
347 void CubeTransitionApp::OnImageLoaded(ResourceImage image)
349 mIsImageLoading = false;
350 mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
351 mParent.Remove(mCurrentImage);
352 mParent.Add(mNextImage);
353 mCurrentImage = mNextImage;
356 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
358 if(mCurrentEffect == mCubeWaveEffect)
360 mCurrentEffect = mCubeCrossEffect;
361 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_CROSS) );
362 mEffectChangeButton.SetBackgroundImage(mImageCross);
365 else if(mCurrentEffect == mCubeCrossEffect)
367 mCurrentEffect = mCubeFoldEffect;
368 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_FOLD) );
369 mEffectChangeButton.SetBackgroundImage(mImageFold);
373 mCurrentEffect = mCubeWaveEffect;
374 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_WAVE) );
375 mEffectChangeButton.SetBackgroundImage(mImageWave);
378 // Set the current image to cube transition effect
379 // only need to set at beginning or change from another effect
380 mCurrentEffect.SetCurrentImage(mCurrentImage);
384 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
386 mSlideshow = !mSlideshow;
389 mPanGestureDetector.Detach( mParent );
390 mSlideshowButton.SetBackgroundImage( mIconSlideshowStop );
391 mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
392 mPanDisplacement = Vector2( -10.f, 0.f );
397 mPanGestureDetector.Attach( mParent );
398 mSlideshowButton.SetBackgroundImage( mIconSlideshowStart );
404 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor)
412 bool CubeTransitionApp::OnTimerTick()
416 mIndex = (mIndex + 1)%NUM_IMAGES;
420 //return false to stop the timer
424 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
426 if(event.state == KeyEvent::Down)
428 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
435 // Entry point for Linux & Tizen applications
436 int main( int argc, char **argv )
438 Application application = Application::New( &argc, &argv );
439 CubeTransitionApp test( application );
440 application.MainLoop();