Merge "DALi Version 1.0.47" into devel/master
[platform/core/uifw/dali-demo.git] / examples / motion-stretch / motion-stretch-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-stretch-effect.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29
30
31 namespace // unnamed namespace
32 {
33
34 ////////////////////////////////////////////////////
35 //
36 // Demo setup parameters
37 //
38
39 const float MOTION_STRETCH_ACTOR_WIDTH = 256;                                          // actor size on screen
40 const float MOTION_STRETCH_ACTOR_HEIGHT = 256;                                         // ""
41
42 const int MOTION_STRETCH_NUM_ACTOR_IMAGES = 5;
43 const char* MOTION_STRETCH_ACTOR_IMAGE1( DALI_IMAGE_DIR "image-with-border-1.jpg" );
44 const char* MOTION_STRETCH_ACTOR_IMAGE2( DALI_IMAGE_DIR "image-with-border-2.jpg" );
45 const char* MOTION_STRETCH_ACTOR_IMAGE3( DALI_IMAGE_DIR "image-with-border-3.jpg" );
46 const char* MOTION_STRETCH_ACTOR_IMAGE4( DALI_IMAGE_DIR "image-with-border-4.jpg" );
47 const char* MOTION_STRETCH_ACTOR_IMAGE5( DALI_IMAGE_DIR "image-with-border-5.jpg" );
48
49 const char* MOTION_STRETCH_ACTOR_IMAGES[] = {
50   MOTION_STRETCH_ACTOR_IMAGE1,
51   MOTION_STRETCH_ACTOR_IMAGE2,
52   MOTION_STRETCH_ACTOR_IMAGE3,
53   MOTION_STRETCH_ACTOR_IMAGE4,
54   MOTION_STRETCH_ACTOR_IMAGE5,
55 };
56
57 const int NUM_ACTOR_ANIMATIONS = 4;
58 const int NUM_CAMERA_ANIMATIONS = 2;
59
60
61 const char* BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "background-default.png";
62
63 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
64 const char* LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
65 const char* LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-change-selected.png" );
66 const char* APPLICATION_TITLE( "Motion Stretch" );
67 const char* EFFECTS_OFF_ICON( DALI_IMAGE_DIR "icon-effects-off.png" );
68 const char* EFFECTS_OFF_ICON_SELECTED( DALI_IMAGE_DIR "icon-effects-off-selected.png" );
69 const char* EFFECTS_ON_ICON( DALI_IMAGE_DIR "icon-effects-on.png" );
70 const char* EFFECTS_ON_ICON_SELECTED( DALI_IMAGE_DIR "icon-effects-on-selected.png" );
71
72 // These values depend on the button background image
73 const Vector4 BUTTON_IMAGE_BORDER( Vector4::ONE * 3.0f );
74
75 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
76
77 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
78
79 // move this button down a bit so it is visible on target and not covered up by toolbar
80 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
81
82 const float ORIENTATION_DURATION = 0.5f;                  ///< Time to rotate to new orientation.
83 } // unnamed namespace
84
85
86
87
88 //
89 class MotionStretchExampleApp : public ConnectionTracker
90 {
91 public:
92
93   /**
94      * DeviceOrientation describes the four different
95      * orientations the device can be in based on accelerometer reports.
96      */
97   enum DeviceOrientation
98   {
99     PORTRAIT = 0,
100     LANDSCAPE = 90,
101     PORTRAIT_INVERSE = 180,
102     LANDSCAPE_INVERSE = 270
103   };
104
105   /**
106    * Constructor
107    * @param application class, stored as reference
108    */
109   MotionStretchExampleApp(Application &app)
110   : mApplication(app),
111     mActorEffectsEnabled(false),
112     mCurrentActorAnimation(0),
113     mCurrentImage(0)
114   {
115     // Connect to the Application's Init signal
116     app.InitSignal().Connect(this, &MotionStretchExampleApp::OnInit);
117   }
118
119   ~MotionStretchExampleApp()
120   {
121     // Nothing to do here; everything gets deleted automatically
122   }
123
124   /**
125    * This method gets called once the main loop of application is up and running
126    */
127   void OnInit(Application& app)
128   {
129     // The Init signal is received once (only) during the Application lifetime
130
131     Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
132
133     // Creates a default view with a default tool bar.
134     // The view is added to the stage.
135     mContentLayer = DemoHelper::CreateView( mApplication,
136                                             mView,
137                                             mToolBar,
138                                             BACKGROUND_IMAGE_PATH,
139                                             TOOLBAR_IMAGE,
140                                             APPLICATION_TITLE );
141
142     //Add an slideshow icon on the right of the title
143     mIconEffectsOff = ResourceImage::New( EFFECTS_OFF_ICON );
144     mIconEffectsOffSelected = ResourceImage::New( EFFECTS_OFF_ICON_SELECTED );
145     mIconEffectsOn = ResourceImage::New( EFFECTS_ON_ICON );
146     mIconEffectsOnSelected = ResourceImage::New( EFFECTS_ON_ICON_SELECTED );
147     mActorEffectsButton = Toolkit::PushButton::New();
148     mActorEffectsButton.SetButtonImage( mIconEffectsOff );
149     mActorEffectsButton.SetSelectedImage( mIconEffectsOffSelected );
150     mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
151     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
152
153     // Creates a mode button.
154     // Create a effect toggle button. (right of toolbar)
155     Image imageLayout = ResourceImage::New( LAYOUT_IMAGE );
156     Image imageLayoutSelected = ResourceImage::New( LAYOUT_IMAGE_SELECTED );
157     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
158     layoutButton.SetButtonImage( imageLayout );
159     layoutButton.SetSelectedImage( imageLayoutSelected );
160     layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
161     layoutButton.SetLeaveRequired( true );
162     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
163
164     // Input
165     mTapGestureDetector = TapGestureDetector::New();
166     mTapGestureDetector.Attach( mContentLayer );
167     mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
168
169     // set initial orientation
170     Dali::Window winHandle = app.GetWindow();
171     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
172     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
173     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
174     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
175
176    // winHandle.GetOrientation().ChangedSignal().Connect( this, &MotionStretchExampleApp::OnOrientationChanged );
177     unsigned int degrees = 0;
178     Rotate( static_cast< DeviceOrientation >( degrees ) );
179
180
181     ///////////////////////////////////////////////////////
182     //
183     // Motion stretched actor
184     //
185
186     Image image = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGE1 );
187     mMotionStretchImageActor = ImageActor::New(image);
188     mMotionStretchImageActor.SetParentOrigin( ParentOrigin::CENTER );
189     mMotionStretchImageActor.SetAnchorPoint( AnchorPoint::CENTER );
190     mMotionStretchImageActor.SetSize(MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT);
191
192     mContentLayer.Add( mMotionStretchImageActor );
193
194     // Create shader used for doing motion stretch
195     mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
196     Dali::Property::Index uModelProperty = mMotionStretchEffect.GetPropertyIndex( "uModelLastFrame" );
197     Constraint constraint = Constraint::New<Matrix>( mMotionStretchEffect, uModelProperty, EqualToConstraint() );
198     constraint.AddSource( Source( mMotionStretchImageActor , Actor::Property::WORLD_MATRIX ) );
199     constraint.Apply();
200     mMotionStretchImageActor.SetShaderEffect( mMotionStretchEffect );
201   }
202
203   //////////////////////////////////////////////////////////////
204   //
205   // Device Orientation Support
206   //
207   //
208
209   void OnOrientationChanged( Orientation orientation )
210   {
211     unsigned int degrees = orientation.GetDegrees();
212     Rotate( static_cast< DeviceOrientation >( degrees ) );
213   }
214
215   void Rotate( DeviceOrientation orientation )
216   {
217     // Resize the root actor
218     Vector2 stageSize = Stage::GetCurrent().GetSize();
219     Vector2 targetSize = stageSize;
220     if( orientation == LANDSCAPE ||
221         orientation == LANDSCAPE_INVERSE )
222     {
223       targetSize = Vector2( stageSize.y, stageSize.x );
224     }
225
226     if( mOrientation != orientation )
227     {
228       mOrientation = orientation;
229
230       // check if actor is on stage
231       if( mView.GetParent() )
232       {
233         // has parent so we expect it to be on stage, start animation
234         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
235         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
236         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
237         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
238         mRotateAnimation.Play();
239       }
240       else
241       {
242         // set the rotation to match the orientation
243         mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
244         mView.SetSize( targetSize );
245       }
246     }
247     else
248     {
249       // for first time just set size
250       mView.SetSize( targetSize );
251     }
252   }
253
254   //////////////////////////////////////////////////////////////
255   //
256   // Actor Animation
257   //
258   //
259
260   // move to point on screen that was tapped
261   void OnTap( Actor actor, const TapGesture& tapGesture )
262   {
263     Vector3 destPos;
264     float originOffsetX, originOffsetY;
265
266     // rotate offset (from top left origin to centre) into actor space
267     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
268     actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
269
270     // get dest point in local actor space
271     destPos.x = tapGesture.localPoint.x - originOffsetX;
272     destPos.y = tapGesture.localPoint.y - originOffsetY;
273     destPos.z = 0.0f;
274
275     float animDuration = 0.5f;
276     mActorTapMovementAnimation = Animation::New( animDuration );
277     if ( mMotionStretchImageActor )
278     {
279       mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageActor, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
280     }
281     mActorTapMovementAnimation.SetEndAction( Animation::Bake );
282     mActorTapMovementAnimation.Play();
283
284
285     // perform some spinning etc
286     if(mActorEffectsEnabled)
287     {
288       switch(mCurrentActorAnimation)
289       {
290         // spin around y
291         case 0:
292         {
293           float animDuration = 1.0f;
294           mActorAnimation = Animation::New(animDuration);
295           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
296           mActorAnimation.SetEndAction( Animation::Bake );
297           mActorAnimation.Play();
298         }
299         break;
300
301         // spin around z
302         case 1:
303         {
304           float animDuration = 1.0f;
305           mActorAnimation = Animation::New(animDuration);
306           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
307           mActorAnimation.SetEndAction( Animation::Bake );
308           mActorAnimation.Play();
309         }
310         break;
311
312         // spin around y and z
313         case 2:
314         {
315           float animDuration = 1.0f;
316           mActorAnimation = Animation::New(animDuration);
317           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
318           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
319           mActorAnimation.SetEndAction( Animation::Bake );
320           mActorAnimation.Play();
321         }
322         break;
323
324         // scale
325         case 3:
326         {
327           float animDuration = 1.0f;
328           mActorAnimation = Animation::New(animDuration);
329           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
330           mActorAnimation.SetEndAction( Animation::Bake );
331           mActorAnimation.Play();
332         }
333         break;
334
335         default:
336           break;
337       }
338
339       mCurrentActorAnimation++;
340       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
341       {
342         mCurrentActorAnimation = 0;
343       }
344     }
345   }
346
347   void ToggleActorEffects()
348   {
349     if(!mActorEffectsEnabled)
350     {
351       mActorEffectsEnabled = true;
352       mActorEffectsButton.SetButtonImage( mIconEffectsOn );
353       mActorEffectsButton.SetSelectedImage( mIconEffectsOnSelected );
354     }
355     else
356     {
357       mActorEffectsEnabled = false;
358       mActorEffectsButton.SetButtonImage( mIconEffectsOff );
359       mActorEffectsButton.SetSelectedImage( mIconEffectsOffSelected );
360     }
361   }
362
363   //////////////////////////////////////////////////////////////
364   //
365   // Input handlers
366   //
367   //
368
369   bool OnLayoutButtonClicked( Toolkit::Button button )
370   {
371     ChangeImage();
372     return true;
373   }
374
375   bool OnEffectButtonClicked( Toolkit::Button button )
376   {
377     ToggleActorEffects();
378     return true;
379   }
380
381   /**
382    * Main key event handler
383    */
384   void OnKeyEvent(const KeyEvent& event)
385   {
386     if(event.state == KeyEvent::Down)
387     {
388       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
389       {
390         mApplication.Quit();
391       }
392     }
393   }
394
395   //////////////////////////////////////////////////////////////
396   //
397   // Misc
398   //
399   //
400
401
402   void ChangeImage()
403   {
404     mCurrentImage++;
405     if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
406     {
407       mCurrentImage = 0;
408     }
409
410     Image stretchImage = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage] );
411     mMotionStretchImageActor.SetImage(stretchImage);
412   }
413
414
415 private:
416   Application&               mApplication;            ///< Application instance
417   Toolkit::Control           mView;
418   Toolkit::ToolBar           mToolBar;
419   Image                      mIconEffectsOff;
420   Image                      mIconEffectsOffSelected;
421   Image                      mIconEffectsOn;
422   Image                      mIconEffectsOnSelected;
423   Layer                      mContentLayer;           ///< Content layer (contains actor for this stretch demo)
424
425   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
426
427   // Motion stretch
428   ShaderEffect mMotionStretchEffect;
429   ImageActor mMotionStretchImageActor;
430
431   // animate actor to position where user taps screen
432   Animation mActorTapMovementAnimation;
433
434   // show different animations to demonstrate stretch effect working on an object only movement basis
435   bool mActorEffectsEnabled;
436   Animation mActorAnimation;
437   int mCurrentActorAnimation;
438
439   // offer a selection of images that user can cycle between
440   int mCurrentImage;
441
442   TapGestureDetector mTapGestureDetector;
443
444   DeviceOrientation mOrientation;               ///< Current Device orientation
445   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
446
447 };
448
449 void RunTest(Application& app)
450 {
451   MotionStretchExampleApp test(app);
452
453   app.MainLoop();
454 }
455
456 // Entry point for Linux & Tizen applications
457 //
458 int main(int argc, char **argv)
459 {
460   Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);
461
462   RunTest(app);
463
464   return 0;
465 }