Merge "Remove non-touch related deprecated APIs" into devel/master
[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
153     Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
154
155
156     // Creates a default view with a default tool bar.
157     // The view is added to the stage.
158     mContentLayer = DemoHelper::CreateView( mApplication,
159                                             mView,
160                                             mToolBar,
161                                             BACKGROUND_IMAGE_PATH,
162                                             TOOLBAR_IMAGE,
163                                             APPLICATION_TITLE );
164
165     // Ensure the content layer is a square so the touch area works in all orientations
166     Vector2 stageSize = Stage::GetCurrent().GetSize();
167     float size = std::max( stageSize.width, stageSize.height );
168     mContentLayer.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
169
170     //Add an effects icon on the right of the title
171     mActorEffectsButton = Toolkit::PushButton::New();
172     mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
173     mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
174     mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
175     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
176
177     // Creates a mode button.
178     // Create a effect toggle button. (right of toolbar)
179     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
180     layoutButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
181     layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
182     layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
183     layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
184     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
185
186     // Input
187     mTapGestureDetector = TapGestureDetector::New();
188     mTapGestureDetector.Attach( mContentLayer );
189     mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
190
191     Dali::Window winHandle = app.GetWindow();
192     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
193     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
194     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
195     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
196     winHandle.ResizeSignal().Connect( this, &MotionBlurExampleApp::OnWindowResized );
197
198     // set initial orientation
199     Rotate( PORTRAIT );
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     mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
208     mMotionBlurActorUpdateSize = Size( std::max( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::max( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
209     mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
210
211     mMotionBlurEffect = CreateMotionBlurEffect();
212     mMotionBlurImageView = ImageView::New();
213     SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
214     mMotionBlurImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
215     mMotionBlurImageView.SetProperty( Actor::Property::SIZE, mMotionBlurActorUpdateSize );
216     mMotionBlurImageView.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, mMotionBlurActorUpdateSize );
217
218     mContentLayer.Add( mMotionBlurImageView );
219
220     // Create shader used for doing motion blur
221     mMotionBlurEffect = CreateMotionBlurEffect();
222
223     // set actor shader to the blur one
224     Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
225
226   }
227
228   //////////////////////////////////////////////////////////////
229   //
230   // Device Orientation Support
231   //
232   //
233
234   void OnWindowResized( Window window, Window::WindowSize size )
235   {
236     Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
237   }
238
239   void Rotate( DeviceOrientation orientation )
240   {
241     // Resize the root actor
242     const Vector2 targetSize = Stage::GetCurrent().GetSize();
243
244     if( mOrientation != orientation )
245     {
246       mOrientation = orientation;
247
248       // check if actor is on stage
249       if( mView.GetParent() )
250       {
251         // has parent so we expect it to be on stage, start animation
252         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
253         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
254         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
255         mRotateAnimation.Play();
256       }
257       else
258       {
259         mView.SetProperty( Actor::Property::SIZE, targetSize );
260       }
261     }
262     else
263     {
264       // for first time just set size
265       mView.SetProperty( Actor::Property::SIZE, targetSize );
266     }
267   }
268
269
270   //////////////////////////////////////////////////////////////
271   //
272   // Actor Animation
273   //
274   //
275
276   // move to point on screen that was tapped
277   void OnTap( Actor actor, const TapGesture& tapGesture )
278   {
279     Vector3 destPos;
280     float originOffsetX, originOffsetY;
281
282     // rotate offset (from top left origin to centre) into actor space
283     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
284     actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
285
286     // get dest point in local actor space
287     destPos.x = tapGesture.localPoint.x - originOffsetX;
288     destPos.y = tapGesture.localPoint.y - originOffsetY;
289     destPos.z = 0.0f;
290
291     float animDuration = 0.5f;
292     mActorTapMovementAnimation = Animation::New( animDuration );
293     if ( mMotionBlurImageView )
294     {
295       mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
296     }
297     mActorTapMovementAnimation.SetEndAction( Animation::Bake );
298     mActorTapMovementAnimation.Play();
299
300
301     // perform some spinning etc
302     if(mActorEffectsEnabled)
303     {
304       switch(mCurrentActorAnimation)
305       {
306         // spin around y
307         case 0:
308         {
309           float animDuration = 1.0f;
310           mActorAnimation = Animation::New(animDuration);
311           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
312           mActorAnimation.SetEndAction( Animation::Bake );
313           mActorAnimation.Play();
314         }
315         break;
316
317         // spin around z
318         case 1:
319         {
320           float animDuration = 1.0f;
321           mActorAnimation = Animation::New(animDuration);
322           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
323           mActorAnimation.SetEndAction( Animation::Bake );
324           mActorAnimation.Play();
325         }
326         break;
327
328         // spin around y and z
329         case 2:
330         {
331           float animDuration = 1.0f;
332           mActorAnimation = Animation::New(animDuration);
333           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
334           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
335           mActorAnimation.SetEndAction( Animation::Bake );
336           mActorAnimation.Play();
337         }
338         break;
339
340         // scale
341         case 3:
342         {
343           float animDuration = 1.0f;
344           mActorAnimation = Animation::New(animDuration);
345           mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
346           mActorAnimation.SetEndAction( Animation::Bake );
347           mActorAnimation.Play();
348         }
349         break;
350
351         default:
352           break;
353       }
354
355       mCurrentActorAnimation++;
356       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
357       {
358         mCurrentActorAnimation = 0;
359       }
360     }
361   }
362
363   void ToggleActorEffects()
364   {
365     if(!mActorEffectsEnabled)
366     {
367       mActorEffectsEnabled = true;
368       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
369       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
370     }
371     else
372     {
373       mActorEffectsEnabled = false;
374       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
375       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
376     }
377   }
378
379   //////////////////////////////////////////////////////////////
380   //
381   // Input handlers
382   //
383   //
384
385   bool OnLayoutButtonClicked( Toolkit::Button button )
386   {
387     ChangeImage();
388     return true;
389   }
390
391   bool OnEffectButtonClicked( Toolkit::Button button )
392   {
393     ToggleActorEffects();
394     return true;
395   }
396
397   /**
398    * Main key event handler
399    */
400   void OnKeyEvent(const KeyEvent& event)
401   {
402     if(event.state == KeyEvent::Down)
403     {
404       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
405       {
406         mApplication.Quit();
407       }
408     }
409   }
410
411   //////////////////////////////////////////////////////////////
412   //
413   // Misc
414   //
415   //
416
417
418   void ChangeImage()
419   {
420     mCurrentImage++;
421     if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
422     {
423       mCurrentImage = 0;
424     }
425     SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
426
427   }
428
429
430 private:
431   Application&               mApplication;            ///< Application instance
432   Toolkit::Control           mView;
433   Toolkit::ToolBar           mToolBar;
434
435   Layer                      mContentLayer;           ///< Content layer (contains actor for this blur demo)
436
437   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
438
439   // Motion blur
440   Property::Map mMotionBlurEffect;
441   ImageView mMotionBlurImageView;
442   Size mMotionBlurActorSize;
443   Size mMotionBlurActorUpdateSize;
444
445   // animate actor to position where user taps screen
446   Animation mActorTapMovementAnimation;
447
448   // show different animations to demonstrate blur effect working on an object only movement basis
449   bool mActorEffectsEnabled;
450   Animation mActorAnimation;
451   int mCurrentActorAnimation;
452
453   // offer a selection of images that user can cycle between
454   int mCurrentImage;
455
456   TapGestureDetector mTapGestureDetector;
457
458   DeviceOrientation mOrientation;               ///< Current Device orientation
459   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
460 };
461
462 int DALI_EXPORT_API main(int argc, char **argv)
463 {
464   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
465   MotionBlurExampleApp test(app);
466   app.MainLoop();
467   return 0;
468 }