[dali_1.3.32] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / motion-stretch / motion-stretch-example.cpp
1 /*
2  * Copyright (c) 2018 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/controls/buttons/button-devel.h>
25 #include <dali-toolkit/devel-api/shader-effects/motion-stretch-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_STRETCH_ACTOR_WIDTH = 256;                                          // actor size on screen
41 const float MOTION_STRETCH_ACTOR_HEIGHT = 256;                                         // ""
42
43 const int MOTION_STRETCH_NUM_ACTOR_IMAGES = 5;
44 const char* MOTION_STRETCH_ACTOR_IMAGE1( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
45 const char* MOTION_STRETCH_ACTOR_IMAGE2( DEMO_IMAGE_DIR "image-with-border-2.jpg" );
46 const char* MOTION_STRETCH_ACTOR_IMAGE3( DEMO_IMAGE_DIR "image-with-border-3.jpg" );
47 const char* MOTION_STRETCH_ACTOR_IMAGE4( DEMO_IMAGE_DIR "image-with-border-4.jpg" );
48 const char* MOTION_STRETCH_ACTOR_IMAGE5( DEMO_IMAGE_DIR "image-with-border-5.jpg" );
49
50 const char* MOTION_STRETCH_ACTOR_IMAGES[] = {
51   MOTION_STRETCH_ACTOR_IMAGE1,
52   MOTION_STRETCH_ACTOR_IMAGE2,
53   MOTION_STRETCH_ACTOR_IMAGE3,
54   MOTION_STRETCH_ACTOR_IMAGE4,
55   MOTION_STRETCH_ACTOR_IMAGE5,
56 };
57
58 const int NUM_ACTOR_ANIMATIONS = 4;
59 const int NUM_CAMERA_ANIMATIONS = 2;
60
61
62 const char* BACKGROUND_IMAGE_PATH = DEMO_IMAGE_DIR "background-default.png";
63
64 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
65 const char* LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
66 const char* LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
67 const char* APPLICATION_TITLE( "Motion Stretch" );
68 const char* EFFECTS_OFF_ICON( DEMO_IMAGE_DIR "icon-effects-off.png" );
69 const char* EFFECTS_OFF_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-off-selected.png" );
70 const char* EFFECTS_ON_ICON( DEMO_IMAGE_DIR "icon-effects-on.png" );
71 const char* EFFECTS_ON_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-on-selected.png" );
72
73 // These values depend on the button background image
74 const Vector4 BUTTON_IMAGE_BORDER( Vector4::ONE * 3.0f );
75
76 const float UI_MARGIN = 4.0f;                              ///< Screen Margin for placement of UI buttons
77
78 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 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 } // unnamed namespace
85
86
87
88
89 //
90 class MotionStretchExampleApp : public ConnectionTracker
91 {
92 public:
93
94   /**
95      * DeviceOrientation describes the four different
96      * orientations the device can be in based on accelerometer reports.
97      */
98   enum DeviceOrientation
99   {
100     PORTRAIT = 0,
101     LANDSCAPE = 90,
102     PORTRAIT_INVERSE = 180,
103     LANDSCAPE_INVERSE = 270
104   };
105
106   /**
107    * Constructor
108    * @param application class, stored as reference
109    */
110   MotionStretchExampleApp(Application &app)
111   : mApplication(app),
112     mActorEffectsEnabled(false),
113     mCurrentActorAnimation(0),
114     mCurrentImage(0),
115     mOrientation( PORTRAIT )
116   {
117     // Connect to the Application's Init signal
118     app.InitSignal().Connect(this, &MotionStretchExampleApp::OnInit);
119   }
120
121   ~MotionStretchExampleApp()
122   {
123     // Nothing to do here; everything gets deleted automatically
124   }
125
126   /**
127    * This method gets called once the main loop of application is up and running
128    */
129   void OnInit(Application& app)
130   {
131     // The Init signal is received once (only) during the Application lifetime
132
133     Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
134
135     // Creates a default view with a default tool bar.
136     // The view is added to the stage.
137     mContentLayer = DemoHelper::CreateView( mApplication,
138                                             mView,
139                                             mToolBar,
140                                             BACKGROUND_IMAGE_PATH,
141                                             TOOLBAR_IMAGE,
142                                             APPLICATION_TITLE );
143
144     // Ensure the content layer is a square so the touch area works in all orientations
145     Vector2 stageSize = Stage::GetCurrent().GetSize();
146     float size = std::max( stageSize.width, stageSize.height );
147     mContentLayer.SetSize( size, size );
148
149     //Add an slideshow icon on the right of the title
150     mActorEffectsButton = Toolkit::PushButton::New();
151     mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
152     mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
153     mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
154     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
155
156     // Creates a mode button.
157     // Create a effect toggle button. (right of toolbar)
158     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
159     layoutButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
160     layoutButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
161     layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
162     layoutButton.SetLeaveRequired( true );
163     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
164
165     // Input
166     mTapGestureDetector = TapGestureDetector::New();
167     mTapGestureDetector.Attach( mContentLayer );
168     mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
169
170     // set initial orientation
171     Dali::Window winHandle = app.GetWindow();
172     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
173     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
174     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
175     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
176     winHandle.ResizedSignal().Connect( this, &MotionStretchExampleApp::OnWindowResized );
177
178     // set initial orientation
179     Rotate( PORTRAIT );
180
181     ///////////////////////////////////////////////////////
182     //
183     // Motion stretched actor
184     //
185     mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
186     mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
187     mMotionStretchImageView = ImageView::New();
188     mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
189     mMotionStretchImageView.SetParentOrigin( ParentOrigin::CENTER );
190     mMotionStretchImageView.SetAnchorPoint( AnchorPoint::CENTER );
191     mMotionStretchImageView.SetSize( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT );
192
193     mContentLayer.Add( mMotionStretchImageView );
194
195     // Create shader used for doing motion stretch
196     Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
197   }
198
199   //////////////////////////////////////////////////////////////
200   //
201   // Device Orientation Support
202   //
203   //
204
205   void OnWindowResized( Window::WindowSize size )
206   {
207     Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
208   }
209
210   void Rotate( DeviceOrientation orientation )
211   {
212     // Resize the root actor
213     const Vector2 targetSize = Stage::GetCurrent().GetSize();
214
215     if( mOrientation != orientation )
216     {
217       mOrientation = orientation;
218
219       // check if actor is on stage
220       if( mView.GetParent() )
221       {
222         // has parent so we expect it to be on stage, start animation
223         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
224         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
225         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
226         mRotateAnimation.Play();
227       }
228       else
229       {
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, const 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 ( mMotionStretchImageView )
264     {
265       mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
266     }
267     mActorTapMovementAnimation.SetEndAction( Animation::Bake );
268     mActorTapMovementAnimation.Play();
269
270
271     // perform some spinning etc
272     if(mActorEffectsEnabled)
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.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
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.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
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.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
304           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
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.AnimateBy( Property( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 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 ToggleActorEffects()
334   {
335     if(!mActorEffectsEnabled)
336     {
337       mActorEffectsEnabled = true;
338       mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
339       mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
340     }
341     else
342     {
343       mActorEffectsEnabled = false;
344       mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
345       mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
346     }
347   }
348
349   //////////////////////////////////////////////////////////////
350   //
351   // Input handlers
352   //
353   //
354
355   bool OnLayoutButtonClicked( Toolkit::Button button )
356   {
357     ChangeImage();
358     return true;
359   }
360
361   bool OnEffectButtonClicked( Toolkit::Button button )
362   {
363     ToggleActorEffects();
364     return true;
365   }
366
367   /**
368    * Main key event handler
369    */
370   void OnKeyEvent(const KeyEvent& event)
371   {
372     if(event.state == KeyEvent::Down)
373     {
374       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
375       {
376         mApplication.Quit();
377       }
378     }
379   }
380
381   //////////////////////////////////////////////////////////////
382   //
383   // Misc
384   //
385   //
386
387
388   void ChangeImage()
389   {
390     mCurrentImage++;
391     if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
392     {
393       mCurrentImage = 0;
394     }
395
396     mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
397     mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
398   }
399
400
401 private:
402   Application&               mApplication;            ///< Application instance
403   Toolkit::Control           mView;
404   Toolkit::ToolBar           mToolBar;
405   Layer                      mContentLayer;           ///< Content layer (contains actor for this stretch demo)
406
407   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
408
409   // Motion stretch
410   Property::Map mMotionStretchEffect;
411   ImageView mMotionStretchImageView;
412
413   // animate actor to position where user taps screen
414   Animation mActorTapMovementAnimation;
415
416   // show different animations to demonstrate stretch effect working on an object only movement basis
417   bool mActorEffectsEnabled;
418   Animation mActorAnimation;
419   int mCurrentActorAnimation;
420
421   // offer a selection of images that user can cycle between
422   int mCurrentImage;
423
424   TapGestureDetector mTapGestureDetector;
425
426   DeviceOrientation mOrientation;               ///< Current Device orientation
427   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
428
429 };
430
431 int DALI_EXPORT_API main(int argc, char **argv)
432 {
433   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
434   MotionStretchExampleApp test(app);
435   app.MainLoop();
436   return 0;
437 }