Merge "Moved Gesture::State and -::Type to gesture-enumerations.h." into devel/master
[platform/core/uifw/dali-demo.git] / examples / shadows-and-lights / shadows-and-lights-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 // INTERNAL INCLUDES
19 #include "shared/view.h"
20
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/shadow-view/shadow-view.h>
24
25 #include <iostream>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29 using std::string;
30 using namespace DemoHelper;
31
32 namespace
33 {
34 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-default.png" );
35 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
36
37 const char* APPLICATION_TITLE_PAN_LIGHT( "Lighting: Pan Light" );
38 const char* APPLICATION_TITLE_ROTATE_OBJECT( "Lighting: Rotate Object" );
39 const char* APPLICATION_TITLE_PAN_SCENE( "Lighting: Pan Scene" );
40 const char* APPLICATION_TITLE_ROTATE_SCENE( "Lighting: Rotate Scene" );
41 const char* CHANGE_EFFECT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
42 const char* CHANGE_EFFECT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
43 const char* RESET_ICON( DEMO_IMAGE_DIR "icon-reset.png" );
44 const char* RESET_ICON_SELECTED( DEMO_IMAGE_DIR "icon-reset-selected.png" );
45
46 const char* SCENE_IMAGE_1( DEMO_IMAGE_DIR "gallery-small-10.jpg");
47 const char* SCENE_IMAGE_2( DEMO_IMAGE_DIR "gallery-small-42.jpg");
48 const char* SCENE_IMAGE_3( DEMO_IMAGE_DIR "gallery-small-48.jpg");
49
50 const float MIN_PINCH_SCALE( 0.3f );
51 const float MAX_PINCH_SCALE( 2.05f );
52
53 const float R3_2(0.8660254);
54 const Vector3 TOP_POINT(  0.0f, -1.0f,  0.0f);
55 const Vector3 LEFT_POINT( -R3_2, 0.5f,  0.0f);
56 const Vector3 RIGHT_POINT( R3_2, 0.5f,  0.0f);
57 const Vector3 FRONT_POINT( 0.0f, 0.0f, 20.0f);
58
59 const Vector2 DEFAULT_WINDOW_SIZE( 480.0f, 800.0f );
60
61 const float X_ROTATION_DISPLACEMENT_FACTOR = 60.f;
62 const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.f;
63 const float LIGHT_PAN_X_DISPLACEMENT_FACTOR = 1/360.f;
64 const float LIGHT_PAN_Y_DISPLACEMENT_FACTOR = 1/360.f;
65
66 }
67
68 /**
69  * This example shows a fixed point light onto an animating set of images
70  * casting a shadow onto a wall. The whole scene can be rotated.
71  */
72
73 class TestApp : public ConnectionTracker
74 {
75 public:
76
77   /**
78    * Constructor
79    * @param application class, stored as reference
80    */
81   TestApp(Application &app)
82   : mApp(app),
83     mView(),
84     mContents(),
85     mSceneActor(),
86     mAnimation(),
87     mSceneAnimation(),
88     mPaused( false ),
89     mShadowView(),
90     mShadowPlaneBg(),
91     mShadowPlane(),
92     mCastingLight(),
93     mLightAnchor(),
94     mImageActor1(),
95     mImageActor2(),
96     mImageActor3(),
97     mPanGestureDetector(),
98     mPinchGestureDetector(),
99     mTapGestureDetector(),
100     mTranslation( 22.0f, -1.0f, 0.0f ),
101     mSceneXRotation( Degree(-6.0f) ), // Initial values give a reasonable off-straight view.
102     mSceneYRotation( Degree(20.0f) ),
103     mLightXRotation( Degree(-1.5f) ),
104     mLightYRotation( Degree(-9.5f) ),
105     mObjectXRotation(0.0f),
106     mObjectYRotation(0.0f),
107     mPinchScale(0.6f),
108     mScaleAtPinchStart(0.6f),
109     mAngle1Index( Property::INVALID_INDEX ),
110     mAngle3Index( Property::INVALID_INDEX ),
111     mTitleActor(),
112     mPanState( PAN_LIGHT )
113   {
114     app.InitSignal().Connect(this, &TestApp::Create);
115     app.TerminateSignal().Connect(this, &TestApp::Terminate);
116   }
117
118   ~TestApp()
119   {
120     // Nothing to do here; All the members of this class get deleted automatically and they delete their children
121   }
122
123 public:
124
125   struct RotationConstraint
126   {
127     RotationConstraint(float sign)
128     : mSign(sign)
129     {
130     }
131
132     void operator()( Quaternion& current, const PropertyInputContainer& inputs )
133     {
134       Radian angle( inputs[0]->GetFloat() );
135       current = Quaternion( angle * mSign, Vector3::YAXIS );
136     }
137
138     float mSign;
139   };
140
141   /**
142    * This method gets called once the main loop of application is up and running
143    */
144   void Create(Application& app)
145   {
146     srand(0); // Want repeatable path
147
148     app.GetWindow().KeyEventSignal().Connect(this, &TestApp::OnKeyEvent);
149
150     CreateToolbarAndView(app);
151     CreateShadowViewAndLights();
152     CreateScene();
153   }
154
155   void CreateToolbarAndView(Application& app)
156   {
157     // Creates a default view with a default tool bar.
158     // The view is added to the window.
159     Toolkit::ToolBar toolBar;
160     mContents = DemoHelper::CreateView( app,
161                                         mView,
162                                         toolBar,
163                                         BACKGROUND_IMAGE,
164                                         TOOLBAR_IMAGE,
165                                         "" );
166
167     // Add an effect-changing button on the right of the tool bar.
168     Toolkit::PushButton effectChangeButton = Toolkit::PushButton::New();
169     effectChangeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_EFFECT_IMAGE );
170     effectChangeButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_EFFECT_IMAGE_SELECTED );
171     effectChangeButton.ClickedSignal().Connect( this, &TestApp::OnEffectButtonClicked );
172     toolBar.AddControl( effectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
173
174     // Add title to the tool bar.
175     mTitleActor = DemoHelper::CreateToolBarLabel( "" );
176     toolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HORIZONTAL_CENTER );
177
178     // Set Title text
179     mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_LIGHT) );
180
181     //Add a reset button
182     Toolkit::PushButton resetButton = Toolkit::PushButton::New();
183     resetButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, RESET_ICON );
184     resetButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, RESET_ICON_SELECTED );
185     resetButton.ClickedSignal().Connect( this, &TestApp::OnResetPressed );
186     toolBar.AddControl( resetButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_CENTER, DemoHelper::DEFAULT_PLAY_PADDING );
187
188     // Setup
189     mView.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, 0.0f ) );
190
191     mContents.SetProperty( Layer::Property::BEHAVIOR, Layer::LAYER_3D );
192     mContents.SetProperty( Actor::Property::POSITION, mTranslation );
193     mContents.SetProperty( Actor::Property::ORIENTATION, CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
194     mContents.SetProperty( Actor::Property::SCALE, Vector3( mPinchScale, mPinchScale, mPinchScale ) );
195
196     mPanGestureDetector = PanGestureDetector::New();
197     mPanGestureDetector.Attach( mView );
198     mPanGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPan);
199
200     mPinchGestureDetector = PinchGestureDetector::New();
201     mPinchGestureDetector.Attach( mView );
202     mPinchGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPinch);
203
204     mTapGestureDetector = TapGestureDetector::New();
205     mTapGestureDetector.Attach( mView );
206     mTapGestureDetector.DetectedSignal().Connect(this, &TestApp::OnTap);
207   }
208
209
210
211   void CreateShadowViewAndLights()
212   {
213     mShadowView = Toolkit::ShadowView::New();
214     mShadowView.SetProperty( Dali::Actor::Property::NAME,"Container");
215     mShadowView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
216     mShadowView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
217     mShadowView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
218     mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
219     mContents.Add(mShadowView);
220
221     mShadowPlaneBg = ImageView::New( DEMO_IMAGE_DIR "brick-wall.jpg" );
222     mShadowPlaneBg.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
223     mShadowPlaneBg.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
224     mShadowPlaneBg.SetProperty( Dali::Actor::Property::NAME,"Plane");
225     mShadowPlaneBg.SetProperty( Actor::Property::SIZE, Vector2(1000.0f, 1000.0f) );
226     mContents.Add(mShadowPlaneBg);
227     mShadowPlaneBg.SetProperty( Actor::Property::POSITION, Vector3(50.0f, 50.0f, -200.0f));
228
229     mShadowView.SetShadowPlaneBackground(mShadowPlaneBg);
230     mShadowView.Activate();
231
232     mLightAnchor = Actor::New();
233     mLightAnchor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
234     mLightAnchor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
235     mLightAnchor.SetProperty( Actor::Property::ORIENTATION, CalculateWorldRotation( mLightXRotation, mLightYRotation ) );
236
237     // Work out a scaling factor as the initial light position was calculated for desktop
238     // Need to scale light position as scene actor size is based on window size (i.e. much bigger on device)
239     Vector2 windowSize( mApp.GetWindow().GetSize() );
240     float scaleFactor = windowSize.x / DEFAULT_WINDOW_SIZE.x;
241
242     mCastingLight = Actor::New();
243     mCastingLight.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
244     mCastingLight.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
245     mCastingLight.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, 800.0f ) * scaleFactor );
246
247     TextLabel text = TextLabel::New( "Light" );
248     text.SetProperty( TextLabel::Property::POINT_SIZE, 20.0f );
249     text.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
250     text.SetProperty( Actor::Property::COLOR, Color::BLUE );
251
252     mCastingLight.Add(text);
253     mLightAnchor.Add(mCastingLight);
254     mShadowPlaneBg.Add(mLightAnchor);
255
256     text.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
257     mShadowView.SetPointLight(mCastingLight);
258   }
259
260   void CreateScene()
261   {
262     mSceneActor = Actor::New();
263     mSceneActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
264
265     // Create and add images to the scene actor:
266     mImageActor1 = ImageView::New( SCENE_IMAGE_1 );
267     mImageActor2 = ImageView::New( SCENE_IMAGE_2 );
268     mImageActor3 = ImageView::New( SCENE_IMAGE_3 );
269
270     mImageActor1.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
271     mImageActor2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
272     mImageActor3.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
273
274     mImageActor2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
275
276     mImageActor1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER_LEFT);
277     mImageActor1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER_RIGHT);
278
279     mImageActor3.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER_RIGHT);
280     mImageActor3.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER_LEFT);
281
282     mSceneActor.Add(mImageActor2);
283     mImageActor2.Add(mImageActor1);
284     mImageActor2.Add(mImageActor3);
285
286     Property::Index angleIndex = mImageActor2.RegisterProperty("angle", Property::Value( Dali::ANGLE_30 ) );
287     Source angleSrc( mImageActor2, angleIndex );
288
289     Constraint constraint = Constraint::New<Quaternion>( mImageActor1, Actor::Property::ORIENTATION, RotationConstraint(-1.0f) );
290     constraint.AddSource( angleSrc );
291     constraint.Apply();
292
293     constraint = Constraint::New<Quaternion>( mImageActor3, Actor::Property::ORIENTATION, RotationConstraint(+1.0f) );
294     constraint.AddSource( angleSrc );
295     constraint.Apply();
296
297     mSceneAnimation = Animation::New(2.5f);
298
299     // Want to animate angle from 30 => -30 and back again smoothly.
300
301     mSceneAnimation.AnimateTo( Property( mImageActor2, angleIndex ), Property::Value(-Dali::ANGLE_30), AlphaFunction::SIN );
302
303     mSceneAnimation.SetLooping(true);
304     mSceneAnimation.Play();
305
306     mSceneActor.SetProperty( Actor::Property::SIZE, Vector2( 250.0f, 250.0f ) );
307     mSceneActor.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, 130.0f ) );
308     mShadowView.Add(mSceneActor);
309   }
310
311
312   Quaternion CalculateWorldRotation( Radian XRotation, Radian YRotation )
313   {
314     Quaternion p( XRotation, Vector3::XAXIS );
315     Quaternion q( YRotation, Vector3::YAXIS );
316     return p*q;
317   }
318
319   void OnTap(Dali::Actor actor, const TapGesture& gesture)
320   {
321     if( mSceneAnimation )
322     {
323       if( ! mPaused )
324       {
325         mSceneAnimation.Pause();
326         mPaused = true;
327       }
328       else
329       {
330         mSceneAnimation.Play();
331         mPaused = false;
332       }
333     }
334   }
335
336   void OnPan(Actor actor, const PanGesture& gesture)
337   {
338     switch (gesture.GetState())
339     {
340       case GestureState::CONTINUING:
341       {
342         const Vector2& displacement = gesture.GetDisplacement();
343         switch(mPanState)
344         {
345           case PAN_LIGHT:
346           {
347             mLightXRotation = mLightXRotation - displacement.y * LIGHT_PAN_X_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
348             mLightXRotation = Clamp(mLightXRotation, -Dali::ANGLE_45, Dali::ANGLE_45 );
349             mLightYRotation = mLightYRotation + displacement.x * LIGHT_PAN_Y_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
350             mLightYRotation = Clamp(mLightYRotation, -Dali::ANGLE_45, Dali::ANGLE_45 );
351             mLightAnchor.SetProperty( Actor::Property::ORIENTATION, CalculateWorldRotation( mLightXRotation, mLightYRotation ) );
352             break;
353           }
354
355           case PAN_SCENE:
356           {
357             mTranslation += Vector3(displacement.x, displacement.y, 0.f);
358             mContents.SetProperty( Actor::Property::POSITION, mTranslation );
359             break;
360           }
361
362           case ROTATE_SCENE:
363           {
364             mSceneXRotation = mSceneXRotation - displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
365             mSceneXRotation = Clamp( mSceneXRotation, -Dali::ANGLE_90, Dali::ANGLE_90 );
366             mSceneYRotation = mSceneYRotation + displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
367             mSceneYRotation = Clamp( mSceneYRotation, -Dali::ANGLE_90, Dali::ANGLE_90 );
368             mContents.SetProperty( Actor::Property::ORIENTATION, CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
369             break;
370           }
371
372           case ROTATE_OBJECT:
373           {
374             mObjectXRotation = mObjectXRotation - displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
375             mObjectYRotation = mObjectYRotation + displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
376             mSceneActor.SetProperty( Actor::Property::ORIENTATION, CalculateWorldRotation( mObjectXRotation, mObjectYRotation ) );
377             break;
378           }
379         }
380       }
381       break;
382
383       case GestureState::FINISHED:
384         // Start animation at last known speed
385         break;
386
387       default:
388         break;
389     }
390   }
391
392   void OnPinch(Actor actor, const PinchGesture& gesture)
393   {
394     if (gesture.GetState() == GestureState::STARTED)
395     {
396       mScaleAtPinchStart = mContents.GetCurrentProperty< Vector3 >( Actor::Property::SCALE ).x;
397     }
398     mPinchScale = Clamp(mScaleAtPinchStart * gesture.GetScale(), MIN_PINCH_SCALE, MAX_PINCH_SCALE);
399
400     mContents.SetProperty( Actor::Property::SCALE, Vector3( mPinchScale, mPinchScale, mPinchScale ) );
401   }
402
403   void Terminate(Application& app)
404   {
405     if( mSceneActor )
406     {
407       mApp.GetWindow().Remove(mSceneActor);
408     }
409     if( mView )
410     {
411       mApp.GetWindow().Remove(mView);
412     }
413   }
414
415   void OnKeyEvent(const KeyEvent& event)
416   {
417     if(event.GetState() == KeyEvent::DOWN)
418     {
419       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
420       {
421         mApp.Quit();
422       }
423     }
424   }
425
426   bool OnEffectButtonClicked( Toolkit::Button button )
427   {
428     switch(mPanState)
429     {
430       case PAN_LIGHT:
431         mPanState = ROTATE_OBJECT;
432         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_ROTATE_OBJECT) );
433         break;
434       case ROTATE_OBJECT:
435         mPanState = ROTATE_SCENE;
436         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_ROTATE_SCENE) );
437         break;
438       case ROTATE_SCENE:
439         mPanState = PAN_SCENE;
440         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_SCENE) );
441         break;
442       case PAN_SCENE:
443         mPanState = PAN_LIGHT;
444         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_LIGHT) );
445         break;
446       default:
447         break;
448     }
449
450     return true;
451   }
452
453   bool OnResetPressed( Toolkit::Button button )
454   {
455     // Reset translation
456     mTranslation = Vector3::ZERO;
457     mContents.SetProperty( Actor::Property::POSITION, mTranslation );
458
459     // Align scene so that light anchor orientation is Z Axis
460     mSceneXRotation = -mLightXRotation;
461     mSceneYRotation = -mLightYRotation;
462     mContents.SetProperty( Actor::Property::ORIENTATION, CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
463
464     return true;
465   }
466
467 private:
468   Application&              mApp;
469   Toolkit::Control          mView;
470   Layer                     mContents;
471   Actor                     mSceneActor;
472   Animation                 mAnimation;
473   Animation                 mSceneAnimation;
474   bool                      mPaused;
475   Toolkit::ShadowView       mShadowView;
476   ImageView                 mShadowPlaneBg;
477   ImageView                 mShadowPlane;
478   Actor                     mCastingLight;
479   Actor                     mLightAnchor;
480   ImageView                 mImageActor1;
481   ImageView                 mImageActor2;
482   ImageView                 mImageActor3;
483   PanGestureDetector        mPanGestureDetector;
484   PinchGestureDetector      mPinchGestureDetector;
485   TapGestureDetector        mTapGestureDetector;
486   Vector3                   mTranslation;
487   Radian                    mSceneXRotation;
488   Radian                    mSceneYRotation;
489   Radian                    mLightXRotation;
490   Radian                    mLightYRotation;
491   Radian                    mObjectXRotation;
492   Radian                    mObjectYRotation;
493   float                     mPinchScale;
494   float                     mScaleAtPinchStart;
495
496   Property::Index           mAngle1Index;
497   Property::Index           mAngle3Index;
498
499   Toolkit::TextLabel         mTitleActor;
500
501   enum PanState
502   {
503     PAN_SCENE,
504     ROTATE_SCENE,
505     PAN_LIGHT,
506     ROTATE_OBJECT
507   };
508
509   PanState                  mPanState;
510 };
511
512 /*****************************************************************************/
513
514 int DALI_EXPORT_API main(int argc, char **argv)
515 {
516   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
517   TestApp theApp(app);
518   app.MainLoop();
519   return 0;
520 }