Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / rendering-skybox / look-camera.cpp
1 /*
2  * Copyright (c) 2020 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/render-tasks/render-task-list.h>
21 #include <dali/public-api/render-tasks/render-task.h>
22 #include <dali/public-api/events/touch-event.h>
23
24 using namespace Dali;
25
26 namespace
27 {
28 // Input sensitivity, the larger value, the more sensitive input
29 // Default value has been chosen empirically
30 const float   CAMERA_SENSITIVITY        ( 90.0f );
31
32 // Vertical angle limit of the camera
33 const float   CAMERA_VERTICAL_LIMIT     ( 80.0f );
34
35 // Position where camera is instantiated by default
36 const Vector3 CAMERA_DEFAULT_POSITION   ( 0.0f, 0.0f, 3.0f );
37
38 // Field-of-View in degrees
39 const float   CAMERA_DEFAULT_FOV        ( 60.0f );
40
41 // Near plane
42 const float   CAMERA_DEFAULT_NEAR       ( 0.1f );
43
44 // Far plane
45 const float   CAMERA_DEFAULT_FAR        ( 100.0f );
46
47 // Default forward vector
48 const Vector3 CAMERA_FORWARD            ( 0.0f, 0.0f, 1.0f );
49
50 // Default up vector
51 const Vector3 CAMERA_UP                 ( Vector3::YAXIS );
52 }
53
54 LookCamera::LookCamera()
55 : mCameraYawPitch( 0.0f, 180.0f ),
56   mFovY( CAMERA_DEFAULT_FOV ),
57   mNear( CAMERA_DEFAULT_NEAR ),
58   mFar( CAMERA_DEFAULT_FAR ),
59   mCameraPosition( CAMERA_DEFAULT_POSITION )
60 {
61 }
62
63 LookCamera::~LookCamera()
64 {
65   mTimer.Stop();
66   mCameraActor.Remove( mInterceptorActor );
67 }
68
69 void LookCamera::Initialise( Window window, const Vector3& position, float fovY, float near, float far )
70 {
71   mWindow = window;
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 windowSize = mWindow.GetSize();
95
96   // ---------------------------------------------------------------------
97   // update rotation
98   Vector2 tmp( mScreenLookDelta );
99   mScreenLookDelta = Vector2::ZERO;
100
101   float yaw = ( (tmp.y / windowSize.x ) * CAMERA_SENSITIVITY );
102   float pitch = ( (tmp.x / windowSize.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   mCameraActor = mWindow.GetRenderTaskList().GetTask(0).GetCameraActor();
123   mCameraActor.SetProperty( Dali::Actor::Property::NAME, "LookCamera" );
124   mCameraActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
125   mCameraActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
126   mCameraActor.SetFieldOfView( Radian( Degree( mFovY ) ) );
127
128   // should be read from file
129   mCameraActor.SetNearClippingPlane( mNear );
130   mCameraActor.SetFarClippingPlane( mFar );
131   mCameraActor.SetProperty( Actor::Property::POSITION, mCameraPosition );
132 }
133
134 void LookCamera::CreateInterceptorActor()
135 {
136   Vector2 windowSize = mWindow.GetSize();
137
138   mInterceptorActor = Actor::New();
139   mInterceptorActor.SetProperty( Dali::Actor::Property::NAME, "InputInterceptor" );
140   mInterceptorActor.SetProperty( Actor::Property::SIZE, Vector3( windowSize.width, windowSize.height, 1 ) );
141   mInterceptorActor.SetProperty( Actor::Property::POSITION, Vector3( 0.0, 0.0, 1.0  ) );
142   mInterceptorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
143   mInterceptorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
144   mCameraActor.Add( mInterceptorActor );
145
146   // Connect TouchedSignal to interceptor actor
147   mInterceptorActor.TouchedSignal().Connect( this, &LookCamera::OnTouch );
148 }
149
150 bool LookCamera::OnTouch( Actor actor, const TouchEvent& touch )
151 {
152   for( int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i )
153   {
154     Vector2 position( touch.GetScreenPosition( i ) );
155
156     // touch started
157     if( touch.GetState( i ) == PointState::STARTED )
158     {
159       mOldTouchLookPosition = position;
160     }
161     else if( touch.GetState( i ) == PointState::FINISHED ||
162              touch.GetState( i ) == PointState::LEAVE ||
163              touch.GetState( i ) == PointState::INTERRUPTED
164              )
165     {
166       mScreenLookDelta = Vector2::ZERO;
167       mOldTouchLookPosition = Vector2::ZERO;
168     }
169     else // on motion
170     {
171       mScreenLookDelta.x += ( position.x - mOldTouchLookPosition.x );
172       mScreenLookDelta.y += ( position.y - mOldTouchLookPosition.y );
173       mOldTouchLookPosition = position;
174     }
175   }
176
177   return true;
178 }