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 );
251 clippingBox.y = Stage::GetCurrent()->GetSize().height - clippingBox.y - clippingBox.height;
253 // layerNode is being used in a separate thread; queue a message to set the value
254 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
258 void Layer::SetDepthTestDisabled( bool disable )
260 if( disable != mDepthTestDisabled )
262 mDepthTestDisabled = disable;
264 // Send message .....
265 // layerNode is being used in a separate thread; queue a message to set the value
266 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
270 bool Layer::IsDepthTestDisabled() const
272 return mDepthTestDisabled || (mBehavior == Dali::Layer::LAYER_2D);
275 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
277 if( function != mSortFunction )
279 mSortFunction = function;
281 // layerNode is being used in a separate thread; queue a message to set the value
282 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
286 void Layer::SetTouchConsumed( bool consume )
288 mTouchConsumed = consume;
291 bool Layer::IsTouchConsumed() const
293 return mTouchConsumed;
296 void Layer::SetHoverConsumed( bool consume )
298 mHoverConsumed = consume;
301 bool Layer::IsHoverConsumed() const
303 return mHoverConsumed;
306 SceneGraph::Node* Layer::CreateNode() const
308 return SceneGraph::Layer::New();
311 void Layer::OnStageConnectionInternal()
315 DALI_ASSERT_DEBUG( NULL == mLayerList );
317 // Find the ordered layer-list
318 // This is different for Layers added via Integration::GetSystemOverlay()
319 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
321 if( parent->IsLayer() )
323 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
324 mLayerList = parentLayer->mLayerList;
329 DALI_ASSERT_DEBUG( NULL != mLayerList );
330 mLayerList->RegisterLayer( *this );
333 void Layer::OnStageDisconnectionInternal()
335 mLayerList->UnregisterLayer(*this);
337 // mLayerList is only valid when on-stage
341 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
343 DALI_ASSERT_DEBUG( mNode != NULL );
344 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
347 unsigned int Layer::GetDefaultPropertyCount() const
349 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
352 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
354 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
355 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
357 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
358 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
360 indices.PushBack( index );
364 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
366 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
368 return Actor::IsDefaultPropertyWritable( index );
371 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
374 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
376 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
378 return Actor::IsDefaultPropertyAnimatable( index );
381 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
384 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
386 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
388 return Actor::IsDefaultPropertyAConstraintInput( index );
391 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
394 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
396 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
398 return Actor::GetDefaultPropertyType( index );
401 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
403 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
405 return DEFAULT_PROPERTY_DETAILS[index].type;
408 // index out-of-bounds
409 return Property::NONE;
412 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
414 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
416 return Actor::GetDefaultPropertyName( index );
419 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
420 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
422 return DEFAULT_PROPERTY_DETAILS[index].name;
428 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
430 Property::Index index = Property::INVALID_INDEX;
432 // Look for name in current class' default properties
433 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
435 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
436 if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
438 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
442 if( Property::INVALID_INDEX == index )
444 // If not found, check in base class
445 index = Actor::GetDefaultPropertyIndex( name );
451 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
453 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
455 Actor::SetDefaultProperty( index, propertyValue );
461 case Dali::Layer::Property::CLIPPING_ENABLE:
463 SetClipping( propertyValue.Get<bool>() );
466 case Dali::Layer::Property::CLIPPING_BOX:
468 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
469 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
472 case Dali::Layer::Property::BEHAVIOR:
474 Behavior behavior = Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BehaviorTable, BehaviorTableCount );
475 SetBehavior( behavior );
480 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
488 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
491 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
493 ret = Actor::GetDefaultProperty( index );
499 case Dali::Layer::Property::CLIPPING_ENABLE:
504 case Dali::Layer::Property::CLIPPING_BOX:
509 case Dali::Layer::Property::BEHAVIOR:
511 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BehaviorTable, BehaviorTableCount );
516 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
525 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
528 Layer* layer = dynamic_cast<Layer*>( object );
532 if( 0 == actionName.compare( ACTION_RAISE ) )
537 else if( 0 == actionName.compare( ACTION_LOWER ) )
542 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
547 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
549 layer->LowerToBottom();
557 } // namespace Internal