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