Remove ResourceImage usage from demos
[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 #include "shared/utility.h"
24
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>
31
32
33 using namespace Dali;
34
35 using Dali::Toolkit::TextLabel;
36
37 // LOCAL STUFF
38 namespace
39 {
40
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" );
55
56 const char* IMAGES[] =
57 {
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",
79 };
80 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
81
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);
98
99 // The duration of the current image staying on screen when slideshow is on
100 const int VIEWINGTIME = 2000; // 2 seconds
101
102 } // namespace
103
104 class CubeTransitionApp : public ConnectionTracker
105 {
106 public:
107
108   /**
109    * Constructor
110    * @param application class, stored as reference
111    */
112   CubeTransitionApp( Application& application );
113
114   ~CubeTransitionApp();
115
116 private:
117
118   /**
119    * This method gets called once the main loop of application is up and running
120    */
121   void OnInit( Application& application );
122   /**
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.
126    */
127   void OnPanGesture( Actor actor, const PanGesture& gesture );
128   /**
129    * Load the next image and start the transition;
130    */
131   void GoToNextImage();
132   /**
133    * Main key event handler
134    */
135   void OnKeyEvent(const KeyEvent& event);
136   /**
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
140    */
141   bool OnEffectButtonClicked( Toolkit::Button button );
142   /**
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
146    */
147   bool OnSildeshowButtonClicked( Toolkit::Button button );
148   /**
149    * Callback function of cube transition completed signal
150    * @param[in] effect The cube effect used for the transition
151    * @param[in] image The target Image of the completed transition
152    */
153   void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Image image );
154   /**
155    * Callback function of timer tick
156    * The timer is used to count the image display duration in slideshow,
157    */
158   bool OnTimerTick();
159
160 private:
161   Application&                    mApplication;
162   Toolkit::Control                mView;
163   Toolkit::ToolBar                mToolBar;
164   Layer                           mContent;
165   Toolkit::TextLabel              mTitle;
166
167   Vector2                         mViewSize;
168
169   Image                           mCurrentImage;
170   Image                           mNextImage;
171   unsigned int                    mIndex;
172   bool                            mIsImageLoading;
173
174   PanGestureDetector              mPanGestureDetector;
175
176   Toolkit::CubeTransitionEffect   mCubeWaveEffect;
177   Toolkit::CubeTransitionEffect   mCubeCrossEffect;
178   Toolkit::CubeTransitionEffect   mCubeFoldEffect;
179   Toolkit::CubeTransitionEffect   mCurrentEffect;
180
181   bool                            mSlideshow;
182   Timer                           mViewTimer;
183   Toolkit::PushButton             mSlideshowButton;
184
185   Vector2                         mPanPosition;
186   Vector2                         mPanDisplacement;
187
188   Toolkit::PushButton             mEffectChangeButton;
189 };
190
191 CubeTransitionApp::CubeTransitionApp( Application& application )
192 : mApplication( application ),
193   mIndex( 0 ),
194   mIsImageLoading( false ),
195   mSlideshow( false )
196 {
197   mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
198 }
199
200 CubeTransitionApp::~CubeTransitionApp()
201 {
202   //Nothing to do
203 }
204
205 void CubeTransitionApp::OnInit( Application& application )
206 {
207   Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
208
209   // Creates a default view with a default tool bar, the view is added to the stage.
210   mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
211   mContent.SetBehavior( Layer::LAYER_3D );
212
213   // Add an effect-changing button on the right of the tool bar.
214   mEffectChangeButton = Toolkit::PushButton::New();
215   mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
216   mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
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   mTitle = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_WAVE );
222   mToolBar.AddControl( mTitle, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
223
224   //Add an slideshow icon on the right of the title
225   mSlideshowButton = Toolkit::PushButton::New();
226   mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
227   mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
228   mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
229   mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
230
231   // Set size to stage size to avoid seeing a black border on transition
232   mViewSize = Stage::GetCurrent().GetSize();
233
234   // show the first image
235   mCurrentImage = DemoHelper::LoadStageFillingImage( IMAGES[mIndex] );
236
237   //use small cubes
238   mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New( NUM_ROWS_WAVE, NUM_COLUMNS_WAVE );
239   mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
240   mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
241   mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
242
243   mCubeWaveEffect.SetSize( mViewSize );
244   mCubeWaveEffect.SetParentOrigin( ParentOrigin::CENTER );
245   mCubeWaveEffect.SetCurrentImage( mCurrentImage );
246
247   // use big cubes
248   mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS );
249   mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS );
250   mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
251   mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
252
253   mCubeCrossEffect.SetSize( mViewSize );
254   mCubeCrossEffect.SetParentOrigin( ParentOrigin::CENTER );
255   mCubeCrossEffect.SetCurrentImage( mCurrentImage );
256
257   mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New( NUM_ROWS_FOLD, NUM_COLUMNS_FOLD );
258   mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD );
259   mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
260
261   mCubeFoldEffect.SetSize( mViewSize );
262   mCubeFoldEffect.SetParentOrigin( ParentOrigin::CENTER );
263   mCubeFoldEffect.SetCurrentImage( mCurrentImage );
264
265   mViewTimer = Timer::New( VIEWINGTIME );
266   mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
267
268
269   mCurrentEffect = mCubeWaveEffect;
270   mContent.Add( mCurrentEffect );
271
272   // use pan gesture to detect the cursor or finger movement
273   mPanGestureDetector = PanGestureDetector::New();
274   mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
275   mPanGestureDetector.Attach( mContent );
276 }
277
278 // signal handler, called when the pan gesture is detected
279 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
280 {
281   // does not response when the transition has not finished
282   if( mIsImageLoading || mCubeWaveEffect.IsTransitioning() || mCubeCrossEffect.IsTransitioning() || mCubeFoldEffect.IsTransitioning() || mSlideshow )
283   {
284     return;
285   }
286
287   if( gesture.state == Gesture::Continuing )
288   {
289     if( gesture.displacement.x < 0)
290     {
291       mIndex = (mIndex + 1)%NUM_IMAGES;
292     }
293     else
294     {
295       mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
296     }
297
298     mPanPosition = gesture.position;
299     mPanDisplacement = gesture.displacement;
300     GoToNextImage();
301   }
302 }
303
304 void CubeTransitionApp::GoToNextImage()
305 {
306   mNextImage = DemoHelper::LoadStageFillingImage( IMAGES[ mIndex ] );
307   mCurrentEffect.SetTargetImage( mNextImage );
308   mIsImageLoading = false;
309   mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
310   mCurrentImage = mNextImage;
311 }
312
313 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
314 {
315   mContent.Remove( mCurrentEffect );
316   if(mCurrentEffect == mCubeWaveEffect)
317   {
318     mCurrentEffect = mCubeCrossEffect;
319     mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_CROSS) );
320     mEffectChangeButton.SetUnselectedImage( EFFECT_CROSS_IMAGE );
321     mEffectChangeButton.SetSelectedImage( EFFECT_CROSS_IMAGE_SELECTED );
322
323   }
324   else if(mCurrentEffect == mCubeCrossEffect)
325   {
326     mCurrentEffect = mCubeFoldEffect;
327     mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_FOLD) );
328     mEffectChangeButton.SetUnselectedImage( EFFECT_FOLD_IMAGE );
329     mEffectChangeButton.SetSelectedImage( EFFECT_FOLD_IMAGE_SELECTED );
330   }
331   else
332   {
333     mCurrentEffect = mCubeWaveEffect;
334     mTitle.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_WAVE) );
335     mEffectChangeButton.SetUnselectedImage( EFFECT_WAVE_IMAGE );
336     mEffectChangeButton.SetSelectedImage( EFFECT_WAVE_IMAGE_SELECTED );
337   }
338   mContent.Add( mCurrentEffect );
339
340   // Set the current image to cube transition effect
341   // only need to set at beginning or change from another effect
342   mCurrentEffect.SetCurrentImage( mCurrentImage );
343   return true;
344 }
345
346 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
347 {
348   mSlideshow = !mSlideshow;
349   if( mSlideshow )
350   {
351     mPanGestureDetector.Detach( mContent );
352     mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_STOP_ICON );
353     mSlideshowButton.SetSelectedImage( SLIDE_SHOW_STOP_ICON_SELECTED );
354     mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
355     mPanDisplacement = Vector2( -10.f, 0.f );
356     mViewTimer.Start();
357   }
358   else
359   {
360     mPanGestureDetector.Attach( mContent );
361     mSlideshowButton.SetUnselectedImage( SLIDE_SHOW_START_ICON );
362     mSlideshowButton.SetSelectedImage( SLIDE_SHOW_START_ICON_SELECTED );
363     mViewTimer.Stop();
364   }
365   return true;
366 }
367
368 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, Image image )
369 {
370   if( mSlideshow )
371   {
372     mViewTimer.Start();
373   }
374 }
375
376 bool CubeTransitionApp::OnTimerTick()
377 {
378   if(mSlideshow)
379   {
380     mIndex = (mIndex + 1)%NUM_IMAGES;
381     GoToNextImage();
382   }
383
384   //return false to stop the timer
385   return false;
386 }
387
388 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
389 {
390   if(event.state == KeyEvent::Down)
391   {
392     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
393     {
394       mApplication.Quit();
395     }
396   }
397 }
398
399 // Entry point for Linux & Tizen applications
400 int DALI_EXPORT_API main( int argc, char **argv )
401 {
402   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
403   CubeTransitionApp test( application );
404   application.MainLoop();
405
406   return 0;
407 }