8167dfc5aa6928af0bb2d8f6e87b2593b9f374cc
[platform/core/uifw/dali-demo.git] / examples / motion-stretch / motion-stretch-example.cpp
1 /*
2  * Copyright (c) 2020 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/devel-api/actors/actor-devel.h>
24 #include <dali-toolkit/dali-toolkit.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     Window window = app.GetWindow();
133
134     window.KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
135
136     // Creates a default view with a default tool bar.
137     // The view is added to the window.
138     mContentLayer = DemoHelper::CreateView( mApplication,
139                                             mView,
140                                             mToolBar,
141                                             BACKGROUND_IMAGE_PATH,
142                                             TOOLBAR_IMAGE,
143                                             APPLICATION_TITLE );
144
145     // Ensure the content layer is a square so the touch area works in all orientations
146     Vector2 windowSize = window.GetSize();
147     float size = std::max( windowSize.width, windowSize.height );
148     mContentLayer.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
149
150     //Add an slideshow icon on the right of the title
151     mActorEffectsButton = Toolkit::PushButton::New();
152     mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
153     mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
154     mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
155     mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_CENTER, DemoHelper::DEFAULT_PLAY_PADDING );
156
157     // Creates a mode button.
158     // Create a effect toggle button. (right of toolbar)
159     Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
160     layoutButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
161     layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
162     layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
163     layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
164     mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
165
166     // Input
167     mTapGestureDetector = TapGestureDetector::New();
168     mTapGestureDetector.Attach( mContentLayer );
169     mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
170
171     // set initial orientation
172     Dali::Window winHandle = app.GetWindow();
173     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
174     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
175     winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
176     winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
177     winHandle.ResizeSignal().Connect( this, &MotionStretchExampleApp::OnWindowResized );
178
179     // set initial orientation
180     Rotate( PORTRAIT );
181
182     ///////////////////////////////////////////////////////
183     //
184     // Motion stretched actor
185     //
186     mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
187     mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
188     mMotionStretchImageView = ImageView::New();
189     mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
190     mMotionStretchImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
191     mMotionStretchImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
192     mMotionStretchImageView.SetProperty( Actor::Property::SIZE, Vector2( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT ) );
193     // Add stretch padding
194     mMotionStretchImageView.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, Vector2( MOTION_STRETCH_ACTOR_WIDTH+32, MOTION_STRETCH_ACTOR_HEIGHT+32 ) );
195
196     mContentLayer.Add( mMotionStretchImageView );
197
198     // Create shader used for doing motion stretch
199     Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
200   }
201
202   //////////////////////////////////////////////////////////////
203   //
204   // Device Orientation Support
205   //
206   //
207
208   void OnWindowResized( Window window, Window::WindowSize size )
209   {
210     Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
211   }
212
213   void Rotate( DeviceOrientation orientation )
214   {
215     // Resize the root actor
216     const Vector2 targetSize = mApplication.GetWindow().GetSize();
217
218     if( mOrientation != orientation )
219     {
220       mOrientation = orientation;
221
222       // check if actor is on window
223       if( mView.GetParent() )
224       {
225         // has parent so we expect it to be on window, start animation
226         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
227         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
228         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
229         mRotateAnimation.Play();
230       }
231       else
232       {
233         mView.SetProperty( Actor::Property::SIZE, targetSize );
234       }
235     }
236     else
237     {
238       // for first time just set size
239       mView.SetProperty( Actor::Property::SIZE, targetSize );
240     }
241   }
242
243   //////////////////////////////////////////////////////////////
244   //
245   // Actor Animation
246   //
247   //
248
249   // move to point on screen that was tapped
250   void OnTap( Actor actor, const TapGesture& tapGesture )
251   {
252     Vector3 destPos;
253     float originOffsetX, originOffsetY;
254
255     // rotate offset (from top left origin to centre) into actor space
256     Vector2 windowSize = mApplication.GetWindow().GetSize();
257     actor.ScreenToLocal(originOffsetX, originOffsetY, windowSize.width * 0.5f, windowSize.height * 0.5f);
258
259     // get dest point in local actor space
260     const Vector2& localPoint = tapGesture.GetLocalPoint();
261     destPos.x = localPoint.x - originOffsetX;
262     destPos.y = localPoint.y - originOffsetY;
263     destPos.z = 0.0f;
264
265     float animDuration = 0.5f;
266     mActorTapMovementAnimation = Animation::New( animDuration );
267     if ( mMotionStretchImageView )
268     {
269       mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
270     }
271     mActorTapMovementAnimation.SetEndAction( Animation::BAKE );
272     mActorTapMovementAnimation.Play();
273
274
275     // perform some spinning etc
276     if(mActorEffectsEnabled)
277     {
278       switch(mCurrentActorAnimation)
279       {
280         // spin around y
281         case 0:
282         {
283           float animDuration = 1.0f;
284           mActorAnimation = Animation::New(animDuration);
285           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
286           mActorAnimation.SetEndAction( Animation::BAKE );
287           mActorAnimation.Play();
288         }
289         break;
290
291         // spin around z
292         case 1:
293         {
294           float animDuration = 1.0f;
295           mActorAnimation = Animation::New(animDuration);
296           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
297           mActorAnimation.SetEndAction( Animation::BAKE );
298           mActorAnimation.Play();
299         }
300         break;
301
302         // spin around y and z
303         case 2:
304         {
305           float animDuration = 1.0f;
306           mActorAnimation = Animation::New(animDuration);
307           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
308           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
309           mActorAnimation.SetEndAction( Animation::BAKE );
310           mActorAnimation.Play();
311         }
312         break;
313
314         // scale
315         case 3:
316         {
317           float animDuration = 1.0f;
318           mActorAnimation = Animation::New(animDuration);
319           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
320           mActorAnimation.SetEndAction( Animation::BAKE );
321           mActorAnimation.Play();
322         }
323         break;
324
325         default:
326           break;
327       }
328
329       mCurrentActorAnimation++;
330       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
331       {
332         mCurrentActorAnimation = 0;
333       }
334     }
335   }
336
337   void ToggleActorEffects()
338   {
339     if(!mActorEffectsEnabled)
340     {
341       mActorEffectsEnabled = true;
342       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
343       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
344     }
345     else
346     {
347       mActorEffectsEnabled = false;
348       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
349       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
350     }
351   }
352
353   //////////////////////////////////////////////////////////////
354   //
355   // Input handlers
356   //
357   //
358
359   bool OnLayoutButtonClicked( Toolkit::Button button )
360   {
361     ChangeImage();
362     return true;
363   }
364
365   bool OnEffectButtonClicked( Toolkit::Button button )
366   {
367     ToggleActorEffects();
368     return true;
369   }
370
371   /**
372    * Main key event handler
373    */
374   void OnKeyEvent(const KeyEvent& event)
375   {
376     if(event.GetState() == KeyEvent::DOWN)
377     {
378       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
379       {
380         mApplication.Quit();
381       }
382     }
383   }
384
385   //////////////////////////////////////////////////////////////
386   //
387   // Misc
388   //
389   //
390
391
392   void ChangeImage()
393   {
394     mCurrentImage++;
395     if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
396     {
397       mCurrentImage = 0;
398     }
399
400     mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
401     mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
402   }
403
404
405 private:
406   Application&               mApplication;            ///< Application instance
407   Toolkit::Control           mView;
408   Toolkit::ToolBar           mToolBar;
409   Layer                      mContentLayer;           ///< Content layer (contains actor for this stretch demo)
410
411   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
412
413   // Motion stretch
414   Property::Map mMotionStretchEffect;
415   ImageView mMotionStretchImageView;
416
417   // animate actor to position where user taps screen
418   Animation mActorTapMovementAnimation;
419
420   // show different animations to demonstrate stretch effect working on an object only movement basis
421   bool mActorEffectsEnabled;
422   Animation mActorAnimation;
423   int mCurrentActorAnimation;
424
425   // offer a selection of images that user can cycle between
426   int mCurrentImage;
427
428   TapGestureDetector mTapGestureDetector;
429
430   DeviceOrientation mOrientation;               ///< Current Device orientation
431   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
432
433 };
434
435 int DALI_EXPORT_API main(int argc, char **argv)
436 {
437   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
438   MotionStretchExampleApp test(app);
439   app.MainLoop();
440   return 0;
441 }