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