1 #include <dali-toolkit/dali-toolkit.h>
2 #include <dali/public-api/object/property-map.h>
3 #include <dali-toolkit/devel-api/align-enums.h>
4 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
7 using namespace Dali::Toolkit;
11 // Keeps information about each model for access.
14 Control control; // Control housing the mesh visual of the model.
15 Vector2 rotation; // Keeps track of rotation about x and y axis for manual rotation.
16 Animation rotationAnimation; // Automatically rotates when left alone.
20 const char * const MODEL_FILE_TABLE[] =
22 DEMO_MODEL_DIR "Dino.obj",
23 DEMO_MODEL_DIR "ToyRobot-Metal.obj",
24 DEMO_MODEL_DIR "Toyrobot-Plastic.obj"
27 const char * const MATERIAL_FILE_TABLE[] =
29 DEMO_MODEL_DIR "Dino.mtl",
30 DEMO_MODEL_DIR "ToyRobot-Metal.mtl",
31 DEMO_MODEL_DIR "Toyrobot-Plastic.mtl"
34 const char * const TEXTURES_PATH( DEMO_IMAGE_DIR "" );
36 // Possible shading modes.
37 MeshVisual::ShadingMode::Value SHADING_MODE_TABLE[] =
39 MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING,
40 MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING,
41 MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING
45 const char * const PAUSE = " || ";
46 const char * const PLAY = " > ";
47 const char * const FIXED = "FIXED";
48 const char * const MANUAL = "MANUAL";
49 const char * const FRONT = "FRONT";
50 const char * const BACK = "BACK";
52 // Image urls for the light.
53 const char * const LIGHT_URL_FRONT = DEMO_IMAGE_DIR "light-icon-front.png";
54 const char * const LIGHT_URL_BACK = DEMO_IMAGE_DIR "light-icon-back.png";
56 const float X_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
57 const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
58 const float MODEL_SCALE = 0.75f;
59 const float LIGHT_SCALE = 0.15f;
60 const float BUTTONS_OFFSET_BOTTOM = 0.08f;
61 const float BUTTONS_OFFSET_SIDE = 0.2f;
62 const int NUM_MESHES = 2;
64 // Used to identify actors.
65 const int MODEL_TAG = 0;
66 const int LIGHT_TAG = 1;
67 const int LAYER_TAG = 2;
69 const Vector4 STAGE_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the stage
71 } // unnamed namespace
73 class MeshVisualController : public ConnectionTracker
77 MeshVisualController( Application& application )
78 : mApplication( application ), //Store handle to the application.
79 mModelIndex( 1 ), //Start with metal robot.
80 mShadingModeIndex( 0 ), //Start with texture and detailed specular lighting.
81 mTag( -1 ), //Non-valid default, which will get set to a correct value when used.
82 mSelectedModelIndex( -1 ), //Non-valid default, which will get set to a correct value when used.
83 mPaused( false ), //Animations play by default.
84 mLightFixed( true ), //The light is fixed by default.
85 mLightFront( true ) //The light is in front by default.
87 // Connect to the Application's Init signal
88 mApplication.InitSignal().Connect( this, &MeshVisualController::Create );
91 ~MeshVisualController()
95 // The Init signal is received once (only) during the Application lifetime
96 void Create( Application& application )
98 // Get a handle to the stage
99 Stage stage = Stage::GetCurrent();
100 stage.SetBackgroundColor( STAGE_COLOR );
102 //Set up root layer to receive touch gestures.
103 Layer rootLayer = stage.GetRootLayer();
104 rootLayer.RegisterProperty( "Tag", LAYER_TAG ); //Used to differentiate between different kinds of actor.
105 rootLayer.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
107 //Place models on the scene.
108 SetupModels( rootLayer );
110 //Place buttons on the scene.
111 SetupButtons( rootLayer );
113 //Add a light to the scene.
114 SetupLight( rootLayer );
116 //Allow for exiting of the application via key presses.
117 stage.KeyEventSignal().Connect( this, &MeshVisualController::OnKeyEvent );
120 //Loads and adds the models to the scene, inside containers for hit detection.
121 void SetupModels( Layer layer )
123 //Add containers to house each renderer-holding-actor.
124 for( int i = 0; i < NUM_MESHES; i++ )
126 mContainers[i] = Actor::New();
127 mContainers[i].SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
128 mContainers[i].RegisterProperty( "Tag", MODEL_TAG ); //Used to differentiate between different kinds of actor.
129 mContainers[i].RegisterProperty( "Model", Property::Value( i ) ); //Used to index into the model.
130 mContainers[i].TouchSignal().Connect( this, &MeshVisualController::OnTouch );
131 layer.Add( mContainers[i] );
134 //Position each container individually on screen
136 //Main, central model
137 mContainers[0].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
138 mContainers[0].SetParentOrigin( ParentOrigin::CENTER );
139 mContainers[0].SetAnchorPoint( AnchorPoint::CENTER );
142 mContainers[1].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
143 mContainers[1].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
144 mContainers[1].SetAnchorPoint( AnchorPoint::TOP_LEFT );
147 for( int i = 0; i < NUM_MESHES; i++ )
149 //Create control to display model
150 Control control = Control::New();
151 control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
152 control.SetParentOrigin( ParentOrigin::CENTER );
153 control.SetAnchorPoint( AnchorPoint::CENTER );
154 mContainers[i].Add( control );
156 //Make model spin to demonstrate 3D
157 Animation rotationAnimation = Animation::New( 15.0f );
158 float spin = i % 2 == 0 ? 1.0f : -1.0f; //Make actors spin in different directions to better show independence.
159 rotationAnimation.AnimateBy( Property( control, Actor::Property::ORIENTATION ),
160 Quaternion( Degree( 0.0f ), Degree( spin * 360.0f ), Degree( 0.0f ) ) );
161 rotationAnimation.SetLooping( true );
162 rotationAnimation.Play();
164 //Store model information in corresponding structs.
165 mModels[i].control = control;
166 mModels[i].rotation.x = 0.0f;
167 mModels[i].rotation.y = 0.0f;
168 mModels[i].rotationAnimation = rotationAnimation;
171 //Calling this sets the model in the controls.
175 //Place the various buttons on the bottom of the screen, with title labels where necessary.
176 void SetupButtons( Layer layer )
178 //Actor for positioning model and shading mode buttons.
179 Actor positionActorModel = Actor::New();
180 positionActorModel.SetParentOrigin( Vector3( BUTTONS_OFFSET_SIDE, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
181 positionActorModel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
182 layer.Add( positionActorModel );
184 //Create button for model changing.
185 PushButton modelButton = Toolkit::PushButton::New();
186 modelButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
187 modelButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeModelClicked );
188 modelButton.SetParentOrigin( ParentOrigin::TOP_CENTER );
189 modelButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
190 modelButton.SetProperty( Toolkit::Button::Property::LABEL, "Model" );
191 positionActorModel.Add( modelButton );
193 //Create button for shading mode changing.
194 PushButton shadingModeButton = Toolkit::PushButton::New();
195 shadingModeButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
196 shadingModeButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeShadingModeClicked );
197 shadingModeButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
198 shadingModeButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
199 shadingModeButton.SetProperty( Toolkit::Button::Property::LABEL, "Shading Mode" );
200 positionActorModel.Add( shadingModeButton );
202 //Text label title for changing model or shading mode.
203 TextLabel changeTitleLabel = TextLabel::New( "Change" );
204 changeTitleLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
205 changeTitleLabel.SetProperty( TextLabel::Property::UNDERLINE, "{\"thickness\":\"2.0\"}" );
206 changeTitleLabel.SetParentOrigin( ParentOrigin::TOP_CENTER );
207 changeTitleLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
208 modelButton.Add( changeTitleLabel );
210 //Create button for pausing animations.
211 PushButton pauseButton = Toolkit::PushButton::New();
212 pauseButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
213 pauseButton.ClickedSignal().Connect( this, &MeshVisualController::OnPauseClicked );
214 pauseButton.SetParentOrigin( Vector3( 0.5, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
215 pauseButton.SetAnchorPoint( AnchorPoint::CENTER );
216 pauseButton.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
217 layer.Add( pauseButton );
219 //Actor for positioning light position buttons.
220 Actor positionActorLight = Actor::New();
221 positionActorLight.SetParentOrigin( Vector3( 1.0 - BUTTONS_OFFSET_SIDE, 1.0 - BUTTONS_OFFSET_BOTTOM, 0.5 ) );
222 positionActorLight.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
223 layer.Add( positionActorLight );
225 //Create button for switching between manual and fixed light position.
226 PushButton lightModeButton = Toolkit::PushButton::New();
227 lightModeButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
228 lightModeButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeLightModeClicked );
229 lightModeButton.SetParentOrigin( ParentOrigin::TOP_CENTER );
230 lightModeButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
231 lightModeButton.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
232 positionActorLight.Add( lightModeButton );
234 //Create button for switching between front and back light position.
235 PushButton lightSideButton = Toolkit::PushButton::New();
236 lightSideButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
237 lightSideButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeLightSideClicked );
238 lightSideButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
239 lightSideButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
240 lightSideButton.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
241 positionActorLight.Add( lightSideButton );
243 //Text label title for light position mode.
244 TextLabel lightTitleLabel = TextLabel::New( "Light Position" );
245 lightTitleLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
246 lightTitleLabel.SetProperty( TextLabel::Property::UNDERLINE, "{\"thickness\":\"2.0\"}" );
247 lightTitleLabel.SetParentOrigin( ParentOrigin::TOP_CENTER );
248 lightTitleLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
249 lightModeButton.Add( lightTitleLabel );
252 //Add a point light source the the scene, on a layer above the first.
253 void SetupLight( Layer baseLayer )
255 //Create control to act as light source of scene.
256 mLightSource = Control::New();
257 mLightSource.RegisterProperty( "Tag", LIGHT_TAG );
259 //Set size of control based on screen dimensions.
260 Stage stage = Stage::GetCurrent();
261 if( stage.GetSize().width < stage.GetSize().height )
264 mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH );
265 mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
266 mLightSource.SetSizeModeFactor( Vector3( LIGHT_SCALE, 0.0f, 0.0f ) );
271 mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
272 mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::WIDTH );
273 mLightSource.SetSizeModeFactor( Vector3( 0.0f, LIGHT_SCALE, 0.0f ) );
276 //Set position relative to top left, as the light source property is also relative to the top left.
277 mLightSource.SetParentOrigin( ParentOrigin::TOP_LEFT );
278 mLightSource.SetAnchorPoint( AnchorPoint::CENTER );
279 mLightSource.SetPosition( Stage::GetCurrent().GetSize().x * 0.85f, Stage::GetCurrent().GetSize().y * 0.125 );
281 //Supply an image to represent the light.
284 //Connect to touch signal for dragging.
285 mLightSource.TouchSignal().Connect( this, &MeshVisualController::OnTouch );
287 //Place the light source on a layer above the base, so that it is rendered above everything else.
288 Layer upperLayer = Layer::New();
289 upperLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
290 upperLayer.SetParentOrigin( ParentOrigin::CENTER );
291 upperLayer.SetAnchorPoint( AnchorPoint::CENTER );
293 baseLayer.Add( upperLayer );
294 upperLayer.Add( mLightSource );
296 //Decide which light to use to begin with.
300 //Sets the image to use for the light source depending on whether the light is in front or behind.
303 std::string imageUrl;
307 imageUrl = LIGHT_URL_FRONT;
311 imageUrl = LIGHT_URL_BACK;
314 Property::Map lightMap;
315 lightMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
316 lightMap.Insert( ImageVisual::Property::URL, imageUrl );
317 mLightSource.SetProperty( Control::Property::BACKGROUND, Property::Value( lightMap ) );
320 //Updates the displayed models to account for parameter changes.
323 //Create mesh property map
325 map.Insert( Visual::Property::TYPE, Visual::MESH );
326 map.Insert( DevelVisual::Property::TRANSFORM,
327 Property::Map().Add( DevelVisual::Transform::Property::ORIGIN, Align::CENTER )
328 .Add( DevelVisual::Transform::Property::ANCHOR_POINT, Align::CENTER ) );
329 map.Insert( MeshVisual::Property::OBJECT_URL, MODEL_FILE_TABLE[mModelIndex] );
330 map.Insert( MeshVisual::Property::MATERIAL_URL, MATERIAL_FILE_TABLE[mModelIndex] );
331 map.Insert( MeshVisual::Property::TEXTURES_PATH, TEXTURES_PATH );
332 map.Insert( MeshVisual::Property::SHADING_MODE, SHADING_MODE_TABLE[mShadingModeIndex] );
334 //Set the two controls to use the mesh
335 for( int i = 0; i < NUM_MESHES; i++ )
337 mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
341 //Set the mode used to light the models.
354 //Make the models use a fixed, invisible light above the center of the stage.
357 //Hide draggable source
358 mLightSource.SetVisible( false );
360 //Use stage dimensions to place light at center, offset in z axis.
361 Stage stage = Stage::GetCurrent();
362 float width = stage.GetSize().width;
363 float height = stage.GetSize().height;
364 Vector3 lightPosition = Vector3( width / 2.0f, height / 2.0f,
365 ( mLightFront ? 1 : -1 ) * std::max( width, height ) * 5.0f );
367 //Set global light position
368 for( int i = 0; i < NUM_MESHES; ++i )
370 mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
374 //Make the models use a light source that the user can drag around.
375 void UseManualLight()
377 //Show draggable source
378 mLightSource.SetVisible( true );
380 //Update to switch light position of models to that of the source.
384 //Updates the light position for each model to account for changes in the source on screen.
387 //Set light position to the x and y of the light control, offset into/out of the screen.
388 Vector3 controlPosition = mLightSource.GetCurrentPosition();
389 Vector3 lightPosition = Vector3( controlPosition.x, controlPosition.y,
390 ( mLightFront ? 1 : -1 ) * Stage::GetCurrent().GetSize().x / 2.0f );
392 for( int i = 0; i < NUM_MESHES; ++i )
394 mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
398 //If the light source is touched, move it by dragging it.
399 //If a model is touched, rotate it by panning around.
400 bool OnTouch( Actor actor, const TouchData& touch )
402 switch( touch.GetState( 0 ) )
404 case PointState::DOWN:
406 //Determine what was touched.
407 actor.GetProperty( actor.GetPropertyIndex( "Tag" ) ).Get( mTag );
409 if( mTag == MODEL_TAG )
411 //Find out which model has been selected
412 actor.GetProperty( actor.GetPropertyIndex( "Model" ) ).Get( mSelectedModelIndex );
414 //Pause current animation, as the touch gesture will be used to manually rotate the model
415 mModels[mSelectedModelIndex].rotationAnimation.Pause();
417 //Store start points.
418 mPanStart = touch.GetScreenPosition( 0 );
419 mRotationStart = mModels[mSelectedModelIndex].rotation;
424 case PointState::MOTION:
426 //Switch on the kind of actor we're interacting with.
429 case MODEL_TAG: //Rotate model
431 //Calculate displacement and corresponding rotation.
432 Vector2 displacement = touch.GetScreenPosition( 0 ) - mPanStart;
433 mModels[mSelectedModelIndex].rotation = Vector2( mRotationStart.x - displacement.y / Y_ROTATION_DISPLACEMENT_FACTOR, // Y displacement rotates around X axis
434 mRotationStart.y + displacement.x / X_ROTATION_DISPLACEMENT_FACTOR ); // X displacement rotates around Y axis
435 Quaternion rotation = Quaternion( Radian( mModels[mSelectedModelIndex].rotation.x ), Vector3::XAXIS) *
436 Quaternion( Radian( mModels[mSelectedModelIndex].rotation.y ), Vector3::YAXIS);
439 mModels[mSelectedModelIndex].control.SetOrientation( rotation );
443 case LIGHT_TAG: //Drag light
445 //Set light source to new position and update the models accordingly.
446 mLightSource.SetPosition( Vector3( touch.GetScreenPosition( 0 ) ) );
455 case PointState::INTERRUPTED: //Same as finished.
456 case PointState::FINISHED:
458 if( mTag == MODEL_TAG )
460 //Return to automatic animation
463 mModels[mSelectedModelIndex].rotationAnimation.Play();
471 //Other touch states do nothing.
479 //Cycle through the list of models.
480 bool OnChangeModelClicked( Toolkit::Button button )
489 //Cycle through the list of shading modes.
490 bool OnChangeShadingModeClicked( Toolkit::Button button )
492 ++mShadingModeIndex %= 3;
499 //Pause all animations, and keep them paused even after user panning.
500 //This button is a toggle, so pressing again will start the animations again.
501 bool OnPauseClicked( Toolkit::Button button )
503 //Toggle pause state.
506 //If we wish to pause animations, do so and keep them paused.
509 for( int i = 0; i < NUM_MESHES ; ++i )
511 mModels[i].rotationAnimation.Pause();
514 button.SetProperty( Toolkit::Button::Property::LABEL, PLAY );
516 else //Unpause all animations again.
518 for( int i = 0; i < NUM_MESHES ; ++i )
520 mModels[i].rotationAnimation.Play();
523 button.SetProperty( Toolkit::Button::Property::LABEL, PAUSE );
530 //Switch between a fixed light source above/behind the screen, and a light source the user can drag around.
531 bool OnChangeLightModeClicked( Toolkit::Button button )
534 mLightFixed = !mLightFixed;
538 button.SetProperty( Toolkit::Button::Property::LABEL, FIXED );
542 button.SetProperty( Toolkit::Button::Property::LABEL, MANUAL );
550 //Switch between the light being in front of and behind the models.
551 bool OnChangeLightSideClicked( Toolkit::Button button )
554 mLightFront = !mLightFront;
558 button.SetProperty( Toolkit::Button::Property::LABEL, FRONT );
562 button.SetProperty( Toolkit::Button::Property::LABEL, BACK );
565 //Change light image.
568 //Update light to account for the change.
574 //If escape or the back button is pressed, quit the application (and return to the launcher)
575 void OnKeyEvent( const KeyEvent& event )
577 if( event.state == KeyEvent::Down )
579 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
587 Application& mApplication;
589 //The models displayed on screen, including information about rotation.
590 Model mModels[NUM_MESHES];
591 Actor mContainers[NUM_MESHES];
593 //Acts as a global light source, which can be dragged around.
594 Control mLightSource;
596 //Used to detect panning to rotate the selected model.
598 Vector2 mRotationStart;
600 int mModelIndex; //Index of model to load.
601 int mShadingModeIndex; //Index of shading mode to use.
602 int mTag; //Identifies what kind of actor has been selected in OnTouch.
603 int mSelectedModelIndex; //Index of model selected on screen.
604 bool mPaused; //If true, all animations are paused and should stay so.
605 bool mLightFixed; //If false, the light is in manual.
606 bool mLightFront; //Bool for light being in front or behind the models.
609 // Entry point for Linux & Tizen applications
611 int main( int argc, char **argv )
613 Application application = Application::New( &argc, &argv );
614 MeshVisualController test( application );
615 application.MainLoop();