2 * Copyright (c) 2017 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/controls/buttons/button-devel.h>
28 #include <dali-toolkit/devel-api/controls/buttons/toggle-button.h>
29 #include <dali-toolkit/devel-api/transition-effects/cube-transition-effect.h>
30 #include <dali-toolkit/devel-api/transition-effects/cube-transition-cross-effect.h>
31 #include <dali-toolkit/devel-api/transition-effects/cube-transition-fold-effect.h>
32 #include <dali-toolkit/devel-api/transition-effects/cube-transition-wave-effect.h>
37 using Dali::Toolkit::TextLabel;
43 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
44 const char * const APPLICATION_TITLE_WAVE( "Cube Transition: Wave" );
45 const char * const APPLICATION_TITLE_CROSS( "Cube Transition: Cross" );
46 const char * const APPLICATION_TITLE_FOLD( "Cube Transition: Fold" );
47 const char * const EFFECT_WAVE_IMAGE( DEMO_IMAGE_DIR "icon-effect-wave.png" );
48 const char * const EFFECT_WAVE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-effect-wave-selected.png" );
49 const char * const EFFECT_CROSS_IMAGE( DEMO_IMAGE_DIR "icon-effect-cross.png" );
50 const char * const EFFECT_CROSS_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-effect-cross-selected.png" );
51 const char * const EFFECT_FOLD_IMAGE( DEMO_IMAGE_DIR "icon-effect-fold.png" );
52 const char * const EFFECT_FOLD_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-effect-fold-selected.png" );
53 const char * const SLIDE_SHOW_START_ICON( DEMO_IMAGE_DIR "icon-play.png" );
54 const char * const SLIDE_SHOW_START_ICON_SELECTED( DEMO_IMAGE_DIR "icon-play-selected.png" );
55 const char * const SLIDE_SHOW_STOP_ICON( DEMO_IMAGE_DIR "icon-stop.png" );
56 const char * const SLIDE_SHOW_STOP_ICON_SELECTED( DEMO_IMAGE_DIR "icon-stop-selected.png" );
58 const char* IMAGES[] =
60 DEMO_IMAGE_DIR "gallery-large-1.jpg",
61 DEMO_IMAGE_DIR "gallery-large-2.jpg",
62 DEMO_IMAGE_DIR "gallery-large-3.jpg",
63 DEMO_IMAGE_DIR "gallery-large-4.jpg",
64 DEMO_IMAGE_DIR "gallery-large-5.jpg",
65 DEMO_IMAGE_DIR "gallery-large-6.jpg",
66 DEMO_IMAGE_DIR "gallery-large-7.jpg",
67 DEMO_IMAGE_DIR "gallery-large-8.jpg",
68 DEMO_IMAGE_DIR "gallery-large-9.jpg",
69 DEMO_IMAGE_DIR "gallery-large-10.jpg",
70 DEMO_IMAGE_DIR "gallery-large-11.jpg",
71 DEMO_IMAGE_DIR "gallery-large-12.jpg",
72 DEMO_IMAGE_DIR "gallery-large-13.jpg",
73 DEMO_IMAGE_DIR "gallery-large-14.jpg",
74 DEMO_IMAGE_DIR "gallery-large-15.jpg",
75 DEMO_IMAGE_DIR "gallery-large-16.jpg",
76 DEMO_IMAGE_DIR "gallery-large-17.jpg",
77 DEMO_IMAGE_DIR "gallery-large-18.jpg",
78 DEMO_IMAGE_DIR "gallery-large-19.jpg",
79 DEMO_IMAGE_DIR "gallery-large-20.jpg",
80 DEMO_IMAGE_DIR "gallery-large-21.jpg",
82 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
84 // the number of cubes: NUM_COLUMNS*NUM_ROWS
85 // better choose the numbers that can divide viewAreaSize.x
86 const int NUM_COLUMNS_WAVE(16);
87 const int NUM_COLUMNS_CROSS(8);
88 const int NUM_COLUMNS_FOLD(8);
89 // better choose the numbers that can divide viewAreaSize.y
90 const int NUM_ROWS_WAVE(20);
91 const int NUM_ROWS_CROSS(10);
92 const int NUM_ROWS_FOLD(10);
93 //transition effect duration
94 const float ANIMATION_DURATION_WAVE(1.5f);
95 const float ANIMATION_DURATION_CROSS(1.f);
96 const float ANIMATION_DURATION_FOLD(1.f);
97 //transition effect displacement
98 const float CUBE_DISPLACEMENT_WAVE(70.f);
99 const float CUBE_DISPLACEMENT_CROSS(30.f);
101 // The duration of the current image staying on screen when slideshow is on
102 const int VIEWINGTIME = 2000; // 2 seconds
106 class CubeTransitionApp : public ConnectionTracker
112 * @param application class, stored as reference
114 CubeTransitionApp( Application& application );
116 ~CubeTransitionApp();
121 * This method gets called once the main loop of application is up and running
123 void OnInit( Application& application );
125 * PanGesture callback. This method gets called when the pan gesture is detected.
126 * @param[in] actor The actor receiving the pan gesture.
127 * @param[in] gesture The detected pan gesture.
129 void OnPanGesture( Actor actor, const PanGesture& gesture );
131 * Load the next image and start the transition;
133 void GoToNextImage();
135 * Main key event handler
137 void OnKeyEvent(const KeyEvent& event);
139 * Callback function of effect-switch button
140 * Change the effect when the effect button is clicked
141 * @param[in] button The handle of the clicked button
143 bool OnEffectButtonClicked( Toolkit::Button button );
145 * Callback function of slideshow button
146 * Start or stop the automatical image display when the slideshow button is clicked
147 * @param[in] button The handle of the clicked button
149 bool OnSildeshowButtonClicked( Toolkit::Button button );
151 * Callback function of cube transition completed signal
152 * @param[in] effect The cube effect used for the transition
153 * @param[in] texture The target Texture of the completed transition
155 void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Texture image );
157 * Callback function of timer tick
158 * The timer is used to count the image display duration in slideshow,
163 * Loads image, resizes it to the size of stage and creates a textue out of it
164 * @param[in] filepath Path to the image file
165 * @return New texture object
167 Texture LoadStageFillingTexture( const char* filepath );
170 Application& mApplication;
171 Toolkit::Control mView;
172 Toolkit::ToolBar mToolBar;
174 Toolkit::TextLabel mTitle;
178 Texture mCurrentTexture;
179 Texture mNextTexture;
181 bool mIsImageLoading;
183 PanGestureDetector mPanGestureDetector;
185 Toolkit::CubeTransitionEffect mCubeWaveEffect;
186 Toolkit::CubeTransitionEffect mCubeCrossEffect;
187 Toolkit::CubeTransitionEffect mCubeFoldEffect;
188 Toolkit::CubeTransitionEffect mCurrentEffect;
192 Toolkit::PushButton mSlideshowButton;
194 Vector2 mPanPosition;
195 Vector2 mPanDisplacement;
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 );
221 // Add an effect changing toggle button
222 Toolkit::ToggleButton effectChangeToggleButton = Toolkit::ToggleButton::ToggleButton::New();
224 effectChangeToggleButton.SetProperty( Toolkit::ToggleButton::Property::STATE_VISUALS,
225 Property::Array().Add( EFFECT_WAVE_IMAGE )
226 .Add( EFFECT_CROSS_IMAGE)
227 .Add( EFFECT_FOLD_IMAGE )
230 effectChangeToggleButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
231 mToolBar.AddControl( effectChangeToggleButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
233 // Add title to the tool bar.
234 mTitle = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_WAVE );
235 mToolBar.AddControl( mTitle, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
237 //Add an slideshow icon on the right of the title
238 mSlideshowButton = Toolkit::PushButton::New();
239 mSlideshowButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, SLIDE_SHOW_START_ICON );
240 mSlideshowButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, SLIDE_SHOW_START_ICON_SELECTED );
241 mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
242 mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
244 // Set size to stage size to avoid seeing a black border on transition
245 mViewSize = Stage::GetCurrent().GetSize();
247 // show the first image
248 mCurrentTexture = LoadStageFillingTexture( IMAGES[mIndex] );
251 mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New( NUM_ROWS_WAVE, NUM_COLUMNS_WAVE );
252 mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
253 mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
254 mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
256 mCubeWaveEffect.SetSize( mViewSize );
257 mCubeWaveEffect.SetParentOrigin( ParentOrigin::CENTER );
258 mCubeWaveEffect.SetCurrentTexture( mCurrentTexture );
261 mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS );
262 mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS );
263 mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
264 mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
266 mCubeCrossEffect.SetSize( mViewSize );
267 mCubeCrossEffect.SetParentOrigin( ParentOrigin::CENTER );
268 mCubeCrossEffect.SetCurrentTexture( mCurrentTexture );
270 mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New( NUM_ROWS_FOLD, NUM_COLUMNS_FOLD );
271 mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD );
272 mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
274 mCubeFoldEffect.SetSize( mViewSize );
275 mCubeFoldEffect.SetParentOrigin( ParentOrigin::CENTER );
276 mCubeFoldEffect.SetCurrentTexture( mCurrentTexture );
278 mViewTimer = Timer::New( VIEWINGTIME );
279 mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
282 mCurrentEffect = mCubeWaveEffect;
283 mContent.Add( mCurrentEffect );
285 // use pan gesture to detect the cursor or finger movement
286 mPanGestureDetector = PanGestureDetector::New();
287 mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
288 mPanGestureDetector.Attach( mContent );
291 // signal handler, called when the pan gesture is detected
292 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
294 // does not response when the transition has not finished
295 if( mIsImageLoading || mCubeWaveEffect.IsTransitioning() || mCubeCrossEffect.IsTransitioning() || mCubeFoldEffect.IsTransitioning() || mSlideshow )
300 if( gesture.state == Gesture::Continuing )
302 if( gesture.displacement.x < 0)
304 mIndex = (mIndex + 1)%NUM_IMAGES;
308 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
311 mPanPosition = gesture.position;
312 mPanDisplacement = gesture.displacement;
317 void CubeTransitionApp::GoToNextImage()
319 mNextTexture = LoadStageFillingTexture( IMAGES[ mIndex ] );
320 mCurrentEffect.SetTargetTexture( mNextTexture );
321 mIsImageLoading = false;
322 mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
323 mCurrentTexture = mNextTexture;
326 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
328 mContent.Remove( mCurrentEffect );
329 if(mCurrentEffect == mCubeWaveEffect)
331 mCurrentEffect = mCubeCrossEffect;
332 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_CROSS) );
334 else if(mCurrentEffect == mCubeCrossEffect)
336 mCurrentEffect = mCubeFoldEffect;
337 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_FOLD) );
341 mCurrentEffect = mCubeWaveEffect;
342 mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_WAVE) );
344 mContent.Add( mCurrentEffect );
346 // Set the current image to cube transition effect
347 // only need to set at beginning or change from another effect
348 mCurrentEffect.SetCurrentTexture( mCurrentTexture );
352 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
354 mSlideshow = !mSlideshow;
357 mPanGestureDetector.Detach( mContent );
358 mSlideshowButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, SLIDE_SHOW_STOP_ICON );
359 mSlideshowButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, SLIDE_SHOW_STOP_ICON_SELECTED );
360 mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
361 mPanDisplacement = Vector2( -10.f, 0.f );
366 mPanGestureDetector.Attach( mContent );
367 mSlideshowButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, SLIDE_SHOW_START_ICON );
368 mSlideshowButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, SLIDE_SHOW_START_ICON_SELECTED );
374 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Texture texture )
382 bool CubeTransitionApp::OnTimerTick()
386 mIndex = (mIndex + 1)%NUM_IMAGES;
390 //return false to stop the timer
394 Texture CubeTransitionApp::LoadStageFillingTexture( const char* filepath )
396 ImageDimensions dimensions( Stage::GetCurrent().GetSize().x, Stage::GetCurrent().GetSize().y );
397 BitmapLoader loader = BitmapLoader::New( filepath, dimensions, FittingMode::SCALE_TO_FILL );
399 PixelData pixelData = loader.GetPixelData();
400 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
401 texture.Upload( pixelData );
405 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
407 if(event.state == KeyEvent::Down)
409 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
416 // Entry point for Linux & Tizen applications
417 int DALI_EXPORT_API main( int argc, char **argv )
419 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
420 CubeTransitionApp test( application );
421 application.MainLoop();