Mesh demo improvements. 88/77388/1
authorAndrew Poor <andrew.poor@samsung.com>
Wed, 29 Jun 2016 12:50:16 +0000 (13:50 +0100)
committerAndrew Poor <andrew.poor@samsung.com>
Wed, 29 Jun 2016 12:50:16 +0000 (13:50 +0100)
Change-Id: I605525a41398bedf5db8675719d4d32475517f03

examples/mesh-renderer/mesh-renderer-example.cpp

index ffe5bfa..805a5c1 100644 (file)
@@ -44,25 +44,26 @@ namespace
 
   const float X_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
   const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
-  const float MODEL_SCALE = 0.45f;
+  const float MODEL_SCALE = 0.75f;
+  const int NUM_MESHES = 3;
 
 } //End namespace
 
-class SharedMeshRendererController : public ConnectionTracker
+class MeshRendererController : public ConnectionTracker
 {
 public:
 
-  SharedMeshRendererController( Application& application )
+  MeshRendererController( Application& application )
   : mApplication( application ),   //Store handle to the application.
     mModelIndex( 1 ),              //Start with metal robot.
     mShaderIndex( 0 ),             //Start with all textures.
     mSelectedModelIndex( 0 )       //Non-valid default, which will get set to a correct value when used.
   {
     // Connect to the Application's Init signal
-    mApplication.InitSignal().Connect( this, &SharedMeshRendererController::Create );
+    mApplication.InitSignal().Connect( this, &MeshRendererController::Create );
   }
 
-  ~SharedMeshRendererController()
+  ~MeshRendererController()
   {
   }
 
@@ -79,6 +80,9 @@ public:
 
     //Setup and load the 3D models and buttons
     LoadScene();
+
+    //Allow for exiting of the application via key presses.
+    stage.KeyEventSignal().Connect( this, &MeshRendererController::OnKeyEvent );
   }
 
   //Sets up the on-screen elements.
@@ -91,79 +95,81 @@ public:
     layer.SetParentOrigin( ParentOrigin::CENTER );
     layer.SetAnchorPoint( AnchorPoint::CENTER );
     layer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
-    layer.SetBehavior( Layer::LAYER_3D );
+    layer.SetBehavior( Layer::LAYER_2D ); //We use a 2D layer as this is closer to UI work than full 3D scene creation.
+    layer.SetDepthTestDisabled( false ); //Enable depth testing, as otherwise the 2D layer would not do so.
     stage.Add( layer );
 
-    //Containers to house each renderer-holding-actor, to provide a constant hitbox for pan detection.
-    Actor container1 = Actor::New();
-    container1.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
-    container1.SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
-    container1.SetParentOrigin( ParentOrigin::CENTER );
-    container1.SetAnchorPoint( AnchorPoint::CENTER );
-    container1.SetPosition( stage.GetSize().width * 0.25, 0.0 ); //Place on right half of screen.
-    container1.RegisterProperty( "Tag", Property::Value( 0 ) ); // Used to identify this actor and index into the model.
-    layer.Add( container1 );
-
-    Actor container2 = Actor::New();
-    container2.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
-    container2.SetSizeModeFactor( Vector3( MODEL_SCALE / 2, MODEL_SCALE / 2, 0.0f ) );
-    container2.SetParentOrigin( ParentOrigin::CENTER );
-    container2.SetAnchorPoint( AnchorPoint::CENTER );
-    container2.SetPosition( stage.GetSize().width * -0.25, 0.0 ); //Place on left half of screen.
-    container2.RegisterProperty( "Tag", Property::Value( 1 ) ); // Used to identify this actor and index into the model.
-    layer.Add( container2 );
-
-    //Attach gesture detector to pan models when rotated.
+    //Create gesture detector for panning of models.
     mPanGestureDetector = PanGestureDetector::New();
