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