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),
122 mBehavior(Dali::Layer::LAYER_2D),
124 mDepthTestDisabled(false),
125 mTouchConsumed(false),
126 mHoverConsumed(false)
130 void Layer::OnInitialize()
138 unsigned int Layer::GetDepth() const
140 return mLayerList ? mLayerList->GetDepth( this ) : 0u;
147 mLayerList->RaiseLayer(*this);
155 mLayerList->LowerLayer(*this);
159 void Layer::RaiseAbove( const Internal::Layer& target )
161 // cannot raise above ourself, both have to be on stage
162 if( ( this != &target ) && OnStage() && target.OnStage() )
164 // get parameters depth
165 const unsigned int targetDepth = target.GetDepth();
166 if( GetDepth() < targetDepth )
173 void Layer::LowerBelow( const Internal::Layer& target )
175 // cannot lower below ourself, both have to be on stage
176 if( ( this != &target ) && OnStage() && target.OnStage() )
178 // get parameters depth
179 const unsigned int targetDepth = target.GetDepth();
180 if( GetDepth() > targetDepth )
187 void Layer::RaiseToTop()
191 mLayerList->RaiseLayerToTop(*this);
195 void Layer::LowerToBottom()
199 mLayerList->LowerLayerToBottom(*this);
203 void Layer::MoveAbove( const Internal::Layer& target )
205 // cannot raise above ourself, both have to be on stage
206 if( ( this != &target ) && mLayerList && target.OnStage() )
208 mLayerList->MoveLayerAbove(*this, target );
212 void Layer::MoveBelow( const Internal::Layer& target )
214 // cannot lower below ourself, both have to be on stage
215 if( ( this != &target ) && mLayerList && target.OnStage() )
217 mLayerList->MoveLayerBelow(*this, target );
221 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
223 mBehavior = behavior;
225 // notify update side object
226 SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
229 void Layer::SetClipping(bool enabled)
231 if (enabled != mIsClipping)
233 mIsClipping = enabled;
235 // layerNode is being used in a separate thread; queue a message to set the value
236 SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
240 void Layer::SetClippingBox(int x, int y, int width, int height)
242 if( ( x != mClippingBox.x ) ||
243 ( y != mClippingBox.y ) ||
244 ( width != mClippingBox.width ) ||
245 ( height != mClippingBox.height ) )
247 // Clipping box is not animatable; this is the most up-to-date value
248 mClippingBox.Set(x, y, width, height);
250 // Convert mClippingBox to GL based coordinates (from bottom-left)
251 ClippingBox clippingBox( mClippingBox );
253 StagePtr stage = Stage::GetCurrent();
256 clippingBox.y = stage->GetSize().height - clippingBox.y - clippingBox.height;
258 // layerNode is being used in a separate thread; queue a message to set the value
259 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
264 void Layer::SetDepthTestDisabled( bool disable )
266 if( disable != mDepthTestDisabled )
268 mDepthTestDisabled = disable;
270 // Send message .....
271 // layerNode is being used in a separate thread; queue a message to set the value
272 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
276 bool Layer::IsDepthTestDisabled() const
278 return mDepthTestDisabled || (mBehavior == Dali::Layer::LAYER_2D);
281 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
283 if( function != mSortFunction )
285 mSortFunction = function;
287 // layerNode is being used in a separate thread; queue a message to set the value
288 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
292 void Layer::SetTouchConsumed( bool consume )
294 mTouchConsumed = consume;
297 bool Layer::IsTouchConsumed() const
299 return mTouchConsumed;
302 void Layer::SetHoverConsumed( bool consume )
304 mHoverConsumed = consume;
307 bool Layer::IsHoverConsumed() const
309 return mHoverConsumed;
312 SceneGraph::Node* Layer::CreateNode() const
314 return SceneGraph::Layer::New();
317 void Layer::OnStageConnectionInternal()
321 DALI_ASSERT_DEBUG( NULL == mLayerList );
323 // Find the ordered layer-list
324 // This is different for Layers added via Integration::GetSystemOverlay()
325 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
327 if( parent->IsLayer() )
329 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
330 mLayerList = parentLayer->mLayerList;
335 DALI_ASSERT_DEBUG( NULL != mLayerList );
336 mLayerList->RegisterLayer( *this );
339 void Layer::OnStageDisconnectionInternal()
341 mLayerList->UnregisterLayer(*this);
343 // mLayerList is only valid when on-stage
347 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
349 DALI_ASSERT_DEBUG( mNode != NULL );
350 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
353 unsigned int Layer::GetDefaultPropertyCount() const
355 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
358 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
360 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
361 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
363 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
364 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
366 indices.PushBack( index );
370 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
372 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
374 return Actor::IsDefaultPropertyWritable( index );
377 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
380 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
382 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
384 return Actor::IsDefaultPropertyAnimatable( index );
387 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
390 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
392 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
394 return Actor::IsDefaultPropertyAConstraintInput( index );
397 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
400 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
402 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
404 return Actor::GetDefaultPropertyType( index );
407 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
409 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
411 return DEFAULT_PROPERTY_DETAILS[index].type;
414 // index out-of-bounds
415 return Property::NONE;
418 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
420 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
422 return Actor::GetDefaultPropertyName( index );
425 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
426 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
428 return DEFAULT_PROPERTY_DETAILS[index].name;
434 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
436 Property::Index index = Property::INVALID_INDEX;
438 // Look for name in current class' default properties
439 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
441 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
442 if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
444 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
448 if( Property::INVALID_INDEX == index )
450 // If not found, check in base class
451 index = Actor::GetDefaultPropertyIndex( name );
457 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
459 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
461 Actor::SetDefaultProperty( index, propertyValue );
467 case Dali::Layer::Property::CLIPPING_ENABLE:
469 SetClipping( propertyValue.Get<bool>() );
472 case Dali::Layer::Property::CLIPPING_BOX:
474 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
475 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
478 case Dali::Layer::Property::BEHAVIOR:
480 Behavior behavior(Dali::Layer::LAYER_2D);
481 if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BehaviorTable, BehaviorTableCount, behavior ) )
483 SetBehavior( behavior );
489 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
497 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
500 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
502 ret = Actor::GetDefaultProperty( index );
508 case Dali::Layer::Property::CLIPPING_ENABLE:
513 case Dali::Layer::Property::CLIPPING_BOX:
518 case Dali::Layer::Property::BEHAVIOR:
520 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BehaviorTable, BehaviorTableCount );
525 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
534 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
537 Layer* layer = dynamic_cast<Layer*>( object );
541 if( 0 == actionName.compare( ACTION_RAISE ) )
546 else if( 0 == actionName.compare( ACTION_LOWER ) )
551 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
556 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
558 layer->LowerToBottom();
566 } // namespace Internal