-    mPanGestureDetector.Attach( container1 );
-    mPanGestureDetector.Attach( container2 );
-    mPanGestureDetector.DetectedSignal().Connect( this, &SharedMeshRendererController::OnPan );
-
-    //Create actors to display meshes.
-    Control control1 = Control::New();
-    control1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
-    control1.SetParentOrigin( ParentOrigin::CENTER );
-    control1.SetAnchorPoint( AnchorPoint::CENTER );
-    container1.Add( control1 );
-
-    Control control2 = Control::New();
-    control2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
-    control2.SetParentOrigin( ParentOrigin::CENTER );
-    control2.SetAnchorPoint( AnchorPoint::CENTER );
-    container2.Add( control2 );
-
-    //Make actors spin to demonstrate 3D.
-    Animation rotationAnimation1 = Animation::New( 15.0f );
-    rotationAnimation1.AnimateBy( Property( control1, Actor::Property::ORIENTATION ),
-                                  Quaternion( Degree( 0.0f ), Degree( 360.0f ), Degree( 0.0f ) ) );
-    rotationAnimation1.SetLooping( true );
-    rotationAnimation1.Play();
-
-    Animation rotationAnimation2 = Animation::New( 15.0f );
-    rotationAnimation2.AnimateBy( Property( control2, Actor::Property::ORIENTATION ),
-                                  Quaternion( Degree( 0.0f ), Degree( -360.0f ), Degree( 0.0f ) ) );
-    rotationAnimation2.SetLooping( true );
-    rotationAnimation2.Play();
-
-    //Store model information in corresponding structs.
-    mModels[0].control = control1;
-    mModels[0].rotation.x = 0.0f;
-    mModels[0].rotation.y = 0.0f;
-    mModels[0].rotationAnimation = rotationAnimation1;
-
-    mModels[1].control = control2;
-    mModels[1].rotation.x = 0.0f;
-    mModels[1].rotation.y = 0.0f;
-    mModels[1].rotationAnimation = rotationAnimation2;
-
-    //Calling this sets the model in the two actors.
+    mPanGestureDetector.DetectedSignal().Connect( this, &MeshRendererController::OnPan );
+
+    //Add containers to house each renderer-holding-actor.
+    for( int i = 0; i < NUM_MESHES; i++ )
+    {
+      mContainers[i] = Actor::New();
+      mContainers[i].SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
+      mContainers[i].RegisterProperty( "Tag", Property::Value( i ) ); //Used to identify the actor and index into the model.
+
+      //Position each container on screen
+      if( i == 0 )
+      {
+        //Main, central model
+        mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
+        mContainers[i].SetParentOrigin( ParentOrigin::CENTER );
+        mContainers[i].SetAnchorPoint( AnchorPoint::CENTER );
+      }
+      else if( i == 1 )
+      {
+        //Top left model
+        mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
+        mContainers[i].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
+        mContainers[i].SetAnchorPoint( AnchorPoint::TOP_LEFT );
+      }
+      else if( i == 2 )
+      {
+        //Top right model
+        mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
+        mContainers[i].SetParentOrigin( Vector3( 0.95, 0.03, 0.5 ) ); //Offset from top right
+        mContainers[i].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
+      }
+
+      mPanGestureDetector.Attach( mContainers[i] );
+      layer.Add( mContainers[i] );
+    }
+
+    //Set up models
+    for( int i = 0; i < NUM_MESHES; i++ )
+    {
+      //Create control to display model
+      Control control = Control::New();
+      control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+      control.SetParentOrigin( ParentOrigin::CENTER );
+      control.SetAnchorPoint( AnchorPoint::CENTER );
+      mContainers[i].Add( control );
+
+      //Make model spin to demonstrate 3D
+      Animation rotationAnimation = Animation::New( 15.0f );
+      float spin = i % 2 == 0 ? 1.0f : -1.0f; //Make actors spin in different directions to better show independence.
+      rotationAnimation.AnimateBy( Property( control, Actor::Property::ORIENTATION ),
+                                   Quaternion( Degree( 0.0f ), Degree( spin * 360.0f ), Degree( 0.0f ) ) );
+      rotationAnimation.SetLooping( true );
+      rotationAnimation.Play();
+
+      //Store model information in corresponding structs.
+      mModels[i].control = control;
+      mModels[i].rotation.x = 0.0f;
+      mModels[i].rotation.y = 0.0f;
+      mModels[i].rotationAnimation = rotationAnimation;
+    }
+
+    //Calling this sets the model in the controls.
     ReloadModel();
 
     //Create button for model changing
     Toolkit::PushButton modelButton = Toolkit::PushButton::New();
     modelButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
-    modelButton.ClickedSignal().Connect( this, &SharedMeshRendererController::OnChangeModelClicked );
-    modelButton.SetParentOrigin( Vector3( 0.1, 0.9, 0.5 ) ); //Offset from bottom left
+    modelButton.ClickedSignal().Connect( this, &MeshRendererController::OnChangeModelClicked );
+    modelButton.SetParentOrigin( Vector3( 0.1, 0.95, 0.5 ) ); //Offset from bottom left
     modelButton.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
     modelButton.SetLabelText( "Change Model" );
     layer.Add( modelButton );
@@ -171,8 +177,8 @@ public:
     //Create button for shader changing
     Toolkit::PushButton shaderButton = Toolkit::PushButton::New();
     shaderButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
-    shaderButton.ClickedSignal().Connect( this, &SharedMeshRendererController::OnChangeShaderClicked );
-    shaderButton.SetParentOrigin( Vector3( 0.9, 0.9, 0.5 ) ); //Offset from bottom right
+    shaderButton.ClickedSignal().Connect( this, &MeshRendererController::OnChangeShaderClicked );
+    shaderButton.SetParentOrigin( Vector3( 0.9, 0.95, 0.5 ) ); //Offset from bottom right
     shaderButton.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
     shaderButton.SetLabelText( "Change Shader" );
     layer.Add( shaderButton );
@@ -190,8 +196,10 @@ public:
     map.Insert( "shaderType", SHADER_TYPE[mShaderIndex] );
 
     //Set the two controls to use the mesh
-    mModels[0].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
-    mModels[1].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
+    for( int i = 0; i < NUM_MESHES; i++ )
+    {
+      mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
+    }
   }
 
   //Rotates the panned model based on the gesture.
@@ -263,11 +271,24 @@ public:
     return true;
   }
 
+  //If escape or the back button is pressed, quit the application (and return to the launcher)
+  void OnKeyEvent( const KeyEvent& event )
+  {
+    if( event.state == KeyEvent::Down )
+    {
+      if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
+      {
+        mApplication.Quit();
+      }
+    }
+  }
+
 private:
   Application&  mApplication;
 
   //The models displayed on screen, including information about rotation.
-  Model mModels[2];
+  Model mModels[NUM_MESHES];
+  Actor mContainers[NUM_MESHES];
 
   //Used to detect panning to rotate the selected model.
   PanGestureDetector mPanGestureDetector;
@@ -279,7 +300,7 @@ private:
 
 void RunTest( Application& application )
 {
-  SharedMeshRendererController test( application );
+  MeshRendererController test( application );
 
   application.MainLoop();
 }