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