[dali_1.0.37] Merge branch 'tizen'
[platform/core/uifw/dali-demo.git] / examples / dissolve-effect / dissolve-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_HIGHP( "Dissolve Effect(highp)" );
37 const char * const APPLICATION_TITLE_MEDIUMP( "Dissolve Effect(mediump)" );
38 const char * const EFFECT_HIGHP_IMAGE( DALI_IMAGE_DIR "icon-highp.png" );
39 const char * const EFFECT_MEDIUMP_IMAGE( DALI_IMAGE_DIR "icon-mediump.png" );
40 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
41 const char * const 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
68 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
69
70 // The duration of the current image staying on screen when slideshow is on
71 const int VIEWINGTIME = 2000; // 2 seconds
72
73 const float TRANSITION_DURATION = 2.5f; //2.5 second
74
75 const float INITIAL_DEPTH = -10.0f;
76
77 /**
78  * @brief Load an image, scaled-down to no more than the stage dimensions.
79  *
80  * Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
81  * load time to cover the entire stage with pixels with no borders,
82  * and filter mode ImageAttributes::BoxThenLinear to sample the image with
83  * maximum quality.
84  */
85 ResourceImage LoadStageFillingImage( const char * const imagePath )
86 {
87   Size stageSize = Stage::GetCurrent().GetSize();
88   ImageAttributes attributes;
89   attributes.SetSize( stageSize.x, stageSize.y );
90   attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
91   attributes.SetScalingMode( ImageAttributes::ScaleToFill );
92   return ResourceImage::New( imagePath, attributes );
93 }
94
95 } // namespace
96
97 class DissolveEffectApp : public ConnectionTracker
98 {
99 public:
100
101   /**
102    * Constructor
103    * @param application class, stored as reference
104    */
105   DissolveEffectApp( Application& application );
106
107   ~DissolveEffectApp();
108
109 private:
110
111   /**
112    * This method gets called once the main loop of application is up and running
113    */
114   void OnInit( Application& application );
115   /**
116    * PanGesture callback. This method gets called when the pan gesture is detected.
117    * @param[in] actor The actor receiving the pan gesture.
118    * @param[in] gesture The detected pan gesture.
119    */
120   void OnPanGesture( Actor actor, const PanGesture& gesture );
121
122   /**
123    * Set up the animations for transition
124    * @param[in] position The point ( locates within rectange {(0,0),(0,1),(1,0),(1,1)} ) passing through the central line of the dissolve effect
125    * @param[in] displacement The direction of the central line of the dissolve effect
126    */
127   void StartTransition(Vector2 position, Vector2 displacement);
128   /**
129    * Callback function of effect-switch button
130    * Change the precision of the effect shader 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(Animation& source);
146   /**
147    * Callback function of timer tick
148    * The timer is used to count the image display duration after cube transition in slideshow,
149    */
150   bool OnTimerTick();
151
152   /**
153    * Main key event handler
154    */
155   void OnKeyEvent(const KeyEvent& event);
156
157 private:
158   Application&                    mApplication;
159   Toolkit::View                   mView;
160   Toolkit::ToolBar                mToolBar;
161   Layer                           mContent;
162   Toolkit::TextLabel              mTitleActor;
163   Actor                           mParent;
164
165   ImageActor                      mCurrentImage;
166   ImageActor                      mNextImage;
167   unsigned int                    mIndex;
168
169   Toolkit::DissolveEffect         mCurrentImageEffect;
170   Toolkit::DissolveEffect         mNextImageEffect;
171   bool                            mUseHighPrecision;
172   Animation                       mAnimation;
173
174   PanGestureDetector              mPanGestureDetector;
175   bool                            mIsTransiting;
176
177   bool                            mSlideshow;
178   Timer                           mViewTimer;
179   bool                            mTimerReady;
180   unsigned int                    mCentralLineIndex;
181
182   Image                           mIconPlay;
183   Image                           mIconStop;
184   Toolkit::PushButton             mPlayStopButton;
185
186   Image                           mIconHighP;
187   Image                           mIconMediumP;
188   Toolkit::PushButton             mEffectChangeButton;
189 };
190
191 DissolveEffectApp::DissolveEffectApp( Application& application )
192 : mApplication( application ),
193   mIndex( 0 ),
194   mUseHighPrecision(true),
195   mIsTransiting( false ),
196   mSlideshow( false ),
197   mTimerReady( false ),
198   mCentralLineIndex( 0 )
199 {
200   mApplication.InitSignal().Connect( this, &DissolveEffectApp::OnInit );
201 }
202
203 DissolveEffectApp::~DissolveEffectApp()
204 {
205   //Nothing to do
206 }
207
208 void DissolveEffectApp::OnInit( Application& application )
209 {
210   DemoHelper::RequestThemeChange();
211
212   Stage::GetCurrent().KeyEventSignal().Connect(this, &DissolveEffectApp::OnKeyEvent);
213
214   // Creates a default view with a default tool bar, the view is added to the stage.
215   mContent = DemoHelper::CreateView( application, mView,mToolBar, "", TOOLBAR_IMAGE, "" );
216
217   // Add an effect-changing button on the right of the tool bar.
218   mIconHighP = ResourceImage::New( EFFECT_HIGHP_IMAGE );
219   mIconMediumP = ResourceImage::New( EFFECT_MEDIUMP_IMAGE );
220   mEffectChangeButton = Toolkit::PushButton::New();
221   mEffectChangeButton.SetBackgroundImage(mIconHighP);
222   mEffectChangeButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnEffectButtonClicked );
223   mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
224
225   // Add title to the tool bar.
226   mTitleActor = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_HIGHP );
227   mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
228
229   // Add an slide-show button on the right of the title
230   mIconPlay = ResourceImage::New( PLAY_ICON );
231   mIconStop = ResourceImage::New( STOP_ICON );
232   mPlayStopButton = Toolkit::PushButton::New();
233   mPlayStopButton.SetBackgroundImage( mIconPlay );
234   mPlayStopButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnSildeshowButtonClicked );
235   mToolBar.AddControl( mPlayStopButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
236
237   // use pan gesture to detect the cursor or finger movement
238   mPanGestureDetector = PanGestureDetector::New();
239   mPanGestureDetector.DetectedSignal().Connect( this, &DissolveEffectApp::OnPanGesture );
240
241   // create the dissolve effect object
242   mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
243   mNextImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
244
245   mViewTimer = Timer::New( VIEWINGTIME );
246   mViewTimer.TickSignal().Connect( this, &DissolveEffectApp::OnTimerTick );
247   mTimerReady = true;
248
249   // Set size to stage size to avoid seeing a black border on transition
250   mParent = Actor::New();
251   mParent.SetSize( Stage::GetCurrent().GetSize() );
252   mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
253   mContent.Add( mParent );
254
255   // show the first image
256   mCurrentImage = ImageActor::New( LoadStageFillingImage( IMAGES[mIndex] ) );
257   mCurrentImage.SetRelayoutEnabled( false );
258   mCurrentImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
259   mCurrentImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
260   mCurrentImage.SetSizeScalePolicy( FIT_WITH_ASPECT_RATIO );
261   mParent.Add( mCurrentImage );
262
263   mPanGestureDetector.Attach( mCurrentImage );
264 }
265
266 // signal handler, called when the pan gesture is detected
267 void DissolveEffectApp::OnPanGesture( Actor actor, const PanGesture& gesture )
268 {
269   // does not response when the animation has not finished
270   if( mIsTransiting || mSlideshow )
271   {
272     return;
273   }
274
275   if( gesture.state == Gesture::Continuing )
276   {
277     if( gesture.displacement.x < 0)
278     {
279       mIndex = (mIndex + 1)%NUM_IMAGES;
280     }
281     else
282     {
283       mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
284     }
285
286     Image image = LoadStageFillingImage( IMAGES[ mIndex ] );
287     mNextImage = ImageActor::New( image );
288     mNextImage.SetRelayoutEnabled( false );
289     mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
290     mNextImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
291     mNextImage.SetSizeScalePolicy( FIT_WITH_ASPECT_RATIO );
292     mNextImage.SetZ(INITIAL_DEPTH);
293     mParent.Add( mNextImage );
294     Vector2 size = Vector2( mCurrentImage.GetCurrentSize() );
295     StartTransition( gesture.position / size, gesture.displacement * Vector2(1.0, size.x/size.y));
296   }
297 }
298
299 void DissolveEffectApp::StartTransition(Vector2 position, Vector2 displacement)
300 {
301   mAnimation = Animation::New(TRANSITION_DURATION);
302
303   mCurrentImageEffect.SetCentralLine(position,displacement);
304   mCurrentImageEffect.SetDistortion(0.0f);
305   mCurrentImage.SetShaderEffect(mCurrentImageEffect);
306   mAnimation.AnimateTo( Property(mCurrentImageEffect, mCurrentImageEffect.GetDistortionPropertyName()), 1.0f, AlphaFunctions::Linear );
307
308   mNextImage.SetOpacity(0.0f);
309   mAnimation.AnimateTo( Property( mNextImage, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunctions::Linear );
310
311   if(mUseHighPrecision)
312   {
313     mNextImageEffect.SetCentralLine(position,-displacement);
314     mNextImageEffect.SetDistortion(1.0f);
315     mNextImage.SetShaderEffect(mNextImageEffect);
316     mAnimation.AnimateTo( Property(mNextImageEffect, mNextImageEffect.GetDistortionPropertyName()), 0.0f, AlphaFunctions::Linear );
317   }
318   else
319   {
320     mAnimation.AnimateTo( Property( mNextImage, Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), AlphaFunctions::Linear );
321   }
322
323   mAnimation.FinishedSignal().Connect( this, &DissolveEffectApp::OnTransitionCompleted );
324   mAnimation.Play();
325   mIsTransiting = true;
326 }
327
328 void DissolveEffectApp::OnKeyEvent(const KeyEvent& event)
329 {
330   if(event.state == KeyEvent::Down)
331   {
332     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
333     {
334       mApplication.Quit();
335     }
336   }
337 }
338
339 bool DissolveEffectApp::OnEffectButtonClicked( Toolkit::Button button )
340 {
341   mUseHighPrecision = !mUseHighPrecision;
342   mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
343   if(mUseHighPrecision)
344   {
345     mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_HIGHP) );
346     mEffectChangeButton.SetBackgroundImage(mIconHighP);
347   }
348   else
349   {
350     mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_MEDIUMP) );
351     mEffectChangeButton.SetBackgroundImage(mIconMediumP);
352   }
353
354   return true;
355 }
356
357 bool DissolveEffectApp::OnSildeshowButtonClicked( Toolkit::Button button )
358 {
359   mSlideshow = !mSlideshow;
360   if( mSlideshow )
361   {
362     mPlayStopButton.SetBackgroundImage( mIconStop );
363     mPanGestureDetector.Detach( mParent );
364     mViewTimer.Start();
365     mTimerReady = false;
366   }
367   else
368   {
369     mPlayStopButton.SetBackgroundImage( mIconPlay );
370     mTimerReady = true;
371     mPanGestureDetector.Attach( mParent );
372   }
373   return true;
374 }
375
376 void DissolveEffectApp::OnTransitionCompleted( Animation& source )
377 {
378   mCurrentImage.RemoveShaderEffect();
379   mNextImage.RemoveShaderEffect();
380   mParent.Remove( mCurrentImage );
381   mPanGestureDetector.Detach( mCurrentImage );
382   mCurrentImage = mNextImage;
383   mPanGestureDetector.Attach( mCurrentImage );
384   mIsTransiting = false;
385
386   if( mSlideshow)
387   {
388     mViewTimer.Start();
389     mTimerReady = false;
390   }
391 }
392
393 bool DissolveEffectApp::OnTimerTick()
394 {
395   mTimerReady = true;
396   if(mSlideshow)
397   {
398     mIndex = (mIndex + 1)%NUM_IMAGES;
399     Image image = LoadStageFillingImage( IMAGES[ mIndex ] );
400     mNextImage = ImageActor::New( image );
401     mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
402     mNextImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
403     mNextImage.SetSizeScalePolicy( FIT_WITH_ASPECT_RATIO );
404     mNextImage.SetZ(INITIAL_DEPTH);
405     mParent.Add( mNextImage );
406     switch( mCentralLineIndex%4 )
407     {
408       case 0:
409       {
410         StartTransition(Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f));
411         break;
412       }
413       case 1:
414       {
415         StartTransition(Vector2(0.5f,0.0f), Vector2(0.0f, 1.0f));
416         break;
417       }
418       case 2:
419       {
420         StartTransition(Vector2(0.0f,0.5f), Vector2(1.0f, 0.0f));
421         break;
422       }
423       default:
424       {
425         StartTransition(Vector2(0.5f,1.0f), Vector2(0.0f, -1.0f));
426         break;
427       }
428
429     }
430     mCentralLineIndex++;
431   }
432   return false;   //return false to stop the timer
433 }
434
435 // Entry point for Linux & Tizen applications
436 int main( int argc, char **argv )
437 {
438   Application application = Application::New( &argc, &argv );
439   DissolveEffectApp test( application );
440   application.MainLoop();
441
442   return 0;
443 }