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>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/internal/event/common/property-index-ranges.h>
25 #include <dali/internal/event/common/stage-impl.h>
26 #include <dali/internal/event/actors/layer-list.h>
28 using Dali::Internal::SceneGraph::UpdateManager;
33 const Property::Index Layer::CLIPPING_ENABLE = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
34 const Property::Index Layer::CLIPPING_BOX = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 1;
38 bool Layer::mFirstInstance = true;
39 Actor::DefaultPropertyLookup* Layer::mDefaultLayerPropertyLookup = NULL;
46 return Dali::Layer::New();
49 TypeRegistration mType( typeid(Dali::Layer), typeid(Dali::Actor), Create );
51 TypeAction a1(mType, Dali::Layer::ACTION_RAISE, &Layer::DoAction);
52 TypeAction a2(mType, Dali::Layer::ACTION_LOWER, &Layer::DoAction);
53 TypeAction a3(mType, Dali::Layer::ACTION_RAISE_TO_TOP, &Layer::DoAction);
54 TypeAction a4(mType, Dali::Layer::ACTION_LOWER_TO_BOTTOM, &Layer::DoAction);
56 const std::string DEFAULT_LAYER_PROPERTY_NAMES[] =
61 const int DEFAULT_LAYER_PROPERTY_COUNT = sizeof( DEFAULT_LAYER_PROPERTY_NAMES ) / sizeof( std::string );
63 const Property::Type DEFAULT_LAYER_PROPERTY_TYPES[DEFAULT_LAYER_PROPERTY_COUNT] =
65 Property::BOOLEAN, // "clipping-enable",
66 Property::RECTANGLE, // "clipping-box",
69 } // unnamed namespace
73 LayerPtr layer( new Layer( Actor::LAYER ) );
75 // Second-phase construction
81 LayerPtr Layer::NewRoot( Stage& stage, LayerList& layerList, UpdateManager& manager, bool systemLevel )
83 LayerPtr root( new Layer( Actor::ROOT_LAYER ) );
85 // Second-phase construction
86 SceneGraph::Layer* layer = static_cast<SceneGraph::Layer*>( root->CreateNode() );
87 InstallRootMessage( manager, *layer, systemLevel ); // Transfer ownership to scene-graph
89 // Keep a raw pointer to the layer node.
92 // stage must be set for the root layer
93 root->mStage = &stage;
95 // root actor is immediately considered to be on-stage
96 root->mIsOnStage = true;
98 // The root actor will not emit a stage connection signal so set the signalled flag here as well
99 root->mOnStageSignalled = true;
101 // layer-list must be set for the root layer
102 root->mLayerList = &layerList;
103 layerList.RegisterLayer( *root );
108 Layer::Layer( Actor::DerivedType type )
111 mClippingBox(0,0,0,0),
112 mSortFunction(Dali::Layer::ZValue),
114 mDepthTestDisabled(false),
115 mTouchConsumed(false),
116 mHoverConsumed(false)
120 void Layer::OnInitialize()
122 if(Layer::mFirstInstance)
124 mDefaultLayerPropertyLookup = new DefaultPropertyLookup();
125 const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
126 for ( int i = 0; i < DEFAULT_LAYER_PROPERTY_COUNT; ++i )
128 (*mDefaultLayerPropertyLookup)[DEFAULT_LAYER_PROPERTY_NAMES[i]] = i + start;
130 Layer::mFirstInstance = false;
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::SetClipping(bool enabled)
223 if (enabled != mIsClipping)
225 mIsClipping = enabled;
227 // layerNode is being used in a separate thread; queue a message to set the value
228 SetClippingMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mIsClipping );
232 void Layer::SetClippingBox(int x, int y, int width, int height)
234 if( ( x != mClippingBox.x ) ||
235 ( y != mClippingBox.y ) ||
236 ( width != mClippingBox.width ) ||
237 ( height != mClippingBox.height ) )
239 // Clipping box is not animatable; this is the most up-to-date value
240 mClippingBox.Set(x, y, width, height);
242 // Convert mClippingBox to GL based coordinates (from bottom-left)
243 ClippingBox clippingBox( mClippingBox );
244 clippingBox.y = mStage->GetSize().height - clippingBox.y - clippingBox.height;
246 // layerNode is being used in a separate thread; queue a message to set the value
247 SetClippingBoxMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), clippingBox );
251 void Layer::SetDepthTestDisabled( bool disable )
253 if( disable != mDepthTestDisabled )
255 mDepthTestDisabled = disable;
257 // Send message .....
258 // layerNode is being used in a separate thread; queue a message to set the value
259 SetDepthTestDisabledMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mDepthTestDisabled );
263 bool Layer::IsDepthTestDisabled() const
265 return mDepthTestDisabled;
268 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
270 if( function != mSortFunction )
272 mSortFunction = function;
274 // layerNode is being used in a separate thread; queue a message to set the value
275 SetSortFunctionMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mSortFunction );
279 void Layer::SetTouchConsumed( bool consume )
281 mTouchConsumed = consume;
284 bool Layer::IsTouchConsumed() const
286 return mTouchConsumed;
289 void Layer::SetHoverConsumed( bool consume )
291 mHoverConsumed = consume;
294 bool Layer::IsHoverConsumed() const
296 return mHoverConsumed;
299 SceneGraph::Node* Layer::CreateNode() const
301 return SceneGraph::Layer::New();
304 void Layer::OnStageConnectionInternal()
308 DALI_ASSERT_DEBUG( NULL == mLayerList );
310 // Find the ordered layer-list
311 // This is different for Layers added via Integration::GetSystemOverlay()
312 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
314 if( parent->IsLayer() )
316 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
317 mLayerList = parentLayer->mLayerList;
322 DALI_ASSERT_DEBUG( NULL != mLayerList );
323 mLayerList->RegisterLayer( *this );
326 void Layer::OnStageDisconnectionInternal()
328 mLayerList->UnregisterLayer(*this);
330 // mLayerList is only valid when on-stage
334 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
336 DALI_ASSERT_DEBUG( mNode != NULL );
337 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
340 unsigned int Layer::GetDefaultPropertyCount() const
342 return Actor::GetDefaultPropertyCount() + DEFAULT_LAYER_PROPERTY_COUNT;
345 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
347 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
348 indices.reserve( indices.size() + DEFAULT_LAYER_PROPERTY_COUNT );
350 int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
351 for ( int i = 0; i < DEFAULT_LAYER_PROPERTY_COUNT; ++i, ++index )
353 indices.push_back( index );
357 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
359 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
361 return Actor::IsDefaultPropertyWritable(index);
369 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
371 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
373 return Actor::IsDefaultPropertyAnimatable(index);
381 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
383 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
385 return Actor::IsDefaultPropertyAConstraintInput(index);
387 return true; // our properties can be used as an input to a constraint
390 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
392 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
394 return Actor::GetDefaultPropertyType(index);
398 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
400 if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
402 return DEFAULT_LAYER_PROPERTY_TYPES[index];
406 // index out-of-bounds
407 return Property::NONE;
413 const std::string& Layer::GetDefaultPropertyName( Property::Index index ) const
415 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
417 return Actor::GetDefaultPropertyName(index);
421 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
423 if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
425 return DEFAULT_LAYER_PROPERTY_NAMES[index];
429 // index out-of-bounds
430 static const std::string INVALID_PROPERTY_NAME;
431 return INVALID_PROPERTY_NAME;
436 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
438 Property::Index index = Property::INVALID_INDEX;
440 DALI_ASSERT_DEBUG( NULL != mDefaultLayerPropertyLookup );
442 // Look for name in current class' default properties
443 DefaultPropertyLookup::const_iterator result = mDefaultLayerPropertyLookup->find( name );
444 if ( mDefaultLayerPropertyLookup->end() != result )
446 index = result->second;
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::CLIPPING_ENABLE:
469 SetClipping( propertyValue.Get<bool>() );
472 case Dali::Layer::CLIPPING_BOX:
474 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
475 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
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::CLIPPING_ENABLE:
504 case Dali::Layer::CLIPPING_BOX:
511 DALI_LOG_WARNING("Unknown property (%d)\n", index);
520 bool Layer::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
523 Layer* layer = dynamic_cast<Layer*>(object);
527 if(Dali::Layer::ACTION_RAISE == actionName)
532 else if(Dali::Layer::ACTION_LOWER == actionName)
537 else if(Dali::Layer::ACTION_RAISE_TO_TOP == actionName)
542 else if(Dali::Layer::ACTION_LOWER_TO_BOTTOM == actionName)
544 layer->LowerToBottom();
552 } // namespace Internal