Merge "Add a comment" into devel/master
[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 )
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 );
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.SetRootLayer( &(*root) );
114   layerList.RegisterLayer( *root );
115
116   return root;
117 }
118
119 Layer::Layer( Actor::DerivedType type )
120 : Actor( type ),
121   mLayerList( NULL ),
122   mClippingBox( 0, 0, 0, 0 ),
123   mSortFunction( Layer::ZValue ),
124   mBehavior( Dali::Layer::LAYER_2D ),
125   mIsClipping( false ),
126   mDepthTestDisabled( true ),
127   mTouchConsumed( false ),
128   mHoverConsumed( false )
129 {
130 }
131
132 void Layer::OnInitialize()
133 {
134 }
135
136 Layer::~Layer()
137 {
138 }
139
140 unsigned int Layer::GetDepth() const
141 {
142   return mLayerList ? mLayerList->GetDepth( this ) : 0u;
143 }
144
145 void Layer::Raise()
146 {
147   if ( mLayerList )
148   {
149     mLayerList->RaiseLayer(*this);
150   }
151 }
152
153 void Layer::Lower()
154 {
155   if ( mLayerList )
156   {
157     mLayerList->LowerLayer(*this);
158   }
159 }
160
161 void Layer::RaiseAbove( const Internal::Layer& target )
162 {
163   // cannot raise above ourself, both have to be on stage
164   if( ( this != &target ) && OnStage() && target.OnStage() )
165   {
166     // get parameters depth
167     const uint32_t targetDepth = target.GetDepth();
168     if( GetDepth() < targetDepth )
169     {
170       MoveAbove( target );
171     }
172   }
173 }
174
175 void Layer::LowerBelow( const Internal::Layer& target )
176 {
177   // cannot lower below ourself, both have to be on stage
178   if( ( this != &target ) && OnStage() && target.OnStage() )
179   {
180     // get parameters depth
181     const uint32_t targetDepth = target.GetDepth();
182     if( GetDepth() > targetDepth )
183     {
184       MoveBelow( target );
185     }
186   }
187 }
188
189 void Layer::RaiseToTop()
190 {
191   if ( mLayerList )
192   {
193     mLayerList->RaiseLayerToTop(*this);
194   }
195 }
196
197 void Layer::LowerToBottom()
198 {
199   if ( mLayerList )
200   {
201     mLayerList->LowerLayerToBottom(*this);
202   }
203 }
204
205 void Layer::MoveAbove( const Internal::Layer& target )
206 {
207   // cannot raise above ourself, both have to be on stage
208   if( ( this != &target ) && mLayerList && target.OnStage() )
209   {
210     mLayerList->MoveLayerAbove(*this, target );
211   }
212 }
213
214 void Layer::MoveBelow( const Internal::Layer& target )
215 {
216   // cannot lower below ourself, both have to be on stage
217   if( ( this != &target ) && mLayerList && target.OnStage() )
218   {
219     mLayerList->MoveLayerBelow(*this, target );
220   }
221 }
222
223 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
224 {
225   mBehavior = behavior;
226
227   // Notify update side object.
228   SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
229   // By default, disable depth test for LAYER_2D, and enable for LAYER_3D.
230   SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_2D );
231 }
232
233 void Layer::SetClipping(bool enabled)
234 {
235   if (enabled != mIsClipping)
236   {
237     mIsClipping = enabled;
238
239     // layerNode is being used in a separate thread; queue a message to set the value
240     SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
241   }
242 }
243
244 void Layer::SetClippingBox(int x, int y, int width, int height)
245 {
246   if( ( x != mClippingBox.x ) ||
247       ( y != mClippingBox.y ) ||
248       ( width != mClippingBox.width ) ||
249       ( height != mClippingBox.height ) )
250   {
251     // Clipping box is not animatable; this is the most up-to-date value
252     mClippingBox.Set(x, y, width, height);
253
254     // Convert mClippingBox to GL based coordinates (from bottom-left)
255     ClippingBox clippingBox( mClippingBox );
256
257     StagePtr stage = Stage::GetCurrent();
258     if( stage )
259     {
260       clippingBox.y = static_cast<int32_t>( stage->GetSize().height ) - clippingBox.y - clippingBox.height;
261
262       // layerNode is being used in a separate thread; queue a message to set the value
263       SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
264     }
265   }
266 }
267
268 void Layer::SetDepthTestDisabled( bool disable )
269 {
270   if( disable != mDepthTestDisabled )
271   {
272     mDepthTestDisabled = disable;
273
274     // Send message.
275     // layerNode is being used in a separate thread; queue a message to set the value
276     SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
277   }
278 }
279
280 bool Layer::IsDepthTestDisabled() const
281 {
282   return mDepthTestDisabled;
283 }
284
285 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
286 {
287   if( function != mSortFunction )
288   {
289     mSortFunction = function;
290
291     // layerNode is being used in a separate thread; queue a message to set the value
292     SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
293   }
294 }
295
296 void Layer::SetTouchConsumed( bool consume )
297 {
298   mTouchConsumed = consume;
299 }
300
301 bool Layer::IsTouchConsumed() const
302 {
303   return mTouchConsumed;
304 }
305
306 void Layer::SetHoverConsumed( bool consume )
307 {
308   mHoverConsumed = consume;
309 }
310
311 bool Layer::IsHoverConsumed() const
312 {
313   return mHoverConsumed;
314 }
315
316 SceneGraph::Node* Layer::CreateNode() const
317 {
318   return SceneGraph::Layer::New( mId );
319 }
320
321 void Layer::OnStageConnectionInternal()
322 {
323   if ( !mIsRoot )
324   {
325     DALI_ASSERT_DEBUG( NULL == mLayerList );
326
327     // Find the ordered layer-list
328     // This is different for Layers added via Integration::GetSystemOverlay()
329     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
330     {
331       if( parent->IsLayer() )
332       {
333         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
334         mLayerList = parentLayer->mLayerList;
335       }
336     }
337   }
338
339   DALI_ASSERT_DEBUG( NULL != mLayerList );
340   mLayerList->RegisterLayer( *this );
341 }
342
343 void Layer::OnStageDisconnectionInternal()
344 {
345   mLayerList->UnregisterLayer(*this);
346
347   // mLayerList is only valid when on-stage
348   mLayerList = NULL;
349 }
350
351 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
352 {
353   DALI_ASSERT_DEBUG( mNode != NULL );
354   return dynamic_cast< const SceneGraph::Layer& >( *mNode );
355 }
356
357 unsigned int Layer::GetDefaultPropertyCount() const
358 {
359   return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
360 }
361
362 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
363 {
364   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
365   indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
366
367   int32_t index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
368   for ( int32_t i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
369   {
370     indices.PushBack( index );
371   }
372 }
373
374 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
375 {
376   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
377   {
378     return Actor::IsDefaultPropertyWritable( index );
379   }
380
381   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
382 }
383
384 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
385 {
386   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
387   {
388     return Actor::IsDefaultPropertyAnimatable( index );
389   }
390
391   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
392 }
393
394 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
395 {
396   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
397   {
398     return Actor::IsDefaultPropertyAConstraintInput( index );
399   }
400
401   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
402 }
403
404 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
405 {
406   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
407   {
408     return Actor::GetDefaultPropertyType( index );
409   }
410
411   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
412
413   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
414   {
415     return DEFAULT_PROPERTY_DETAILS[index].type;
416   }
417
418   // index out-of-bounds
419   return Property::NONE;
420 }
421
422 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
423 {
424   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
425   {
426     return Actor::GetDefaultPropertyName( index );
427   }
428
429   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
430   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
431   {
432     return DEFAULT_PROPERTY_DETAILS[index].name;
433   }
434
435   return NULL;
436 }
437
438 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
439 {
440   Property::Index index = Property::INVALID_INDEX;
441
442   // Look for name in current class' default properties
443   for( int32_t i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
444   {
445     const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
446     if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
447     {
448       index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
449       break;
450     }
451   }
452   if( Property::INVALID_INDEX == index )
453   {
454     // If not found, check in base class
455     index = Actor::GetDefaultPropertyIndex( name );
456   }
457
458   return index;
459 }
460
461 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
462 {
463   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
464   {
465     Actor::SetDefaultProperty( index, propertyValue );
466   }
467   else
468   {
469     switch( index )
470     {
471       case Dali::Layer::Property::CLIPPING_ENABLE:
472       {
473         SetClipping( propertyValue.Get<bool>() );
474         break;
475       }
476       case Dali::Layer::Property::CLIPPING_BOX:
477       {
478         Rect<int32_t> clippingBox( propertyValue.Get<Rect<int32_t> >() );
479         SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
480         break;
481       }
482       case Dali::Layer::Property::BEHAVIOR:
483       {
484         Behavior behavior(Dali::Layer::LAYER_2D);
485         if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
486         {
487           SetBehavior( behavior );
488         }
489         break;
490       }
491       default:
492       {
493         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
494         break;
495       }
496     } // switch(index)
497
498   } // else
499 }
500
501 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
502 {
503   Property::Value ret;
504   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
505   {
506     ret = Actor::GetDefaultProperty( index );
507   }
508   else
509   {
510     switch( index )
511     {
512       case Dali::Layer::Property::CLIPPING_ENABLE:
513       {
514         ret = mIsClipping;
515         break;
516       }
517       case Dali::Layer::Property::CLIPPING_BOX:
518       {
519         ret = mClippingBox;
520         break;
521       }
522       case Dali::Layer::Property::BEHAVIOR:
523       {
524         ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
525         break;
526       }
527       default:
528       {
529         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
530         break;
531       }
532     } // switch(index)
533   }
534
535   return ret;
536 }
537
538 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
539 {
540   Property::Value ret;
541   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
542   {
543     ret = Actor::GetDefaultPropertyCurrentValue( index );
544   }
545   else
546   {
547     ret = GetDefaultProperty( index ); // Layer only has event-side properties
548   }
549
550   return ret;
551 }
552
553 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
554 {
555   bool done = false;
556   Layer* layer = dynamic_cast<Layer*>( object );
557
558   if( layer )
559   {
560     if( 0 == actionName.compare( ACTION_RAISE ) )
561     {
562       layer->Raise();
563       done = true;
564     }
565     else if( 0 == actionName.compare( ACTION_LOWER ) )
566     {
567       layer->Lower();
568       done = true;
569     }
570     else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
571     {
572       layer->RaiseToTop();
573       done = true;
574     }
575     else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
576     {
577       layer->LowerToBottom();
578       done = true;
579     }
580   }
581
582   return done;
583 }
584
585 } // namespace Internal
586
587 } // namespace Dali