Merge "Change a label of check box button" into 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 // LOCAL STUFF
30 namespace
31 {
32
33 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
34 const char * const APPLICATION_TITLE_HIGHP( "Dissolve Effect(highp)" );
35 const char * const APPLICATION_TITLE_MEDIUMP( "Dissolve Effect(mediump)" );
36 const char * const EFFECT_HIGHP_IMAGE( DALI_IMAGE_DIR "icon-highp.png" );
37 const char * const EFFECT_MEDIUMP_IMAGE( DALI_IMAGE_DIR "icon-mediump.png" );
38 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
39 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
40
41 const char* IMAGES[] =
42 {
43   DALI_IMAGE_DIR "gallery-large-1.jpg",
44   DALI_IMAGE_DIR "gallery-large-2.jpg",
45   DALI_IMAGE_DIR "gallery-large-3.jpg",
46   DALI_IMAGE_DIR "gallery-large-4.jpg",
47   DALI_IMAGE_DIR "gallery-large-5.jpg",
48   DALI_IMAGE_DIR "gallery-large-6.jpg",
49   DALI_IMAGE_DIR "gallery-large-7.jpg",
50   DALI_IMAGE_DIR "gallery-large-8.jpg",
51   DALI_IMAGE_DIR "gallery-large-9.jpg",
52   DALI_IMAGE_DIR "gallery-large-10.jpg",
53   DALI_IMAGE_DIR "gallery-large-11.jpg",
54   DALI_IMAGE_DIR "gallery-large-12.jpg",
55   DALI_IMAGE_DIR "gallery-large-13.jpg",
56   DALI_IMAGE_DIR "gallery-large-14.jpg",
57   DALI_IMAGE_DIR "gallery-large-15.jpg",
58   DALI_IMAGE_DIR "gallery-large-16.jpg",
59   DALI_IMAGE_DIR "gallery-large-17.jpg",
60   DALI_IMAGE_DIR "gallery-large-18.jpg",
61   DALI_IMAGE_DIR "gallery-large-19.jpg",
62   DALI_IMAGE_DIR "gallery-large-20.jpg",
63   DALI_IMAGE_DIR "gallery-large-21.jpg",
64 };
65
66 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
67
68 // The duration of the current image staying on screen when slideshow is on
69 const int VIEWINGTIME = 2000; // 2 seconds
70
71 const float TRANSITION_DURATION = 2.5f; //2.5 second
72
73 const float INITIAL_DEPTH = -10.0f;
74 } // namespace
75
76 class DissolveEffectApp : public ConnectionTracker
77 {
78 public:
79
80   /**
81    * Constructor
82    * @param application class, stored as reference
83    */
84   DissolveEffectApp( Application& application );
85
86   ~DissolveEffectApp();
87
88 private:
89
90   /**
91    * This method gets called once the main loop of application is up and running
92    */
93   void OnInit( Application& application );
94   /**
95    * PanGesture callback. This method gets called when the pan gesture is detected.
96    * @param[in] actor The actor receiving the pan gesture.
97    * @param[in] gesture The detected pan gesture.
98    */
99   void OnPanGesture( Actor actor, const PanGesture& gesture );
100
101   /**
102    * Set up the animations for transition
103    * @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
104    * @param[in] displacement The direction of the central line of the dissolve effect
105    */
106   void StartTransition(Vector2 position, Vector2 displacement);
107   /**
108    * Callback function of effect-switch button
109    * Change the precision of the effect shader when the effect button is clicked
110    * @param[in] button The handle of the clicked button
111    */
112   bool OnEffectButtonClicked( Toolkit::Button button );
113   /**
114    * Callback function of slideshow button
115    * Start or stop the automatical image display when the slideshow button is clicked
116    * @param[in] button The handle of the clicked button
117    */
118   bool OnSildeshowButtonClicked( Toolkit::Button button );
119   /**
120    * Callback function of cube transition completed signal
121    * @param[in] effect The cube effect used for the transition
122    * @param[in] imageActor The target imageActor of the completed transition
123    */
124   void OnTransitionCompleted(Animation& source);
125   /**
126    * Callback function of timer tick
127    * The timer is used to count the image display duration after cube transition in slideshow,
128    */
129   bool OnTimerTick();
130
131   /**
132    * Main key event handler
133    */
134   void OnKeyEvent(const KeyEvent& event);
135
136 private:
137   Application&                    mApplication;
138   Toolkit::View                   mView;
139   Toolkit::ToolBar                mToolBar;
140   Layer                           mContent;
141   Toolkit::TextView               mTitleActor;
142   Actor                           mParent;
143
144   ImageActor                      mCurrentImage;
145   ImageActor                      mNextImage;
146   unsigned int                    mIndex;
147   Constraint                      mSizeConstraint;
148
149   Toolkit::DissolveEffect         mCurrentImageEffect;
150   Toolkit::DissolveEffect         mNextImageEffect;
151   bool                            mUseHighPrecision;
152   Animation                       mAnimation;
153
154   PanGestureDetector              mPanGestureDetector;
155   bool                            mIsTransiting;
156
157   bool                            mSlideshow;
158   Timer                           mViewTimer;
159   bool                            mTimerReady;
160   unsigned int                    mCentralLineIndex;
161
162   Image                           mIconPlay;
163   Image                           mIconStop;
164   Toolkit::PushButton             mPlayStopButton;
165
166   Image                           mIconHighP;
167   Image                           mIconMediumP;
168   Toolkit::PushButton             mEffectChangeButton;
169 };
170
171 DissolveEffectApp::DissolveEffectApp( Application& application )
172 : mApplication( application ),
173   mIndex( 0 ),
174   mUseHighPrecision(true),
175   mIsTransiting( false ),
176   mSlideshow( false ),
177   mTimerReady( false ),
178   mCentralLineIndex( 0 )
179 {
180   mApplication.InitSignal().Connect( this, &DissolveEffectApp::OnInit );
181 }
182
183 DissolveEffectApp::~DissolveEffectApp()
184 {
185   //Nothing to do
186 }
187
188 void DissolveEffectApp::OnInit( Application& application )
189 {
190   Stage::GetCurrent().KeyEventSignal().Connect(this, &DissolveEffectApp::OnKeyEvent);
191
192   // Creates a default view with a default tool bar, the view is added to the stage.
193   mContent = DemoHelper::CreateView( application, mView,mToolBar, "", TOOLBAR_IMAGE, "" );
194
195   // Add an effect-changing button on the right of the tool bar.
196   mIconHighP = ResourceImage::New( EFFECT_HIGHP_IMAGE );
197   mIconMediumP = ResourceImage::New( EFFECT_MEDIUMP_IMAGE );
198   mEffectChangeButton = Toolkit::PushButton::New();
199   mEffectChangeButton.SetBackgroundImage(mIconHighP);
200   mEffectChangeButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnEffectButtonClicked );
201   mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
202
203   // Add title to the tool bar.
204   mTitleActor = Toolkit::TextView::New();
205   mTitleActor.SetText( APPLICATION_TITLE_HIGHP );
206   mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
207   mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
208
209   // Add an slide-show button on the right of the title
210   mIconPlay = ResourceImage::New( PLAY_ICON );
211   mIconStop = ResourceImage::New( STOP_ICON );
212   mPlayStopButton = Toolkit::PushButton::New();
213   mPlayStopButton.SetBackgroundImage( mIconPlay );
214   mPlayStopButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnSildeshowButtonClicked );
215   mToolBar.AddControl( mPlayStopButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
216
217   // use pan gesture to detect the cursor or finger movement
218   mPanGestureDetector = PanGestureDetector::New();
219   mPanGestureDetector.DetectedSignal().Connect( this, &DissolveEffectApp::OnPanGesture );
220
221   // create the dissolve effect object
222   mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
223   mNextImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
224
225   mViewTimer = Timer::New( VIEWINGTIME );
226   mViewTimer.TickSignal().Connect( this, &DissolveEffectApp::OnTimerTick );
227   mTimerReady = true;
228
229   // Set size to stage size to avoid seeing a black border on transition
230   mParent = Actor::New();
231   mParent.SetSize( Stage::GetCurrent().GetSize() );
232   mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
233   mContent.Add( mParent );
234
235   mSizeConstraint= Constraint::New<Vector3>( Actor::SCALE, LocalSource( Actor::SIZE ), ParentSource( Actor::SIZE ), ScaleToFitKeepAspectRatioConstraint() );
236
237   // show the first image
238   mCurrentImage = ImageActor::New( ResourceImage::New( IMAGES[mIndex] ) );
239   mCurrentImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
240   mCurrentImage.ApplyConstraint( mSizeConstraint );
241   mParent.Add( mCurrentImage );
242
243   mPanGestureDetector.Attach( mCurrentImage );
244 }
245
246 // signal handler, called when the pan gesture is detected
247 void DissolveEffectApp::OnPanGesture( Actor actor, const PanGesture& gesture )
248 {
249   // does not response when the animation has not finished
250   if( mIsTransiting || mSlideshow )
251   {
252     return;
253   }
254
255   if( gesture.state == Gesture::Continuing )
256   {
257     if( gesture.displacement.x < 0)
258     {
259       mIndex = (mIndex + 1)%NUM_IMAGES;
260     }
261     else
262     {
263       mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
264     }
265
266     Image image = ResourceImage::New( IMAGES[ mIndex ] );
267     mNextImage = ImageActor::New( image );
268     mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
269     mNextImage.ApplyConstraint( mSizeConstraint );
270     mNextImage.SetZ(INITIAL_DEPTH);
271     mParent.Add( mNextImage );
272     Vector2 size = Vector2( mCurrentImage.GetCurrentSize() );
273     StartTransition( gesture.position / size, gesture.displacement * Vector2(1.0, size.x/size.y));
274   }
275 }
276
277 void DissolveEffectApp::StartTransition(Vector2 position, Vector2 displacement)
278 {
279   mAnimation = Animation::New(TRANSITION_DURATION);
280
281   mCurrentImageEffect.SetCentralLine(position,displacement);
282   mCurrentImageEffect.SetDistortion(0.0f);
283   mCurrentImage.SetShaderEffect(mCurrentImageEffect);
284   mAnimation.AnimateTo( Property(mCurrentImageEffect, mCurrentImageEffect.GetDistortionPropertyName()), 1.0f, AlphaFunctions::Linear );
285
286   mNextImage.SetOpacity(0.0f);
287   mAnimation.OpacityTo( mNextImage, 1.0, AlphaFunctions::Linear );
288
289   if(mUseHighPrecision)
290   {
291     mNextImageEffect.SetCentralLine(position,-displacement);
292     mNextImageEffect.SetDistortion(1.0f);
293     mNextImage.SetShaderEffect(mNextImageEffect);
294     mAnimation.AnimateTo( Property(mNextImageEffect, mNextImageEffect.GetDistortionPropertyName()), 0.0f, AlphaFunctions::Linear );
295   }
296   else
297   {
298     mAnimation.MoveTo(mNextImage, Vector3(0.0f, 0.0f, 0.0f), AlphaFunctions::Linear);
299   }
300
301   mAnimation.FinishedSignal().Connect( this, &DissolveEffectApp::OnTransitionCompleted );
302   mAnimation.Play();
303   mIsTransiting = true;
304 }
305
306 void DissolveEffectApp::OnKeyEvent(const KeyEvent& event)
307 {
308   if(event.state == KeyEvent::Down)
309   {
310     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
311     {
312       mApplication.Quit();
313     }
314   }
315 }
316
317 bool DissolveEffectApp::OnEffectButtonClicked( Toolkit::Button button )
318 {
319   mUseHighPrecision = !mUseHighPrecision;
320   mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
321   if(mUseHighPrecision)
322   {
323     mTitleActor.SetText( APPLICATION_TITLE_HIGHP );
324     mEffectChangeButton.SetBackgroundImage(mIconHighP);
325   }
326   else
327   {
328     mTitleActor.SetText( APPLICATION_TITLE_MEDIUMP );
329     mEffectChangeButton.SetBackgroundImage(mIconMediumP);
330   }
331   mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
332
333   return true;
334 }
335
336 bool DissolveEffectApp::OnSildeshowButtonClicked( Toolkit::Button button )
337 {
338   mSlideshow = !mSlideshow;
339   if( mSlideshow )
340   {
341     mPlayStopButton.SetBackgroundImage( mIconStop );
342     mPanGestureDetector.Detach( mParent );
343     mViewTimer.Start();
344     mTimerReady = false;
345   }
346   else
347   {
348     mPlayStopButton.SetBackgroundImage( mIconPlay );
349     mTimerReady = true;
350     mPanGestureDetector.Attach( mParent );
351   }
352   return true;
353 }
354
355 void DissolveEffectApp::OnTransitionCompleted( Animation& source )
356 {
357   mCurrentImage.RemoveShaderEffect();
358   mNextImage.RemoveShaderEffect();
359   mParent.Remove( mCurrentImage );
360   mPanGestureDetector.Detach( mCurrentImage );
361   mCurrentImage = mNextImage;
362   mPanGestureDetector.Attach( mCurrentImage );
363   mIsTransiting = false;
364
365   if( mSlideshow)
366   {
367     mViewTimer.Start();
368     mTimerReady = false;
369   }
370 }
371
372 bool DissolveEffectApp::OnTimerTick()
373 {
374   mTimerReady = true;
375   if(mSlideshow)
376   {
377     mIndex = (mIndex + 1)%NUM_IMAGES;
378     Image image = ResourceImage::New( IMAGES[ mIndex ] );
379     mNextImage = ImageActor::New( image );
380     mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
381     mNextImage.ApplyConstraint( mSizeConstraint );
382     mNextImage.SetZ(INITIAL_DEPTH);
383     mParent.Add( mNextImage );
384     switch( mCentralLineIndex%4 )
385     {
386       case 0:
387       {
388         StartTransition(Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f));
389         break;
390       }
391       case 1:
392       {
393         StartTransition(Vector2(0.5f,0.0f), Vector2(0.0f, 1.0f));
394         break;
395       }
396       case 2:
397       {
398         StartTransition(Vector2(0.0f,0.5f), Vector2(1.0f, 0.0f));
399         break;
400       }
401       default:
402       {
403         StartTransition(Vector2(0.5f,1.0f), Vector2(0.0f, -1.0f));
404         break;
405       }
406
407     }
408     mCentralLineIndex++;
409   }
410   return false;   //return false to stop the timer
411 }
412
413 // Entry point for Linux & SLP applications
414 int main( int argc, char **argv )
415 {
416   Application application = Application::New( &argc, &argv );
417   DissolveEffectApp test( application );
418   application.MainLoop();
419
420   return 0;
421 }