2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/actors/layer-impl.h>
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>
31 using Dali::Internal::SceneGraph::UpdateManager;
39 typedef Layer::Behavior Behavior;
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 )
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 )
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";
72 return Dali::Layer::New();
75 TypeRegistration mType( typeid( Dali::Layer ), typeid( Dali::Actor ), Create );
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 );
82 } // unnamed namespace
87 LayerPtr layer( new Layer( Actor::LAYER ) );
89 // Second-phase construction
95 LayerPtr Layer::NewRoot( LayerList& layerList, UpdateManager& manager, bool systemLevel )
97 LayerPtr root( new Layer( Actor::ROOT_LAYER ) );
99 // Second-phase construction
100 SceneGraph::Layer* layer = static_cast<SceneGraph::Layer*>( root->CreateNode() );
101 InstallRootMessage( manager, *layer, systemLevel ); // Transfer ownership to scene-graph
103 // Keep a raw pointer to the layer node.
106 // root actor is immediately considered to be on-stage
107 root->mIsOnStage = true;
109 // The root actor will not emit a stage connection signal so set the signalled flag here as well
110 root->mOnStageSignalled = true;
112 // layer-list must be set for the root layer
113 root->mLayerList = &layerList;
114 layerList.RegisterLayer( *root );
119 Layer::Layer( Actor::DerivedType type )
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 )
132 void Layer::OnInitialize()
140 unsigned int Layer::GetDepth() const
142 return mLayerList ? mLayerList->GetDepth( this ) : 0u;
149 mLayerList->RaiseLayer(*this);
157 mLayerList->LowerLayer(*this);
161 void Layer::RaiseAbove( const Internal::Layer& target )
163 // cannot raise above ourself, both have to be on stage
164 if( ( this != &target ) && OnStage() && target.OnStage() )
166 // get parameters depth
167 const unsigned int targetDepth = target.GetDepth();
168 if( GetDepth() < targetDepth )
175 void Layer::LowerBelow( const Internal::Layer& target )
177 // cannot lower below ourself, both have to be on stage
178 if( ( this != &target ) && OnStage() && target.OnStage() )
180 // get parameters depth
181 const unsigned int targetDepth = target.GetDepth();
182 if( GetDepth() > targetDepth )
189 void Layer::RaiseToTop()
193 mLayerList->RaiseLayerToTop(*this);
197 void Layer::LowerToBottom()
201 mLayerList->LowerLayerToBottom(*this);
205 void Layer::MoveAbove( const Internal::Layer& target )
207 // cannot raise above ourself, both have to be on stage
208 if( ( this != &target ) && mLayerList && target.OnStage() )
210 mLayerList->MoveLayerAbove(*this, target );
214 void Layer::MoveBelow( const Internal::Layer& target )
216 // cannot lower below ourself, both have to be on stage
217 if( ( this != &target ) && mLayerList && target.OnStage() )
219 mLayerList->MoveLayerBelow(*this, target );
223 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
225 mBehavior = behavior;
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 );
233 void Layer::SetClipping(bool enabled)
235 if (enabled != mIsClipping)
237 mIsClipping = enabled;
239 // layerNode is being used in a separate thread; queue a message to set the value
240 SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
244 void Layer::SetClippingBox(int x, int y, int width, int height)
246 if( ( x != mClippingBox.x ) ||
247 ( y != mClippingBox.y ) ||
248 ( width != mClippingBox.width ) ||
249 ( height != mClippingBox.height ) )
251 // Clipping box is not animatable; this is the most up-to-date value
252 mClippingBox.Set(x, y, width, height);
254 // Convert mClippingBox to GL based coordinates (from bottom-left)
255 ClippingBox clippingBox( mClippingBox );
257 StagePtr stage = Stage::GetCurrent();
260 clippingBox.y = stage->GetSize().height - clippingBox.y - clippingBox.height;
262 // layerNode is being used in a separate thread; queue a message to set the value
263 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
268 void Layer::SetDepthTestDisabled( bool disable )
270 if( disable != mDepthTestDisabled )
272 mDepthTestDisabled = disable;
275 // layerNode is being used in a separate thread; queue a message to set the value
276 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
280 bool Layer::IsDepthTestDisabled() const
282 return mDepthTestDisabled;
285 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
287 if( function != mSortFunction )
289 mSortFunction = function;
291 // layerNode is being used in a separate thread; queue a message to set the value
292 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
296 void Layer::SetTouchConsumed( bool consume )
298 mTouchConsumed = consume;
301 bool Layer::IsTouchConsumed() const
303 return mTouchConsumed;
306 void Layer::SetHoverConsumed( bool consume )
308 mHoverConsumed = consume;
311 bool Layer::IsHoverConsumed() const
313 return mHoverConsumed;
316 SceneGraph::Node* Layer::CreateNode() const
318 return SceneGraph::Layer::New();
321 void Layer::OnStageConnectionInternal()
325 DALI_ASSERT_DEBUG( NULL == mLayerList );
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() )
331 if( parent->IsLayer() )
333 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
334 mLayerList = parentLayer->mLayerList;
339 DALI_ASSERT_DEBUG( NULL != mLayerList );
340 mLayerList->RegisterLayer( *this );
343 void Layer::OnStageDisconnectionInternal()
345 mLayerList->UnregisterLayer(*this);
347 // mLayerList is only valid when on-stage
351 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
353 DALI_ASSERT_DEBUG( mNode != NULL );
354 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
357 unsigned int Layer::GetDefaultPropertyCount() const
359 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
362 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
364 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
365 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
367 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
368 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
370 indices.PushBack( index );
374 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
376 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
378 return Actor::IsDefaultPropertyWritable( index );
381 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
384 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
386 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
388 return Actor::IsDefaultPropertyAnimatable( index );
391 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
394 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
396 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
398 return Actor::IsDefaultPropertyAConstraintInput( index );
401 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
404 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
406 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
408 return Actor::GetDefaultPropertyType( index );
411 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
413 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
415 return DEFAULT_PROPERTY_DETAILS[index].type;
418 // index out-of-bounds
419 return Property::NONE;
422 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
424 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
426 return Actor::GetDefaultPropertyName( index );
429 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
430 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
432 return DEFAULT_PROPERTY_DETAILS[index].name;
438 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
440 Property::Index index = Property::INVALID_INDEX;
442 // Look for name in current class' default properties
443 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
445 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
446 if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
448 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
452 if( Property::INVALID_INDEX == index )
454 // If not found, check in base class
455 index = Actor::GetDefaultPropertyIndex( name );
461 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
463 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
465 Actor::SetDefaultProperty( index, propertyValue );
471 case Dali::Layer::Property::CLIPPING_ENABLE:
473 SetClipping( propertyValue.Get<bool>() );
476 case Dali::Layer::Property::CLIPPING_BOX:
478 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
479 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
482 case Dali::Layer::Property::BEHAVIOR:
484 Behavior behavior(Dali::Layer::LAYER_2D);
485 if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
487 SetBehavior( behavior );
493 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
501 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
504 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
506 ret = Actor::GetDefaultProperty( index );
512 case Dali::Layer::Property::CLIPPING_ENABLE:
517 case Dali::Layer::Property::CLIPPING_BOX:
522 case Dali::Layer::Property::BEHAVIOR:
524 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
529 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
538 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
541 Layer* layer = dynamic_cast<Layer*>( object );
545 if( 0 == actionName.compare( ACTION_RAISE ) )
550 else if( 0 == actionName.compare( ACTION_LOWER ) )
555 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
560 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
562 layer->LowerToBottom();
570 } // namespace Internal