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] texture The target Texture of the completed transition
153 void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Texture image );
155 * Callback function of timer tick
156 * The timer is used to count the image display duration in slideshow,
161 * Loads image, resizes it to the size of stage and creates a textue out of it
162 * @param[in] filepath Path to the image file
163 * @return New texture object
165 Texture LoadStageFillingTexture( const char* filepath );
168 Application& mApplication;
169 Toolkit::Control mView;
170 Toolkit::ToolBar mToolBar;
172 Toolkit::TextLabel mTitle;
176 Texture mCurrentTexture;
177 Texture mNextTexture;
179 bool mIsImageLoading;
181 PanGestureDetector mPanGestureDetector;
183 Toolkit::CubeTransitionEffect mCubeWaveEffect;
184 Toolkit::CubeTransitionEffect mCubeCrossEffect;
185 Toolkit::CubeTransitionEffect mCubeFoldEffect;
186 Toolkit::CubeTransitionEffect mCurrentEffect;
190 Toolkit::PushButton mSlideshowButton;
192 Vector2 mPanPosition;
193 Vector2 mPanDisplacement;
195 Toolkit::PushButton mEffectChangeButton;
198 CubeTransitionApp::CubeTransitionApp( Application& application )
199 : mApplication( application ),
201 mIsImageLoading( false ),
204 mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
207 CubeTransitionApp::~CubeTransitionApp()
212 void CubeTransitionApp::OnInit( Application& application )
214 Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
216 // Creates a default view with a default tool bar, the view is added to the stage.
217 mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
218 mContent.SetBehavior( Layer::LAYER_3D );
220 // Add an effect-changing button on the right of the tool bar.
221 mEffectChangeButton = Toolkit::PushButton::New();
222 mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
223 mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
224 mEffectChangeButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
225 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
227 // Add title to the tool bar.
228 mTitle = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_WAVE );
229 mToolBar.AddControl( mTitle, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
231 //Add an slideshow icon on the right of the title
232 mSlideshowButton = Toolkit::PushButton::New();
233 mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
234 mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
235 mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
236 mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
238 // Set size to stage size to avoid seeing a black border on transition
239 mViewSize = Stage::GetCurrent().GetSize();
241 // show the first image
242 mCurrentTexture = LoadStageFillingTexture( IMAGES[mIndex] );
245 mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New( NUM_ROWS_WAVE, NUM_COLUMNS_WAVE );
246 mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
247 mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
248 mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
250 mCubeWaveEffect.SetSize( mViewSize );
251 mCubeWaveEffect.SetParentOrigin( ParentOrigin::CENTER );
252 mCubeWaveEffect.SetCurrentTexture( mCurrentTexture );
255 mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS );
256 mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS );
257 mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
258 mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
260 mCubeCrossEffect.SetSize( mViewSize );
261 mCubeCrossEffect.SetParentOrigin( ParentOrigin::CENTER );
262 mCubeCrossEffect.SetCurrentTexture( mCurrentTexture );
264 mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New( NUM_ROWS_FOLD, NUM_COLUMNS_FOLD );
265 mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD );
266 mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
268 mCubeFoldEffect.SetSize( mViewSize );
269 mCubeFoldEffect.SetParentOrigin( ParentOrigin::CENTER );
270 mCubeFoldEffect.SetCurrentTexture( mCurrentTexture );
272 mViewTimer = Timer::New( VIEWINGTIME );
273 mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
276 mCurrentEffect = mCubeWaveEffect;
277 mContent.Add( mCurrentEffect );
279 // use pan gesture to detect the cursor or finger movement
280 mPanGestureDetector = PanGestureDetector::New();
281 mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
282 mPanGestureDetector.Attach( mContent );
285 // signal handler, called when the pan gesture is detected
286 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
288 // does not response when the transition has not finished
289 if( mIsImageLoading || mCubeWaveEffect.IsTransitioning() || mCubeCrossEffect.IsTransitioning() || mCubeFoldEffect.IsTransitioning() || mSlideshow )
294 if( gesture.state == Gesture::Continuing )
296 if( gesture.displacement.x < 0)
298 mIndex = (mIndex + 1)%NUM_IMAGES;
302 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
305 mPanPosition = gesture.position;
306 mPanDisplacement = gesture.displacement;
311 void CubeTransitionApp::GoToNextImage()
313 mNextTexture = LoadStageFillingTexture( IMAGES[ mIndex ] );
314 mCurrentEffect.SetTargetTexture( mNextTexture );
315 mIsImageLoading = false;
316 mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
317 mCurrentTexture = mNextTexture;
320 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
322 mContent.Remove( mCurrentEffect );
323 if(mCurrentEffect == mCubeWaveEffect)
325 mCurrentEffect = mCubeCrossEffect;
326 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_CROSS) );
327 mEffectChangeButton.SetUnselectedImage( EFFECT_CROSS_IMAGE );
328 mEffectChangeButton.SetSelectedImage( EFFECT_CROSS_IMAGE_SELECTED );
331 else if(mCurrentEffect == mCubeCrossEffect)
333 mCurrentEffect = mCubeFoldEffect;
334 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_FOLD) );
335 mEffectChangeButton.SetUnselectedImage( EFFECT_FOLD_IMAGE );
336 mEffectChangeButton.SetSelectedImage( EFFECT_FOLD_IMAGE_SELECTED );
340 mCurrentEffect = mCubeWaveEffect;
341 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_WAVE) );
342 mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
343 mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
345 mContent.Add( mCurrentEffect );
347 // Set the current image to cube transition effect
348 // only need to set at beginning or change from another effect
349 mCurrentEffect.SetCurrentTexture( mCurrentTexture );
353 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
355 mSlideshow = !mSlideshow;
358 mPanGestureDetector.Detach( mContent );
359 mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_STOP_ICON );
360 mSlideshowButton.SetSelectedImage( SLIDE_SHOW_STOP_ICON_SELECTED );
361 mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
362 mPanDisplacement = Vector2( -10.f, 0.f );
367 mPanGestureDetector.Attach( mContent );
368 mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
369 mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
375 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Texture texture )
383 bool CubeTransitionApp::OnTimerTick()
387 mIndex = (mIndex + 1)%NUM_IMAGES;
391 //return false to stop the timer
395 Texture CubeTransitionApp::LoadStageFillingTexture( const char* filepath )
397 ImageDimensions dimensions( Stage::GetCurrent().GetSize().x, Stage::GetCurrent().GetSize().y );
398 BitmapLoader loader = BitmapLoader::New( filepath, dimensions, FittingMode::SCALE_TO_FILL );
400 PixelData pixelData = loader.GetPixelData();
401 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
402 texture.Upload( pixelData );
406 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
408 if(event.state == KeyEvent::Down)
410 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
417 // Entry point for Linux & Tizen applications
418 int DALI_EXPORT_API main( int argc, char **argv )
420 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
421 CubeTransitionApp test( application );
422 application.MainLoop();