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 Vector3 operator()( const Vector3& current, const PropertyInput& property )
105 Vector3 position = property.GetVector3();
111 struct QuaternionEqualToConstraint
113 QuaternionEqualToConstraint()
117 Quaternion operator()( const Quaternion& current, const PropertyInput& property )
119 return property.GetQuaternion();
123 struct RotationConstraint
125 RotationConstraint(float sign)
130 Quaternion operator()( const Quaternion& current, const PropertyInput& property )
132 Degree angle(property.GetFloat());
133 return Quaternion( Radian(angle) * mSign, Vector3::YAXIS );
140 * This method gets called once the main loop of application is up and running
142 void Create(Application& app)
144 srand(0); // Want repeatable path
146 Stage::GetCurrent().KeyEventSignal().Connect(this, &TestApp::OnKeyEvent);
148 CreateToolbarAndView(app);
149 CreateShadowViewAndLights();
153 void CreateToolbarAndView(Application& app)
155 // Creates a default view with a default tool bar.
156 // The view is added to the stage.
157 Toolkit::ToolBar toolBar;
158 mContents = DemoHelper::CreateView( app,
165 // Add an effect-changing button on the right of the tool bar.
166 Image imageChangeEffect = ResourceImage::New( CHANGE_EFFECT_IMAGE );
167 Toolkit::PushButton effectChangeButton = Toolkit::PushButton::New();
168 effectChangeButton.SetBackgroundImage(imageChangeEffect);
169 effectChangeButton.ClickedSignal().Connect( this, &TestApp::OnEffectButtonClicked );
170 toolBar.AddControl( effectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
172 // Add title to the tool bar.
176 Image resetImage = ResourceImage::New( RESET_ICON );
177 Toolkit::PushButton resetButton = Toolkit::PushButton::New();
178 resetButton.SetBackgroundImage( resetImage );
179 resetButton.ClickedSignal().Connect( this, &TestApp::OnResetPressed );
180 toolBar.AddControl( resetButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
183 mView.SetPosition(Vector3(0.0f, 0.0f, -50));
185 mContents.SetPosition(mTranslation);
186 mContents.SetRotation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
187 mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
189 mPanGestureDetector = PanGestureDetector::New();
190 mPanGestureDetector.Attach( mView );
191 mPanGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPan);
193 mPinchGestureDetector = PinchGestureDetector::New();
194 mPinchGestureDetector.Attach( mView );
195 mPinchGestureDetector.DetectedSignal().Connect(this, &TestApp::OnPinch);
197 mTapGestureDetector = TapGestureDetector::New();
198 mTapGestureDetector.Attach( mView );
199 mTapGestureDetector.DetectedSignal().Connect(this, &TestApp::OnTap);
204 void CreateShadowViewAndLights()
206 mShadowView = Toolkit::ShadowView::New();
207 mShadowView.SetName("Container");
208 mShadowView.SetParentOrigin(ParentOrigin::CENTER);
209 mShadowView.SetAnchorPoint(AnchorPoint::CENTER);
210 mShadowView.SetSizeMode( SIZE_EQUAL_TO_PARENT );
211 mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
212 mContents.Add(mShadowView);
214 Image brickWall = ResourceImage::New(DALI_IMAGE_DIR "brick-wall.jpg");
215 mShadowPlaneBg = ImageActor::New(brickWall);
216 mShadowPlaneBg.SetParentOrigin(ParentOrigin::CENTER);
217 mShadowPlaneBg.SetAnchorPoint(AnchorPoint::CENTER);
218 mShadowPlaneBg.SetName("Plane");
219 mShadowPlaneBg.SetSize(1000.0f, 1000.0f);
220 mContents.Add(mShadowPlaneBg);
221 mShadowPlaneBg.SetPosition(Vector3(50.0f, 50.0f, -200.0f));
223 mShadowView.SetShadowPlane(mShadowPlaneBg);
224 mShadowView.Activate();
226 mLightAnchor = Actor::New();
227 mLightAnchor.SetParentOrigin(ParentOrigin::CENTER);
228 mLightAnchor.SetAnchorPoint(AnchorPoint::CENTER);
229 mLightAnchor.SetRotation(CalculateWorldRotation(Radian(mLightLongitudinal), Radian(mLightAxisTilt)));
231 // Work out a scaling factor as the initial light position was calculated for desktop
232 // Need to scale light position as scene actor size is based on stage size (i.e. much bigger on device)
233 Vector2 stageSize( Stage::GetCurrent().GetSize() );
234 float scaleFactor = stageSize.x / DEFAULT_STAGE_SIZE.x;
236 mCastingLight = Actor::New();
237 mCastingLight.SetParentOrigin(ParentOrigin::CENTER);
238 mCastingLight.SetAnchorPoint(AnchorPoint::CENTER);
239 mCastingLight.SetPosition( Vector3( 0.0f, 0.0f, 800.0f ) * scaleFactor );
241 mLightAnchor.Add(mCastingLight);
242 mShadowPlaneBg.Add(mLightAnchor);
244 mShadowView.SetPointLight(mCastingLight);
249 mSceneActor = Actor::New();
250 mSceneActor.SetParentOrigin(ParentOrigin::CENTER);
252 // Create and add images to the scene actor:
253 mImageActor1 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_1) );
254 mImageActor2 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_2) );
255 mImageActor3 = ImageActor::New( ResourceImage::New(SCENE_IMAGE_3) );
258 mImageActor2.SetParentOrigin(ParentOrigin::CENTER);
260 mImageActor1.SetParentOrigin(ParentOrigin::CENTER_LEFT);
261 mImageActor1.SetAnchorPoint(AnchorPoint::CENTER_RIGHT);
263 mImageActor3.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
264 mImageActor3.SetAnchorPoint(AnchorPoint::CENTER_LEFT);
266 mSceneActor.Add(mImageActor2);
267 mImageActor2.Add(mImageActor1);
268 mImageActor2.Add(mImageActor3);
270 Property::Index angleIndex = mImageActor2.RegisterProperty("angle", Property::Value(30.0f));
271 Source angleSrc( mImageActor2, angleIndex );
272 mImageActor1.ApplyConstraint(Constraint::New<Quaternion>( Actor::Property::Rotation, angleSrc,
273 RotationConstraint(-1.0f)));
274 mImageActor3.ApplyConstraint(Constraint::New<Quaternion>( Actor::Property::Rotation, angleSrc,
275 RotationConstraint(+1.0f)));
277 mSceneAnimation = Animation::New(2.5f);
279 // Want to animate angle from 30 => -30 and back again smoothly.
281 mSceneAnimation.AnimateTo( Property( mImageActor2, angleIndex ), Property::Value(-30.0f), AlphaFunctions::Sin );
283 mSceneAnimation.SetLooping(true);
284 mSceneAnimation.Play();
286 mSceneActor.SetSize(250.0f, 250.0f);
287 mSceneActor.SetPosition(0.0f, 0.0f, 130.0f);
288 mShadowView.Add(mSceneActor);
292 Quaternion CalculateWorldRotation(Radian longitude, Radian axisTilt )
294 Quaternion q(longitude, Vector3::YAXIS);
295 Quaternion p(axisTilt, Vector3::XAXIS);
299 void OnTap(Dali::Actor actor, const TapGesture& gesture)
301 if( mSceneAnimation )
305 mSceneAnimation.Pause();
310 mSceneAnimation.Play();
316 void OnPan(Actor actor, const PanGesture& gesture)
318 switch (gesture.state)
320 case Gesture::Continuing:
326 mLightLongitudinal += gesture.displacement.x/4.0f;
327 mLightAxisTilt -= gesture.displacement.y/6.0f;
328 mLightAxisTilt = Clamp<float>(mLightAxisTilt, -90.0f, 90.0f);
329 mLightAnchor.SetRotation(CalculateWorldRotation(Radian(mLightLongitudinal), Radian(mLightAxisTilt)));
335 mTranslation += Vector3(gesture.displacement.x, gesture.displacement.y, 0.f);
336 mContents.SetPosition(mTranslation);
342 mLongitudinal += gesture.displacement.x/4.0f;
343 mAxisTilt -= gesture.displacement.y/6.0f;
344 mAxisTilt = Clamp<float>(mAxisTilt, -90.0f, 90.0f);
345 mContents.SetRotation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
351 mObjectLongitudinal += gesture.displacement.x/4.0f;
352 mObjectAxisTilt -= gesture.displacement.y/6.0f;
353 mObjectAxisTilt = Clamp<float>(mObjectAxisTilt, -90.0f, 90.0f);
354 mSceneActor.SetRotation(CalculateWorldRotation(Radian(mObjectLongitudinal), Radian(mObjectAxisTilt)));
361 case Gesture::Finished:
362 // Start animation at last known speed
370 void OnPinch(Actor actor, const PinchGesture& gesture)
372 if (gesture.state == Gesture::Started)
374 mScaleAtPinchStart = mContents.GetCurrentScale().x;
376 mPinchScale = Clamp(mScaleAtPinchStart * gesture.scale, MIN_PINCH_SCALE, MAX_PINCH_SCALE);
378 mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
381 void Terminate(Application& app)
385 Stage::GetCurrent().Remove(mSceneActor);
389 Stage::GetCurrent().Remove(mView);
393 void OnKeyEvent(const KeyEvent& event)
395 if(event.state == KeyEvent::Down)
397 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
404 bool OnEffectButtonClicked( Toolkit::Button button )
409 bool OnResetPressed( Toolkit::Button button )
412 mTranslation = Vector3::ZERO;
413 mContents.SetPosition(mTranslation);
415 // Align scene so that light anchor orientation is Z Axis
416 mAxisTilt = -mLightAxisTilt;
417 mLongitudinal = -mLightLongitudinal;
418 mContents.SetRotation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
428 Animation mAnimation;
429 Animation mSceneAnimation;
431 Toolkit::ShadowView mShadowView;
432 ImageActor mShadowPlaneBg;
433 ImageActor mShadowPlane;
436 ImageActor mImageActor1;
437 ImageActor mImageActor2;
438 ImageActor mImageActor3;
439 PanGestureDetector mPanGestureDetector;
440 PinchGestureDetector mPinchGestureDetector;
441 TapGestureDetector mTapGestureDetector;
442 Vector3 mTranslation;
443 Degree mLongitudinal;
445 Degree mLightLongitudinal;
446 Degree mLightAxisTilt;
447 Degree mObjectLongitudinal;
448 Degree mObjectAxisTilt;
450 float mScaleAtPinchStart;
452 Property::Index mAngle1Index;
453 Property::Index mAngle3Index;
466 /*****************************************************************************/
469 RunTest(Application& app)
475 /*****************************************************************************/
478 main(int argc, char **argv)
480 Application app = Application::New(&argc, &argv);