00339350eeff845f0fba7dd73298fa5f9347e605
[platform/core/uifw/dali-demo.git] / examples / cube-transition-effect / cube-transition-effect-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <math.h>
20
21 // INTERNAL INCLUDES
22 #include "shared/view.h"
23
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>
30
31
32 using namespace Dali;
33
34 using Dali::Toolkit::TextLabel;
35
36 // LOCAL STUFF
37 namespace
38 {
39
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_WAVE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-effect-wave-selected.png" );
46 const char * const EFFECT_CROSS_IMAGE( DALI_IMAGE_DIR "icon-effect-cross.png" );
47 const char * const EFFECT_CROSS_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-effect-cross-selected.png" );
48 const char * const EFFECT_FOLD_IMAGE( DALI_IMAGE_DIR "icon-effect-fold.png" );
49 const char * const EFFECT_FOLD_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-effect-fold-selected.png" );
50 const char * const SLIDE_SHOW_START_ICON( DALI_IMAGE_DIR "icon-play.png" );
51 const char * const SLIDE_SHOW_START_ICON_SELECTED( DALI_IMAGE_DIR "icon-play-selected.png" );
52 const char * const SLIDE_SHOW_STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
53 const char * const SLIDE_SHOW_STOP_ICON_SELECTED( DALI_IMAGE_DIR "icon-stop-selected.png" );
54
55 const char* IMAGES[] =
56 {
57   DALI_IMAGE_DIR "gallery-large-1.jpg",
58   DALI_IMAGE_DIR "gallery-large-2.jpg",
59   DALI_IMAGE_DIR "gallery-large-3.jpg",
60   DALI_IMAGE_DIR "gallery-large-4.jpg",
61   DALI_IMAGE_DIR "gallery-large-5.jpg",
62   DALI_IMAGE_DIR "gallery-large-6.jpg",
63   DALI_IMAGE_DIR "gallery-large-7.jpg",
64   DALI_IMAGE_DIR "gallery-large-8.jpg",
65   DALI_IMAGE_DIR "gallery-large-9.jpg",
66   DALI_IMAGE_DIR "gallery-large-10.jpg",
67   DALI_IMAGE_DIR "gallery-large-11.jpg",
68   DALI_IMAGE_DIR "gallery-large-12.jpg",
69   DALI_IMAGE_DIR "gallery-large-13.jpg",
70   DALI_IMAGE_DIR "gallery-large-14.jpg",
71   DALI_IMAGE_DIR "gallery-large-15.jpg",
72   DALI_IMAGE_DIR "gallery-large-16.jpg",
73   DALI_IMAGE_DIR "gallery-large-17.jpg",
74   DALI_IMAGE_DIR "gallery-large-18.jpg",
75   DALI_IMAGE_DIR "gallery-large-19.jpg",
76   DALI_IMAGE_DIR "gallery-large-20.jpg",
77   DALI_IMAGE_DIR "gallery-large-21.jpg",
78 };
79 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
80
81 // the number of cubes: NUM_COLUMNS*NUM_ROWS
82 // better choose the numbers that can divide viewAreaSize.x
83 const int NUM_COLUMNS_WAVE(16);
84 const int NUM_COLUMNS_CROSS(8);
85 const int NUM_COLUMNS_FOLD(8);
86 // better choose the numbers that can divide viewAreaSize.y
87 const int NUM_ROWS_WAVE(20);
88 const int NUM_ROWS_CROSS(10);
89 const int NUM_ROWS_FOLD(10);
90 //transition effect duration
91 const float ANIMATION_DURATION_WAVE(1.5f);
92 const float ANIMATION_DURATION_CROSS(1.f);
93 const float ANIMATION_DURATION_FOLD(1.f);
94 //transition effect displacement
95 const float CUBE_DISPLACEMENT_WAVE(70.f);
96 const float CUBE_DISPLACEMENT_CROSS(30.f);
97
98 // The duration of the current image staying on screen when slideshow is on
99 const int VIEWINGTIME = 2000; // 2 seconds
100
101 /**
102  * @brief Load an image, scaled-down to no more than the stage dimensions.
103  *
104  * Uses image scaling mode SCALE_TO_FILL to resize the image at
105  * load time to cover the entire stage with pixels with no borders,
106  * and filter mode BOX_THEN_LINEAR to sample the image with
107  * maximum quality.
108  */
109 ResourceImage LoadStageFillingImage( const char * const imagePath )
110 {
111   Size stageSize = Stage::GetCurrent().GetSize();
112   return ResourceImage::New( imagePath, ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
113 }
114
115 } // namespace
116
117 class CubeTransitionApp : public ConnectionTracker
118 {
119 public:
120
121   /**
122    * Constructor
123    * @param application class, stored as reference
124    */
125   CubeTransitionApp( Application& application );
126
127   ~CubeTransitionApp();
128
129 private:
130
131   /**
132    * This method gets called once the main loop of application is up and running
133    */
134   void OnInit( Application& application );
135   /**
136    * PanGesture callback. This method gets called when the pan gesture is detected.
137    * @param[in] actor The actor receiving the pan gesture.
138    * @param[in] gesture The detected pan gesture.
139    */
140   void OnPanGesture( Actor actor, const PanGesture& gesture );
141   /**
142    * Load the next image and start the transition;
143    */
144   void GoToNextImage();
145   /**
146    * Callback function of image resource loading succeed
147    * Start the transition
148    * @param[in] image The image content of the imageActor for transition
149    */
150   void OnImageLoaded(ResourceImage image);
151   /**
152    * Main key event handler
153    */
154   void OnKeyEvent(const KeyEvent& event);
155   /**
156    * Callback function of effect-switch button
157    * Change the effect when the effect button is clicked
158    * @param[in] button The handle of the clicked button
159    */
160   bool OnEffectButtonClicked( Toolkit::Button button );
161   /**
162    * Callback function of slideshow button
163    * Start or stop the automatical image display when the slideshow button is clicked
164    * @param[in] button The handle of the clicked button
165    */
166   bool OnSildeshowButtonClicked( Toolkit::Button button );
167   /**
168    * Callback function of cube transition completed signal
169    * @param[in] effect The cube effect used for the transition
170    * @param[in] imageActor The target imageActor of the completed transition
171    */
172   void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor);
173   /**
174    * Callback function of timer tick
175    * The timer is used to count the image display duration in slideshow,
176    */
177   bool OnTimerTick();
178
179 private:
180   Application&                    mApplication;
181   Toolkit::Control                mView;
182   Toolkit::ToolBar                mToolBar;
183   Layer                           mContent;
184   Toolkit::TextLabel              mTitleActor;
185   Actor                           mParent;
186
187   Vector2                         mViewSize;
188
189   ImageActor                      mCurrentImage;
190   ImageActor                      mNextImage;
191   unsigned int                    mIndex;
192   bool                            mIsImageLoading;
193
194   PanGestureDetector              mPanGestureDetector;
195
196   Toolkit::CubeTransitionEffect   mCubeWaveEffect;
197   Toolkit::CubeTransitionEffect   mCubeCrossEffect;
198   Toolkit::CubeTransitionEffect   mCubeFoldEffect;
199   Toolkit::CubeTransitionEffect   mCurrentEffect;
200
201   bool                            mSlideshow;
202   Timer                           mViewTimer;
203   Toolkit::PushButton             mSlideshowButton;
204
205   Vector2                         mPanPosition;
206   Vector2                         mPanDisplacement;
207
208   Toolkit::PushButton             mEffectChangeButton;
209 };
210
211 CubeTransitionApp::CubeTransitionApp( Application& application )
212 : mApplication( application ),
213   mIndex( 0 ),
214   mIsImageLoading( false ),
215   mSlideshow( false )
216 {
217   mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
218 }
219
220 CubeTransitionApp::~CubeTransitionApp()
221 {
222   //Nothing to do
223 }
224
225 void CubeTransitionApp::OnInit( Application& application )
226 {
227   Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
228
229   // Creates a default view with a default tool bar, the view is added to the stage.
230   mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
231   mContent.SetBehavior( Layer::LAYER_3D );
232
233   // Add an effect-changing button on the right of the tool bar.
234   mEffectChangeButton = Toolkit::PushButton::New();
235   mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
236   mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
237   mEffectChangeButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
238   mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
239
240   // Add title to the tool bar.
241   mTitleActor = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_WAVE );
242   mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
243
244   //Add an slideshow icon on the right of the title
245   mSlideshowButton = Toolkit::PushButton::New();
246   mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
247   mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
248   mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
249   mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
250
251   // Set size to stage size to avoid seeing a black border on transition
252   mViewSize = Stage::GetCurrent().GetSize();
253
254   mParent = Actor::New();
255   mParent.SetSize( mViewSize );
256   mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
257   mContent.Add( mParent );
258
259   // use pan gesture to detect the cursor or finger movement
260   mPanGestureDetector = PanGestureDetector::New();
261   mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
262   mPanGestureDetector.Attach( mParent );
263
264   //use small cubes
265   mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New(NUM_ROWS_WAVE, NUM_COLUMNS_WAVE, mViewSize);
266   mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
267   mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
268   mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
269   mParent.Add(mCubeWaveEffect.GetRoot());
270   // use big cubes
271   mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS, mViewSize);
272   mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS);
273   mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
274   mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
275   mParent.Add(mCubeCrossEffect.GetRoot());
276
277   mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New(NUM_ROWS_FOLD, NUM_COLUMNS_FOLD, mViewSize);
278   mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD);
279   mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
280   mParent.Add(mCubeFoldEffect.GetRoot());
281
282   mViewTimer = Timer::New( VIEWINGTIME );
283   mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
284
285   // show the first image
286   mCurrentImage = ImageActor::New( LoadStageFillingImage( IMAGES[mIndex] ) );
287   mCurrentImage.SetPositionInheritanceMode( USE_PARENT_POSITION );
288   mCurrentImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
289   mCurrentImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
290   mParent.Add( mCurrentImage );
291
292   mCurrentEffect = mCubeWaveEffect;
293   mCurrentEffect.SetCurrentImage( mCurrentImage );
294 }
295
296 // signal handler, called when the pan gesture is detected
297 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
298 {
299   // does not response when the transition has not finished
300   if( mIsImageLoading || mCubeWaveEffect.IsTransiting() || mCubeCrossEffect.IsTransiting() || mCubeFoldEffect.IsTransiting() || mSlideshow )
301   {
302     return;
303   }
304
305   if( gesture.state == Gesture::Continuing )
306   {
307     if( gesture.displacement.x < 0)
308     {
309       mIndex = (mIndex + 1)%NUM_IMAGES;
310     }
311     else
312     {
313       mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
314     }
315
316     mPanPosition = gesture.position;
317     mPanDisplacement = gesture.displacement;
318     GoToNextImage();
319   }
320 }
321
322 void CubeTransitionApp::GoToNextImage()
323 {
324   ResourceImage image = LoadStageFillingImage( IMAGES[ mIndex ] );
325   mNextImage = ImageActor::New( image );
326
327   mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION);
328   mNextImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
329   mNextImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
330   mCurrentEffect.SetTargetImage(mNextImage);
331   if( image.GetLoadingState() == ResourceLoadingSucceeded )
332   {
333     mIsImageLoading = false;
334     OnImageLoaded( image );
335   }
336   else
337   {
338     mIsImageLoading = true;
339     image.LoadingFinishedSignal().Connect( this, &CubeTransitionApp::OnImageLoaded );
340   }
341 }
342
343 void CubeTransitionApp::OnImageLoaded(ResourceImage image)
344 {
345    mIsImageLoading = false;
346    mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
347    mParent.Remove(mCurrentImage);
348    mParent.Add(mNextImage);
349    mCurrentImage = mNextImage;
350 }
351
352 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
353 {
354   if(mCurrentEffect == mCubeWaveEffect)
355   {
356     mCurrentEffect = mCubeCrossEffect;
357     mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_CROSS) );
358     mEffectChangeButton.SetUnselectedImage( EFFECT_CROSS_IMAGE );
359     mEffectChangeButton.SetSelectedImage( EFFECT_CROSS_IMAGE_SELECTED );
360
361   }
362   else if(mCurrentEffect == mCubeCrossEffect)
363   {
364     mCurrentEffect = mCubeFoldEffect;
365     mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_FOLD) );
366     mEffectChangeButton.SetUnselectedImage( EFFECT_FOLD_IMAGE );
367     mEffectChangeButton.SetSelectedImage( EFFECT_FOLD_IMAGE_SELECTED );
368   }
369   else
370   {
371     mCurrentEffect = mCubeWaveEffect;
372     mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_WAVE) );
373     mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
374     mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
375   }
376
377   // Set the current image to cube transition effect
378   // only need to set at beginning or change from another effect
379   mCurrentEffect.SetCurrentImage(mCurrentImage);
380   return true;
381 }
382
383 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
384 {
385   mSlideshow = !mSlideshow;
386   if( mSlideshow )
387   {
388     mPanGestureDetector.Detach( mParent );
389     mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_STOP_ICON );
390     mSlideshowButton.SetSelectedImage( SLIDE_SHOW_STOP_ICON_SELECTED );
391     mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
392     mPanDisplacement = Vector2( -10.f, 0.f );
393     mViewTimer.Start();
394   }
395   else
396   {
397     mPanGestureDetector.Attach( mParent );
398     mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
399     mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
400     mViewTimer.Stop();
401   }
402   return true;
403 }
404
405 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor)
406 {
407   if( mSlideshow )
408   {
409     mViewTimer.Start();
410   }
411 }
412
413 bool CubeTransitionApp::OnTimerTick()
414 {
415   if(mSlideshow)
416   {
417     mIndex = (mIndex + 1)%NUM_IMAGES;
418     GoToNextImage();
419   }
420
421   //return false to stop the timer
422   return false;
423 }
424
425 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
426 {
427   if(event.state == KeyEvent::Down)
428   {
429     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
430     {
431       mApplication.Quit();
432     }
433   }
434 }
435
436 // Entry point for Linux & Tizen applications
437 int main( int argc, char **argv )
438 {
439   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
440   CubeTransitionApp test( application );
441   application.MainLoop();
442
443   return 0;
444 }