Add a mechanism to specify a callback on every frame
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / layer-impl.cpp
1 /*
2  * Copyright (c) 2018 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/actors/layer-impl.h>
20
21 // EXTERNAL INCLUDES
22
23 // INTERNAL INCLUDES
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/internal/event/actors/layer-list.h>
28 #include <dali/internal/event/common/property-helper.h>
29 #include <dali/internal/event/common/stage-impl.h>
30
31 using Dali::Internal::SceneGraph::UpdateManager;
32
33 namespace Dali
34 {
35
36 namespace
37 {
38
39 typedef Layer::Behavior Behavior;
40
41 DALI_ENUM_TO_STRING_TABLE_BEGIN( BEHAVIOR )
42 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_2D )
43 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_3D )
44 DALI_ENUM_TO_STRING_TABLE_END( BEHAVIOR )
45
46 } // namespace
47
48 namespace Internal
49 {
50
51 namespace
52 {
53
54 // Properties
55
56 //              Name                Type      writable animatable constraint-input  enum for index-checking
57 DALI_PROPERTY_TABLE_BEGIN
58 DALI_PROPERTY( "clippingEnable",    BOOLEAN,    true,    false,   true,   Dali::Layer::Property::CLIPPING_ENABLE )
59 DALI_PROPERTY( "clippingBox",       RECTANGLE,  true,    false,   true,   Dali::Layer::Property::CLIPPING_BOX    )
60 DALI_PROPERTY( "behavior",          STRING,     true,    false,   false,  Dali::Layer::Property::BEHAVIOR        )
61 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
62
63 // Actions
64
65 const char* const ACTION_RAISE =           "raise";
66 const char* const ACTION_LOWER =           "lower";
67 const char* const ACTION_RAISE_TO_TOP =    "raiseToTop";
68 const char* const ACTION_LOWER_TO_BOTTOM = "lowerToBottom";
69
70 BaseHandle Create()
71 {
72   return Dali::Layer::New();
73 }
74
75 TypeRegistration mType( typeid( Dali::Layer ), typeid( Dali::Actor ), Create );
76
77 TypeAction a1( mType, ACTION_RAISE, &Layer::DoAction );
78 TypeAction a2( mType, ACTION_LOWER, &Layer::DoAction );
79 TypeAction a3( mType, ACTION_RAISE_TO_TOP, &Layer::DoAction );
80 TypeAction a4( mType, ACTION_LOWER_TO_BOTTOM, &Layer::DoAction );
81
82 } // unnamed namespace
83
84
85 LayerPtr Layer::New()
86 {
87   LayerPtr layer( new Layer( Actor::LAYER ) );
88
89   // Second-phase construction
90   layer->Initialize();
91
92   return layer;
93 }
94
95 LayerPtr Layer::NewRoot( LayerList& layerList, UpdateManager& manager, bool systemLevel )
96 {
97   LayerPtr root( new Layer( Actor::ROOT_LAYER ) );
98
99   // Second-phase construction, keep a raw pointer to the layer node.
100   SceneGraph::Layer* rootLayer = static_cast<SceneGraph::Layer*>( root->CreateNode() );
101   root->mNode = rootLayer;
102   OwnerPointer< SceneGraph::Layer > transferOwnership( rootLayer );
103   InstallRootMessage( manager, transferOwnership, systemLevel );
104
105   // root actor is immediately considered to be on-stage
106   root->mIsOnStage = true;
107
108   // The root actor will not emit a stage connection signal so set the signalled flag here as well
109   root->mOnStageSignalled = true;
110
111   // layer-list must be set for the root layer
112   root->mLayerList = &layerList;
113   layerList.RegisterLayer( *root );
114
115   return root;
116 }
117
118 Layer::Layer( Actor::DerivedType type )
119 : Actor( type ),
120   mLayerList( NULL ),
121   mClippingBox( 0, 0, 0, 0 ),
122   mSortFunction( Layer::ZValue ),
123   mBehavior( Dali::Layer::LAYER_2D ),
124   mIsClipping( false ),
125   mDepthTestDisabled( true ),
126   mTouchConsumed( false ),
127   mHoverConsumed( false )
128 {
129 }
130
131 void Layer::OnInitialize()
132 {
133 }
134
135 Layer::~Layer()
136 {
137 }
138
139 unsigned int Layer::GetDepth() const
140 {
141   return mLayerList ? mLayerList->GetDepth( this ) : 0u;
142 }
143
144 void Layer::Raise()
145 {
146   if ( mLayerList )
147   {
148     mLayerList->RaiseLayer(*this);
149   }
150 }
151
152 void Layer::Lower()
153 {
154   if ( mLayerList )
155   {
156     mLayerList->LowerLayer(*this);
157   }
158 }
159
160 void Layer::RaiseAbove( const Internal::Layer& target )
161 {
162   // cannot raise above ourself, both have to be on stage
163   if( ( this != &target ) && OnStage() && target.OnStage() )
164   {
165     // get parameters depth
166     const unsigned int targetDepth = target.GetDepth();
167     if( GetDepth() < targetDepth )
168     {
169       MoveAbove( target );
170     }
171   }
172 }
173
174 void Layer::LowerBelow( const Internal::Layer& target )
175 {
176   // cannot lower below ourself, both have to be on stage
177   if( ( this != &target ) && OnStage() && target.OnStage() )
178   {
179     // get parameters depth
180     const unsigned int targetDepth = target.GetDepth();
181     if( GetDepth() > targetDepth )
182     {
183       MoveBelow( target );
184     }
185   }
186 }
187
188 void Layer::RaiseToTop()
189 {
190   if ( mLayerList )
191   {
192     mLayerList->RaiseLayerToTop(*this);
193   }
194 }
195
196 void Layer::LowerToBottom()
197 {
198   if ( mLayerList )
199   {
200     mLayerList->LowerLayerToBottom(*this);
201   }
202 }
203
204 void Layer::MoveAbove( const Internal::Layer& target )
205 {
206   // cannot raise above ourself, both have to be on stage
207   if( ( this != &target ) && mLayerList && target.OnStage() )
208   {
209     mLayerList->MoveLayerAbove(*this, target );
210   }
211 }
212
213 void Layer::MoveBelow( const Internal::Layer& target )
214 {
215   // cannot lower below ourself, both have to be on stage
216   if( ( this != &target ) && mLayerList && target.OnStage() )
217   {
218     mLayerList->MoveLayerBelow(*this, target );
219   }
220 }
221
222 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
223 {
224   mBehavior = behavior;
225
226   // Notify update side object.
227   SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
228   // By default, disable depth test for LAYER_2D, and enable for LAYER_3D.
229   SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_2D );
230 }
231
232 void Layer::SetClipping(bool enabled)
233 {
234   if (enabled != mIsClipping)
235   {
236     mIsClipping = enabled;
237
238     // layerNode is being used in a separate thread; queue a message to set the value
239     SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
240   }
241 }
242
243 void Layer::SetClippingBox(int x, int y, int width, int height)
244 {
245   if( ( x != mClippingBox.x ) ||
246       ( y != mClippingBox.y ) ||
247       ( width != mClippingBox.width ) ||
248       ( height != mClippingBox.height ) )
249   {
250     // Clipping box is not animatable; this is the most up-to-date value
251     mClippingBox.Set(x, y, width, height);
252
253     // Convert mClippingBox to GL based coordinates (from bottom-left)
254     ClippingBox clippingBox( mClippingBox );
255
256     StagePtr stage = Stage::GetCurrent();
257     if( stage )
258     {
259       clippingBox.y = stage->GetSize().height - clippingBox.y - clippingBox.height;
260
261       // layerNode is being used in a separate thread; queue a message to set the value
262       SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
263     }
264   }
265 }
266
267 void Layer::SetDepthTestDisabled( bool disable )
268 {
269   if( disable != mDepthTestDisabled )
270   {
271     mDepthTestDisabled = disable;
272
273     // Send message.
274     // layerNode is being used in a separate thread; queue a message to set the value
275     SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
276   }
277 }
278
279 bool Layer::IsDepthTestDisabled() const
280 {
281   return mDepthTestDisabled;
282 }
283
284 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
285 {
286   if( function != mSortFunction )
287   {
288     mSortFunction = function;
289
290     // layerNode is being used in a separate thread; queue a message to set the value
291     SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
292   }
293 }
294
295 void Layer::SetTouchConsumed( bool consume )
296 {
297   mTouchConsumed = consume;
298 }
299
300 bool Layer::IsTouchConsumed() const
301 {
302   return mTouchConsumed;
303 }
304
305 void Layer::SetHoverConsumed( bool consume )
306 {
307   mHoverConsumed = consume;
308 }
309
310 bool Layer::IsHoverConsumed() const
311 {
312   return mHoverConsumed;
313 }
314
315 SceneGraph::Node* Layer::CreateNode() const
316 {
317   return SceneGraph::Layer::New( mId );
318 }
319
320 void Layer::OnStageConnectionInternal()
321 {
322   if ( !mIsRoot )
323   {
324     DALI_ASSERT_DEBUG( NULL == mLayerList );
325
326     // Find the ordered layer-list
327     // This is different for Layers added via Integration::GetSystemOverlay()
328     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
329     {
330       if( parent->IsLayer() )
331       {
332         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
333         mLayerList = parentLayer->mLayerList;
334       }
335     }
336   }
337
338   DALI_ASSERT_DEBUG( NULL != mLayerList );
339   mLayerList->RegisterLayer( *this );
340 }
341
342 void Layer::OnStageDisconnectionInternal()
343 {
344   mLayerList->UnregisterLayer(*this);
345
346   // mLayerList is only valid when on-stage
347   mLayerList = NULL;
348 }
349
350 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
351 {
352   DALI_ASSERT_DEBUG( mNode != NULL );
353   return dynamic_cast< const SceneGraph::Layer& >( *mNode );
354 }
355
356 unsigned int Layer::GetDefaultPropertyCount() const
357 {
358   return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
359 }
360
361 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
362 {
363   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
364   indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
365
366   int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
367   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
368   {
369     indices.PushBack( index );
370   }
371 }
372
373 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
374 {
375   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
376   {
377     return Actor::IsDefaultPropertyWritable( index );
378   }
379
380   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
381 }
382
383 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
384 {
385   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
386   {
387     return Actor::IsDefaultPropertyAnimatable( index );
388   }
389
390   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
391 }
392
393 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
394 {
395   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
396   {
397     return Actor::IsDefaultPropertyAConstraintInput( index );
398   }
399
400   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
401 }
402
403 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
404 {
405   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
406   {
407     return Actor::GetDefaultPropertyType( index );
408   }
409
410   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
411
412   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
413   {
414     return DEFAULT_PROPERTY_DETAILS[index].type;
415   }
416
417   // index out-of-bounds
418   return Property::NONE;
419 }
420
421 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
422 {
423   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
424   {
425     return Actor::GetDefaultPropertyName( index );
426   }
427
428   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
429   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
430   {
431     return DEFAULT_PROPERTY_DETAILS[index].name;
432   }
433
434   return NULL;
435 }
436
437 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
438 {
439   Property::Index index = Property::INVALID_INDEX;
440
441   // Look for name in current class' default properties
442   for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
443   {
444     const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
445     if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
446     {
447       index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
448       break;
449     }
450   }
451   if( Property::INVALID_INDEX == index )
452   {
453     // If not found, check in base class
454     index = Actor::GetDefaultPropertyIndex( name );
455   }
456
457   return index;
458 }
459
460 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
461 {
462   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
463   {
464     Actor::SetDefaultProperty( index, propertyValue );
465   }
466   else
467   {
468     switch( index )
469     {
470       case Dali::Layer::Property::CLIPPING_ENABLE:
471       {
472         SetClipping( propertyValue.Get<bool>() );
473         break;
474       }
475       case Dali::Layer::Property::CLIPPING_BOX:
476       {
477         Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
478         SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
479         break;
480       }
481       case Dali::Layer::Property::BEHAVIOR:
482       {
483         Behavior behavior(Dali::Layer::LAYER_2D);
484         if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
485         {
486           SetBehavior( behavior );
487         }
488         break;
489       }
490       default:
491       {
492         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
493         break;
494       }
495     } // switch(index)
496
497   } // else
498 }
499
500 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
501 {
502   Property::Value ret;
503   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
504   {
505     ret = Actor::GetDefaultProperty( index );
506   }
507   else
508   {
509     switch( index )
510     {
511       case Dali::Layer::Property::CLIPPING_ENABLE:
512       {
513         ret = mIsClipping;
514         break;
515       }
516       case Dali::Layer::Property::CLIPPING_BOX:
517       {
518         ret = mClippingBox;
519         break;
520       }
521       case Dali::Layer::Property::BEHAVIOR:
522       {
523         ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
524         break;
525       }
526       default:
527       {
528         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
529         break;
530       }
531     } // switch(index)
532   }
533
534   return ret;
535 }
536
537 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
538 {
539   Property::Value ret;
540   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
541   {
542     ret = Actor::GetDefaultPropertyCurrentValue( index );
543   }
544   else
545   {
546     ret = GetDefaultProperty( index ); // Layer only has event-side properties
547   }
548
549   return ret;
550 }
551
552 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
553 {
554   bool done = false;
555   Layer* layer = dynamic_cast<Layer*>( object );
556
557   if( layer )
558   {
559     if( 0 == actionName.compare( ACTION_RAISE ) )
560     {
561       layer->Raise();
562       done = true;
563     }
564     else if( 0 == actionName.compare( ACTION_LOWER ) )
565     {
566       layer->Lower();
567       done = true;
568     }
569     else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
570     {
571       layer->RaiseToTop();
572       done = true;
573     }
574     else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
575     {
576       layer->LowerToBottom();
577       done = true;
578     }
579   }
580
581   return done;
582 }
583
584 } // namespace Internal
585
586 } // namespace Dali