2 * Copyright (c) 2019 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/scene-impl.h>
30 #include <dali/internal/event/common/event-thread-services.h>
32 using Dali::Internal::SceneGraph::UpdateManager;
40 typedef Layer::Behavior Behavior;
42 DALI_ENUM_TO_STRING_TABLE_BEGIN( BEHAVIOR )
43 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_UI )
44 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_3D )
45 DALI_ENUM_TO_STRING_TABLE_END( BEHAVIOR )
57 // Name Type writable animatable constraint-input enum for index-checking
58 DALI_PROPERTY_TABLE_BEGIN
59 DALI_PROPERTY( "clippingEnable", BOOLEAN, true, false, true, Dali::Layer::Property::CLIPPING_ENABLE )
60 DALI_PROPERTY( "clippingBox", RECTANGLE, true, false, true, Dali::Layer::Property::CLIPPING_BOX )
61 DALI_PROPERTY( "behavior", STRING, true, false, false, Dali::Layer::Property::BEHAVIOR )
62 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, LayerDefaultProperties )
66 const char* const ACTION_RAISE = "raise";
67 const char* const ACTION_LOWER = "lower";
68 const char* const ACTION_RAISE_TO_TOP = "raiseToTop";
69 const char* const ACTION_LOWER_TO_BOTTOM = "lowerToBottom";
73 return Dali::Layer::New();
76 TypeRegistration mType( typeid( Dali::Layer ), typeid( Dali::Actor ), Create, LayerDefaultProperties );
78 TypeAction a1( mType, ACTION_RAISE, &Layer::DoAction );
79 TypeAction a2( mType, ACTION_LOWER, &Layer::DoAction );
80 TypeAction a3( mType, ACTION_RAISE_TO_TOP, &Layer::DoAction );
81 TypeAction a4( mType, ACTION_LOWER_TO_BOTTOM, &Layer::DoAction );
83 } // unnamed namespace
88 // create node, nodes are owned by UpdateManager
89 SceneGraph::Layer* layerNode = SceneGraph::Layer::New();
90 OwnerPointer< SceneGraph::Node > transferOwnership( layerNode );
91 AddNodeMessage( EventThreadServices::Get().GetUpdateManager(), transferOwnership );
92 LayerPtr layer( new Layer( Actor::LAYER, *layerNode ) );
94 // Second-phase construction
100 LayerPtr Layer::NewRoot( LayerList& layerList )
102 // create node, nodes are owned by UpdateManager
103 SceneGraph::Layer* rootLayer = SceneGraph::Layer::New();
104 OwnerPointer< SceneGraph::Layer > transferOwnership( rootLayer );
105 InstallRootMessage( EventThreadServices::Get().GetUpdateManager(), transferOwnership );
107 LayerPtr root( new Layer( Actor::ROOT_LAYER, *rootLayer ) );
109 // root actor is immediately considered to be on-stage
110 root->mIsOnStage = true;
112 // The root actor will not emit a stage connection signal so set the signalled flag here as well
113 root->mOnStageSignalled = true;
115 // layer-list must be set for the root layer
116 root->mLayerList = &layerList;
117 layerList.SetRootLayer( &(*root) );
118 layerList.RegisterLayer( *root );
123 Layer::Layer( Actor::DerivedType type, const SceneGraph::Layer& layer )
124 : Actor( type, layer ),
126 mClippingBox( 0, 0, 0, 0 ),
127 mSortFunction( Layer::ZValue ),
128 mBehavior( Dali::Layer::LAYER_UI ),
129 mIsClipping( false ),
130 mDepthTestDisabled( true ),
131 mTouchConsumed( false ),
132 mHoverConsumed( false )
136 void Layer::OnInitialize()
144 // Guard to allow handle destruction after Core has been destroyed
145 if( EventThreadServices::IsCoreRunning() )
147 UninstallRootMessage( GetEventThreadServices().GetUpdateManager(), &GetSceneLayerOnStage() );
149 GetEventThreadServices().UnregisterObject( this );
154 unsigned int Layer::GetDepth() const
156 return mLayerList ? mLayerList->GetDepth( this ) : 0u;
163 mLayerList->RaiseLayer(*this);
171 mLayerList->LowerLayer(*this);
175 void Layer::RaiseAbove( const Internal::Layer& target )
177 // cannot raise above ourself, both have to be on stage
178 if( ( this != &target ) && OnStage() && target.OnStage() )
180 // get parameters depth
181 const uint32_t targetDepth = target.GetDepth();
182 if( GetDepth() < targetDepth )
189 void Layer::LowerBelow( const Internal::Layer& target )
191 // cannot lower below ourself, both have to be on stage
192 if( ( this != &target ) && OnStage() && target.OnStage() )
194 // get parameters depth
195 const uint32_t targetDepth = target.GetDepth();
196 if( GetDepth() > targetDepth )
203 void Layer::RaiseToTop()
207 mLayerList->RaiseLayerToTop(*this);
211 void Layer::LowerToBottom()
215 mLayerList->LowerLayerToBottom(*this);
219 void Layer::MoveAbove( const Internal::Layer& target )
221 // cannot raise above ourself, both have to be on stage
222 if( ( this != &target ) && mLayerList && target.OnStage() )
224 mLayerList->MoveLayerAbove(*this, target );
228 void Layer::MoveBelow( const Internal::Layer& target )
230 // cannot lower below ourself, both have to be on stage
231 if( ( this != &target ) && mLayerList && target.OnStage() )
233 mLayerList->MoveLayerBelow(*this, target );
237 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
239 mBehavior = behavior;
241 // Notify update side object.
242 SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
243 // By default, disable depth test for LAYER_UI, and enable for LAYER_3D.
244 SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_UI );
247 void Layer::SetClipping(bool enabled)
249 if (enabled != mIsClipping)
251 mIsClipping = enabled;
253 // layerNode is being used in a separate thread; queue a message to set the value
254 SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
258 void Layer::SetClippingBox(int x, int y, int width, int height)
260 if( ( x != mClippingBox.x ) ||
261 ( y != mClippingBox.y ) ||
262 ( width != mClippingBox.width ) ||
263 ( height != mClippingBox.height ) )
265 // Clipping box is not animatable; this is the most up-to-date value
266 mClippingBox.Set(x, y, width, height);
268 // Convert mClippingBox to GL based coordinates (from bottom-left)
269 ClippingBox clippingBox( mClippingBox );
273 clippingBox.y = static_cast<int32_t>( mScene->GetSize().height ) - clippingBox.y - clippingBox.height;
275 // layerNode is being used in a separate thread; queue a message to set the value
276 SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
281 void Layer::SetDepthTestDisabled( bool disable )
283 if( disable != mDepthTestDisabled )
285 mDepthTestDisabled = disable;
288 // layerNode is being used in a separate thread; queue a message to set the value
289 SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
293 bool Layer::IsDepthTestDisabled() const
295 return mDepthTestDisabled;
298 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
300 if( function != mSortFunction )
302 mSortFunction = function;
304 // layerNode is being used in a separate thread; queue a message to set the value
305 SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
309 void Layer::SetTouchConsumed( bool consume )
311 mTouchConsumed = consume;
314 bool Layer::IsTouchConsumed() const
316 return mTouchConsumed;
319 void Layer::SetHoverConsumed( bool consume )
321 mHoverConsumed = consume;
324 bool Layer::IsHoverConsumed() const
326 return mHoverConsumed;
329 void Layer::OnStageConnectionInternal()
333 DALI_ASSERT_DEBUG( NULL == mLayerList );
335 // Find the ordered layer-list
336 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
338 if( parent->IsLayer() )
340 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
341 mLayerList = parentLayer->mLayerList;
346 DALI_ASSERT_DEBUG( NULL != mLayerList );
347 mLayerList->RegisterLayer( *this );
350 void Layer::OnStageDisconnectionInternal()
352 mLayerList->UnregisterLayer(*this);
354 // mLayerList is only valid when on-stage
358 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
360 return static_cast< const SceneGraph::Layer& >( GetNode() ); // we know our node is a layer node
363 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
365 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
367 Actor::SetDefaultProperty( index, propertyValue );
373 case Dali::Layer::Property::CLIPPING_ENABLE:
375 SetClipping( propertyValue.Get<bool>() );
378 case Dali::Layer::Property::CLIPPING_BOX:
380 Rect<int32_t> clippingBox( propertyValue.Get<Rect<int32_t> >() );
381 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
384 case Dali::Layer::Property::BEHAVIOR:
386 Behavior behavior(Dali::Layer::LAYER_UI);
387 if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
389 SetBehavior( behavior );
395 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
403 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
406 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
408 ret = Actor::GetDefaultProperty( index );
414 case Dali::Layer::Property::CLIPPING_ENABLE:
419 case Dali::Layer::Property::CLIPPING_BOX:
424 case Dali::Layer::Property::BEHAVIOR:
426 ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
431 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
440 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
443 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
445 ret = Actor::GetDefaultPropertyCurrentValue( index );
449 ret = GetDefaultProperty( index ); // Layer only has event-side properties
455 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
458 Layer* layer = dynamic_cast<Layer*>( object );
462 if( 0 == actionName.compare( ACTION_RAISE ) )
467 else if( 0 == actionName.compare( ACTION_LOWER ) )
472 else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
477 else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
479 layer->LowerToBottom();
487 } // namespace Internal