Merge branch 'new_text' into tizen
[platform/core/uifw/dali-demo.git] / examples / motion-blur / motion-blur-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 #include <sstream>
19 #include <iomanip>
20
21 #include "shared/view.h"
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28
29
30 namespace // unnamed namespace
31 {
32
33 ////////////////////////////////////////////////////
34 //
35 // Demo setup parameters
36 //
37
38 //#define MULTIPLE_MOTION_BLURRED_ACTORS
39 #ifndef MULTIPLE_MOTION_BLURRED_ACTORS
40
41 const float MOTION_BLUR_ACTOR_WIDTH = 256;                                          // actor size on screen
42 const float MOTION_BLUR_ACTOR_HEIGHT = 256;                                         // ""
43
44 #else //#ifndef MULTIPLE_MOTION_BLURRED_ACTORS
45
46 const float MOTION_BLUR_ACTOR_WIDTH = 150;                                          // actor size on screen
47 const float MOTION_BLUR_ACTOR_HEIGHT = 112;                                         // ""
48
49 #endif //#ifndef MULTIPLE_MOTION_BLURRED_ACTORS
50
51
52 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
53
54 const int MOTION_BLUR_NUM_ACTOR_IMAGES = 5;
55 const char* MOTION_BLUR_ACTOR_IMAGE1( DALI_IMAGE_DIR "image-with-border-1.jpg" );
56 const char* MOTION_BLUR_ACTOR_IMAGE2( DALI_IMAGE_DIR "image-with-border-2.jpg" );
57 const char* MOTION_BLUR_ACTOR_IMAGE3( DALI_IMAGE_DIR "image-with-border-3.jpg" );
58 const char* MOTION_BLUR_ACTOR_IMAGE4( DALI_IMAGE_DIR "image-with-border-4.jpg" );
59 const char* MOTION_BLUR_ACTOR_IMAGE5( DALI_IMAGE_DIR "image-with-border-1.jpg" );
60
61 const char* MOTION_BLUR_ACTOR_IMAGES[] = {
62   MOTION_BLUR_ACTOR_IMAGE1,
63   MOTION_BLUR_ACTOR_IMAGE2,
64   MOTION_BLUR_ACTOR_IMAGE3,
65   MOTION_BLUR_ACTOR_IMAGE4,
66   MOTION_BLUR_ACTOR_IMAGE5,
67 };
68
69 const int NUM_ACTOR_ANIMATIONS = 4;
70 const int NUM_CAMERA_ANIMATIONS = 2;
71
72
73 const char* BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "background-default.png";
74
75 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
76 const char* LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
77 const char* APPLICATION_TITLE( "Motion Blur" );
78 const char* EFFECTS_OFF_ICON( DALI_IMAGE_DIR "icon-effects-off.png" );
79 const char* EFFECTS_ON_ICON( DALI_IMAGE_DIR "icon-effects-on.png" );
80
81 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
82
83 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
84 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT( 0.55f, 0.06f, 1.0f );
85 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT( 1.0f, 1.0f, 1.0f );
86
87 // move this button down a bit so it is visible on target and not covered up by toolbar
88 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
89
90 const float ORIENTATION_DURATION = 0.5f;                  ///< Time to rotate to new orientation.
91
92 /**
93  * @brief Load an image, scaled-down to no more than the dimensions passed in.
94  *
95  * Uses ImageAttributes::ShrinkToFit which ensures the resulting image is
96  * smaller than or equal to the specified dimensions while preserving its
97  * original aspect ratio.
98  */
99 ResourceImage LoadImageFittedInBox( const char * const imagePath, uint32_t maxWidth, uint32_t maxHeight )
100 {
101   // Load the image nicely scaled-down to fit within the specified max width and height:
102   ImageAttributes attributes;
103   attributes.SetSize( maxWidth, maxHeight);
104   attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
105   attributes.SetScalingMode( ImageAttributes::ShrinkToFit );
106   return ResourceImage::New( imagePath, attributes );
107 }
108
109 } // unnamed namespace
110
111
112 //
113 class MotionBlurExampleApp : public ConnectionTracker
114 {
115 public:
116
117   /**
118      * DeviceOrientation describes the four different
119      * orientations the device can be in based on accelerometer reports.
120      */
121   enum DeviceOrientation
122   {
123     PORTRAIT = 0,
124     LANDSCAPE = 90,
125     PORTRAIT_INVERSE = 180,
126     LANDSCAPE_INVERSE = 270
127   };
128
129   /**
130    * Constructor
131    * @param application class, stored as reference
132    */
133   MotionBlurExampleApp(Application &app)
134   : mApplication(app),
135     mActorEffectsEnabled(false),
136     mCurrentActorAnimation(0),
137     mCurrentImage(0)
138   {
139     // Connect to the Application's Init signal
140     app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
141   }
142
143   ~MotionBlurExampleApp()
144   {
145     // Nothing to do here; everything gets deleted automatically
146   }
147
148   /**
149    * This method gets called once the main loop of application is up and running
150    */
151   void OnInit(Application& app)
152   {
153     // The Init signal is received once (only) during the Application lifetime
154
155     Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
156
157
158     // Creates a default view with a default tool bar.
159     // The view is added to the stage.
160     mContentLayer = DemoHelper::CreateView( mApplication,
161                                             mView,
162                                             mToolBar,
163                                             BACKGROUND_IMAGE_PATH,
164                                             TOOLBAR_IMAGE,
165                                             APPLICATION_TITLE );
166
167     //Add an effects icon on the right of the title
168     mIconEffectsOff = ResourceImage::New( EFFECTS_OFF_ICON );
169     mIconEffectsOn = ResourceImage::New( EFFECTS_ON_ICON );
170     mActorEffectsButton = Toolkit::PushButton::New();
171     mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
172     mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
173     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
174
175     // Creates a mode button.
176     // Create a effect toggle button. (right of toolbar)
177     Image imageLayout = ResourceImage::New( LAYOUT_IMAGE );
178     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
179     layoutButton.SetBackgroundImage(imageLayout);
180     layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
181     layoutButton.SetLeaveRequired( true );
182     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
183
184     // Input
185     mTapGestureDetector = TapGestureDetector::New();
186     mTapGestureDetector.Attach( mContentLayer );
187     mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
188
189     Dali::Window winHandle = app.GetWindow();
190     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
191     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
192     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
193     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
194
195     // set initial orientation
196     app.GetOrientation().ChangedSignal().Connect( this, &MotionBlurExampleApp::OnOrientationChanged );
197     unsigned int degrees = app.GetOrientation().GetDegrees();
198     Rotate( static_cast< DeviceOrientation >( degrees ) );
199
200
201     ///////////////////////////////////////////////////////
202     //
203     // Motion blurred actor
204     //
205
206     // Scale down actor to fit on very low resolution screens with space to interact:
207     Size stageSize = Stage::GetCurrent().GetSize();
208     mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
209     mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
210
211     Image image = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
212     mMotionBlurImageActor = ImageActor::New(image);
213     mMotionBlurImageActor.SetParentOrigin( ParentOrigin::CENTER );
214     mMotionBlurImageActor.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
215
216     mContentLayer.Add( mMotionBlurImageActor );
217
218     // Create shader used for doing motion blur
219     mMotionBlurEffect = MotionBlurEffect::Apply(mMotionBlurImageActor);
220
221
222 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
223
224     ///////////////////////////////////////////////////////
225     //
226     // Motion blurred actor 2
227     //
228
229     mMotionBlurImageActor2 = ImageActor::New(image);
230     mMotionBlurImageActor2.SetParentOrigin( ParentOrigin::CENTER );
231     mMotionBlurImageActor2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
232     mMotionBlurImageActor2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
233     mMotionBlurImageActor.Add( mMotionBlurImageActor2 );
234
235     // Create shader used for doing motion blur
236     mMotionBlurEffect2 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
237
238     // set actor shader to the blur one
239     mMotionBlurImageActor2.SetShaderEffect( mMotionBlurEffect2 );
240
241
242     ///////////////////////////////////////////////////////
243     //
244     // Motion blurred actor 3
245     //
246
247     mMotionBlurImageActor3 = ImageActor::New(image);
248     mMotionBlurImageActor3.SetParentOrigin( ParentOrigin::CENTER );
249     mMotionBlurImageActor3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
250     mMotionBlurImageActor3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
251     mMotionBlurImageActor.Add( mMotionBlurImageActor3 );
252
253     // Create shader used for doing motion blur
254     mMotionBlurEffect3 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
255
256     // set actor shader to the blur one
257     mMotionBlurImageActor3.SetShaderEffect( mMotionBlurEffect3 );
258
259
260     ///////////////////////////////////////////////////////
261     //
262     // Motion blurred actor 4
263     //
264
265     mMotionBlurImageActor4 = ImageActor::New(image);
266     mMotionBlurImageActor4.SetParentOrigin( ParentOrigin::CENTER );
267     mMotionBlurImageActor4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
268     mMotionBlurImageActor4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
269     mMotionBlurImageActor.Add( mMotionBlurImageActor4 );
270
271     // Create shader used for doing motion blur
272     mMotionBlurEffect4 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
273
274     // set actor shader to the blur one
275     mMotionBlurImageActor4.SetShaderEffect( mMotionBlurEffect4 );
276
277
278     ///////////////////////////////////////////////////////
279     //
280     // Motion blurred actor 5
281     //
282
283     mMotionBlurImageActor5 = ImageActor::New(image);
284     mMotionBlurImageActor5.SetParentOrigin( ParentOrigin::CENTER );
285     mMotionBlurImageActor5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
286     mMotionBlurImageActor5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
287     mMotionBlurImageActor.Add( mMotionBlurImageActor5 );
288
289     // Create shader used for doing motion blur
290     mMotionBlurEffect5 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
291
292     // set actor shader to the blur one
293     mMotionBlurImageActor5.SetShaderEffect( mMotionBlurEffect5 );
294 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
295   }
296
297   //////////////////////////////////////////////////////////////
298   //
299   // Device Orientation Support
300   //
301   //
302
303   void OnOrientationChanged( Orientation orientation )
304   {
305     unsigned int degrees = orientation.GetDegrees();
306     Rotate( static_cast< DeviceOrientation >( degrees ) );
307   }
308
309   void Rotate( DeviceOrientation orientation )
310   {
311     // Resize the root actor
312     Vector2 stageSize = Stage::GetCurrent().GetSize();
313     Vector2 targetSize = stageSize;
314     if( orientation == LANDSCAPE ||
315         orientation == LANDSCAPE_INVERSE )
316     {
317       targetSize = Vector2( stageSize.y, stageSize.x );
318     }
319
320     if( mOrientation != orientation )
321     {
322       mOrientation = orientation;
323
324       // check if actor is on stage
325       if( mView.GetParent() )
326       {
327         // has parent so we expect it to be on stage, start animation
328         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
329         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunctions::EaseOut );
330         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
331         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
332         mRotateAnimation.Play();
333       }
334       else
335       {
336         // set the rotation to match the orientation
337         mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
338         mView.SetSize( targetSize );
339       }
340     }
341     else
342     {
343       // for first time just set size
344       mView.SetSize( targetSize );
345     }
346   }
347
348
349   //////////////////////////////////////////////////////////////
350   //
351   // Actor Animation
352   //
353   //
354
355   // move to point on screen that was tapped
356   void OnTap( Actor actor, const TapGesture& tapGesture )
357   {
358     Vector3 destPos;
359     float originOffsetX, originOffsetY;
360
361     // rotate offset (from top left origin to centre) into actor space
362     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
363     actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
364
365     // get dest point in local actor space
366     destPos.x = tapGesture.localPoint.x - originOffsetX;
367     destPos.y = tapGesture.localPoint.y - originOffsetY;
368     destPos.z = 0.0f;
369
370     float animDuration = 0.5f;
371     mActorTapMovementAnimation = Animation::New( animDuration );
372     if ( mMotionBlurImageActor )
373     {
374       mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageActor, Actor::Property::POSITION), destPos, AlphaFunctions::EaseInOutSine, TimePeriod(animDuration) );
375     }
376     mActorTapMovementAnimation.SetEndAction( Animation::Bake );
377     mActorTapMovementAnimation.Play();
378
379
380     // perform some spinning etc
381     if(mActorEffectsEnabled)
382     {
383       switch(mCurrentActorAnimation)
384       {
385         // spin around y
386         case 0:
387         {
388           float animDuration = 1.0f;
389           mActorAnimation = Animation::New(animDuration);
390           mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunctions::EaseInOut );
391           mActorAnimation.SetEndAction( Animation::Bake );
392           mActorAnimation.Play();
393         }
394         break;
395
396         // spin around z
397         case 1:
398         {
399           float animDuration = 1.0f;
400           mActorAnimation = Animation::New(animDuration);
401           mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunctions::EaseInOut );
402           mActorAnimation.SetEndAction( Animation::Bake );
403           mActorAnimation.Play();
404         }
405         break;
406
407         // spin around y and z
408         case 2:
409         {
410           float animDuration = 1.0f;
411           mActorAnimation = Animation::New(animDuration);
412           mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunctions::EaseInOut );
413           mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunctions::EaseInOut );
414           mActorAnimation.SetEndAction( Animation::Bake );
415           mActorAnimation.Play();
416         }
417         break;
418
419         // scale
420         case 3:
421         {
422           float animDuration = 1.0f;
423           mActorAnimation = Animation::New(animDuration);
424           mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunctions::Bounce, TimePeriod( 0.0f, 1.0f ) );
425           mActorAnimation.SetEndAction( Animation::Bake );
426           mActorAnimation.Play();
427         }
428         break;
429
430         default:
431           break;
432       }
433
434       mCurrentActorAnimation++;
435       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
436       {
437         mCurrentActorAnimation = 0;
438       }
439     }
440   }
441
442   void ToggleActorEffects()
443   {
444     if(!mActorEffectsEnabled)
445     {
446       mActorEffectsEnabled = true;
447       mActorEffectsButton.SetBackgroundImage( mIconEffectsOn );
448     }
449     else
450     {
451       mActorEffectsEnabled = false;
452       mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
453     }
454   }
455
456   //////////////////////////////////////////////////////////////
457   //
458   // Input handlers
459   //
460   //
461
462   bool OnLayoutButtonClicked( Toolkit::Button button )
463   {
464     ChangeImage();
465     return true;
466   }
467
468   bool OnEffectButtonClicked( Toolkit::Button button )
469   {
470     ToggleActorEffects();
471     return true;
472   }
473
474   /**
475    * Main key event handler
476    */
477   void OnKeyEvent(const KeyEvent& event)
478   {
479     if(event.state == KeyEvent::Down)
480     {
481       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
482       {
483         mApplication.Quit();
484       }
485     }
486   }
487
488   //////////////////////////////////////////////////////////////
489   //
490   // Misc
491   //
492   //
493
494
495   void ChangeImage()
496   {
497     mCurrentImage++;
498     if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
499     {
500       mCurrentImage = 0;
501     }
502
503     Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
504     mMotionBlurImageActor.SetImage(blurImage);
505   }
506
507
508 private:
509   Application&               mApplication;            ///< Application instance
510   Toolkit::View              mView;
511   Toolkit::ToolBar           mToolBar;
512   Image                      mIconEffectsOff;
513   Image                      mIconEffectsOn;
514
515   Layer                      mContentLayer;           ///< Content layer (contains actor for this blur demo)
516
517   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
518
519   // Motion blur
520   MotionBlurEffect mMotionBlurEffect;
521   ImageActor mMotionBlurImageActor;
522   Size mMotionBlurActorSize;
523
524 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
525   MotionBlurEffect mMotionBlurEffect2;
526   MotionBlurEffect mMotionBlurEffect3;
527   MotionBlurEffect mMotionBlurEffect4;
528   MotionBlurEffect mMotionBlurEffect5;
529
530   ImageActor mMotionBlurImageActor2;
531   ImageActor mMotionBlurImageActor3;
532   ImageActor mMotionBlurImageActor4;
533   ImageActor mMotionBlurImageActor5;
534 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
535
536   // animate actor to position where user taps screen
537   Animation mActorTapMovementAnimation;
538
539   // show different animations to demonstrate blur effect working on an object only movement basis
540   bool mActorEffectsEnabled;
541   Animation mActorAnimation;
542   int mCurrentActorAnimation;
543
544   // offer a selection of images that user can cycle between
545   int mCurrentImage;
546
547   TapGestureDetector mTapGestureDetector;
548
549   DeviceOrientation mOrientation;               ///< Current Device orientation
550   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
551
552   Popup mInstructionsPopup;                     ///< Info Popup
553 };
554
555 void RunTest(Application& app)
556 {
557   MotionBlurExampleApp test(app);
558
559   app.MainLoop();
560 }
561
562 // Entry point for Linux & Tizen applications
563 //
564 int main(int argc, char **argv)
565 {
566   Application app = Application::New(&argc, &argv);
567
568   RunTest(app);
569
570   return 0;
571 }