Merge "Cleaned up image scaling and filtering example by removing (commented out...
[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     mActorEffectsButton = Toolkit::PushButton::New();
144     mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
145     mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
146     mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
147     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
148
149     // Creates a mode button.
150     // Create a effect toggle button. (right of toolbar)
151     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
152     layoutButton.SetUnselectedImage( LAYOUT_IMAGE );
153     layoutButton.SetSelectedImage( LAYOUT_IMAGE_SELECTED );
154     layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
155     layoutButton.SetLeaveRequired( true );
156     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
157
158     // Input
159     mTapGestureDetector = TapGestureDetector::New();
160     mTapGestureDetector.Attach( mContentLayer );
161     mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
162
163     // set initial orientation
164     Dali::Window winHandle = app.GetWindow();
165     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
166     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
167     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
168     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
169
170    // winHandle.GetOrientation().ChangedSignal().Connect( this, &MotionStretchExampleApp::OnOrientationChanged );
171     unsigned int degrees = 0;
172     Rotate( static_cast< DeviceOrientation >( degrees ) );
173
174
175     ///////////////////////////////////////////////////////
176     //
177     // Motion stretched actor
178     //
179
180     Image image = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGE1 );
181     mMotionStretchImageActor = ImageActor::New(image);
182     mMotionStretchImageActor.SetParentOrigin( ParentOrigin::CENTER );
183     mMotionStretchImageActor.SetAnchorPoint( AnchorPoint::CENTER );
184     mMotionStretchImageActor.SetSize(MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT);
185
186     mContentLayer.Add( mMotionStretchImageActor );
187
188     // Create shader used for doing motion stretch
189     mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
190     Dali::Property::Index uModelProperty = mMotionStretchEffect.GetPropertyIndex( "uModelLastFrame" );
191     Constraint constraint = Constraint::New<Matrix>( mMotionStretchEffect, uModelProperty, EqualToConstraint() );
192     constraint.AddSource( Source( mMotionStretchImageActor , Actor::Property::WORLD_MATRIX ) );
193     constraint.Apply();
194     mMotionStretchImageActor.SetShaderEffect( mMotionStretchEffect );
195   }
196
197   //////////////////////////////////////////////////////////////
198   //
199   // Device Orientation Support
200   //
201   //
202
203   void OnOrientationChanged( Orientation orientation )
204   {
205     unsigned int degrees = orientation.GetDegrees();
206     Rotate( static_cast< DeviceOrientation >( degrees ) );
207   }
208
209   void Rotate( DeviceOrientation orientation )
210   {
211     // Resize the root actor
212     Vector2 stageSize = Stage::GetCurrent().GetSize();
213     Vector2 targetSize = stageSize;
214     if( orientation == LANDSCAPE ||
215         orientation == LANDSCAPE_INVERSE )
216     {
217       targetSize = Vector2( stageSize.y, stageSize.x );
218     }
219
220     if( mOrientation != orientation )
221     {
222       mOrientation = orientation;
223
224       // check if actor is on stage
225       if( mView.GetParent() )
226       {
227         // has parent so we expect it to be on stage, start animation
228         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
229         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
230         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
231         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
232         mRotateAnimation.Play();
233       }
234       else
235       {
236         // set the rotation to match the orientation
237         mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
238         mView.SetSize( targetSize );
239       }
240     }
241     else
242     {
243       // for first time just set size
244       mView.SetSize( targetSize );
245     }
246   }
247
248   //////////////////////////////////////////////////////////////
249   //
250   // Actor Animation
251   //
252   //
253
254   // move to point on screen that was tapped
255   void OnTap( Actor actor, const TapGesture& tapGesture )
256   {
257     Vector3 destPos;
258     float originOffsetX, originOffsetY;
259
260     // rotate offset (from top left origin to centre) into actor space
261     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
262     actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
263
264     // get dest point in local actor space
265     destPos.x = tapGesture.localPoint.x - originOffsetX;
266     destPos.y = tapGesture.localPoint.y - originOffsetY;
267     destPos.z = 0.0f;
268
269     float animDuration = 0.5f;
270     mActorTapMovementAnimation = Animation::New( animDuration );
271     if ( mMotionStretchImageActor )
272     {
273       mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageActor, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
274     }
275     mActorTapMovementAnimation.SetEndAction( Animation::Bake );
276     mActorTapMovementAnimation.Play();
277
278
279     // perform some spinning etc
280     if(mActorEffectsEnabled)
281     {
282       switch(mCurrentActorAnimation)
283       {
284         // spin around y
285         case 0:
286         {
287           float animDuration = 1.0f;
288           mActorAnimation = Animation::New(animDuration);
289           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
290           mActorAnimation.SetEndAction( Animation::Bake );
291           mActorAnimation.Play();
292         }
293         break;
294
295         // spin around z
296         case 1:
297         {
298           float animDuration = 1.0f;
299           mActorAnimation = Animation::New(animDuration);
300           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
301           mActorAnimation.SetEndAction( Animation::Bake );
302           mActorAnimation.Play();
303         }
304         break;
305
306         // spin around y and z
307         case 2:
308         {
309           float animDuration = 1.0f;
310           mActorAnimation = Animation::New(animDuration);
311           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
312           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
313           mActorAnimation.SetEndAction( Animation::Bake );
314           mActorAnimation.Play();
315         }
316         break;
317
318         // scale
319         case 3:
320         {
321           float animDuration = 1.0f;
322           mActorAnimation = Animation::New(animDuration);
323           mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
324           mActorAnimation.SetEndAction( Animation::Bake );
325           mActorAnimation.Play();
326         }
327         break;
328
329         default:
330           break;
331       }
332
333       mCurrentActorAnimation++;
334       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
335       {
336         mCurrentActorAnimation = 0;
337       }
338     }
339   }
340
341   void ToggleActorEffects()
342   {
343     if(!mActorEffectsEnabled)
344     {
345       mActorEffectsEnabled = true;
346       mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
347       mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
348     }
349     else
350     {
351       mActorEffectsEnabled = false;
352       mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
353       mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
354     }
355   }
356
357   //////////////////////////////////////////////////////////////
358   //
359   // Input handlers
360   //
361   //
362
363   bool OnLayoutButtonClicked( Toolkit::Button button )
364   {
365     ChangeImage();
366     return true;
367   }
368
369   bool OnEffectButtonClicked( Toolkit::Button button )
370   {
371     ToggleActorEffects();
372     return true;
373   }
374
375   /**
376    * Main key event handler
377    */
378   void OnKeyEvent(const KeyEvent& event)
379   {
380     if(event.state == KeyEvent::Down)
381     {
382       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
383       {
384         mApplication.Quit();
385       }
386     }
387   }
388
389   //////////////////////////////////////////////////////////////
390   //
391   // Misc
392   //
393   //
394
395
396   void ChangeImage()
397   {
398     mCurrentImage++;
399     if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
400     {
401       mCurrentImage = 0;
402     }
403
404     Image stretchImage = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage] );
405     mMotionStretchImageActor.SetImage(stretchImage);
406   }
407
408
409 private:
410   Application&               mApplication;            ///< Application instance
411   Toolkit::Control           mView;
412   Toolkit::ToolBar           mToolBar;
413   Layer                      mContentLayer;           ///< Content layer (contains actor for this stretch demo)
414
415   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
416
417   // Motion stretch
418   ShaderEffect mMotionStretchEffect;
419   ImageActor mMotionStretchImageActor;
420
421   // animate actor to position where user taps screen
422   Animation mActorTapMovementAnimation;
423
424   // show different animations to demonstrate stretch effect working on an object only movement basis
425   bool mActorEffectsEnabled;
426   Animation mActorAnimation;
427   int mCurrentActorAnimation;
428
429   // offer a selection of images that user can cycle between
430   int mCurrentImage;
431
432   TapGestureDetector mTapGestureDetector;
433
434   DeviceOrientation mOrientation;               ///< Current Device orientation
435   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
436
437 };
438
439 void RunTest(Application& app)
440 {
441   MotionStretchExampleApp test(app);
442
443   app.MainLoop();
444 }
445
446 // Entry point for Linux & Tizen applications
447 //
448 int main(int argc, char **argv)
449 {
450   Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);
451
452   RunTest(app);
453
454   return 0;
455 }