1 #include <dali-toolkit/dali-toolkit.h>
4 using namespace Dali::Toolkit;
8 // Keeps information about each model for access.
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.
17 const char * const MODEL_FILE_TABLE[] =
19 DEMO_MODEL_DIR "Dino.obj",
20 DEMO_MODEL_DIR "ToyRobot-Metal.obj",
21 DEMO_MODEL_DIR "Toyrobot-Plastic.obj"
24 const char * const MATERIAL_FILE_TABLE[] =
26 DEMO_MODEL_DIR "Dino.mtl",
27 DEMO_MODEL_DIR "ToyRobot-Metal.mtl",
28 DEMO_MODEL_DIR "Toyrobot-Plastic.mtl"
31 const char * const TEXTURES_PATH( DEMO_IMAGE_DIR "" );
33 // Possible shading modes.
34 MeshVisual::ShadingMode::Value SHADING_MODE_TABLE[] =
36 MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING,
37 MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING,
38 MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING
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";
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";
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;
61 // Used to identify actors.
62 const int MODEL_TAG = 0;
63 const int LIGHT_TAG = 1;
64 const int LAYER_TAG = 2;
66 const Vector4 STAGE_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the stage
68 } // unnamed namespace
70 class MeshVisualController : public ConnectionTracker
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.
84 // Connect to the Application's Init signal
85 mApplication.InitSignal().Connect( this, &MeshVisualController::Create );
88 ~MeshVisualController()
92 // The Init signal is received once (only) during the Application lifetime
93 void Create( Application& application )
95 // Get a handle to the stage
96 Stage stage = Stage::GetCurrent();
97 stage.SetBackgroundColor( STAGE_COLOR );
99 //Set up root layer to receive touch gestures.
100 Layer rootLayer = stage.GetRootLayer();
101 rootLayer.RegisterProperty( "Tag", LAYER_TAG ); //Used to differentiate between different kinds of actor.
102 rootLayer.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
104 //Place models on the scene.
105 SetupModels( rootLayer );
107 //Place buttons on the scene.
108 SetupButtons( rootLayer );
110 //Add a light to the scene.
111 SetupLight( rootLayer );
113 //Allow for exiting of the application via key presses.
114 stage.KeyEventSignal().Connect( this, &MeshVisualController::OnKeyEvent );
117 //Loads and adds the models to the scene, inside containers for hit detection.
118 void SetupModels( Layer layer )
120 //Add containers to house each renderer-holding-actor.
121 for( int i = 0; i < NUM_MESHES; i++ )
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] );
131 //Position each container individually on screen
133 //Main, central model
134 mContainers[0].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
135 mContainers[0].SetParentOrigin( ParentOrigin::CENTER );
136 mContainers[0].SetAnchorPoint( AnchorPoint::CENTER );
139 mContainers[1].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
140 mContainers[1].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
141 mContainers[1].SetAnchorPoint( AnchorPoint::TOP_LEFT );
144 for( int i = 0; i < NUM_MESHES; i++ )
146 //Create control to display model
147 Control control = Control::New();
148 control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
149 control.SetParentOrigin( ParentOrigin::CENTER );
150 control.SetAnchorPoint( AnchorPoint::CENTER );
151 mContainers[i].Add( control );
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();
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;
168 //Calling this sets the model in the controls.
172 //Place the various buttons on the bottom of the screen, with title labels where necessary.
173 void SetupButtons( Layer layer )
175 //Actor for positioning model and shading mode buttons.
176 Actor positionActorModel = Actor::New();
177 positionActorModel.SetParentOrigin( Vector3( BUTTONS_OFFSET_SIDE, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
178 positionActorModel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
179 layer.Add( positionActorModel );
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.SetParentOrigin( ParentOrigin::TOP_CENTER );
186 modelButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
187 modelButton.SetProperty( Toolkit::Button::Property::LABEL, "Model" );
188 positionActorModel.Add( modelButton );
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.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
195 shadingModeButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
196 shadingModeButton.SetProperty( Toolkit::Button::Property::LABEL, "Shading Mode" );
197 positionActorModel.Add( shadingModeButton );
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.SetParentOrigin( ParentOrigin::TOP_CENTER );
204 changeTitleLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
205 modelButton.Add( changeTitleLabel );
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.SetParentOrigin( Vector3( 0.5, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
212 pauseButton.SetAnchorPoint( AnchorPoint::CENTER );
213 pauseButton.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
214 layer.Add( pauseButton );
216 //Actor for positioning light position buttons.
217 Actor positionActorLight = Actor::New();
218 positionActorLight.SetParentOrigin( Vector3( 1.0 - BUTTONS_OFFSET_SIDE, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
219 positionActorLight.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
220 layer.Add( positionActorLight );
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.SetParentOrigin( ParentOrigin::TOP_CENTER );
227 lightModeButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
228 lightModeButton.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
229 positionActorLight.Add( lightModeButton );
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.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
236 lightSideButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
237 lightSideButton.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
238 positionActorLight.Add( lightSideButton );
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.SetParentOrigin( ParentOrigin::TOP_CENTER );
245 lightTitleLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
246 lightModeButton.Add( lightTitleLabel );
249 //Add a point light source the the scene, on a layer above the first.
250 void SetupLight( Layer baseLayer )
252 //Create control to act as light source of scene.
253 mLightSource = Control::New();
254 mLightSource.RegisterProperty( "Tag", LIGHT_TAG );
256 //Set size of control based on screen dimensions.
257 Stage stage = Stage::GetCurrent();
258 if( stage.GetSize().width < stage.GetSize().height )
261 mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH );
262 mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
263 mLightSource.SetSizeModeFactor( Vector3( LIGHT_SCALE, 0.0f, 0.0f ) );
268 mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
269 mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::WIDTH );
270 mLightSource.SetSizeModeFactor( Vector3( 0.0f, LIGHT_SCALE, 0.0f ) );
273 //Set position relative to top left, as the light source property is also relative to the top left.
274 mLightSource.SetParentOrigin( ParentOrigin::TOP_LEFT );
275 mLightSource.SetAnchorPoint( AnchorPoint::CENTER );
276 mLightSource.SetPosition( Stage::GetCurrent().GetSize().x * 0.85f, Stage::GetCurrent().GetSize().y * 0.125 );
278 //Supply an image to represent the light.
281 //Connect to touch signal for dragging.
282 mLightSource.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
284 //Place the light source on a layer above the base, so that it is rendered above everything else.
285 Layer upperLayer = Layer::New();
286 upperLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
287 upperLayer.SetParentOrigin( ParentOrigin::CENTER );
288 upperLayer.SetAnchorPoint( AnchorPoint::CENTER );
290 baseLayer.Add( upperLayer );
291 upperLayer.Add( mLightSource );
293 //Decide which light to use to begin with.
297 //Sets the image to use for the light source depending on whether the light is in front or behind.
300 std::string imageUrl;
304 imageUrl = LIGHT_URL_FRONT;
308 imageUrl = LIGHT_URL_BACK;
311 Property::Map lightMap;
312 lightMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
313 lightMap.Insert( ImageVisual::Property::URL, imageUrl );
314 mLightSource.SetProperty( Control::Property::BACKGROUND, Property::Value( lightMap ) );
317 //Updates the displayed models to account for parameter changes.
320 //Create mesh property map
322 map.Insert( Toolkit::Visual::Property::TYPE, Visual::MESH );
323 map.Insert( Visual::Property::TRANSFORM,
324 Property::Map().Add( Visual::Transform::Property::ORIGIN, Align::CENTER )
325 .Add( Visual::Transform::Property::ANCHOR_POINT, Align::CENTER ) );
326 map.Insert( MeshVisual::Property::OBJECT_URL, MODEL_FILE_TABLE[mModelIndex] );
327 map.Insert( MeshVisual::Property::MATERIAL_URL, MATERIAL_FILE_TABLE[mModelIndex] );
328 map.Insert( MeshVisual::Property::TEXTURES_PATH, TEXTURES_PATH );
329 map.Insert( MeshVisual::Property::SHADING_MODE, SHADING_MODE_TABLE[mShadingModeIndex] );
331 //Set the two controls to use the mesh
332 for( int i = 0; i < NUM_MESHES; i++ )
334 mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
338 //Set the mode used to light the models.
351 //Make the models use a fixed, invisible light above the center of the stage.
354 //Hide draggable source
355 mLightSource.SetVisible( false );
357 //Use stage dimensions to place light at center, offset in z axis.
358 Stage stage = Stage::GetCurrent();
359 float width = stage.GetSize().width;
360 float height = stage.GetSize().height;
361 Vector3 lightPosition = Vector3( width / 2.0f, height / 2.0f,
362 ( mLightFront ? 1 : -1 ) * std::max( width, height ) * 5.0f );
364 //Set global light position
365 for( int i = 0; i < NUM_MESHES; ++i )
367 mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
371 //Make the models use a light source that the user can drag around.
372 void UseManualLight()
374 //Show draggable source
375 mLightSource.SetVisible( true );
377 //Update to switch light position of models to that of the source.
381 //Updates the light position for each model to account for changes in the source on screen.
384 //Set light position to the x and y of the light control, offset into/out of the screen.
385 Vector3 controlPosition = mLightSource.GetCurrentPosition();
386 Vector3 lightPosition = Vector3( controlPosition.x, controlPosition.y,
387 ( mLightFront ? 1 : -1 ) * Stage::GetCurrent().GetSize().x / 2.0f );
389 for( int i = 0; i < NUM_MESHES; ++i )
391 mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
395 //If the light source is touched, move it by dragging it.
396 //If a model is touched, rotate it by panning around.
397 bool OnTouch( Actor actor, const TouchData& touch )
399 switch( touch.GetState( 0 ) )
401 case PointState::DOWN:
403 //Determine what was touched.
404 actor.GetProperty( actor.GetPropertyIndex( "Tag" ) ).Get( mTag );
406 if( mTag == MODEL_TAG )
408 //Find out which model has been selected
409 actor.GetProperty( actor.GetPropertyIndex( "Model" ) ).Get( mSelectedModelIndex );
411 //Pause current animation, as the touch gesture will be used to manually rotate the model
412 mModels[mSelectedModelIndex].rotationAnimation.Pause();
414 //Store start points.
415 mPanStart = touch.GetScreenPosition( 0 );
416 mRotationStart = mModels[mSelectedModelIndex].rotation;
421 case PointState::MOTION:
423 //Switch on the kind of actor we're interacting with.
426 case MODEL_TAG: //Rotate model
428 //Calculate displacement and corresponding rotation.
429 Vector2 displacement = touch.GetScreenPosition( 0 ) - mPanStart;
430 mModels[mSelectedModelIndex].rotation = Vector2( mRotationStart.x - displacement.y / Y_ROTATION_DISPLACEMENT_FACTOR, // Y displacement rotates around X axis
431 mRotationStart.y + displacement.x / X_ROTATION_DISPLACEMENT_FACTOR ); // X displacement rotates around Y axis
432 Quaternion rotation = Quaternion( Radian( mModels[mSelectedModelIndex].rotation.x ), Vector3::XAXIS) *
433 Quaternion( Radian( mModels[mSelectedModelIndex].rotation.y ), Vector3::YAXIS);
436 mModels[mSelectedModelIndex].control.SetOrientation( rotation );
440 case LIGHT_TAG: //Drag light
442 //Set light source to new position and update the models accordingly.
443 mLightSource.SetPosition( Vector3( touch.GetScreenPosition( 0 ) ) );
452 case PointState::INTERRUPTED: //Same as finished.
453 case PointState::FINISHED:
455 if( mTag == MODEL_TAG )
457 //Return to automatic animation
460 mModels[mSelectedModelIndex].rotationAnimation.Play();
468 //Other touch states do nothing.
476 //Cycle through the list of models.
477 bool OnChangeModelClicked( Toolkit::Button button )
486 //Cycle through the list of shading modes.
487 bool OnChangeShadingModeClicked( Toolkit::Button button )
489 ++mShadingModeIndex %= 3;
496 //Pause all animations, and keep them paused even after user panning.
497 //This button is a toggle, so pressing again will start the animations again.
498 bool OnPauseClicked( Toolkit::Button button )
500 //Toggle pause state.
503 //If we wish to pause animations, do so and keep them paused.
506 for( int i = 0; i < NUM_MESHES ; ++i )
508 mModels[i].rotationAnimation.Pause();
511 button.SetProperty( Toolkit::Button::Property::LABEL, PLAY );
513 else //Unpause all animations again.
515 for( int i = 0; i < NUM_MESHES ; ++i )
517 mModels[i].rotationAnimation.Play();
520 button.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
527 //Switch between a fixed light source above/behind the screen, and a light source the user can drag around.
528 bool OnChangeLightModeClicked( Toolkit::Button button )
531 mLightFixed = !mLightFixed;
535 button.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
539 button.SetProperty( Toolkit::Button::Property::LABEL, MANUAL );
547 //Switch between the light being in front of and behind the models.
548 bool OnChangeLightSideClicked( Toolkit::Button button )
551 mLightFront = !mLightFront;
555 button.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
559 button.SetProperty( Toolkit::Button::Property::LABEL, BACK );
562 //Change light image.
565 //Update light to account for the change.
571 //If escape or the back button is pressed, quit the application (and return to the launcher)
572 void OnKeyEvent( const KeyEvent& event )
574 if( event.state == KeyEvent::Down )
576 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
584 Application& mApplication;
586 //The models displayed on screen, including information about rotation.
587 Model mModels[NUM_MESHES];
588 Actor mContainers[NUM_MESHES];
590 //Acts as a global light source, which can be dragged around.
591 Control mLightSource;
593 //Used to detect panning to rotate the selected model.
595 Vector2 mRotationStart;
597 int mModelIndex; //Index of model to load.
598 int mShadingModeIndex; //Index of shading mode to use.
599 int mTag; //Identifies what kind of actor has been selected in OnTouch.
600 int mSelectedModelIndex; //Index of model selected on screen.
601 bool mPaused; //If true, all animations are paused and should stay so.
602 bool mLightFixed; //If false, the light is in manual.
603 bool mLightFront; //Bool for light being in front or behind the models.
606 int DALI_EXPORT_API main( int argc, char **argv )
608 Application application = Application::New( &argc, &argv );
609 MeshVisualController test( application );
610 application.MainLoop();