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