Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / layer-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 // CLASS HEADER
18 #include <dali/internal/event/actors/layer-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/common/dali-common.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/property-index-ranges.h>
24 #include <dali/internal/event/common/stage-impl.h>
25 #include <dali/internal/event/actors/layer-list.h>
26
27 using Dali::Internal::SceneGraph::UpdateManager;
28
29 namespace Dali
30 {
31
32 const Property::Index Layer::CLIPPING_ENABLE = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
33 const Property::Index Layer::CLIPPING_BOX    = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 1;
34
35 namespace Internal
36 {
37 bool Layer::mFirstInstance = true;
38 Actor::DefaultPropertyLookup* Layer::mDefaultLayerPropertyLookup = NULL;
39
40 namespace
41 {
42
43 BaseHandle Create()
44 {
45   return Dali::Layer::New();
46 }
47
48 TypeRegistration mType( typeid(Dali::Layer), typeid(Dali::Actor), Create );
49
50 TypeAction a1(mType, Dali::Layer::ACTION_RAISE, &Layer::DoAction);
51 TypeAction a2(mType, Dali::Layer::ACTION_LOWER, &Layer::DoAction);
52 TypeAction a3(mType, Dali::Layer::ACTION_RAISE_TO_TOP, &Layer::DoAction);
53 TypeAction a4(mType, Dali::Layer::ACTION_LOWER_TO_BOTTOM, &Layer::DoAction);
54
55 const std::string DEFAULT_LAYER_PROPERTY_NAMES[] =
56 {
57   "clipping-enable",
58   "clipping-box"
59 };
60 const int DEFAULT_LAYER_PROPERTY_COUNT = sizeof( DEFAULT_LAYER_PROPERTY_NAMES ) / sizeof( std::string );
61
62 const Property::Type DEFAULT_LAYER_PROPERTY_TYPES[DEFAULT_LAYER_PROPERTY_COUNT] =
63 {
64   Property::BOOLEAN,    // "clipping-enable",
65   Property::RECTANGLE,  // "clipping-box",
66 };
67
68 } // unnamed namespace
69
70 LayerPtr Layer::New()
71 {
72   LayerPtr layer( new Layer( Actor::LAYER ) );
73
74   // Second-phase construction
75   layer->Initialize();
76
77   return layer;
78 }
79
80 LayerPtr Layer::NewRoot( Stage& stage, LayerList& layerList, UpdateManager& manager, bool systemLevel )
81 {
82   LayerPtr root( new Layer( Actor::ROOT_LAYER ) );
83
84   // Second-phase construction
85   SceneGraph::Layer* layer = static_cast<SceneGraph::Layer*>( root->CreateNode() );
86   InstallRootMessage( manager, *layer, systemLevel ); // Transfer ownership to scene-graph
87
88   // Keep a raw pointer to the layer node.
89   root->mNode = layer;
90
91   // stage must be set for the root layer
92   root->mStage = &stage;
93
94   // root actor is immediately considered to be on-stage
95   root->mIsOnStage = true;
96
97   // The root actor will not emit a stage connection signal so set the signalled flag here as well
98   root->mOnStageSignalled = true;
99
100   // layer-list must be set for the root layer
101   root->mLayerList = &layerList;
102   layerList.RegisterLayer( *root );
103
104   return root;
105 }
106
107 Layer::Layer( Actor::DerivedType type )
108 : Actor( type ),
109   mLayerList(NULL),
110   mClippingBox(0,0,0,0),
111   mSortFunction(Dali::Layer::ZValue),
112   mIsClipping(false),
113   mDepthTestDisabled(false)
114 {
115 }
116
117 void Layer::OnInitialize()
118 {
119   if(Layer::mFirstInstance)
120   {
121     mDefaultLayerPropertyLookup = new DefaultPropertyLookup();
122     const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
123     for ( int i = 0; i < DEFAULT_LAYER_PROPERTY_COUNT; ++i )
124     {
125       (*mDefaultLayerPropertyLookup)[DEFAULT_LAYER_PROPERTY_NAMES[i]] = i + start;
126     }
127     Layer::mFirstInstance = false;
128   }
129 }
130
131 Layer::~Layer()
132 {
133 }
134
135 unsigned int Layer::GetDepth() const
136 {
137   return mLayerList ? mLayerList->GetDepth( this ) : 0u;
138 }
139
140 void Layer::Raise()
141 {
142   if ( mLayerList )
143   {
144     mLayerList->RaiseLayer(*this);
145   }
146 }
147
148 void Layer::Lower()
149 {
150   if ( mLayerList )
151   {
152     mLayerList->LowerLayer(*this);
153   }
154 }
155
156 void Layer::RaiseAbove( const Internal::Layer& target )
157 {
158   // cannot raise above ourself, both have to be on stage
159   if( ( this != &target ) && OnStage() && target.OnStage() )
160   {
161     // get parameters depth
162     const unsigned int targetDepth = target.GetDepth();
163     if( GetDepth() < targetDepth )
164     {
165       MoveAbove( target );
166     }
167   }
168 }
169
170 void Layer::LowerBelow( const Internal::Layer& target )
171 {
172   // cannot lower below ourself, both have to be on stage
173   if( ( this != &target ) && OnStage() && target.OnStage() )
174   {
175     // get parameters depth
176     const unsigned int targetDepth = target.GetDepth();
177     if( GetDepth() > targetDepth )
178     {
179       MoveBelow( target );
180     }
181   }
182 }
183
184 void Layer::RaiseToTop()
185 {
186   if ( mLayerList )
187   {
188     mLayerList->RaiseLayerToTop(*this);
189   }
190 }
191
192 void Layer::LowerToBottom()
193 {
194   if ( mLayerList )
195   {
196     mLayerList->LowerLayerToBottom(*this);
197   }
198 }
199
200 void Layer::MoveAbove( const Internal::Layer& target )
201 {
202   // cannot raise above ourself, both have to be on stage
203   if( ( this != &target ) && mLayerList && target.OnStage() )
204   {
205     mLayerList->MoveLayerAbove(*this, target );
206   }
207 }
208
209 void Layer::MoveBelow( const Internal::Layer& target )
210 {
211   // cannot lower below ourself, both have to be on stage
212   if( ( this != &target ) && mLayerList && target.OnStage() )
213   {
214     mLayerList->MoveLayerBelow(*this, target );
215   }
216 }
217
218 void Layer::SetClipping(bool enabled)
219 {
220   if (enabled != mIsClipping)
221   {
222     mIsClipping = enabled;
223
224     // layerNode is being used in a separate thread; queue a message to set the value
225     SetClippingMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mIsClipping );
226   }
227 }
228
229 void Layer::SetClippingBox(int x, int y, int width, int height)
230 {
231   if( ( x != mClippingBox.x ) ||
232       ( y != mClippingBox.y ) ||
233       ( width != mClippingBox.width ) ||
234       ( height != mClippingBox.height ) )
235   {
236     // Clipping box is not animatable; this is the most up-to-date value
237     mClippingBox.Set(x, y, width, height);
238
239     // layerNode is being used in a separate thread; queue a message to set the value
240     SetClippingBoxMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mClippingBox );
241   }
242 }
243
244 void Layer::SetDepthTestDisabled( bool disable )
245 {
246   if( disable != mDepthTestDisabled )
247   {
248     mDepthTestDisabled = disable;
249
250     // Send message .....
251     // layerNode is being used in a separate thread; queue a message to set the value
252     SetDepthTestDisabledMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mDepthTestDisabled );
253   }
254 }
255
256 bool Layer::IsDepthTestDisabled() const
257 {
258   return mDepthTestDisabled;
259 }
260
261 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
262 {
263   if( function != mSortFunction )
264   {
265     mSortFunction = function;
266
267     // layerNode is being used in a separate thread; queue a message to set the value
268     SetSortFunctionMessage( mStage->GetUpdateInterface(), GetSceneLayerOnStage(), mSortFunction );
269   }
270 }
271
272 SceneGraph::Node* Layer::CreateNode() const
273 {
274   return SceneGraph::Layer::New();
275 }
276
277 void Layer::OnStageConnectionInternal()
278 {
279   if ( !mIsRoot )
280   {
281     DALI_ASSERT_DEBUG( NULL == mLayerList );
282
283     // Find the ordered layer-list
284     // This is different for Layers added via Integration::GetSystemOverlay()
285     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
286     {
287       if( parent->IsLayer() )
288       {
289         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
290         mLayerList = parentLayer->mLayerList;
291       }
292     }
293   }
294
295   DALI_ASSERT_DEBUG( NULL != mLayerList );
296   mLayerList->RegisterLayer( *this );
297 }
298
299 void Layer::OnStageDisconnectionInternal()
300 {
301   mLayerList->UnregisterLayer(*this);
302
303   // mLayerList is only valid when on-stage
304   mLayerList = NULL;
305 }
306
307 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
308 {
309   DALI_ASSERT_DEBUG( mNode != NULL );
310   return dynamic_cast< const SceneGraph::Layer& >( *mNode );
311 }
312
313 unsigned int Layer::GetDefaultPropertyCount() const
314 {
315   return Actor::GetDefaultPropertyCount() + DEFAULT_LAYER_PROPERTY_COUNT;
316 }
317
318 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
319 {
320   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
321   indices.reserve( indices.size() + DEFAULT_LAYER_PROPERTY_COUNT );
322
323   int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
324   for ( int i = 0; i < DEFAULT_LAYER_PROPERTY_COUNT; ++i, ++index )
325   {
326     indices.push_back( index );
327   }
328 }
329
330 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
331 {
332   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
333   {
334     return Actor::IsDefaultPropertyWritable(index);
335   }
336   else
337   {
338     return true;
339   }
340 }
341
342 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
343 {
344   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
345   {
346     return Actor::IsDefaultPropertyAnimatable(index);
347   }
348   else
349   {
350     return false;
351   }
352 }
353
354 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
355 {
356   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
357   {
358     return Actor::IsDefaultPropertyAConstraintInput(index);
359   }
360   return true; // our properties can be used as an input to a constraint
361 }
362
363 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
364 {
365   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
366   {
367     return Actor::GetDefaultPropertyType(index);
368   }
369   else
370   {
371     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
372
373     if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
374     {
375       return DEFAULT_LAYER_PROPERTY_TYPES[index];
376     }
377     else
378     {
379       // index out-of-bounds
380       return Property::NONE;
381     }
382   }
383 }
384
385
386 const std::string& Layer::GetDefaultPropertyName( Property::Index index ) const
387 {
388   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
389   {
390     return Actor::GetDefaultPropertyName(index);
391   }
392   else
393   {
394     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
395
396     if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
397     {
398       return DEFAULT_LAYER_PROPERTY_NAMES[index];
399     }
400     else
401     {
402       // index out-of-bounds
403       static const std::string INVALID_PROPERTY_NAME;
404       return INVALID_PROPERTY_NAME;
405     }
406   }
407 }
408
409 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
410 {
411   Property::Index index = Property::INVALID_INDEX;
412
413   DALI_ASSERT_DEBUG( NULL != mDefaultLayerPropertyLookup );
414
415   // Look for name in current class' default properties
416   DefaultPropertyLookup::const_iterator result = mDefaultLayerPropertyLookup->find( name );
417   if ( mDefaultLayerPropertyLookup->end() != result )
418   {
419     index = result->second;
420   }
421   else
422   {
423     // If not found, check in base class
424     index = Actor::GetDefaultPropertyIndex( name );
425   }
426
427   return index;
428 }
429
430 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
431 {
432   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
433   {
434     Actor::SetDefaultProperty(index, propertyValue);
435   }
436   else
437   {
438     switch(index)
439     {
440       case Dali::Layer::CLIPPING_ENABLE:
441       {
442         mIsClipping = propertyValue.Get<bool>();
443         break;
444       }
445       case Dali::Layer::CLIPPING_BOX:
446       {
447         mClippingBox = propertyValue.Get<Rect<int> >();
448         break;
449       }
450       default:
451       {
452         DALI_LOG_WARNING("Unknown property (%d)\n", index);
453         break;
454       }
455     } // switch(index)
456
457   } // else
458 }
459
460 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
461 {
462   Property::Value ret;
463   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
464   {
465     ret = Actor::GetDefaultProperty(index);
466   }
467   else
468   {
469     switch(index)
470     {
471       case Dali::Layer::CLIPPING_ENABLE:
472       {
473         ret = mIsClipping;
474         break;
475       }
476       case Dali::Layer::CLIPPING_BOX:
477       {
478         ret = mClippingBox;
479         break;
480       }
481       default:
482       {
483         DALI_LOG_WARNING("Unknown property (%d)\n", index);
484         break;
485       }
486     } // switch(index)
487   }
488
489   return ret;
490 }
491
492 bool Layer::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
493 {
494   bool done = false;
495   Layer* layer = dynamic_cast<Layer*>(object);
496
497   if(layer)
498   {
499     if(Dali::Layer::ACTION_RAISE == actionName)
500     {
501       layer->Raise();
502       done = true;
503     }
504     else if(Dali::Layer::ACTION_LOWER == actionName)
505     {
506       layer->Lower();
507       done = true;
508     }
509     else if(Dali::Layer::ACTION_RAISE_TO_TOP == actionName)
510     {
511       layer->RaiseToTop();
512       done = true;
513     }
514     else if(Dali::Layer::ACTION_LOWER_TO_BOTTOM == actionName)
515     {
516       layer->LowerToBottom();
517       done = true;
518     }
519   }
520
521   return done;
522 }
523
524 } // namespace Internal
525
526 } // namespace Dali
527