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>
26 using namespace Dali::Toolkit;
28 using namespace DemoHelper;
32 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-default.png" );
33 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
35 const char* APPLICATION_TITLE_PAN_LIGHT( "Lighting: Pan Light" );
36 const char* APPLICATION_TITLE_PAN_OBJECT( "Lighting: Pan Object" );
37 const char* APPLICATION_TITLE_PAN_SCENE( "Lighting: Pan Scene" );
38 const char* APPLICATION_TITLE_ROTATE_SCENE( "Lighting: Rotate Scene" );
39 const char* CHANGE_EFFECT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
40 const char* RESET_ICON( DALI_IMAGE_DIR "icon-reset.png" );
42 const char* SCENE_IMAGE_1( DALI_IMAGE_DIR "gallery-small-10.jpg");
43 const char* SCENE_IMAGE_2( DALI_IMAGE_DIR "gallery-small-42.jpg");
44 const char* SCENE_IMAGE_3( DALI_IMAGE_DIR "gallery-small-48.jpg");
46 const Quaternion JAUNTY_ROTATION(Math::PI/5.0f, Math::PI/5.0f, 0.0f); // Euler angles
47 const float MIN_PINCH_SCALE( 0.3f );
48 const float MAX_PINCH_SCALE( 2.05f );
50 const float R3_2(0.8660254);
51 const Vector3 TOP_POINT( 0.0f, -1.0f, 0.0f);
52 const Vector3 LEFT_POINT( -R3_2, 0.5f, 0.0f);
53 const Vector3 RIGHT_POINT( R3_2, 0.5f, 0.0f);
54 const Vector3 FRONT_POINT( 0.0f, 0.0f, 20.0f);
56 const Vector2 DEFAULT_STAGE_SIZE( 480.0f, 800.0f );
61 * This example shows a fixed point light onto an animating set of images
62 * casting a shadow onto a wall. The whole scene can be rotated.
65 class TestApp : public ConnectionTracker
71 * @param application class, stored as reference
73 TestApp(Application &app)
76 mTranslation(Vector3::ZERO),
79 mLightLongitudinal(0.0f),
81 mObjectLongitudinal(0.0f),
82 mObjectAxisTilt(0.0f),
84 mScaleAtPinchStart(0.5f),
87 app.InitSignal().Connect(this, &TestApp::Create);
88 app.TerminateSignal().Connect(this, &TestApp::Terminate);
93 // Nothing to do here; All the members of this class get deleted automatically and they delete their children
97 struct PositionInFrontOf
103 void operator()( Vector3& current, const PropertyInputContainer& inputs )
105 current = inputs[0]->GetVector3();
110 struct RotationConstraint
112 RotationConstraint(float sign)
117 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
119 Degree angle( inputs[0]->GetFloat() );
120 current = Quaternion( Radian(angle) * mSign, Vector3::YAXIS );
127 * This method gets called once the main loop of application is up and running
129 void Create(Application& app)
131 DemoHelper::RequestThemeChange();
133 srand(0); // Want repeatable path
135 Stage::GetCurrent().KeyEventSignal().Connect(this, &TestApp::OnKeyEvent);
137 CreateToolbarAndView(app);
138 CreateShadowViewAndLights();
142 void CreateToolbarAndView(Application& app)
144 // Creates a default view with a default tool bar.
145 // The view is added to the stage.
146 Toolkit::ToolBar toolBar;
147 mContents = DemoHelper::CreateView( app,
154 // Add an effect-changing button on the right of the tool bar.
155 Image imageChangeEffect = ResourceImage::New( CHANGE_EFFECT_IMAGE );
156 Toolkit::PushButton effectChangeButton = Toolkit::PushButton::New();
157 effectChangeButton.SetBackgroundImage(imageChangeEffect);
158 effectChangeButton.ClickedSignal().Connect( this, &TestApp::OnEffectButtonClicked );
159 toolBar.AddControl( effectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
161 // Add title to the tool bar.
162 mTitleActor = DemoHelper::CreateToolBarLabel( "" );
163 toolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
166 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_SCENE) );
169 Image resetImage = ResourceImage::New( RESET_ICON );
170 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
171 resetButton.SetBackgroundImage( resetImage );
172 resetButton.ClickedSignal().Connect( this, &TestApp::OnResetPressed );
173 toolBar.AddControl( resetButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
176 mView.SetPosition(Vector3(0.0f, 0.0f, -50));
178 mContents.SetPosition(mTranslation);
179 mContents.SetOrientation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
180 mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
182 mPanGestureDetector = PanGestureDetector::New();
183 mPanGestureDetector.Attach( mView );
184 mPanGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPan);
186 mPinchGestureDetector = PinchGestureDetector::New();
187 mPinchGestureDetector.Attach( mView );
188 mPinchGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPinch);
190 mTapGestureDetector = TapGestureDetector::New();
191 mTapGestureDetector.Attach( mView );
192 mTapGestureDetector.DetectedSignal().Connect(this, &TestApp::OnTap);
197 void CreateShadowViewAndLights()
199 mShadowView = Toolkit::ShadowView::New();
200 mShadowView.SetName("Container");
201 mShadowView.SetParentOrigin(ParentOrigin::CENTER);
202 mShadowView.SetAnchorPoint(AnchorPoint::CENTER);
203 mShadowView.SetRelayoutEnabled( true );
204 mShadowView.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
205 mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
206 mContents.Add(mShadowView);
208 Image brickWall = ResourceImage::New(DALI_IMAGE_DIR "brick-wall.jpg");
209 mShadowPlaneBg = ImageActor::New(brickWall);
210 mShadowPlaneBg.SetParentOrigin(ParentOrigin::CENTER);
211 mShadowPlaneBg.SetAnchorPoint(AnchorPoint::CENTER);
212 mShadowPlaneBg.SetName("Plane");
213 mShadowPlaneBg.SetSize(1000.0f, 1000.0f);
214 mContents.Add(mShadowPlaneBg);
215 mShadowPlaneBg.SetPosition(Vector3(50.0f, 50.0f, -200.0f));
217 mShadowView.SetShadowPlane(mShadowPlaneBg);
218 mShadowView.Activate();
220 mLightAnchor = Actor::New();
221 mLightAnchor.SetParentOrigin(ParentOrigin::CENTER);
222 mLightAnchor.SetAnchorPoint(AnchorPoint::CENTER);
223 mLightAnchor.SetOrientation(CalculateWorldRotation(Radian(mLightLongitudinal), Radian(mLightAxisTilt)));
225 // Work out a scaling factor as the initial light position was calculated for desktop
226 // Need to scale light position as scene actor size is based on stage size (i.e. much bigger on device)
227 Vector2 stageSize( Stage::GetCurrent().GetSize() );
228 float scaleFactor = stageSize.x / DEFAULT_STAGE_SIZE.x;
230 mCastingLight = Actor::New();
231 mCastingLight.SetParentOrigin(ParentOrigin::CENTER);
232 mCastingLight.SetAnchorPoint(AnchorPoint::CENTER);
233 mCastingLight.SetPosition( Vector3( 0.0f, 0.0f, 800.0f ) * scaleFactor );
235 TextLabel text = TextLabel::New( "Light" );
236 text.SetProperty( TextLabel::Property::POINT_SIZE, 20.0f );
237 text.SetResizePolicy( USE_NATURAL_SIZE, WIDTH );
238 text.SetResizePolicy( USE_NATURAL_SIZE, HEIGHT );
239 text.SetColor( Color::BLUE );
241 mCastingLight.Add(text);
242 mLightAnchor.Add(mCastingLight);
243 mShadowPlaneBg.Add(mLightAnchor);
245 text.SetParentOrigin(ParentOrigin::CENTER);
246 mShadowView.SetPointLight(mCastingLight);
251 mSceneActor = Actor::New();
252 mSceneActor.SetParentOrigin(ParentOrigin::CENTER);
254 // Create and add images to the scene actor:
255 mImageActor1 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_1) );
256 mImageActor2 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_2) );
257 mImageActor3 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_3) );
259 mImageActor1.SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
260 mImageActor2.SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
261 mImageActor3.SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
263 mImageActor2.SetParentOrigin(ParentOrigin::CENTER);
265 mImageActor1.SetParentOrigin(ParentOrigin::CENTER_LEFT);
266 mImageActor1.SetAnchorPoint(AnchorPoint::CENTER_RIGHT);
268 mImageActor3.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
269 mImageActor3.SetAnchorPoint(AnchorPoint::CENTER_LEFT);
271 mSceneActor.Add(mImageActor2);
272 mImageActor2.Add(mImageActor1);
273 mImageActor2.Add(mImageActor3);
275 Property::Index angleIndex = mImageActor2.RegisterProperty("angle", Property::Value(30.0f));
276 Source angleSrc( mImageActor2, angleIndex );
278 Constraint constraint = Constraint::New<Quaternion>( mImageActor1, Actor::Property::ORIENTATION, RotationConstraint(-1.0f) );
279 constraint.AddSource( angleSrc );
282 constraint = Constraint::New<Quaternion>( mImageActor3, Actor::Property::ORIENTATION, RotationConstraint(+1.0f) );
283 constraint.AddSource( angleSrc );
286 mSceneAnimation = Animation::New(2.5f);
288 // Want to animate angle from 30 => -30 and back again smoothly.
290 mSceneAnimation.AnimateTo( Property( mImageActor2, angleIndex ), Property::Value(-30.0f), AlphaFunctions::Sin );
292 mSceneAnimation.SetLooping(true);
293 mSceneAnimation.Play();
295 mSceneActor.SetSize(250.0f, 250.0f);
296 mSceneActor.SetPosition(0.0f, 0.0f, 130.0f);
297 mShadowView.Add(mSceneActor);
301 Quaternion CalculateWorldRotation(Radian longitude, Radian axisTilt )
303 Quaternion q(longitude, Vector3::YAXIS);
304 Quaternion p(axisTilt, Vector3::XAXIS);
308 void OnTap(Dali::Actor actor, const TapGesture& gesture)
310 if( mSceneAnimation )
314 mSceneAnimation.Pause();
319 mSceneAnimation.Play();
325 void OnPan(Actor actor, const PanGesture& gesture)
327 switch (gesture.state)
329 case Gesture::Continuing:
335 mLightLongitudinal += gesture.displacement.x/4.0f;
336 mLightAxisTilt -= gesture.displacement.y/6.0f;
337 mLightAxisTilt = Clamp<float>(mLightAxisTilt, -90.0f, 90.0f);
338 mLightAnchor.SetOrientation(CalculateWorldRotation(Radian(mLightLongitudinal), Radian(mLightAxisTilt)));
344 mTranslation += Vector3(gesture.displacement.x, gesture.displacement.y, 0.f);
345 mContents.SetPosition(mTranslation);
351 mLongitudinal += gesture.displacement.x/4.0f;
352 mAxisTilt -= gesture.displacement.y/6.0f;
353 mAxisTilt = Clamp<float>(mAxisTilt, -90.0f, 90.0f);
354 mContents.SetOrientation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
360 mObjectLongitudinal += gesture.displacement.x/4.0f;
361 mObjectAxisTilt -= gesture.displacement.y/6.0f;
362 mObjectAxisTilt = Clamp<float>(mObjectAxisTilt, -90.0f, 90.0f);
363 mSceneActor.SetOrientation(CalculateWorldRotation(Radian(mObjectLongitudinal), Radian(mObjectAxisTilt)));
370 case Gesture::Finished:
371 // Start animation at last known speed
379 void OnPinch(Actor actor, const PinchGesture& gesture)
381 if (gesture.state == Gesture::Started)
383 mScaleAtPinchStart = mContents.GetCurrentScale().x;
385 mPinchScale = Clamp(mScaleAtPinchStart * gesture.scale, MIN_PINCH_SCALE, MAX_PINCH_SCALE);
387 mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
390 void Terminate(Application& app)
394 Stage::GetCurrent().Remove(mSceneActor);
398 Stage::GetCurrent().Remove(mView);
402 void OnKeyEvent(const KeyEvent& event)
404 if(event.state == KeyEvent::Down)
406 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
413 bool OnEffectButtonClicked( Toolkit::Button button )
418 mPanState = ROTATE_SCENE;
419 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_ROTATE_SCENE) );
422 mPanState = PAN_LIGHT;
423 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_LIGHT) );
426 mPanState = PAN_OBJECT;
427 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_OBJECT) );
430 mPanState = PAN_SCENE;
431 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_SCENE) );
440 bool OnResetPressed( Toolkit::Button button )
443 mTranslation = Vector3::ZERO;
444 mContents.SetPosition(mTranslation);
446 // Align scene so that light anchor orientation is Z Axis
447 mAxisTilt = -mLightAxisTilt;
448 mLongitudinal = -mLightLongitudinal;
449 mContents.SetOrientation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
459 Animation mAnimation;
460 Animation mSceneAnimation;
462 Toolkit::ShadowView mShadowView;
463 ImageActor mShadowPlaneBg;
464 ImageActor mShadowPlane;
467 ImageActor mImageActor1;
468 ImageActor mImageActor2;
469 ImageActor mImageActor3;
470 PanGestureDetector mPanGestureDetector;
471 PinchGestureDetector mPinchGestureDetector;
472 TapGestureDetector mTapGestureDetector;
473 Vector3 mTranslation;
474 Degree mLongitudinal;
476 Degree mLightLongitudinal;
477 Degree mLightAxisTilt;
478 Degree mObjectLongitudinal;
479 Degree mObjectAxisTilt;
481 float mScaleAtPinchStart;
483 Property::Index mAngle1Index;
484 Property::Index mAngle3Index;
486 Toolkit::TextLabel mTitleActor;
499 /*****************************************************************************/
502 RunTest(Application& app)
508 /*****************************************************************************/
511 main(int argc, char **argv)
513 Application app = Application::New(&argc, &argv);