remove (unnecessarily) exported signal and action names
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.cpp
1 /*
2  * Copyright (c) 2014 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 // CLASS HEADER
19 #include <dali/internal/event/common/stage-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <cmath>
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/system-overlay.h>
27 #include <dali/internal/event/actors/layer-impl.h>
28 #include <dali/internal/event/actors/layer-list.h>
29 #include <dali/internal/event/actors/camera-actor-impl.h>
30 #include <dali/internal/event/actor-attachments/camera-attachment-impl.h>
31 #include <dali/internal/event/common/system-overlay-impl.h>
32 #include <dali/internal/event/common/thread-local-storage.h>
33 #include <dali/internal/event/common/property-notification-manager.h>
34 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
35 #include <dali/internal/update/nodes/node.h>
36 #include <dali/internal/event/common/object-registry-impl.h>
37 #include <dali/integration-api/platform-abstraction.h>
38 #include <dali/public-api/common/constants.h>
39 #include <dali/public-api/object/type-registry.h>
40 #include <dali/public-api/render-tasks/render-task-list.h>
41
42 #ifdef DYNAMICS_SUPPORT
43 #include <dali/internal/event/dynamics/dynamics-world-config-impl.h>
44 #include <dali/internal/event/dynamics/dynamics-world-impl.h>
45 #include <dali/integration-api/dynamics/dynamics-factory-intf.h>
46 #include <dali/integration-api/dynamics/dynamics-world-settings.h>
47 #endif
48
49 using Dali::Internal::SceneGraph::Node;
50
51 namespace Dali
52 {
53
54 namespace Internal
55 {
56
57 namespace
58 {
59
60 const float DEFAULT_STEREO_BASE( 65.0f );
61
62 // Signals
63
64 const char* const SIGNAL_KEY_EVENT =                 "key-event";
65 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "event-processing-finished";
66 const char* const SIGNAL_TOUCHED =                   "touched";
67 const char* const SIGNAL_CONTEXT_LOST =              "context-lost";
68 const char* const SIGNAL_CONTEXT_REGAINED =          "context-regained";
69 const char* const SIGNAL_SCENE_CREATED =             "scene-created";
70
71 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
72
73 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT,                 &Stage::DoConnectSignal );
74 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
75 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED,                   &Stage::DoConnectSignal );
76 SignalConnectorType signalConnector4( mType, SIGNAL_CONTEXT_LOST,              &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_REGAINED,          &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector6( mType, SIGNAL_SCENE_CREATED,             &Stage::DoConnectSignal );
79
80 } // unnamed namespace
81
82 StagePtr Stage::New( AnimationPlaylist& playlist,
83                      PropertyNotificationManager& propertyNotificationManager,
84                      SceneGraph::UpdateManager& updateManager,
85                      NotificationManager& notificationManager )
86 {
87   return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
88 }
89
90 void Stage::Initialize()
91 {
92   mObjectRegistry = ObjectRegistry::New();
93
94   // Create the ordered list of layers
95   mLayerList = LayerList::New( *this, false/*not system-level*/ );
96
97   // The stage owns the default layer
98   mRootLayer = Layer::NewRoot( *this, *mLayerList, mUpdateManager, false/*not system-level*/ );
99   mRootLayer->SetName("RootLayer");
100
101   // Create the default camera actor first; this is needed by the RenderTaskList
102   CreateDefaultCameraActor();
103
104   // Create the list of render-tasks
105   mRenderTaskList = RenderTaskList::New( mUpdateManager, *this, false/*not system-level*/ );
106
107   // Create the default render-task
108   Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
109 }
110
111 void Stage::Uninitialize()
112 {
113   // Remove actors added to SystemOverlay
114   delete mSystemOverlay;
115   mSystemOverlay = NULL;
116
117   if( mDefaultCamera )
118   {
119     Remove(*(mDefaultCamera.Get()));
120   }
121
122   if( mRootLayer )
123   {
124     // we are closing down so just delete the root, no point emit disconnect
125     // signals or send messages to update
126     mRootLayer.Reset();
127   }
128 }
129
130 StagePtr Stage::GetCurrent()
131 {
132   return ThreadLocalStorage::Get().GetCurrentStage();
133 }
134
135 bool Stage::IsInstalled()
136 {
137   return ThreadLocalStorage::Created();
138 }
139
140 ObjectRegistry& Stage::GetObjectRegistry()
141 {
142   return *mObjectRegistry;
143 }
144
145 Layer& Stage::GetRootActor()
146 {
147   return *mRootLayer;
148 }
149
150 SceneGraph::UpdateManager& Stage::GetUpdateManager()
151 {
152   return mUpdateManager;
153 }
154
155 EventToUpdate& Stage::GetUpdateInterface()
156 {
157   return mUpdateManager.GetEventToUpdate();
158 }
159
160 AnimationPlaylist& Stage::GetAnimationPlaylist()
161 {
162   return mAnimationPlaylist;
163 }
164
165 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
166 {
167   return mPropertyNotificationManager;
168 }
169
170 void Stage::Add( Actor& actor )
171 {
172   mRootLayer->Add( actor );
173 }
174
175 void Stage::Remove( Actor& actor )
176 {
177   mRootLayer->Remove( actor );
178 }
179
180 void Stage::SetSize(float width, float height)
181 {
182   // Internally we want to report the actual size of the stage.
183   mSize.width  = width;
184   mSize.height = height;
185
186   // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
187   mDefaultCamera->SetPerspectiveProjection( mSize );
188
189   // The depth of the stage gets set to the maximun of these values
190   mRootLayer->SetSize( mSize );
191
192   // Repeat for SystemOverlay actors
193   if( mSystemOverlay )
194   {
195     mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
196   }
197
198   SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
199 }
200
201 Vector2 Stage::GetSize() const
202 {
203   return mSize;
204 }
205
206 RenderTaskList& Stage::GetRenderTaskList() const
207 {
208   return *mRenderTaskList;
209 }
210
211 void Stage::CreateDefaultCameraActor()
212 {
213   // The default camera attributes and position is such that
214   // children of the default layer, can be positioned at (0,0) and
215   // be at the top-left of the viewport.
216   mDefaultCamera = CameraActor::New( Size::ZERO );
217   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
218   Add(*(mDefaultCamera.Get()));
219 }
220
221 Actor& Stage::GetDefaultRootActor()
222 {
223   return *mRootLayer;
224 }
225
226 CameraActor& Stage::GetDefaultCameraActor()
227 {
228   return *mDefaultCamera;
229 }
230
231 unsigned int Stage::GetLayerCount() const
232 {
233   return mLayerList->GetLayerCount();
234 }
235
236 Dali::Layer Stage::GetLayer( unsigned int depth ) const
237 {
238   return Dali::Layer(mLayerList->GetLayer( depth ));
239 }
240
241 Dali::Layer Stage::GetRootLayer() const
242 {
243   return Dali::Layer( mRootLayer.Get() );
244 }
245
246 LayerList& Stage::GetLayerList()
247 {
248   return *mLayerList;
249 }
250
251 Integration::SystemOverlay& Stage::GetSystemOverlay()
252 {
253   // Lazily create system-level if requested
254   if( !mSystemOverlay )
255   {
256     mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
257     DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
258
259     mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
260   }
261
262   return *mSystemOverlay;
263 }
264
265 SystemOverlay* Stage::GetSystemOverlayInternal()
266 {
267   SystemOverlay* overlay( NULL );
268
269   if( mSystemOverlay )
270   {
271     overlay = mSystemOverlay->GetImpl();
272   }
273
274   return overlay;
275 }
276
277 void Stage::SetViewMode( ViewMode viewMode )
278 {
279   if( mViewMode != viewMode )
280   {
281     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
282
283     if( mViewMode == MONO )
284     {
285       mDefaultCamera->SetRotation( Degree( 180.0f ), Vector3::YAXIS );
286       mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
287
288       //Create camera and RenderTask for left eye
289       mLeftCamera = CameraActor::New( Size::ZERO );
290       mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
291       mDefaultCamera->Add( *mLeftCamera.Get() );
292       mLeftRenderTask = mRenderTaskList->CreateTask();
293       mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
294       mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
295
296       //Create camera and RenderTask for right eye
297       mRightCamera = CameraActor::New( Size::ZERO );
298       mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
299       mDefaultCamera->Add( *mRightCamera.Get() );
300       mRightRenderTask = mRenderTaskList->CreateTask();
301       mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
302
303       mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
304       mRightCamera->SetType( Dali::Camera::FREE_LOOK );
305     }
306
307     // save new mode
308     mViewMode = viewMode;
309
310     switch( viewMode )
311     {
312       case MONO:
313       {
314         // delete extra stereoscopic render tasks and cameras
315         mRenderTaskList->RemoveTask( mLeftRenderTask );
316         mDefaultCamera->Remove( *mLeftCamera.Get() );
317         mLeftRenderTask.Reset();
318         mLeftCamera.Reset();
319         mRenderTaskList->RemoveTask( mRightRenderTask );
320         mDefaultCamera->Remove( *mRightCamera.Get() );
321         mRightRenderTask.Reset();
322         mRightCamera.Reset();
323
324         mDefaultCamera->SetRotation( Degree( 0.0f ), Vector3::YAXIS );
325         mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
326         mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
327
328         break;
329       }
330       case STEREO_HORIZONTAL:
331       {
332         //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
333         //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
334
335         //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
336         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
337
338         //Calculate aspect ratio
339         float aspect = mSize.width / (mSize.height * 0.5f);
340
341         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
342         mLeftCamera->SetAspectRatio( aspect );
343         mLeftCamera->SetRotation( Degree(-90.0f), Vector3::ZAXIS );
344         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
345         mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
346
347         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0,  -stereoBase) );
348         mRightCamera->SetAspectRatio( aspect );
349         mRightCamera->SetRotation( Degree(-90.0f), Vector3::ZAXIS );
350         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
351         mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
352
353         break;
354       }
355       case STEREO_VERTICAL:
356       {
357         //Calculate separation in pixels along horizontal axis
358         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
359
360         //Recalculate fov based on viewport size
361         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
362
363         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
364         mLeftCamera->SetFieldOfView( fov );
365         mLeftCamera->SetRotation( Degree(0.0f), Vector3::ZAXIS );
366         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
367         mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
368
369         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
370         mRightCamera->SetFieldOfView( fov );
371         mRightCamera->SetRotation( Degree(0.0f), Vector3::ZAXIS );
372         mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
373         mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
374
375         break;
376       }
377       case STEREO_INTERLACED:
378       {
379         break;
380       }
381     }
382   }
383 }
384
385 ViewMode Stage::GetViewMode() const
386 {
387   return mViewMode;
388 }
389
390 void Stage::SetStereoBase( float stereoBase )
391 {
392   if( ! Equals( mStereoBase, stereoBase ) )
393   {
394     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
395     mStereoBase = stereoBase;
396
397     switch( mViewMode  )
398     {
399       case STEREO_HORIZONTAL:
400       {
401         stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
402         float aspect = mSize.width / (mSize.height * 0.5f);
403
404         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
405         mLeftCamera->SetAspectRatio( aspect );
406         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
407
408         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
409         mRightCamera->SetAspectRatio( aspect );
410         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
411
412         break;
413       }
414       case STEREO_VERTICAL:
415       {
416         stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
417         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
418
419         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
420         mLeftCamera->SetFieldOfView( fov );
421         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
422
423         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
424         mRightCamera->SetFieldOfView( fov );
425         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
426
427         break;
428       }
429       default:
430         break;
431     }
432   }
433 }
434
435 float Stage::GetStereoBase() const
436 {
437   return mStereoBase;
438 }
439
440 void Stage::SetBackgroundColor(Vector4 color)
441 {
442   // Cache for public GetBackgroundColor()
443   mBackgroundColor = color;
444
445   // Send message to change color in next frame
446   SetBackgroundColorMessage( mUpdateManager, color );
447 }
448
449 Vector4 Stage::GetBackgroundColor() const
450 {
451   return mBackgroundColor;
452 }
453
454 Vector2 Stage::GetDpi() const
455 {
456   return mDpi;
457 }
458
459 void Stage::SetDpi(Vector2 dpi)
460 {
461   mDpi = dpi;
462 }
463
464 #ifdef DYNAMICS_SUPPORT
465
466 DynamicsNotifier& Stage::GetDynamicsNotifier()
467 {
468   return mDynamicsNotifier;
469 }
470
471 DynamicsWorldPtr Stage::InitializeDynamics(DynamicsWorldConfigPtr config)
472 {
473   if( !mDynamicsFactory )
474   {
475     mDynamicsFactory = ThreadLocalStorage::Get().GetPlatformAbstraction().GetDynamicsFactory();
476   }
477
478   if( mDynamicsFactory && !mDynamicsWorld )
479   {
480     if( mDynamicsFactory->InitializeDynamics( *(config->GetSettings()) ) )
481     {
482       mDynamicsWorld = DynamicsWorld::New();
483       mDynamicsWorld->Initialize( *this, *mDynamicsFactory, config );
484     }
485   }
486   return mDynamicsWorld;
487 }
488
489 DynamicsWorldPtr Stage::GetDynamicsWorld()
490 {
491   return mDynamicsWorld;
492 }
493
494 void Stage::TerminateDynamics()
495 {
496   if( mDynamicsWorld )
497   {
498     mDynamicsWorld->Terminate(*this);
499     mDynamicsWorld = NULL;
500   }
501 }
502
503 #endif // DYNAMICS_SUPPORT
504
505 void Stage::KeepRendering( float durationSeconds )
506 {
507   // Send message to keep rendering
508   KeepRenderingMessage( mUpdateManager, durationSeconds );
509 }
510
511 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
512 {
513   bool connected( true );
514   Stage* stage = dynamic_cast<Stage*>(object);
515
516   if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
517   {
518     stage->KeyEventSignal().Connect( tracker, functor );
519   }
520   else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
521   {
522     stage->EventProcessingFinishedSignal().Connect( tracker, functor );
523   }
524   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
525   {
526     stage->TouchedSignal().Connect( tracker, functor );
527   }
528   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
529   {
530     stage->ContextLostSignal().Connect( tracker, functor );
531   }
532   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
533   {
534     stage->ContextRegainedSignal().Connect( tracker, functor );
535   }
536   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
537   {
538     stage->SceneCreatedSignal().Connect( tracker, functor );
539   }
540   else
541   {
542     // signalName does not match any signal
543     connected = false;
544   }
545
546   return connected;
547 }
548
549 void Stage::EmitKeyEventSignal(const KeyEvent& event)
550 {
551   // Emit the key event signal when no actor in the stage has gained the key input focus
552
553   mKeyEventSignal.Emit( event );
554 }
555
556 void Stage::EmitEventProcessingFinishedSignal()
557 {
558    mEventProcessingFinishedSignal.Emit();
559 }
560
561 void Stage::EmitTouchedSignal( const TouchEvent& touch )
562 {
563   mTouchedSignal.Emit( touch );
564 }
565
566
567 void Stage::EmitSceneCreatedSignal()
568 {
569   mSceneCreatedSignal.Emit();
570 }
571
572 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
573 {
574   return mKeyEventSignal;
575 }
576
577 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
578 {
579   return mEventProcessingFinishedSignal;
580 }
581
582 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
583 {
584   return mTouchedSignal;
585 }
586
587 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
588 {
589   return mContextLostSignal;
590 }
591
592 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
593 {
594   return mContextRegainedSignal;
595 }
596
597 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
598 {
599   return mSceneCreatedSignal;
600 }
601
602 void Stage::NotifyContextLost()
603 {
604   mContextLostSignal.Emit();
605 }
606
607 void Stage::NotifyContextRegained()
608 {
609   mContextRegainedSignal.Emit();
610 }
611
612 Stage::Stage( AnimationPlaylist& playlist,
613               PropertyNotificationManager& propertyNotificationManager,
614               SceneGraph::UpdateManager& updateManager,
615               NotificationManager& notificationManager )
616 : mAnimationPlaylist( playlist ),
617   mPropertyNotificationManager(propertyNotificationManager),
618   mUpdateManager(updateManager),
619   mNotificationManager(notificationManager),
620   mSize(Vector2::ZERO),
621   mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
622   mViewMode( MONO ),
623   mStereoBase( DEFAULT_STEREO_BASE ),
624 #ifdef DYNAMICS_SUPPORT
625   mDynamicsFactory(NULL),
626 #endif
627   mSystemOverlay(NULL)
628 {
629 }
630
631 Stage::~Stage()
632 {
633   delete mSystemOverlay;
634
635   mObjectRegistry.Reset();
636 }
637
638 } // namespace Internal
639
640 } // namespace Dali