Remove more public Setter/Getter APIs from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / rendering-skybox / look-camera.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "look-camera.h"
19
20 #include <dali/public-api/common/stage.h>
21 #include <dali/public-api/render-tasks/render-task-list.h>
22 #include <dali/public-api/render-tasks/render-task.h>
23 #include <dali/public-api/events/touch-data.h>
24 #include <dali/public-api/events/touch-event.h>
25
26 using namespace Dali;
27
28 namespace
29 {
30 // Input sensitivity, the larger value, the more sensitive input
31 // Default value has been chosen empirically
32 const float   CAMERA_SENSITIVITY        ( 90.0f );
33
34 // Vertical angle limit of the camera
35 const float   CAMERA_VERTICAL_LIMIT     ( 80.0f );
36
37 // Position where camera is instantiated by default
38 const Vector3 CAMERA_DEFAULT_POSITION   ( 0.0f, 0.0f, 3.0f );
39
40 // Field-of-View in degrees
41 const float   CAMERA_DEFAULT_FOV        ( 60.0f );
42
43 // Near plane
44 const float   CAMERA_DEFAULT_NEAR       ( 0.1f );
45
46 // Far plane
47 const float   CAMERA_DEFAULT_FAR        ( 100.0f );
48
49 // Default forward vector
50 const Vector3 CAMERA_FORWARD            ( 0.0f, 0.0f, 1.0f );
51
52 // Default up vector
53 const Vector3 CAMERA_UP                 ( Vector3::YAXIS );
54 }
55
56 LookCamera::LookCamera()
57 : mCameraYawPitch( 0.0f, 180.0f ),
58   mFovY( CAMERA_DEFAULT_FOV ),
59   mNear( CAMERA_DEFAULT_NEAR ),
60   mFar( CAMERA_DEFAULT_FAR ),
61   mCameraPosition( CAMERA_DEFAULT_POSITION )
62 {
63 }
64
65 LookCamera::~LookCamera()
66 {
67   mTimer.Stop();
68   mCameraActor.Remove( mInterceptorActor );
69 }
70
71 void LookCamera::Initialise( const Vector3& position, float fovY, float near, float far )
72 {
73   mFovY = fovY;
74   mNear = near;
75   mFar = far;
76
77   // Camera position is shadowed in order to avoid using.GetCurrentProperty< Vector3 >( Actor::Property::POSITION )
78   mCameraPosition = position;
79
80   // Initialise default camera
81   InitialiseDefaultCamera();
82
83   // Create input interceptor actor
84   CreateInterceptorActor();
85
86   // Start timer
87   mTimer = Timer::New( 16 );
88   mTimer.TickSignal().Connect( this, &LookCamera::OnTick );
89   mTimer.Start();
90 }
91
92 bool LookCamera::OnTick()
93 {
94   Vector2 stageSize = Stage::GetCurrent().GetSize();
95
96   // ---------------------------------------------------------------------
97   // update rotation
98   Vector2 tmp( mScreenLookDelta );
99   mScreenLookDelta = Vector2::ZERO;
100
101   float yaw = ( (tmp.y / stageSize.x ) * CAMERA_SENSITIVITY );
102   float pitch = ( (tmp.x / stageSize.y ) * CAMERA_SENSITIVITY );
103   mCameraYawPitch.x -= yaw;
104   mCameraYawPitch.y -= pitch;
105   if( abs( mCameraYawPitch.x ) > CAMERA_VERTICAL_LIMIT )
106   {
107     mCameraYawPitch.x = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.x < 0) ? -1.0f : 1.0f );
108   }
109
110   Quaternion rotation;
111   Quaternion rotX( Degree( mCameraYawPitch.x), Vector3( 1.0f, 0.0f, 0.0f ) );
112   Quaternion rotY( Degree( mCameraYawPitch.y), Vector3( 0.0f, 1.0f, 0.0f ) );
113   rotation = ( rotY * rotX );
114
115   mCameraActor.SetProperty( Actor::Property::ORIENTATION, rotation );
116
117   return true;
118 }
119
120 void LookCamera::InitialiseDefaultCamera()
121 {
122   Stage stage = Stage::GetCurrent();
123   mCameraActor = stage.GetRenderTaskList().GetTask(0).GetCameraActor();
124   mCameraActor.SetProperty( Dali::Actor::Property::NAME, "LookCamera" );
125   mCameraActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
126   mCameraActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
127   mCameraActor.SetFieldOfView( Radian( Degree( mFovY ) ) );
128
129   // should be read from file
130   mCameraActor.SetNearClippingPlane( mNear );
131   mCameraActor.SetFarClippingPlane( mFar );
132   mCameraActor.SetPosition( mCameraPosition );
133 }
134
135 void LookCamera::CreateInterceptorActor()
136 {
137   Stage stage = Stage::GetCurrent();
138
139   mInterceptorActor = Actor::New();
140   mInterceptorActor.SetProperty( Dali::Actor::Property::NAME, "InputInterceptor" );
141   mInterceptorActor.SetSize( Vector3( stage.GetSize().x, stage.GetSize().y, 1 ) );
142   mInterceptorActor.SetPosition( Vector3( 0.0, 0.0, 1.0  ) );
143   mInterceptorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
144   mInterceptorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
145   mCameraActor.Add( mInterceptorActor );
146
147   // Connect TouchSignal to interceptor actor
148   mInterceptorActor.TouchSignal().Connect( this, &LookCamera::OnTouch );
149 }
150
151 bool LookCamera::OnTouch( Actor actor, const TouchData& touch )
152 {
153   Stage stage = Stage::GetCurrent();
154
155   for( int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i )
156   {
157     Vector2 position( touch.GetScreenPosition( i ) );
158
159     // touch started
160     if( touch.GetState( i ) == PointState::STARTED )
161     {
162       mOldTouchLookPosition = position;
163     }
164     else if( touch.GetState( i ) == PointState::FINISHED ||
165              touch.GetState( i ) == PointState::LEAVE ||
166              touch.GetState( i ) == PointState::INTERRUPTED
167              )
168     {
169       mScreenLookDelta = Vector2::ZERO;
170       mOldTouchLookPosition = Vector2::ZERO;
171     }
172     else // on motion
173     {
174       mScreenLookDelta.x += ( position.x - mOldTouchLookPosition.x );
175       mScreenLookDelta.y += ( position.y - mOldTouchLookPosition.y );
176       mOldTouchLookPosition = position;
177     }
178   }
179
180   return true;
181 }