17b9c1dacf470161613441efaf8f1950b1577eb4
[platform/core/uifw/dali-demo.git] / examples / mesh-visual / mesh-visual-example.cpp
1 #include <dali-toolkit/dali-toolkit.h>
2
3 using namespace Dali;
4 using namespace Dali::Toolkit;
5
6 namespace
7 {
8 // Keeps information about each model for access.
9 struct Model
10 {
11   Control control; // Control housing the mesh visual of the model.
12   Vector2 rotation; // Keeps track of rotation about x and y axis for manual rotation.
13   Animation rotationAnimation; // Automatically rotates when left alone.
14 };
15
16 // Files for meshes
17 const char * const MODEL_FILE_TABLE[] =
18 {
19     DEMO_MODEL_DIR "Dino.obj",
20     DEMO_MODEL_DIR "ToyRobot-Metal.obj",
21     DEMO_MODEL_DIR "Toyrobot-Plastic.obj"
22 };
23
24 const char * const MATERIAL_FILE_TABLE[] =
25 {
26     DEMO_MODEL_DIR "Dino.mtl",
27     DEMO_MODEL_DIR "ToyRobot-Metal.mtl",
28     DEMO_MODEL_DIR "Toyrobot-Plastic.mtl"
29 };
30
31 const char * const TEXTURES_PATH( DEMO_IMAGE_DIR "" );
32
33 // Possible shading modes.
34 MeshVisual::ShadingMode::Value SHADING_MODE_TABLE[] =
35 {
36   MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING,
37   MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING,
38   MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING
39 };
40
41 // Button labels.
42 const char * const PAUSE =  "  ||  ";
43 const char * const PLAY =   "  >  ";
44 const char * const FIXED =  "FIXED";
45 const char * const MANUAL = "MANUAL";
46 const char * const FRONT =  "FRONT";
47 const char * const BACK =   "BACK";
48
49 // Image urls for the light.
50 const char * const LIGHT_URL_FRONT = DEMO_IMAGE_DIR "light-icon-front.png";
51 const char * const LIGHT_URL_BACK =  DEMO_IMAGE_DIR "light-icon-back.png";
52
53 const float X_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
54 const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
55 const float MODEL_SCALE =                    0.75f;
56 const float LIGHT_SCALE =                    0.15f;
57 const float BUTTONS_OFFSET_BOTTOM =          0.08f;
58 const float BUTTONS_OFFSET_SIDE =            0.2f;
59 const int   NUM_MESHES =                     2;
60
61 // Used to identify actors.
62 const int MODEL_TAG = 0;
63 const int LIGHT_TAG = 1;
64 const int LAYER_TAG = 2;
65
66 const Vector4 WINDOW_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the window
67
68 } // unnamed namespace
69
70 class MeshVisualController : public ConnectionTracker
71 {
72 public:
73
74   MeshVisualController( Application& application )
75   : mApplication( application ),   //Store handle to the application.
76     mModelIndex( 1 ),              //Start with metal robot.
77     mShadingModeIndex( 0 ),        //Start with texture and detailed specular lighting.
78     mTag( -1 ),                    //Non-valid default, which will get set to a correct value when used.
79     mSelectedModelIndex( -1 ),     //Non-valid default, which will get set to a correct value when used.
80     mPaused( false ),              //Animations play by default.
81     mLightFixed( true ),           //The light is fixed by default.
82     mLightFront( true )            //The light is in front by default.
83   {
84     // Connect to the Application's Init signal
85     mApplication.InitSignal().Connect( this, &MeshVisualController::Create );
86   }
87
88   ~MeshVisualController()
89   {
90   }
91
92   // The Init signal is received once (only) during the Application lifetime
93   void Create( Application& application )
94   {
95     // Get a handle to the window
96     Window window = application.GetWindow();
97     window.SetBackgroundColor( WINDOW_COLOR );
98
99     //Set up root layer to receive touch gestures.
100     Layer rootLayer = window.GetRootLayer();
101     rootLayer.RegisterProperty( "Tag", LAYER_TAG ); //Used to differentiate between different kinds of actor.
102     rootLayer.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
103
104     //Place models on the scene.
105     SetupModels( rootLayer );
106
107     //Place buttons on the scene.
108     SetupButtons( rootLayer );
109
110     //Add a light to the scene.
111     SetupLight( rootLayer );
112
113     //Allow for exiting of the application via key presses.
114     window.KeyEventSignal().Connect( this, &MeshVisualController::OnKeyEvent );
115   }
116
117   //Loads and adds the models to the scene, inside containers for hit detection.
118   void SetupModels( Layer layer )
119   {
120     //Add containers to house each renderer-holding-actor.
121     for( int i = 0; i < NUM_MESHES; i++ )
122     {
123       mContainers[i] = Actor::New();
124       mContainers[i].SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
125       mContainers[i].RegisterProperty( "Tag", MODEL_TAG ); //Used to differentiate between different kinds of actor.
126       mContainers[i].RegisterProperty( "Model", Property::Value( i ) ); //Used to index into the model.
127       mContainers[i].TouchSignal().Connect( this, &MeshVisualController::OnTouch );
128       layer.Add( mContainers[i] );
129     }
130
131     //Position each container individually on screen
132
133     //Main, central model
134     mContainers[0].SetProperty( Actor::Property::SIZE_MODE_FACTOR, Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
135     mContainers[0].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
136     mContainers[0].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
137
138     //Top left model
139     mContainers[1].SetProperty( Actor::Property::SIZE_MODE_FACTOR, Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
140     mContainers[1].SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
141     mContainers[1].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
142
143     //Set up models
144     for( int i = 0; i < NUM_MESHES; i++ )
145     {
146       //Create control to display model
147       Control control = Control::New();
148       control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
149       control.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
150       control.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
151       mContainers[i].Add( control );
152
153       //Make model spin to demonstrate 3D
154       Animation rotationAnimation = Animation::New( 15.0f );
155       float spin = i % 2 == 0 ? 1.0f : -1.0f; //Make actors spin in different directions to better show independence.
156       rotationAnimation.AnimateBy( Property( control, Actor::Property::ORIENTATION ),
157                                    Quaternion( Degree( 0.0f ), Degree( spin * 360.0f ), Degree( 0.0f ) ) );
158       rotationAnimation.SetLooping( true );
159       rotationAnimation.Play();
160
161       //Store model information in corresponding structs.
162       mModels[i].control = control;
163       mModels[i].rotation.x = 0.0f;
164       mModels[i].rotation.y = 0.0f;
165       mModels[i].rotationAnimation = rotationAnimation;
166     }
167
168     //Calling this sets the model in the controls.
169     ReloadModel();
170   }
171
172   //Place the various buttons on the bottom of the screen, with title labels where necessary.
173   void SetupButtons( Layer layer )
174   {
175     //Actor for positioning model and shading mode buttons.
176     Actor positionActorModel = Actor::New();
177     positionActorModel.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( BUTTONS_OFFSET_SIDE, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
178     positionActorModel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
179     layer.Add( positionActorModel );
180
181     //Create button for model changing.
182     PushButton modelButton = Toolkit::PushButton::New();
183     modelButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
184     modelButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeModelClicked );
185     modelButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
186     modelButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
187     modelButton.SetProperty( Toolkit::Button::Property::LABEL, "Model" );
188     positionActorModel.Add( modelButton );
189
190     //Create button for shading mode changing.
191     PushButton shadingModeButton = Toolkit::PushButton::New();
192     shadingModeButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
193     shadingModeButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeShadingModeClicked );
194     shadingModeButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
195     shadingModeButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
196     shadingModeButton.SetProperty( Toolkit::Button::Property::LABEL, "Shading Mode" );
197     positionActorModel.Add( shadingModeButton );
198
199     //Text label title for changing model or shading mode.
200     TextLabel changeTitleLabel = TextLabel::New( "Change" );
201     changeTitleLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
202     changeTitleLabel.SetProperty( TextLabel::Property::UNDERLINE, "{\"thickness\":\"2.0\"}" );
203     changeTitleLabel.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
204     changeTitleLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
205     modelButton.Add( changeTitleLabel );
206
207     //Create button for pausing animations.
208     PushButton pauseButton = Toolkit::PushButton::New();
209     pauseButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
210     pauseButton.ClickedSignal().Connect( this, &MeshVisualController::OnPauseClicked );
211     pauseButton.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 0.5, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
212     pauseButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
213     pauseButton.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
214     layer.Add( pauseButton );
215
216     //Actor for positioning light position buttons.
217     Actor positionActorLight = Actor::New();
218     positionActorLight.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 1.0 - BUTTONS_OFFSET_SIDE, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
219     positionActorLight.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
220     layer.Add( positionActorLight );
221
222     //Create button for switching between manual and fixed light position.
223     PushButton lightModeButton = Toolkit::PushButton::New();
224     lightModeButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
225     lightModeButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeLightModeClicked );
226     lightModeButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
227     lightModeButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
228     lightModeButton.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
229     positionActorLight.Add( lightModeButton );
230
231     //Create button for switching between front and back light position.
232     PushButton lightSideButton = Toolkit::PushButton::New();
233     lightSideButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
234     lightSideButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeLightSideClicked );
235     lightSideButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
236     lightSideButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
237     lightSideButton.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
238     positionActorLight.Add( lightSideButton );
239
240     //Text label title for light position mode.
241     TextLabel lightTitleLabel = TextLabel::New( "Light Position" );
242     lightTitleLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
243     lightTitleLabel.SetProperty( TextLabel::Property::UNDERLINE, "{\"thickness\":\"2.0\"}" );
244     lightTitleLabel.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
245     lightTitleLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
246     lightModeButton.Add( lightTitleLabel );
247   }
248
249   //Add a point light source the the scene, on a layer above the first.
250   void SetupLight( Layer baseLayer )
251   {
252     //Create control to act as light source of scene.
253     mLightSource = Control::New();
254     mLightSource.RegisterProperty( "Tag", LIGHT_TAG );
255
256     //Set size of control based on screen dimensions.
257     Window window = mApplication.GetWindow();
258     Vector2 windowSize = window.GetSize();
259     if( windowSize.width < windowSize.height )
260     {
261       //Scale to width.
262       mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH );
263       mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
264       mLightSource.SetProperty( Actor::Property::SIZE_MODE_FACTOR, Vector3( LIGHT_SCALE, 0.0f, 0.0f ) );
265     }
266     else
267     {
268       //Scale to height.
269       mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
270       mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::WIDTH );
271       mLightSource.SetProperty( Actor::Property::SIZE_MODE_FACTOR, Vector3( 0.0f, LIGHT_SCALE, 0.0f ) );
272     }
273
274     //Set position relative to top left, as the light source property is also relative to the top left.
275     mLightSource.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
276     mLightSource.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
277     mLightSource.SetProperty( Actor::Property::POSITION, Vector2( windowSize.width * 0.85f, windowSize.height * 0.125 ));
278
279     //Supply an image to represent the light.
280     SetLightImage();
281
282     //Connect to touch signal for dragging.
283     mLightSource.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
284
285     //Place the light source on a layer above the base, so that it is rendered above everything else.
286     Layer upperLayer = Layer::New();
287     upperLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
288     upperLayer.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
289     upperLayer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
290
291     baseLayer.Add( upperLayer );
292     upperLayer.Add( mLightSource );
293
294     //Decide which light to use to begin with.
295     SetLightMode();
296   }
297
298   //Sets the image to use for the light source depending on whether the light is in front or behind.
299   void SetLightImage()
300   {
301     std::string imageUrl;
302
303     if( mLightFront )
304     {
305       imageUrl = LIGHT_URL_FRONT;
306     }
307     else
308     {
309       imageUrl = LIGHT_URL_BACK;
310     }
311
312     Property::Map lightMap;
313     lightMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
314     lightMap.Insert( ImageVisual::Property::URL, imageUrl );
315     mLightSource.SetProperty( Control::Property::BACKGROUND, Property::Value( lightMap ) );
316   }
317
318   //Updates the displayed models to account for parameter changes.
319   void ReloadModel()
320   {
321     //Create mesh property map
322     Property::Map map;
323     map.Insert( Toolkit::Visual::Property::TYPE,  Visual::MESH );
324     map.Insert( Visual::Property::TRANSFORM,
325                 Property::Map().Add( Visual::Transform::Property::ORIGIN, Align::CENTER )
326                                .Add( Visual::Transform::Property::ANCHOR_POINT, Align::CENTER ) );
327     map.Insert( MeshVisual::Property::OBJECT_URL, MODEL_FILE_TABLE[mModelIndex] );
328     map.Insert( MeshVisual::Property::MATERIAL_URL, MATERIAL_FILE_TABLE[mModelIndex] );
329     map.Insert( MeshVisual::Property::TEXTURES_PATH, TEXTURES_PATH );
330     map.Insert( MeshVisual::Property::SHADING_MODE, SHADING_MODE_TABLE[mShadingModeIndex] );
331
332     //Set the two controls to use the mesh
333     for( int i = 0; i < NUM_MESHES; i++ )
334     {
335       mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
336     }
337   }
338
339   //Set the mode used to light the models.
340   void SetLightMode()
341   {
342     if( mLightFixed )
343     {
344       UseFixedLight();
345     }
346     else
347     {
348       UseManualLight();
349     }
350   }
351
352   //Make the models use a fixed, invisible light above the center of the window.
353   void UseFixedLight()
354   {
355     //Hide draggable source
356     mLightSource.SetProperty( Actor::Property::VISIBLE, false );
357
358     //Use window dimensions to place light at center, offset in z axis.
359     Window window = mApplication.GetWindow();
360     float width = window.GetSize().GetWidth();
361     float height = window.GetSize().GetHeight();
362     Vector3 lightPosition = Vector3( width / 2.0f, height / 2.0f,
363                                      ( mLightFront ? 1 : -1 ) * std::max( width, height ) * 5.0f );
364
365     //Set global light position
366     for( int i = 0; i < NUM_MESHES; ++i )
367     {
368       mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
369     }
370   }
371
372   //Make the models use a light source that the user can drag around.
373   void UseManualLight()
374   {
375     //Show draggable source
376     mLightSource.SetProperty( Actor::Property::VISIBLE, true );
377
378     //Update to switch light position of models to that of the source.
379     UpdateLight();
380   }
381
382   //Updates the light position for each model to account for changes in the source on screen.
383   void UpdateLight()
384   {
385     //Set light position to the x and y of the light control, offset into/out of the screen.
386     Vector3 controlPosition = mLightSource.GetCurrentProperty< Vector3 >( Actor::Property::POSITION );
387     Vector3 lightPosition = Vector3( controlPosition.x, controlPosition.y,
388                                      ( mLightFront ? 1 : -1 ) * mApplication.GetWindow().GetSize().GetWidth() / 2.0f );
389
390     for( int i = 0; i < NUM_MESHES; ++i )
391     {
392       mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
393     }
394   }
395
396   //If the light source is touched, move it by dragging it.
397   //If a model is touched, rotate it by panning around.
398   bool OnTouch( Actor actor, const TouchEvent& touch )
399   {
400     switch( touch.GetState( 0 ) )
401     {
402       case PointState::DOWN:
403       {
404         //Determine what was touched.
405         actor.GetProperty( actor.GetPropertyIndex( "Tag" ) ).Get( mTag );
406
407         if( mTag == MODEL_TAG )
408         {
409           //Find out which model has been selected
410           actor.GetProperty( actor.GetPropertyIndex( "Model" ) ).Get( mSelectedModelIndex );
411
412           //Pause current animation, as the touch gesture will be used to manually rotate the model
413           mModels[mSelectedModelIndex].rotationAnimation.Pause();
414
415           //Store start points.
416           mPanStart = touch.GetScreenPosition( 0 );
417           mRotationStart = mModels[mSelectedModelIndex].rotation;
418         }
419
420         break;
421       }
422       case PointState::MOTION:
423       {
424         //Switch on the kind of actor we're interacting with.
425         switch( mTag )
426         {
427           case MODEL_TAG: //Rotate model
428           {
429             //Calculate displacement and corresponding rotation.
430             Vector2 displacement = touch.GetScreenPosition( 0 ) - mPanStart;
431             mModels[mSelectedModelIndex].rotation = Vector2( mRotationStart.x - displacement.y / Y_ROTATION_DISPLACEMENT_FACTOR,   // Y displacement rotates around X axis
432                                                              mRotationStart.y + displacement.x / X_ROTATION_DISPLACEMENT_FACTOR ); // X displacement rotates around Y axis
433             Quaternion rotation = Quaternion( Radian( mModels[mSelectedModelIndex].rotation.x ), Vector3::XAXIS) *
434                                   Quaternion( Radian( mModels[mSelectedModelIndex].rotation.y ), Vector3::YAXIS);
435
436             //Apply rotation.
437             mModels[mSelectedModelIndex].control.SetProperty( Actor::Property::ORIENTATION, rotation );
438
439             break;
440           }
441           case LIGHT_TAG: //Drag light
442           {
443             //Set light source to new position and update the models accordingly.
444             mLightSource.SetProperty( Actor::Property::POSITION, Vector3( touch.GetScreenPosition( 0 ) ) );
445             UpdateLight();
446
447             break;
448           }
449         }
450
451         break;
452       }
453       case PointState::INTERRUPTED: //Same as finished.
454       case PointState::FINISHED:
455       {
456         if( mTag == MODEL_TAG )
457         {
458           //Return to automatic animation
459           if( !mPaused )
460           {
461             mModels[mSelectedModelIndex].rotationAnimation.Play();
462           }
463         }
464
465         break;
466       }
467       default:
468       {
469         //Other touch states do nothing.
470         break;
471       }
472     }
473
474     return true;
475   }
476
477   //Cycle through the list of models.
478   bool OnChangeModelClicked( Toolkit::Button button )
479   {
480     ++mModelIndex %= 3;
481
482     ReloadModel();
483
484     return true;
485   }
486
487   //Cycle through the list of shading modes.
488   bool OnChangeShadingModeClicked( Toolkit::Button button )
489   {
490     ++mShadingModeIndex %= 3;
491
492     ReloadModel();
493
494     return true;
495   }
496
497   //Pause all animations, and keep them paused even after user panning.
498   //This button is a toggle, so pressing again will start the animations again.
499   bool OnPauseClicked( Toolkit::Button button )
500   {
501     //Toggle pause state.
502     mPaused = !mPaused;
503
504     //If we wish to pause animations, do so and keep them paused.
505     if( mPaused )
506     {
507       for( int i = 0; i < NUM_MESHES ; ++i )
508       {
509         mModels[i].rotationAnimation.Pause();
510       }
511
512       button.SetProperty( Toolkit::Button::Property::LABEL, PLAY );
513     }
514     else //Unpause all animations again.
515     {
516       for( int i = 0; i < NUM_MESHES ; ++i )
517       {
518         mModels[i].rotationAnimation.Play();
519       }
520
521       button.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
522     }
523
524     return true;
525   }
526
527
528   //Switch between a fixed light source above/behind the screen, and a light source the user can drag around.
529   bool OnChangeLightModeClicked( Toolkit::Button button )
530   {
531     //Toggle state.
532     mLightFixed = !mLightFixed;
533
534     if( mLightFixed )
535     {
536       button.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
537     }
538     else
539     {
540       button.SetProperty( Toolkit::Button::Property::LABEL, MANUAL );
541     }
542
543     SetLightMode();
544
545     return true;
546   }
547
548   //Switch between the light being in front of and behind the models.
549   bool OnChangeLightSideClicked( Toolkit::Button button )
550   {
551     //Toggle state.
552     mLightFront = !mLightFront;
553
554     if( mLightFront )
555     {
556       button.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
557     }
558     else
559     {
560       button.SetProperty( Toolkit::Button::Property::LABEL, BACK );
561     }
562
563     //Change light image.
564     SetLightImage();
565
566     //Update light to account for the change.
567     SetLightMode();
568
569     return true;
570   }
571
572   //If escape or the back button is pressed, quit the application (and return to the launcher)
573   void OnKeyEvent( const KeyEvent& event )
574   {
575     if( event.GetState() == KeyEvent::DOWN )
576     {
577       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
578       {
579         mApplication.Quit();
580       }
581     }
582   }
583
584 private:
585   Application&  mApplication;
586
587   //The models displayed on screen, including information about rotation.
588   Model mModels[NUM_MESHES];
589   Actor mContainers[NUM_MESHES];
590
591   //Acts as a global light source, which can be dragged around.
592   Control mLightSource;
593
594   //Used to detect panning to rotate the selected model.
595   Vector2 mPanStart;
596   Vector2 mRotationStart;
597
598   int mModelIndex; //Index of model to load.
599   int mShadingModeIndex; //Index of shading mode to use.
600   int mTag; //Identifies what kind of actor has been selected in OnTouch.
601   int mSelectedModelIndex; //Index of model selected on screen.
602   bool mPaused; //If true, all animations are paused and should stay so.
603   bool mLightFixed; //If false, the light is in manual.
604   bool mLightFront; //Bool for light being in front or behind the models.
605 };
606
607 int DALI_EXPORT_API main( int argc, char **argv )
608 {
609   Application application = Application::New( &argc, &argv );
610   MeshVisualController test( application );
611   application.MainLoop();
612   return 0;
613 }