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