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"
23 #include "shared/utility.h"
25 #include <dali/dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali-toolkit/devel-api/transition-effects/cube-transition-effect.h>
28 #include <dali-toolkit/devel-api/transition-effects/cube-transition-cross-effect.h>
29 #include <dali-toolkit/devel-api/transition-effects/cube-transition-fold-effect.h>
30 #include <dali-toolkit/devel-api/transition-effects/cube-transition-wave-effect.h>
35 using Dali::Toolkit::TextLabel;
41 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
42 const char * const APPLICATION_TITLE_WAVE( "Cube Transition: Wave" );
43 const char * const APPLICATION_TITLE_CROSS( "Cube Transition: Cross" );
44 const char * const APPLICATION_TITLE_FOLD( "Cube Transition: Fold" );
45 const char * const EFFECT_WAVE_IMAGE( DEMO_IMAGE_DIR "icon-effect-wave.png" );
46 const char * const EFFECT_WAVE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-effect-wave-selected.png" );
47 const char * const EFFECT_CROSS_IMAGE( DEMO_IMAGE_DIR "icon-effect-cross.png" );
48 const char * const EFFECT_CROSS_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-effect-cross-selected.png" );
49 const char * const EFFECT_FOLD_IMAGE( DEMO_IMAGE_DIR "icon-effect-fold.png" );
50 const char * const EFFECT_FOLD_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-effect-fold-selected.png" );
51 const char * const SLIDE_SHOW_START_ICON( DEMO_IMAGE_DIR "icon-play.png" );
52 const char * const SLIDE_SHOW_START_ICON_SELECTED( DEMO_IMAGE_DIR "icon-play-selected.png" );
53 const char * const SLIDE_SHOW_STOP_ICON( DEMO_IMAGE_DIR "icon-stop.png" );
54 const char * const SLIDE_SHOW_STOP_ICON_SELECTED( DEMO_IMAGE_DIR "icon-stop-selected.png" );
56 const char* IMAGES[] =
58 DEMO_IMAGE_DIR "gallery-large-1.jpg",
59 DEMO_IMAGE_DIR "gallery-large-2.jpg",
60 DEMO_IMAGE_DIR "gallery-large-3.jpg",
61 DEMO_IMAGE_DIR "gallery-large-4.jpg",
62 DEMO_IMAGE_DIR "gallery-large-5.jpg",
63 DEMO_IMAGE_DIR "gallery-large-6.jpg",
64 DEMO_IMAGE_DIR "gallery-large-7.jpg",
65 DEMO_IMAGE_DIR "gallery-large-8.jpg",
66 DEMO_IMAGE_DIR "gallery-large-9.jpg",
67 DEMO_IMAGE_DIR "gallery-large-10.jpg",
68 DEMO_IMAGE_DIR "gallery-large-11.jpg",
69 DEMO_IMAGE_DIR "gallery-large-12.jpg",
70 DEMO_IMAGE_DIR "gallery-large-13.jpg",
71 DEMO_IMAGE_DIR "gallery-large-14.jpg",
72 DEMO_IMAGE_DIR "gallery-large-15.jpg",
73 DEMO_IMAGE_DIR "gallery-large-16.jpg",
74 DEMO_IMAGE_DIR "gallery-large-17.jpg",
75 DEMO_IMAGE_DIR "gallery-large-18.jpg",
76 DEMO_IMAGE_DIR "gallery-large-19.jpg",
77 DEMO_IMAGE_DIR "gallery-large-20.jpg",
78 DEMO_IMAGE_DIR "gallery-large-21.jpg",
80 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
82 // the number of cubes: NUM_COLUMNS*NUM_ROWS
83 // better choose the numbers that can divide viewAreaSize.x
84 const int NUM_COLUMNS_WAVE(16);
85 const int NUM_COLUMNS_CROSS(8);
86 const int NUM_COLUMNS_FOLD(8);
87 // better choose the numbers that can divide viewAreaSize.y
88 const int NUM_ROWS_WAVE(20);
89 const int NUM_ROWS_CROSS(10);
90 const int NUM_ROWS_FOLD(10);
91 //transition effect duration
92 const float ANIMATION_DURATION_WAVE(1.5f);
93 const float ANIMATION_DURATION_CROSS(1.f);
94 const float ANIMATION_DURATION_FOLD(1.f);
95 //transition effect displacement
96 const float CUBE_DISPLACEMENT_WAVE(70.f);
97 const float CUBE_DISPLACEMENT_CROSS(30.f);
99 // The duration of the current image staying on screen when slideshow is on
100 const int VIEWINGTIME = 2000; // 2 seconds
104 class CubeTransitionApp : public ConnectionTracker
110 * @param application class, stored as reference
112 CubeTransitionApp( Application& application );
114 ~CubeTransitionApp();
119 * This method gets called once the main loop of application is up and running
121 void OnInit( Application& application );
123 * PanGesture callback. This method gets called when the pan gesture is detected.
124 * @param[in] actor The actor receiving the pan gesture.
125 * @param[in] gesture The detected pan gesture.
127 void OnPanGesture( Actor actor, const PanGesture& gesture );
129 * Load the next image and start the transition;
131 void GoToNextImage();
133 * Main key event handler
135 void OnKeyEvent(const KeyEvent& event);
137 * Callback function of effect-switch button
138 * Change the effect when the effect button is clicked
139 * @param[in] button The handle of the clicked button
141 bool OnEffectButtonClicked( Toolkit::Button button );
143 * Callback function of slideshow button
144 * Start or stop the automatical image display when the slideshow button is clicked
145 * @param[in] button The handle of the clicked button
147 bool OnSildeshowButtonClicked( Toolkit::Button button );
149 * Callback function of cube transition completed signal
150 * @param[in] effect The cube effect used for the transition
151 * @param[in] image The target Image of the completed transition
153 void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Image image );
155 * Callback function of timer tick
156 * The timer is used to count the image display duration in slideshow,
161 Application& mApplication;
162 Toolkit::Control mView;
163 Toolkit::ToolBar mToolBar;
165 Toolkit::TextLabel mTitle;
172 bool mIsImageLoading;
174 PanGestureDetector mPanGestureDetector;
176 Toolkit::CubeTransitionEffect mCubeWaveEffect;
177 Toolkit::CubeTransitionEffect mCubeCrossEffect;
178 Toolkit::CubeTransitionEffect mCubeFoldEffect;
179 Toolkit::CubeTransitionEffect mCurrentEffect;
183 Toolkit::PushButton mSlideshowButton;
185 Vector2 mPanPosition;
186 Vector2 mPanDisplacement;
188 Toolkit::PushButton mEffectChangeButton;
191 CubeTransitionApp::CubeTransitionApp( Application& application )
192 : mApplication( application ),
194 mIsImageLoading( false ),
197 mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
200 CubeTransitionApp::~CubeTransitionApp()
205 void CubeTransitionApp::OnInit( Application& application )
207 Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
209 // Creates a default view with a default tool bar, the view is added to the stage.
210 mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
211 mContent.SetBehavior( Layer::LAYER_3D );
213 // Add an effect-changing button on the right of the tool bar.
214 mEffectChangeButton = Toolkit::PushButton::New();
215 mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
216 mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
217 mEffectChangeButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
218 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
220 // Add title to the tool bar.
221 mTitle = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_WAVE );
222 mToolBar.AddControl( mTitle, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
224 //Add an slideshow icon on the right of the title
225 mSlideshowButton = Toolkit::PushButton::New();
226 mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
227 mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
228 mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
229 mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
231 // Set size to stage size to avoid seeing a black border on transition
232 mViewSize = Stage::GetCurrent().GetSize();
234 // show the first image
235 mCurrentImage = DemoHelper::LoadStageFillingImage( IMAGES[mIndex] );
238 mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New( NUM_ROWS_WAVE, NUM_COLUMNS_WAVE );
239 mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
240 mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
241 mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
243 mCubeWaveEffect.SetSize( mViewSize );
244 mCubeWaveEffect.SetParentOrigin( ParentOrigin::CENTER );
245 mCubeWaveEffect.SetCurrentImage( mCurrentImage );
248 mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS );
249 mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS );
250 mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
251 mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
253 mCubeCrossEffect.SetSize( mViewSize );
254 mCubeCrossEffect.SetParentOrigin( ParentOrigin::CENTER );
255 mCubeCrossEffect.SetCurrentImage( mCurrentImage );
257 mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New( NUM_ROWS_FOLD, NUM_COLUMNS_FOLD );
258 mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD );
259 mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
261 mCubeFoldEffect.SetSize( mViewSize );
262 mCubeFoldEffect.SetParentOrigin( ParentOrigin::CENTER );
263 mCubeFoldEffect.SetCurrentImage( mCurrentImage );
265 mViewTimer = Timer::New( VIEWINGTIME );
266 mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
269 mCurrentEffect = mCubeWaveEffect;
270 mContent.Add( mCurrentEffect );
272 // use pan gesture to detect the cursor or finger movement
273 mPanGestureDetector = PanGestureDetector::New();
274 mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
275 mPanGestureDetector.Attach( mContent );
278 // signal handler, called when the pan gesture is detected
279 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
281 // does not response when the transition has not finished
282 if( mIsImageLoading || mCubeWaveEffect.IsTransitioning() || mCubeCrossEffect.IsTransitioning() || mCubeFoldEffect.IsTransitioning() || mSlideshow )
287 if( gesture.state == Gesture::Continuing )
289 if( gesture.displacement.x < 0)
291 mIndex = (mIndex + 1)%NUM_IMAGES;
295 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
298 mPanPosition = gesture.position;
299 mPanDisplacement = gesture.displacement;
304 void CubeTransitionApp::GoToNextImage()
306 mNextImage = DemoHelper::LoadStageFillingImage( IMAGES[ mIndex ] );
307 mCurrentEffect.SetTargetImage( mNextImage );
308 mIsImageLoading = false;
309 mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
310 mCurrentImage = mNextImage;
313 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
315 mContent.Remove( mCurrentEffect );
316 if(mCurrentEffect == mCubeWaveEffect)
318 mCurrentEffect = mCubeCrossEffect;
319 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_CROSS) );
320 mEffectChangeButton.SetUnselectedImage( EFFECT_CROSS_IMAGE );
321 mEffectChangeButton.SetSelectedImage( EFFECT_CROSS_IMAGE_SELECTED );
324 else if(mCurrentEffect == mCubeCrossEffect)
326 mCurrentEffect = mCubeFoldEffect;
327 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_FOLD) );
328 mEffectChangeButton.SetUnselectedImage( EFFECT_FOLD_IMAGE );
329 mEffectChangeButton.SetSelectedImage( EFFECT_FOLD_IMAGE_SELECTED );
333 mCurrentEffect = mCubeWaveEffect;
334 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_WAVE) );
335 mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
336 mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
338 mContent.Add( mCurrentEffect );
340 // Set the current image to cube transition effect
341 // only need to set at beginning or change from another effect
342 mCurrentEffect.SetCurrentImage( mCurrentImage );
346 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
348 mSlideshow = !mSlideshow;
351 mPanGestureDetector.Detach( mContent );
352 mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_STOP_ICON );
353 mSlideshowButton.SetSelectedImage( SLIDE_SHOW_STOP_ICON_SELECTED );
354 mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
355 mPanDisplacement = Vector2( -10.f, 0.f );
360 mPanGestureDetector.Attach( mContent );
361 mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
362 mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
368 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Image image )
376 bool CubeTransitionApp::OnTimerTick()
380 mIndex = (mIndex + 1)%NUM_IMAGES;
384 //return false to stop the timer
388 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
390 if(event.state == KeyEvent::Down)
392 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
399 // Entry point for Linux & Tizen applications
400 int DALI_EXPORT_API main( int argc, char **argv )
402 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
403 CubeTransitionApp test( application );
404 application.MainLoop();