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)
119 void Layer::OnInitialize()
121 if(Layer::mFirstInstance)
123 mDefaultLayerPropertyLookup = new DefaultPropertyLookup();
124 const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
125 for ( int i = 0; i < DEFAULT_LAYER_PROPERTY_COUNT; ++i )
127 (*mDefaultLayerPropertyLookup)[DEFAULT_LAYER_PROPERTY_NAMES[i]] = i + start;
129 Layer::mFirstInstance = false;
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::SetClipping(bool enabled)
222 if (enabled != mIsClipping)
224 mIsClipping = enabled;
226 // layerNode is being used in a separate thread; queue a message to set the value
227 SetClippingMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mIsClipping );
231 void Layer::SetClippingBox(int x, int y, int width, int height)
233 if( ( x != mClippingBox.x ) ||
234 ( y != mClippingBox.y ) ||
235 ( width != mClippingBox.width ) ||
236 ( height != mClippingBox.height ) )
238 // Clipping box is not animatable; this is the most up-to-date value
239 mClippingBox.Set(x, y, width, height);
241 // Convert mClippingBox to GL based coordinates (from bottom-left)
242 ClippingBox clippingBox( mClippingBox );
243 clippingBox.y = mStage->GetSize().height - clippingBox.y - clippingBox.height;
245 // layerNode is being used in a separate thread; queue a message to set the value
246 SetClippingBoxMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), clippingBox );
250 void Layer::SetDepthTestDisabled( bool disable )
252 if( disable != mDepthTestDisabled )
254 mDepthTestDisabled = disable;
256 // Send message .....
257 // layerNode is being used in a separate thread; queue a message to set the value
258 SetDepthTestDisabledMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mDepthTestDisabled );
262 bool Layer::IsDepthTestDisabled() const
264 return mDepthTestDisabled;
267 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
269 if( function != mSortFunction )
271 mSortFunction = function;
273 // layerNode is being used in a separate thread; queue a message to set the value
274 SetSortFunctionMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mSortFunction );
278 void Layer::SetTouchConsumed( bool consume )
280 mTouchConsumed = consume;
283 bool Layer::IsTouchConsumed() const
285 return mTouchConsumed;
288 SceneGraph::Node* Layer::CreateNode() const
290 return SceneGraph::Layer::New();
293 void Layer::OnStageConnectionInternal()
297 DALI_ASSERT_DEBUG( NULL == mLayerList );
299 // Find the ordered layer-list
300 // This is different for Layers added via Integration::GetSystemOverlay()
301 for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
303 if( parent->IsLayer() )
305 Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
306 mLayerList = parentLayer->mLayerList;
311 DALI_ASSERT_DEBUG( NULL != mLayerList );
312 mLayerList->RegisterLayer( *this );
315 void Layer::OnStageDisconnectionInternal()
317 mLayerList->UnregisterLayer(*this);
319 // mLayerList is only valid when on-stage
323 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
325 DALI_ASSERT_DEBUG( mNode != NULL );
326 return dynamic_cast< const SceneGraph::Layer& >( *mNode );
329 unsigned int Layer::GetDefaultPropertyCount() const
331 return Actor::GetDefaultPropertyCount() + DEFAULT_LAYER_PROPERTY_COUNT;
334 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
336 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
337 indices.reserve( indices.size() + DEFAULT_LAYER_PROPERTY_COUNT );
339 int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
340 for ( int i = 0; i < DEFAULT_LAYER_PROPERTY_COUNT; ++i, ++index )
342 indices.push_back( index );
346 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
348 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
350 return Actor::IsDefaultPropertyWritable(index);
358 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
360 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
362 return Actor::IsDefaultPropertyAnimatable(index);
370 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
372 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
374 return Actor::IsDefaultPropertyAConstraintInput(index);
376 return true; // our properties can be used as an input to a constraint
379 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
381 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
383 return Actor::GetDefaultPropertyType(index);
387 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
389 if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
391 return DEFAULT_LAYER_PROPERTY_TYPES[index];
395 // index out-of-bounds
396 return Property::NONE;
402 const std::string& Layer::GetDefaultPropertyName( Property::Index index ) const
404 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
406 return Actor::GetDefaultPropertyName(index);
410 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
412 if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
414 return DEFAULT_LAYER_PROPERTY_NAMES[index];
418 // index out-of-bounds
419 static const std::string INVALID_PROPERTY_NAME;
420 return INVALID_PROPERTY_NAME;
425 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
427 Property::Index index = Property::INVALID_INDEX;
429 DALI_ASSERT_DEBUG( NULL != mDefaultLayerPropertyLookup );
431 // Look for name in current class' default properties
432 DefaultPropertyLookup::const_iterator result = mDefaultLayerPropertyLookup->find( name );
433 if ( mDefaultLayerPropertyLookup->end() != result )
435 index = result->second;
439 // If not found, check in base class
440 index = Actor::GetDefaultPropertyIndex( name );
446 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
448 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
450 Actor::SetDefaultProperty(index, propertyValue);
456 case Dali::Layer::CLIPPING_ENABLE:
458 SetClipping( propertyValue.Get<bool>() );
461 case Dali::Layer::CLIPPING_BOX:
463 Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
464 SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
469 DALI_LOG_WARNING("Unknown property (%d)\n", index);
477 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
480 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
482 ret = Actor::GetDefaultProperty(index);
488 case Dali::Layer::CLIPPING_ENABLE:
493 case Dali::Layer::CLIPPING_BOX:
500 DALI_LOG_WARNING("Unknown property (%d)\n", index);
509 bool Layer::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
512 Layer* layer = dynamic_cast<Layer*>(object);
516 if(Dali::Layer::ACTION_RAISE == actionName)
521 else if(Dali::Layer::ACTION_LOWER == actionName)
526 else if(Dali::Layer::ACTION_RAISE_TO_TOP == actionName)
531 else if(Dali::Layer::ACTION_LOWER_TO_BOTTOM == actionName)
533 layer->LowerToBottom();
541 } // namespace Internal