2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "shared/view.h"
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/shadow-view/shadow-view.h>
28 using namespace Dali::Toolkit;
30 using namespace DemoHelper;
34 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-default.png" );
35 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
37 const char* APPLICATION_TITLE_PAN_LIGHT( "Lighting: Pan Light" );
38 const char* APPLICATION_TITLE_PAN_OBJECT( "Lighting: Rotate Object" );
39 const char* APPLICATION_TITLE_PAN_SCENE( "Lighting: Pan Scene" );
40 const char* APPLICATION_TITLE_ROTATE_SCENE( "Lighting: Rotate Scene" );
41 const char* CHANGE_EFFECT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
42 const char* CHANGE_EFFECT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-change-selected.png" );
43 const char* RESET_ICON( DALI_IMAGE_DIR "icon-reset.png" );
44 const char* RESET_ICON_SELECTED( DALI_IMAGE_DIR "icon-reset-selected.png" );
46 const char* SCENE_IMAGE_1( DALI_IMAGE_DIR "gallery-small-10.jpg");
47 const char* SCENE_IMAGE_2( DALI_IMAGE_DIR "gallery-small-42.jpg");
48 const char* SCENE_IMAGE_3( DALI_IMAGE_DIR "gallery-small-48.jpg");
50 const float MIN_PINCH_SCALE( 0.3f );
51 const float MAX_PINCH_SCALE( 2.05f );
53 const float R3_2(0.8660254);
54 const Vector3 TOP_POINT( 0.0f, -1.0f, 0.0f);
55 const Vector3 LEFT_POINT( -R3_2, 0.5f, 0.0f);
56 const Vector3 RIGHT_POINT( R3_2, 0.5f, 0.0f);
57 const Vector3 FRONT_POINT( 0.0f, 0.0f, 20.0f);
59 const Vector2 DEFAULT_STAGE_SIZE( 480.0f, 800.0f );
61 const float X_ROTATION_DISPLACEMENT_FACTOR = 60.f;
62 const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.f;
63 const float LIGHT_PAN_X_DISPLACEMENT_FACTOR = 180.f;
64 const float LIGHT_PAN_Y_DISPLACEMENT_FACTOR = 180.f;
69 * This example shows a fixed point light onto an animating set of images
70 * casting a shadow onto a wall. The whole scene can be rotated.
73 class TestApp : public ConnectionTracker
79 * @param application class, stored as reference
81 TestApp(Application &app)
84 mTranslation(Vector3::ZERO),
85 mSceneYRotation( Dali::ANGLE_30 * 0.5f ),
86 mSceneXRotation( Dali::ANGLE_30 ),
87 mLightYRotation(0.0f),
88 mLightXRotation(0.0f),
89 mObjectYRotation(0.0f),
90 mObjectXRotation(0.0f),
92 mScaleAtPinchStart(0.5f),
95 app.InitSignal().Connect(this, &TestApp::Create);
96 app.TerminateSignal().Connect(this, &TestApp::Terminate);
101 // Nothing to do here; All the members of this class get deleted automatically and they delete their children
106 struct RotationConstraint
108 RotationConstraint(float sign)
113 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
115 Radian angle( inputs[0]->GetFloat() );
116 current = Quaternion( angle * mSign, Vector3::YAXIS );
123 * This method gets called once the main loop of application is up and running
125 void Create(Application& app)
127 srand(0); // Want repeatable path
129 Stage::GetCurrent().KeyEventSignal().Connect(this, &TestApp::OnKeyEvent);
131 CreateToolbarAndView(app);
132 CreateShadowViewAndLights();
136 void CreateToolbarAndView(Application& app)
138 // Creates a default view with a default tool bar.
139 // The view is added to the stage.
140 Toolkit::ToolBar toolBar;
141 mContents = DemoHelper::CreateView( app,
148 // Add an effect-changing button on the right of the tool bar.
149 Toolkit::PushButton effectChangeButton = Toolkit::PushButton::New();
150 effectChangeButton.SetUnselectedImage( CHANGE_EFFECT_IMAGE );
151 effectChangeButton.SetSelectedImage( CHANGE_EFFECT_IMAGE_SELECTED );
152 effectChangeButton.ClickedSignal().Connect( this, &TestApp::OnEffectButtonClicked );
153 toolBar.AddControl( effectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
155 // Add title to the tool bar.
156 mTitleActor = DemoHelper::CreateToolBarLabel( "" );
157 toolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
160 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_SCENE) );
163 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
164 resetButton.SetUnselectedImage( RESET_ICON );
165 resetButton.SetSelectedImage( RESET_ICON_SELECTED );
166 resetButton.ClickedSignal().Connect( this, &TestApp::OnResetPressed );
167 toolBar.AddControl( resetButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
170 mView.SetPosition(Vector3(0.0f, 0.0f, -50));
172 mContents.SetBehavior(Layer::LAYER_3D);
173 mContents.SetPosition(mTranslation);
174 mContents.SetOrientation( CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
175 mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
177 mPanGestureDetector = PanGestureDetector::New();
178 mPanGestureDetector.Attach( mView );
179 mPanGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPan);
181 mPinchGestureDetector = PinchGestureDetector::New();
182 mPinchGestureDetector.Attach( mView );
183 mPinchGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPinch);
185 mTapGestureDetector = TapGestureDetector::New();
186 mTapGestureDetector.Attach( mView );
187 mTapGestureDetector.DetectedSignal().Connect(this, &TestApp::OnTap);
192 void CreateShadowViewAndLights()
194 mShadowView = Toolkit::ShadowView::New();
195 mShadowView.SetName("Container");
196 mShadowView.SetParentOrigin(ParentOrigin::CENTER);
197 mShadowView.SetAnchorPoint(AnchorPoint::CENTER);
198 mShadowView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
199 mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
200 mContents.Add(mShadowView);
202 Image brickWall = ResourceImage::New(DALI_IMAGE_DIR "brick-wall.jpg");
203 mShadowPlaneBg = ImageActor::New(brickWall);
204 mShadowPlaneBg.SetParentOrigin(ParentOrigin::CENTER);
205 mShadowPlaneBg.SetAnchorPoint(AnchorPoint::CENTER);
206 mShadowPlaneBg.SetName("Plane");
207 mShadowPlaneBg.SetSize(1000.0f, 1000.0f);
208 mContents.Add(mShadowPlaneBg);
209 mShadowPlaneBg.SetPosition(Vector3(50.0f, 50.0f, -200.0f));
211 mShadowView.SetShadowPlane(mShadowPlaneBg);
212 mShadowView.Activate();
214 mLightAnchor = Actor::New();
215 mLightAnchor.SetParentOrigin(ParentOrigin::CENTER);
216 mLightAnchor.SetAnchorPoint(AnchorPoint::CENTER);
217 mLightAnchor.SetOrientation( CalculateWorldRotation( mLightXRotation, mLightYRotation ) );
219 // Work out a scaling factor as the initial light position was calculated for desktop
220 // Need to scale light position as scene actor size is based on stage size (i.e. much bigger on device)
221 Vector2 stageSize( Stage::GetCurrent().GetSize() );
222 float scaleFactor = stageSize.x / DEFAULT_STAGE_SIZE.x;
224 mCastingLight = Actor::New();
225 mCastingLight.SetParentOrigin(ParentOrigin::CENTER);
226 mCastingLight.SetAnchorPoint(AnchorPoint::CENTER);
227 mCastingLight.SetPosition( Vector3( 0.0f, 0.0f, 800.0f ) * scaleFactor );
229 TextLabel text = TextLabel::New( "Light" );
230 text.SetProperty( TextLabel::Property::POINT_SIZE, 20.0f );
231 text.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
232 text.SetColor( Color::BLUE );
234 mCastingLight.Add(text);
235 mLightAnchor.Add(mCastingLight);
236 mShadowPlaneBg.Add(mLightAnchor);
238 text.SetParentOrigin(ParentOrigin::CENTER);
239 mShadowView.SetPointLight(mCastingLight);
244 mSceneActor = Actor::New();
245 mSceneActor.SetParentOrigin(ParentOrigin::CENTER);
247 // Create and add images to the scene actor:
248 mImageActor1 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_1) );
249 mImageActor2 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_2) );
250 mImageActor3 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_3) );
252 mImageActor1.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
253 mImageActor2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
254 mImageActor3.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
256 mImageActor2.SetParentOrigin(ParentOrigin::CENTER);
258 mImageActor1.SetParentOrigin(ParentOrigin::CENTER_LEFT);
259 mImageActor1.SetAnchorPoint(AnchorPoint::CENTER_RIGHT);
261 mImageActor3.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
262 mImageActor3.SetAnchorPoint(AnchorPoint::CENTER_LEFT);
264 mSceneActor.Add(mImageActor2);
265 mImageActor2.Add(mImageActor1);
266 mImageActor2.Add(mImageActor3);
268 Property::Index angleIndex = mImageActor2.RegisterProperty("angle", Property::Value( Dali::ANGLE_30 ) );
269 Source angleSrc( mImageActor2, angleIndex );
271 Constraint constraint = Constraint::New<Quaternion>( mImageActor1, Actor::Property::ORIENTATION, RotationConstraint(-1.0f) );
272 constraint.AddSource( angleSrc );
275 constraint = Constraint::New<Quaternion>( mImageActor3, Actor::Property::ORIENTATION, RotationConstraint(+1.0f) );
276 constraint.AddSource( angleSrc );
279 mSceneAnimation = Animation::New(2.5f);
281 // Want to animate angle from 30 => -30 and back again smoothly.
283 mSceneAnimation.AnimateTo( Property( mImageActor2, angleIndex ), Property::Value(-Dali::ANGLE_30), AlphaFunction::SIN );
285 mSceneAnimation.SetLooping(true);
286 mSceneAnimation.Play();
288 mSceneActor.SetSize(250.0f, 250.0f);
289 mSceneActor.SetPosition(0.0f, 0.0f, 130.0f);
290 mShadowView.Add(mSceneActor);
294 Quaternion CalculateWorldRotation( Radian XRotation, Radian YRotation )
296 Quaternion p( XRotation, Vector3::XAXIS );
297 Quaternion q( YRotation, Vector3::YAXIS );
301 void OnTap(Dali::Actor actor, const TapGesture& gesture)
303 if( mSceneAnimation )
307 mSceneAnimation.Pause();
312 mSceneAnimation.Play();
318 void OnPan(Actor actor, const PanGesture& gesture)
320 switch (gesture.state)
322 case Gesture::Continuing:
328 mLightXRotation = mLightXRotation - gesture.displacement.y / LIGHT_PAN_X_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
329 mLightXRotation = Clamp(mLightXRotation, -Dali::ANGLE_45, Dali::ANGLE_45 );
330 mLightYRotation = mLightYRotation + gesture.displacement.x / LIGHT_PAN_Y_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
331 mLightYRotation = Clamp(mLightYRotation, -Dali::ANGLE_45, Dali::ANGLE_45 );
332 mLightAnchor.SetOrientation( CalculateWorldRotation( mLightXRotation, mLightYRotation ) );
338 mTranslation += Vector3(gesture.displacement.x, gesture.displacement.y, 0.f);
339 mContents.SetPosition(mTranslation);
345 mSceneXRotation = mSceneXRotation - gesture.displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
346 mSceneXRotation = Clamp( mSceneXRotation, -Dali::ANGLE_90, Dali::ANGLE_90 );
347 mSceneYRotation = mSceneYRotation + gesture.displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
348 mSceneYRotation = Clamp( mSceneYRotation, -Dali::ANGLE_90, Dali::ANGLE_90 );
349 mContents.SetOrientation( CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
355 mObjectXRotation = mObjectXRotation - gesture.displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
356 mObjectYRotation = mObjectYRotation + gesture.displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
357 mSceneActor.SetOrientation( CalculateWorldRotation( mObjectXRotation, mObjectYRotation ) );
364 case Gesture::Finished:
365 // Start animation at last known speed
373 void OnPinch(Actor actor, const PinchGesture& gesture)
375 if (gesture.state == Gesture::Started)
377 mScaleAtPinchStart = mContents.GetCurrentScale().x;
379 mPinchScale = Clamp(mScaleAtPinchStart * gesture.scale, MIN_PINCH_SCALE, MAX_PINCH_SCALE);
381 mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
384 void Terminate(Application& app)
388 Stage::GetCurrent().Remove(mSceneActor);
392 Stage::GetCurrent().Remove(mView);
396 void OnKeyEvent(const KeyEvent& event)
398 if(event.state == KeyEvent::Down)
400 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
407 bool OnEffectButtonClicked( Toolkit::Button button )
412 mPanState = ROTATE_SCENE;
413 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_ROTATE_SCENE) );
416 mPanState = PAN_LIGHT;
417 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_LIGHT) );
420 mPanState = ROTATE_OBJECT;
421 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_OBJECT) );
424 mPanState = PAN_SCENE;
425 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_SCENE) );
434 bool OnResetPressed( Toolkit::Button button )
437 mTranslation = Vector3::ZERO;
438 mContents.SetPosition(mTranslation);
440 // Align scene so that light anchor orientation is Z Axis
441 mSceneXRotation = -mLightXRotation;
442 mSceneYRotation = -mLightYRotation;
443 mContents.SetOrientation( CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
450 Toolkit::Control mView;
453 Animation mAnimation;
454 Animation mSceneAnimation;
456 Toolkit::ShadowView mShadowView;
457 ImageActor mShadowPlaneBg;
458 ImageActor mShadowPlane;
461 ImageActor mImageActor1;
462 ImageActor mImageActor2;
463 ImageActor mImageActor3;
464 PanGestureDetector mPanGestureDetector;
465 PinchGestureDetector mPinchGestureDetector;
466 TapGestureDetector mTapGestureDetector;
467 Vector3 mTranslation;
468 Radian mSceneYRotation;
469 Radian mSceneXRotation;
470 Radian mLightYRotation;
471 Radian mLightXRotation;
472 Radian mObjectYRotation;
473 Radian mObjectXRotation;
475 float mScaleAtPinchStart;
477 Property::Index mAngle1Index;
478 Property::Index mAngle3Index;
480 Toolkit::TextLabel mTitleActor;
493 /*****************************************************************************/
496 RunTest(Application& app)
502 /*****************************************************************************/
505 main(int argc, char **argv)
507 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);