Merge "Remove non-touch related deprecated APIs" into devel/master
[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
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.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
148
149     //Add an slideshow icon on the right of the title
150     mActorEffectsButton = Toolkit::PushButton::New();
151     mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
152     mActorEffectsButton.SetProperty( Toolkit::Button::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::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
160     layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
161     layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
162     layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, 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.ResizeSignal().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.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
190     mMotionStretchImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
191     mMotionStretchImageView.SetProperty( Actor::Property::SIZE, Vector2( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT ) );
192     // Add stretch padding
193     mMotionStretchImageView.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, Vector2( MOTION_STRETCH_ACTOR_WIDTH+32, MOTION_STRETCH_ACTOR_HEIGHT+32 ) );
194
195     mContentLayer.Add( mMotionStretchImageView );
196
197     // Create shader used for doing motion stretch
198     Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
199   }
200
201   //////////////////////////////////////////////////////////////
202   //
203   // Device Orientation Support
204   //
205   //
206
207   void OnWindowResized( Window window, Window::WindowSize size )
208   {
209     Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
210   }
211
212   void Rotate( DeviceOrientation orientation )
213   {
214     // Resize the root actor
215     const Vector2 targetSize = Stage::GetCurrent().GetSize();
216
217     if( mOrientation != orientation )
218     {
219       mOrientation = orientation;
220
221       // check if actor is on stage
222       if( mView.GetParent() )
223       {
224         // has parent so we expect it to be on stage, start animation
225         mRotateAnimation = Animation::New( ORIENTATION_DURATION );
226         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
227         mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
228         mRotateAnimation.Play();
229       }
230       else
231       {
232         mView.SetProperty( Actor::Property::SIZE, targetSize );
233       }
234     }
235     else
236     {
237       // for first time just set size
238       mView.SetProperty( Actor::Property::SIZE, targetSize );
239     }
240   }
241
242   //////////////////////////////////////////////////////////////
243   //
244   // Actor Animation
245   //
246   //
247
248   // move to point on screen that was tapped
249   void OnTap( Actor actor, const TapGesture& tapGesture )
250   {
251     Vector3 destPos;
252     float originOffsetX, originOffsetY;
253
254     // rotate offset (from top left origin to centre) into actor space
255     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
256     actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
257
258     // get dest point in local actor space
259     destPos.x = tapGesture.localPoint.x - originOffsetX;
260     destPos.y = tapGesture.localPoint.y - originOffsetY;
261     destPos.z = 0.0f;
262
263     float animDuration = 0.5f;
264     mActorTapMovementAnimation = Animation::New( animDuration );
265     if ( mMotionStretchImageView )
266     {
267       mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
268     }
269     mActorTapMovementAnimation.SetEndAction( Animation::Bake );
270     mActorTapMovementAnimation.Play();
271
272
273     // perform some spinning etc
274     if(mActorEffectsEnabled)
275     {
276       switch(mCurrentActorAnimation)
277       {
278         // spin around y
279         case 0:
280         {
281           float animDuration = 1.0f;
282           mActorAnimation = Animation::New(animDuration);
283           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
284           mActorAnimation.SetEndAction( Animation::Bake );
285           mActorAnimation.Play();
286         }
287         break;
288
289         // spin around z
290         case 1:
291         {
292           float animDuration = 1.0f;
293           mActorAnimation = Animation::New(animDuration);
294           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
295           mActorAnimation.SetEndAction( Animation::Bake );
296           mActorAnimation.Play();
297         }
298         break;
299
300         // spin around y and z
301         case 2:
302         {
303           float animDuration = 1.0f;
304           mActorAnimation = Animation::New(animDuration);
305           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
306           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, 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         // scale
313         case 3:
314         {
315           float animDuration = 1.0f;
316           mActorAnimation = Animation::New(animDuration);
317           mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
318           mActorAnimation.SetEndAction( Animation::Bake );
319           mActorAnimation.Play();
320         }
321         break;
322
323         default:
324           break;
325       }
326
327       mCurrentActorAnimation++;
328       if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
329       {
330         mCurrentActorAnimation = 0;
331       }
332     }
333   }
334
335   void ToggleActorEffects()
336   {
337     if(!mActorEffectsEnabled)
338     {
339       mActorEffectsEnabled = true;
340       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
341       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
342     }
343     else
344     {
345       mActorEffectsEnabled = false;
346       mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
347       mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
348     }
349   }
350
351   //////////////////////////////////////////////////////////////
352   //
353   // Input handlers
354   //
355   //
356
357   bool OnLayoutButtonClicked( Toolkit::Button button )
358   {
359     ChangeImage();
360     return true;
361   }
362
363   bool OnEffectButtonClicked( Toolkit::Button button )
364   {
365     ToggleActorEffects();
366     return true;
367   }
368
369   /**
370    * Main key event handler
371    */
372   void OnKeyEvent(const KeyEvent& event)
373   {
374     if(event.state == KeyEvent::Down)
375     {
376       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
377       {
378         mApplication.Quit();
379       }
380     }
381   }
382
383   //////////////////////////////////////////////////////////////
384   //
385   // Misc
386   //
387   //
388
389
390   void ChangeImage()
391   {
392     mCurrentImage++;
393     if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
394     {
395       mCurrentImage = 0;
396     }
397
398     mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
399     mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
400   }
401
402
403 private:
404   Application&               mApplication;            ///< Application instance
405   Toolkit::Control           mView;
406   Toolkit::ToolBar           mToolBar;
407   Layer                      mContentLayer;           ///< Content layer (contains actor for this stretch demo)
408
409   PushButton                 mActorEffectsButton;     ///< The actor effects toggling Button.
410
411   // Motion stretch
412   Property::Map mMotionStretchEffect;
413   ImageView mMotionStretchImageView;
414
415   // animate actor to position where user taps screen
416   Animation mActorTapMovementAnimation;
417
418   // show different animations to demonstrate stretch effect working on an object only movement basis
419   bool mActorEffectsEnabled;
420   Animation mActorAnimation;
421   int mCurrentActorAnimation;
422
423   // offer a selection of images that user can cycle between
424   int mCurrentImage;
425
426   TapGestureDetector mTapGestureDetector;
427
428   DeviceOrientation mOrientation;               ///< Current Device orientation
429   Animation mRotateAnimation;                   ///< Animation for rotating between landscape and portrait.
430
431 };
432
433 int DALI_EXPORT_API main(int argc, char **argv)
434 {
435   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
436   MotionStretchExampleApp test(app);
437   app.MainLoop();
438   return 0;
439 }