DALi Version 1.9.28
[platform/core/uifw/dali-demo.git] / examples / motion-blur / motion-blur-example.cpp
1 /*
2  * Copyright (c) 2020 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/devel-api/actors/actor-devel.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/shader-effects/motion-blur-effect.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 const float MOTION_BLUR_ACTOR_WIDTH = 256;                                          // actor size on screen
41 const float MOTION_BLUR_ACTOR_HEIGHT = 256;                                         // ""
42 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
43
44 const int MOTION_BLUR_NUM_ACTOR_IMAGES = 5;
45 const char* MOTION_BLUR_ACTOR_IMAGE1( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
46 const char* MOTION_BLUR_ACTOR_IMAGE2( DEMO_IMAGE_DIR "image-with-border-2.jpg" );
47 const char* MOTION_BLUR_ACTOR_IMAGE3( DEMO_IMAGE_DIR "image-with-border-3.jpg" );
48 const char* MOTION_BLUR_ACTOR_IMAGE4( DEMO_IMAGE_DIR "image-with-border-4.jpg" );
49 const char* MOTION_BLUR_ACTOR_IMAGE5( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
50
51 const char* MOTION_BLUR_ACTOR_IMAGES[] = {
52   MOTION_BLUR_ACTOR_IMAGE1,
53   MOTION_BLUR_ACTOR_IMAGE2,
54   MOTION_BLUR_ACTOR_IMAGE3,
55   MOTION_BLUR_ACTOR_IMAGE4,
56   MOTION_BLUR_ACTOR_IMAGE5,
57 };
58
59 const int NUM_ACTOR_ANIMATIONS = 4;
60 const int NUM_CAMERA_ANIMATIONS = 2;
61
62
63 const char* BACKGROUND_IMAGE_PATH = DEMO_IMAGE_DIR "background-default.png";
64
65 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
66 const char* LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
67 const char* LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
68 const char* APPLICATION_TITLE( "Motion Blur" );
69 const char* EFFECTS_OFF_ICON( DEMO_IMAGE_DIR "icon-effects-off.png" );
70 const char* EFFECTS_OFF_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-off-selected.png" );
71 const char* EFFECTS_ON_ICON( DEMO_IMAGE_DIR "icon-effects-on.png" );
72 const char* EFFECTS_ON_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-on-selected.png" );
73
74 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
75
76 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
77 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT( 0.55f, 0.06f, 1.0f );
78 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT( 1.0f, 1.0f, 1.0f );
79
80 // move this button down a bit so it is visible on target and not covered up by toolbar
81 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
82
83 const float ORIENTATION_DURATION = 0.5f;                  ///< Time to rotate to new orientation.
84
85 /**
86  * @brief Set an image to image view, scaled-down to no more than the dimensions passed in.
87  *
88  * Uses SHRINK_TO_FIT which ensures the resulting image is
89  * smaller than or equal to the specified dimensions while preserving its original aspect ratio.
90  */
91 void SetImageFittedInBox( ImageView& imageView, Property::Map& shaderEffect, const char * const imagePath, int maxWidth, int maxHeight )
92 {
93   Property::Map map;
94   map[Visual::Property::TYPE] = Visual::IMAGE;
95   map[ImageVisual::Property::URL] = imagePath;
96   // Load the image nicely scaled-down to fit within the specified max width and height:
97   map[ImageVisual::Property::DESIRED_WIDTH] = maxWidth;
98   map[ImageVisual::Property::DESIRED_HEIGHT] = maxHeight;
99   map[ImageVisual::Property::FITTING_MODE] = FittingMode::SHRINK_TO_FIT;
100   map[ImageVisual::Property::SAMPLING_MODE] = SamplingMode::BOX_THEN_LINEAR;
101   map.Merge( shaderEffect );
102
103   imageView.SetProperty( ImageView::Property::IMAGE, map );
104 }
105
106 } // unnamed namespace
107
108
109 //
110 class MotionBlurExampleApp : public ConnectionTracker
111 {
112 public:
113
114   /**
115      * DeviceOrientation describes the four different
116      * orientations the device can be in based on accelerometer reports.
117      */
118   enum DeviceOrientation
119   {
120     PORTRAIT = 0,
121     LANDSCAPE = 90,
122     PORTRAIT_INVERSE = 180,
123     LANDSCAPE_INVERSE = 270
124   };
125
126   /**
127    * Constructor
128    * @param application class, stored as reference
129    */
130   MotionBlurExampleApp(Application &app)
131   : mApplication(app),
132     mActorEffectsEnabled(false),
133     mCurrentActorAnimation(0),
134     mCurrentImage(0),
135     mOrientation( PORTRAIT )
136   {
137     // Connect to the Application's Init signal
138     app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
139   }
140
141   ~MotionBlurExampleApp()
142   {
143     // Nothing to do here; everything gets deleted automatically
144   }
145
146   /**
147    * This method gets called once the main loop of application is up and running
148    */
149   void OnInit(Application& app)
150   {
151     // The Init signal is received once (only) during the Application lifetime
152     Window window = app.GetWindow();
153
154     window.KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
155
156
157     // Creates a default view with a default tool bar.
158     // The view is added to the window.
159     mContentLayer = DemoHelper::CreateView( mApplication,
160                                             mView,
161                                             mToolBar,
162                                             BACKGROUND_IMAGE_PATH,
163                                             TOOLBAR_IMAGE,
164                                             APPLICATION_TITLE );
165
166     // Ensure the content layer is a square so the touch area works in all orientations
167     Vector2 windowSize = window.GetSize();
168     float size = std::max( windowSize.width, windowSize.height );
169     mContentLayer.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
170
171     //Add an effects icon on the right of the title
172     mActorEffectsButton = Toolkit::PushButton::New();
173     mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
174     mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
175     mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
176     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_CENTER, DemoHelper::DEFAULT_PLAY_PADDING );
177
178     // Creates a mode button.
179     // Create a effect toggle button. (right of toolbar)
180     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
181     layoutButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
182     layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
183     layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
184     layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
185     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
186
187     // Input
188     mTapGestureDetector = TapGestureDetector::New();
189     mTapGestureDetector.Attach( mContentLayer );
190     mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
191
192     Dali::Window winHandle = app.GetWindow();
193     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
194     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
195     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
196     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
197     winHandle.ResizeSignal().Connect( this, &MotionBlurExampleApp::OnWindowResized );
198
199     // set initial orientation
200     Rotate( PORTRAIT );
201
202     ///////////////////////////////////////////////////////
203     //
204     // Motion blurred actor
205     //
206
207     // Scale down actor to fit on very low resolution screens with space to interact:
208     mMotionBlurActorSize = Size( std::min( windowSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( windowSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
209     mMotionBlurActorUpdateSize = Size( std::max( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::max( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
210     mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
211
212     mMotionBlurEffect = CreateMotionBlurEffect();
213     mMotionBlurImageView = ImageView::New();
214     SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
215     mMotionBlurImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
216     mMotionBlurImageView.SetProperty( Actor::Property::SIZE, mMotionBlurActorUpdateSize );
217     mMotionBlurImageView.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, mMotionBlurActorUpdateSize );
218
219     mContentLayer.Add( mMotionBlurImageView );
220
221     // Create shader used for doing motion blur
222     mMotionBlurEffect = CreateMotionBlurEffect();
223
224     // set actor shader to the blur one
225     Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
226
227   }
228
229   //////////////////////////////////////////////////////////////
230   //
231   // Device Orientation Support
232   //
233   //
234
235   void OnWindowResized( Window window, Window::WindowSize size )
236   {
237     Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
238   }
239
240   void Rotate( DeviceOrientation orientation )
241   {
242     // Resize the root actor
243     const Vector2 targetSize = mApplication.GetWindow().GetSize();
244
245     if( mOrientation != orientation )
246     {
247       mOrientation = orientation;
248
249       // check if actor is on window
250       if( mView.GetParent() )
251       {
252         // has parent so we expect it to be on window, start animation
253         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
254         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
255         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
256         mRotateAnimation.Play();
257       }
258       else
259       {
260         mView.SetProperty( Actor::Property::SIZE, targetSize );
261       }
262     }
263     else
264     {
265       // for first time just set size
266       mView.SetProperty( Actor::Property::SIZE, targetSize );
267     }
268   }
269
270
271   //////////////////////////////////////////////////////////////
272   //
273   // Actor Animation
274   //
275   //
276
277   // move to point on screen that was tapped
278   void OnTap( Actor actor, const TapGesture& tapGesture )
279   {
280     Vector3 destPos;
281     float originOffsetX, originOffsetY;
282
283     // rotate offset (from top left origin to centre) into actor space
284     Vector2 windowSize = mApplication.GetWindow().GetSize();
285     actor.ScreenToLocal(originOffsetX, originOffsetY, windowSize.width * 0.5f, windowSize.height * 0.5f);
286
287     // get dest point in local actor space
288     const Vector2& localPoint = tapGesture.GetLocalPoint();
289     destPos.x = localPoint.x - originOffsetX;
290     destPos.y = localPoint.y - originOffsetY;
291     destPos.z = 0.0f;
292
293     float animDuration = 0.5f;
294     mActorTapMovementAnimation = Animation::New( animDuration );
295     if ( mMotionBlurImageView )
296     {
297       mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
298     }
299     mActorTapMovementAnimation.SetEndAction( Animation::BAKE );
300     mActorTapMovementAnimation.Play();
301
302
303     // perform some spinning etc
304     if(mActorEffectsEnabled)
305     {
306       switch(mCurrentActorAnimation)
307       {
308         // spin around y
309         case 0:
310         {
311           float animDuration = 1.0f;
312           mActorAnimation = Animation::New(animDuration);
313           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
314           mActorAnimation.SetEndAction( Animation::BAKE );
315           mActorAnimation.Play();
316         }
317         break;
318
319         // spin around z
320         case 1:
321         {
322           float animDuration = 1.0f;
323           mActorAnimation = Animation::New(animDuration);
324           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
325           mActorAnimation.SetEndAction( Animation::BAKE );
326           mActorAnimation.Play();
327         }
328         break;
329
330         // spin around y and z
331         case 2:
332         {
333           float animDuration = 1.0f;
334           mActorAnimation = Animation::New(animDuration);
335           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
336           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
337           mActorAnimation.SetEndAction( Animation::BAKE );
338           mActorAnimation.Play();
339         }
340         break;
341
342         // scale
343         case 3:
344         {
345           float animDuration = 1.0f;
346           mActorAnimation = Animation::New(animDuration);
347           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
348           mActorAnimation.SetEndAction( Animation::BAKE );
349           mActorAnimation.Play();
350         }
351         break;
352
353         default:
354           break;
355       }
356
357       mCurrentActorAnimation++;
358       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
359       {
360         mCurrentActorAnimation = 0;
361       }
362     }
363   }
364
365   void ToggleActorEffects()
366   {
367     if(!mActorEffectsEnabled)
368     {
369       mActorEffectsEnabled = true;
370       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
371       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
372     }
373     else
374     {
375       mActorEffectsEnabled = false;
376       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
377       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
378     }
379   }
380
381   //////////////////////////////////////////////////////////////
382   //
383   // Input handlers
384   //
385   //
386
387   bool OnLayoutButtonClicked( Toolkit::Button button )
388   {
389     ChangeImage();
390     return true;
391   }
392
393   bool OnEffectButtonClicked( Toolkit::Button button )
394   {
395     ToggleActorEffects();
396     return true;
397   }
398
399   /**
400    * Main key event handler
401    */
402   void OnKeyEvent(const KeyEvent& event)
403   {
404     if(event.GetState() == KeyEvent::DOWN)
405     {
406       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
407       {
408         mApplication.Quit();
409       }
410     }
411   }
412
413   //////////////////////////////////////////////////////////////
414   //
415   // Misc
416   //
417   //
418
419
420   void ChangeImage()
421   {
422     mCurrentImage++;
423     if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
424     {
425       mCurrentImage = 0;
426     }
427     SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
428
429   }
430
431
432 private:
433   Application&               mApplication;            ///< Application instance
434   Toolkit::Control           mView;
435   Toolkit::ToolBar           mToolBar;
436
437   Layer                      mContentLayer;           ///< Content layer (contains actor for this blur demo)
438
439   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
440
441   // Motion blur
442   Property::Map mMotionBlurEffect;
443   ImageView mMotionBlurImageView;
444   Size mMotionBlurActorSize;
445   Size mMotionBlurActorUpdateSize;
446
447   // animate actor to position where user taps screen
448   Animation mActorTapMovementAnimation;
449
450   // show different animations to demonstrate blur effect working on an object only movement basis
451   bool mActorEffectsEnabled;
452   Animation mActorAnimation;
453   int mCurrentActorAnimation;
454
455   // offer a selection of images that user can cycle between
456   int mCurrentImage;
457
458   TapGestureDetector mTapGestureDetector;
459
460   DeviceOrientation mOrientation;               ///< Current Device orientation
461   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
462 };
463
464 int DALI_EXPORT_API main(int argc, char **argv)
465 {
466   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
467   MotionBlurExampleApp test(app);
468   app.MainLoop();
469   return 0;
470 }