2 * Copyright (c) 2017 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, keep a raw pointer to the layer node.
100 SceneGraph::Layer* rootLayer = static_cast<SceneGraph::Layer*>( root->CreateNode() );
101 root->mNode = rootLayer;
102 OwnerPointer< SceneGraph::Layer > transferOwnership( rootLayer );
103 InstallRootMessage( manager, transferOwnership, systemLevel );
105 // root actor is immediately considered to be on-stage
106 root->mIsOnStage = true;
108 // The root actor will not emit a stage connection signal so set the signalled flag here as well
109 root->mOnStageSignalled = true;
111 // layer-list must be set for the root layer
112 root->mLayerList = &layerList;
113 layerList.RegisterLayer( *root );
118 Layer::Layer( Actor::DerivedType type )
121 mClippingBox( 0, 0, 0, 0 ),
122 mSortFunction( Layer::ZValue ),
123 mBehavior( Dali::Layer::LAYER_2D ),
124 mIsClipping( false ),
125 mDepthTestDisabled( true ),
126 mTouchConsumed( false ),
127 mHoverConsumed( false )
131 void Layer::OnInitialize()
139 unsigned int Layer::GetDepth() const
141 return mLayerList ? mLayerList->GetDepth( this ) : 0u;
148 mLayerList->RaiseLayer(*this);
156 mLayerList->LowerLayer(*this);
160 void Layer::RaiseAbove( const Internal::Layer& target )
162 // cannot raise above ourself, both have to be on stage
163 if( ( this != &target ) && OnStage() && target.OnStage() )
165 // get parameters depth
166 const unsigned int targetDepth = target.GetDepth();
167 if( GetDepth() < targetDepth )
174 void Layer::LowerBelow( const Internal::Layer& target )
176 // cannot lower below ourself, both have to be on stage
177 if( ( this != &target ) && OnStage() && target.OnStage() )
179 // get parameters depth
180 const unsigned int targetDepth = target.GetDepth();
181 if( GetDepth() > targetDepth )
188 void Layer::RaiseToTop()
192 mLayerList->RaiseLayerToTop(*this);
196 void Layer::LowerToBottom()
200 mLayerList->LowerLayerToBottom(*this);
204 void Layer::MoveAbove( const Internal::Layer& target )
206 // cannot raise above ourself, both have to be on stage
207 if( ( this != &target ) && mLayerList && target.OnStage() )
209 mLayerList->MoveLayerAbove(*this, target );
213 void Layer::MoveBelow( const Internal::Layer& target )
215 // cannot lower below ourself, both have to be on stage
216 if( ( this != &target ) && mLayerList && target.OnStage() )
218 mLayerList->MoveLayerBelow(*this, target );
222 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
224 mBehavior = behavior;
226 // Notify update side object.
227 SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
228 // By default, disable depth test for LAYER_2D, and enable for LAYER_3D.
229 SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_2D );
232 void Layer::SetClipping(bool enabled)
234 if (enabled != mIsClipping)
236 mIsClipping = enabled;
238 // layerNode is being used in a separate thread; queue a message to set the value
239 SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
243 void Layer::SetClippingBox(int x, int y, int width, int height)
245 if( ( x != mClippingBox.x ) ||
246 ( y != mClippingBox.y ) ||
247 ( width != mClippingBox.width ) ||
248 ( height != mClippingBox.height ) )
250 // Clipping box is not animatable; this is the most up-to-date value
251 mClippingBox.Set(x, y, width, height);
253 // Convert mClippingBox to GL based coordinates (from bottom-left)
254 ClippingBox clippingBox( mClippingBox );
256 StagePtr stage = Stage::GetCurrent();
259 clippingBox.y = stage->GetSize().height - clippingBox.y - clippingBox.height;
261 // layerNode is being used in a separate thread; queue a message to set the value
262 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
267 void Layer::SetDepthTestDisabled( bool disable )
269 if( disable != mDepthTestDisabled )
271 mDepthTestDisabled = disable;
274 // layerNode is being used in a separate thread; queue a message to set the value
275 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
279 bool Layer::IsDepthTestDisabled() const
281 return mDepthTestDisabled;
284 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
286 if( function != mSortFunction )
288 mSortFunction = function;
290 // layerNode is being used in a separate thread; queue a message to set the value
291 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
295 void Layer::SetTouchConsumed( bool consume )
297 mTouchConsumed = consume;
300 bool Layer::IsTouchConsumed() const
302 return mTouchConsumed;
305 void Layer::SetHoverConsumed( bool consume )
307 mHoverConsumed = consume;
310 bool Layer::IsHoverConsumed() const
312 return mHoverConsumed;
315 SceneGraph::Node* Layer::CreateNode() const
317 return SceneGraph::Layer::New();
320 void Layer::OnStageConnectionInternal()
324 DALI_ASSERT_DEBUG( NULL == mLayerList );
326 // Find the ordered layer-list
327 // This is different for Layers added via Integration::GetSystemOverlay()
328 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
330 if( parent->IsLayer() )
332 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
333 mLayerList = parentLayer->mLayerList;
338 DALI_ASSERT_DEBUG( NULL != mLayerList );
339 mLayerList->RegisterLayer( *this );
342 void Layer::OnStageDisconnectionInternal()
344 mLayerList->UnregisterLayer(*this);
346 // mLayerList is only valid when on-stage
350 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
352 DALI_ASSERT_DEBUG( mNode != NULL );
353 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
356 unsigned int Layer::GetDefaultPropertyCount() const
358 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
361 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
363 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
364 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
366 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
367 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
369 indices.PushBack( index );
373 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
375 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
377 return Actor::IsDefaultPropertyWritable( index );
380 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
383 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
385 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
387 return Actor::IsDefaultPropertyAnimatable( index );
390 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
393 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
395 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
397 return Actor::IsDefaultPropertyAConstraintInput( index );
400 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
403 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
405 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
407 return Actor::GetDefaultPropertyType( index );
410 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
412 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
414 return DEFAULT_PROPERTY_DETAILS[index].type;
417 // index out-of-bounds
418 return Property::NONE;
421 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
423 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
425 return Actor::GetDefaultPropertyName( index );
428 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
429 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
431 return DEFAULT_PROPERTY_DETAILS[index].name;
437 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
439 Property::Index index = Property::INVALID_INDEX;
441 // Look for name in current class' default properties
442 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
444 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
445 if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
447 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
451 if( Property::INVALID_INDEX == index )
453 // If not found, check in base class
454 index = Actor::GetDefaultPropertyIndex( name );
460 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
462 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
464 Actor::SetDefaultProperty( index, propertyValue );
470 case Dali::Layer::Property::CLIPPING_ENABLE:
472 SetClipping( propertyValue.Get<bool>() );
475 case Dali::Layer::Property::CLIPPING_BOX:
477 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
478 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
481 case Dali::Layer::Property::BEHAVIOR:
483 Behavior behavior(Dali::Layer::LAYER_2D);
484 if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
486 SetBehavior( behavior );
492 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
500 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
503 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
505 ret = Actor::GetDefaultProperty( index );
511 case Dali::Layer::Property::CLIPPING_ENABLE:
516 case Dali::Layer::Property::CLIPPING_BOX:
521 case Dali::Layer::Property::BEHAVIOR:
523 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
528 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
537 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
540 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
542 ret = Actor::GetDefaultPropertyCurrentValue( index );
546 ret = GetDefaultProperty( index ); // Layer only has event-side properties
552 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
555 Layer* layer = dynamic_cast<Layer*>( object );
559 if( 0 == actionName.compare( ACTION_RAISE ) )
564 else if( 0 == actionName.compare( ACTION_LOWER ) )
569 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
574 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
576 layer->LowerToBottom();
584 } // namespace Internal