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