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 WINDOW_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the window
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 window
96 Window window = application.GetWindow();
97 window.SetBackgroundColor( WINDOW_COLOR );
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 );
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 window.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].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 );
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 );
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.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
150 control.SetProperty( Actor::Property::ANCHOR_POINT, 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.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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 Window window = mApplication.GetWindow();
258 Vector2 windowSize = window.GetSize();
259 if( windowSize.width < windowSize.height )
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 ) );
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 ) );
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 ));
279 //Supply an image to represent the light.
282 //Connect to touch signal for dragging.
283 mLightSource.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
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 );
291 baseLayer.Add( upperLayer );
292 upperLayer.Add( mLightSource );
294 //Decide which light to use to begin with.
298 //Sets the image to use for the light source depending on whether the light is in front or behind.
301 std::string imageUrl;
305 imageUrl = LIGHT_URL_FRONT;
309 imageUrl = LIGHT_URL_BACK;
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 ) );
318 //Updates the displayed models to account for parameter changes.
321 //Create mesh property 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] );
332 //Set the two controls to use the mesh
333 for( int i = 0; i < NUM_MESHES; i++ )
335 mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
339 //Set the mode used to light the models.
352 //Make the models use a fixed, invisible light above the center of the window.
355 //Hide draggable source
356 mLightSource.SetProperty( Actor::Property::VISIBLE, false );
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 );
365 //Set global light position
366 for( int i = 0; i < NUM_MESHES; ++i )
368 mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
372 //Make the models use a light source that the user can drag around.
373 void UseManualLight()
375 //Show draggable source
376 mLightSource.SetProperty( Actor::Property::VISIBLE, true );
378 //Update to switch light position of models to that of the source.
382 //Updates the light position for each model to account for changes in the source on screen.
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 );
390 for( int i = 0; i < NUM_MESHES; ++i )
392 mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
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 )
400 switch( touch.GetState( 0 ) )
402 case PointState::DOWN:
404 //Determine what was touched.
405 actor.GetProperty( actor.GetPropertyIndex( "Tag" ) ).Get( mTag );
407 if( mTag == MODEL_TAG )
409 //Find out which model has been selected
410 actor.GetProperty( actor.GetPropertyIndex( "Model" ) ).Get( mSelectedModelIndex );
412 //Pause current animation, as the touch gesture will be used to manually rotate the model
413 mModels[mSelectedModelIndex].rotationAnimation.Pause();
415 //Store start points.
416 mPanStart = touch.GetScreenPosition( 0 );
417 mRotationStart = mModels[mSelectedModelIndex].rotation;
422 case PointState::MOTION:
424 //Switch on the kind of actor we're interacting with.
427 case MODEL_TAG: //Rotate model
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);
437 mModels[mSelectedModelIndex].control.SetProperty( Actor::Property::ORIENTATION, rotation );
441 case LIGHT_TAG: //Drag light
443 //Set light source to new position and update the models accordingly.
444 mLightSource.SetProperty( Actor::Property::POSITION, Vector3( touch.GetScreenPosition( 0 ) ) );
453 case PointState::INTERRUPTED: //Same as finished.
454 case PointState::FINISHED:
456 if( mTag == MODEL_TAG )
458 //Return to automatic animation
461 mModels[mSelectedModelIndex].rotationAnimation.Play();
469 //Other touch states do nothing.
477 //Cycle through the list of models.
478 bool OnChangeModelClicked( Toolkit::Button button )
487 //Cycle through the list of shading modes.
488 bool OnChangeShadingModeClicked( Toolkit::Button button )
490 ++mShadingModeIndex %= 3;
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 )
501 //Toggle pause state.
504 //If we wish to pause animations, do so and keep them paused.
507 for( int i = 0; i < NUM_MESHES ; ++i )
509 mModels[i].rotationAnimation.Pause();
512 button.SetProperty( Toolkit::Button::Property::LABEL, PLAY );
514 else //Unpause all animations again.
516 for( int i = 0; i < NUM_MESHES ; ++i )
518 mModels[i].rotationAnimation.Play();
521 button.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
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 )
532 mLightFixed = !mLightFixed;
536 button.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
540 button.SetProperty( Toolkit::Button::Property::LABEL, MANUAL );
548 //Switch between the light being in front of and behind the models.
549 bool OnChangeLightSideClicked( Toolkit::Button button )
552 mLightFront = !mLightFront;
556 button.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
560 button.SetProperty( Toolkit::Button::Property::LABEL, BACK );
563 //Change light image.
566 //Update light to account for the change.
572 //If escape or the back button is pressed, quit the application (and return to the launcher)
573 void OnKeyEvent( const KeyEvent& event )
575 if( event.state == KeyEvent::Down )
577 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
585 Application& mApplication;
587 //The models displayed on screen, including information about rotation.
588 Model mModels[NUM_MESHES];
589 Actor mContainers[NUM_MESHES];
591 //Acts as a global light source, which can be dragged around.
592 Control mLightSource;
594 //Used to detect panning to rotate the selected model.
596 Vector2 mRotationStart;
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.
607 int DALI_EXPORT_API main( int argc, char **argv )
609 Application application = Application::New( &argc, &argv );
610 MeshVisualController test( application );
611 application.MainLoop();