2 * Copyright (c) 2014 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;
38 typedef Dali::Layer::Behavior Behavior;
40 DALI_ENUM_TO_STRING_TABLE_BEGIN( Behavior )
41 DALI_ENUM_TO_STRING( Dali::Layer::LAYER_2D )
42 DALI_ENUM_TO_STRING( Dali::Layer::LAYER_3D )
43 DALI_ENUM_TO_STRING_TABLE_END( Behavior )
54 // Name Type writable animatable constraint-input enum for index-checking
55 DALI_PROPERTY_TABLE_BEGIN
56 DALI_PROPERTY( "clipping-enable", BOOLEAN, true, false, true, Dali::Layer::Property::CLIPPING_ENABLE )
57 DALI_PROPERTY( "clipping-box", RECTANGLE, true, false, true, Dali::Layer::Property::CLIPPING_BOX )
58 DALI_PROPERTY( "behavior", STRING, true, false, false, Dali::Layer::Property::BEHAVIOR )
59 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
63 const char* const ACTION_RAISE = "raise";
64 const char* const ACTION_LOWER = "lower";
65 const char* const ACTION_RAISE_TO_TOP = "raise-to-top";
66 const char* const ACTION_LOWER_TO_BOTTOM = "lower-to-bottom";
70 return Dali::Layer::New();
73 TypeRegistration mType( typeid( Dali::Layer ), typeid( Dali::Actor ), Create );
75 TypeAction a1( mType, ACTION_RAISE, &Layer::DoAction );
76 TypeAction a2( mType, ACTION_LOWER, &Layer::DoAction );
77 TypeAction a3( mType, ACTION_RAISE_TO_TOP, &Layer::DoAction );
78 TypeAction a4( mType, ACTION_LOWER_TO_BOTTOM, &Layer::DoAction );
80 } // unnamed namespace
85 LayerPtr layer( new Layer( Actor::LAYER ) );
87 // Second-phase construction
93 LayerPtr Layer::NewRoot( LayerList& layerList, UpdateManager& manager, bool systemLevel )
95 LayerPtr root( new Layer( Actor::ROOT_LAYER ) );
97 // Second-phase construction
98 SceneGraph::Layer* layer = static_cast<SceneGraph::Layer*>( root->CreateNode() );
99 InstallRootMessage( manager, *layer, systemLevel ); // Transfer ownership to scene-graph
101 // Keep a raw pointer to the layer node.
104 // root actor is immediately considered to be on-stage
105 root->mIsOnStage = true;
107 // The root actor will not emit a stage connection signal so set the signalled flag here as well
108 root->mOnStageSignalled = true;
110 // layer-list must be set for the root layer
111 root->mLayerList = &layerList;
112 layerList.RegisterLayer( *root );
117 Layer::Layer( Actor::DerivedType type )
120 mClippingBox(0,0,0,0),
121 mSortFunction(Layer::ZValue),
123 mDepthTestDisabled(false),
124 mTouchConsumed(false),
125 mHoverConsumed(false)
129 void Layer::OnInitialize()
137 unsigned int Layer::GetDepth() const
139 return mLayerList ? mLayerList->GetDepth( this ) : 0u;
146 mLayerList->RaiseLayer(*this);
154 mLayerList->LowerLayer(*this);
158 void Layer::RaiseAbove( const Internal::Layer& target )
160 // cannot raise above ourself, both have to be on stage
161 if( ( this != &target ) && OnStage() && target.OnStage() )
163 // get parameters depth
164 const unsigned int targetDepth = target.GetDepth();
165 if( GetDepth() < targetDepth )
172 void Layer::LowerBelow( const Internal::Layer& target )
174 // cannot lower below ourself, both have to be on stage
175 if( ( this != &target ) && OnStage() && target.OnStage() )
177 // get parameters depth
178 const unsigned int targetDepth = target.GetDepth();
179 if( GetDepth() > targetDepth )
186 void Layer::RaiseToTop()
190 mLayerList->RaiseLayerToTop(*this);
194 void Layer::LowerToBottom()
198 mLayerList->LowerLayerToBottom(*this);
202 void Layer::MoveAbove( const Internal::Layer& target )
204 // cannot raise above ourself, both have to be on stage
205 if( ( this != &target ) && mLayerList && target.OnStage() )
207 mLayerList->MoveLayerAbove(*this, target );
211 void Layer::MoveBelow( const Internal::Layer& target )
213 // cannot lower below ourself, both have to be on stage
214 if( ( this != &target ) && mLayerList && target.OnStage() )
216 mLayerList->MoveLayerBelow(*this, target );
220 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
222 mBehavior = behavior;
224 // notify update side object
225 SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
228 void Layer::SetClipping(bool enabled)
230 if (enabled != mIsClipping)
232 mIsClipping = enabled;
234 // layerNode is being used in a separate thread; queue a message to set the value
235 SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
239 void Layer::SetClippingBox(int x, int y, int width, int height)
241 if( ( x != mClippingBox.x ) ||
242 ( y != mClippingBox.y ) ||
243 ( width != mClippingBox.width ) ||
244 ( height != mClippingBox.height ) )
246 // Clipping box is not animatable; this is the most up-to-date value
247 mClippingBox.Set(x, y, width, height);
249 // Convert mClippingBox to GL based coordinates (from bottom-left)
250 ClippingBox clippingBox( mClippingBox );
252 StagePtr stage = Stage::GetCurrent();
255 clippingBox.y = stage->GetSize().height - clippingBox.y - clippingBox.height;
257 // layerNode is being used in a separate thread; queue a message to set the value
258 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
263 void Layer::SetDepthTestDisabled( bool disable )
265 if( disable != mDepthTestDisabled )
267 mDepthTestDisabled = disable;
269 // Send message .....
270 // layerNode is being used in a separate thread; queue a message to set the value
271 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
275 bool Layer::IsDepthTestDisabled() const
277 return mDepthTestDisabled || (mBehavior == Dali::Layer::LAYER_2D);
280 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
282 if( function != mSortFunction )
284 mSortFunction = function;
286 // layerNode is being used in a separate thread; queue a message to set the value
287 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
291 void Layer::SetTouchConsumed( bool consume )
293 mTouchConsumed = consume;
296 bool Layer::IsTouchConsumed() const
298 return mTouchConsumed;
301 void Layer::SetHoverConsumed( bool consume )
303 mHoverConsumed = consume;
306 bool Layer::IsHoverConsumed() const
308 return mHoverConsumed;
311 SceneGraph::Node* Layer::CreateNode() const
313 return SceneGraph::Layer::New();
316 void Layer::OnStageConnectionInternal()
320 DALI_ASSERT_DEBUG( NULL == mLayerList );
322 // Find the ordered layer-list
323 // This is different for Layers added via Integration::GetSystemOverlay()
324 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
326 if( parent->IsLayer() )
328 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
329 mLayerList = parentLayer->mLayerList;
334 DALI_ASSERT_DEBUG( NULL != mLayerList );
335 mLayerList->RegisterLayer( *this );
338 void Layer::OnStageDisconnectionInternal()
340 mLayerList->UnregisterLayer(*this);
342 // mLayerList is only valid when on-stage
346 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
348 DALI_ASSERT_DEBUG( mNode != NULL );
349 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
352 unsigned int Layer::GetDefaultPropertyCount() const
354 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
357 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
359 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
360 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
362 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
363 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
365 indices.PushBack( index );
369 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
371 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
373 return Actor::IsDefaultPropertyWritable( index );
376 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
379 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
381 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
383 return Actor::IsDefaultPropertyAnimatable( index );
386 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
389 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
391 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
393 return Actor::IsDefaultPropertyAConstraintInput( index );
396 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
399 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
401 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
403 return Actor::GetDefaultPropertyType( index );
406 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
408 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
410 return DEFAULT_PROPERTY_DETAILS[index].type;
413 // index out-of-bounds
414 return Property::NONE;
417 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
419 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
421 return Actor::GetDefaultPropertyName( index );
424 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
425 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
427 return DEFAULT_PROPERTY_DETAILS[index].name;
433 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
435 Property::Index index = Property::INVALID_INDEX;
437 // Look for name in current class' default properties
438 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
440 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
441 if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
443 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
447 if( Property::INVALID_INDEX == index )
449 // If not found, check in base class
450 index = Actor::GetDefaultPropertyIndex( name );
456 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
458 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
460 Actor::SetDefaultProperty( index, propertyValue );
466 case Dali::Layer::Property::CLIPPING_ENABLE:
468 SetClipping( propertyValue.Get<bool>() );
471 case Dali::Layer::Property::CLIPPING_BOX:
473 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
474 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
477 case Dali::Layer::Property::BEHAVIOR:
479 Behavior behavior(Dali::Layer::LAYER_2D);
480 if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BehaviorTable, BehaviorTableCount, behavior ) )
482 SetBehavior( behavior );
488 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
496 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
499 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
501 ret = Actor::GetDefaultProperty( index );
507 case Dali::Layer::Property::CLIPPING_ENABLE:
512 case Dali::Layer::Property::CLIPPING_BOX:
517 case Dali::Layer::Property::BEHAVIOR:
519 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BehaviorTable, BehaviorTableCount );
524 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
533 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
536 Layer* layer = dynamic_cast<Layer*>( object );
540 if( 0 == actionName.compare( ACTION_RAISE ) )
545 else if( 0 == actionName.compare( ACTION_LOWER ) )
550 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
555 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
557 layer->LowerToBottom();
565 } // namespace Internal