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