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;
39 typedef Layer::Behavior Behavior;
41 DALI_ENUM_TO_STRING_TABLE_BEGIN( Behavior )
42 DALI_ENUM_TO_STRING_INSIDE_CLASS( Layer, LAYER_2D )
43 DALI_ENUM_TO_STRING_INSIDE_CLASS( 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),
126 mDepthTestDisabled(false),
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 );
231 void Layer::SetClipping(bool enabled)
233 if (enabled != mIsClipping)
235 mIsClipping = enabled;
237 // layerNode is being used in a separate thread; queue a message to set the value
238 SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
242 void Layer::SetClippingBox(int x, int y, int width, int height)
244 if( ( x != mClippingBox.x ) ||
245 ( y != mClippingBox.y ) ||
246 ( width != mClippingBox.width ) ||
247 ( height != mClippingBox.height ) )
249 // Clipping box is not animatable; this is the most up-to-date value
250 mClippingBox.Set(x, y, width, height);
252 // Convert mClippingBox to GL based coordinates (from bottom-left)
253 ClippingBox clippingBox( mClippingBox );
255 StagePtr stage = Stage::GetCurrent();
258 clippingBox.y = stage->GetSize().height - clippingBox.y - clippingBox.height;
260 // layerNode is being used in a separate thread; queue a message to set the value
261 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
266 void Layer::SetDepthTestDisabled( bool disable )
268 if( disable != mDepthTestDisabled )
270 mDepthTestDisabled = disable;
272 // Send message .....
273 // layerNode is being used in a separate thread; queue a message to set the value
274 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
278 bool Layer::IsDepthTestDisabled() const
280 return mDepthTestDisabled || (mBehavior == Dali::Layer::LAYER_2D);
283 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
285 if( function != mSortFunction )
287 mSortFunction = function;
289 // layerNode is being used in a separate thread; queue a message to set the value
290 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
294 void Layer::SetTouchConsumed( bool consume )
296 mTouchConsumed = consume;
299 bool Layer::IsTouchConsumed() const
301 return mTouchConsumed;
304 void Layer::SetHoverConsumed( bool consume )
306 mHoverConsumed = consume;
309 bool Layer::IsHoverConsumed() const
311 return mHoverConsumed;
314 SceneGraph::Node* Layer::CreateNode() const
316 return SceneGraph::Layer::New();
319 void Layer::OnStageConnectionInternal()
323 DALI_ASSERT_DEBUG( NULL == mLayerList );
325 // Find the ordered layer-list
326 // This is different for Layers added via Integration::GetSystemOverlay()
327 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
329 if( parent->IsLayer() )
331 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
332 mLayerList = parentLayer->mLayerList;
337 DALI_ASSERT_DEBUG( NULL != mLayerList );
338 mLayerList->RegisterLayer( *this );
341 void Layer::OnStageDisconnectionInternal()
343 mLayerList->UnregisterLayer(*this);
345 // mLayerList is only valid when on-stage
349 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
351 DALI_ASSERT_DEBUG( mNode != NULL );
352 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
355 unsigned int Layer::GetDefaultPropertyCount() const
357 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
360 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
362 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
363 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
365 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
366 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
368 indices.PushBack( index );
372 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
374 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
376 return Actor::IsDefaultPropertyWritable( index );
379 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
382 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
384 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
386 return Actor::IsDefaultPropertyAnimatable( index );
389 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
392 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
394 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
396 return Actor::IsDefaultPropertyAConstraintInput( index );
399 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
402 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
404 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
406 return Actor::GetDefaultPropertyType( index );
409 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
411 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
413 return DEFAULT_PROPERTY_DETAILS[index].type;
416 // index out-of-bounds
417 return Property::NONE;
420 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
422 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
424 return Actor::GetDefaultPropertyName( index );
427 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
428 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
430 return DEFAULT_PROPERTY_DETAILS[index].name;
436 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
438 Property::Index index = Property::INVALID_INDEX;
440 // Look for name in current class' default properties
441 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
443 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
444 if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
446 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
450 if( Property::INVALID_INDEX == index )
452 // If not found, check in base class
453 index = Actor::GetDefaultPropertyIndex( name );
459 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
461 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
463 Actor::SetDefaultProperty( index, propertyValue );
469 case Dali::Layer::Property::CLIPPING_ENABLE:
471 SetClipping( propertyValue.Get<bool>() );
474 case Dali::Layer::Property::CLIPPING_BOX:
476 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
477 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
480 case Dali::Layer::Property::BEHAVIOR:
482 Behavior behavior(Dali::Layer::LAYER_2D);
483 if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BehaviorTable, BehaviorTableCount, behavior ) )
485 SetBehavior( behavior );
491 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
499 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
502 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
504 ret = Actor::GetDefaultProperty( index );
510 case Dali::Layer::Property::CLIPPING_ENABLE:
515 case Dali::Layer::Property::CLIPPING_BOX:
520 case Dali::Layer::Property::BEHAVIOR:
522 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BehaviorTable, BehaviorTableCount );
527 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
536 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
539 Layer* layer = dynamic_cast<Layer*>( object );
543 if( 0 == actionName.compare( ACTION_RAISE ) )
548 else if( 0 == actionName.compare( ACTION_LOWER ) )
553 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
558 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
560 layer->LowerToBottom();
568 } // namespace Internal