Emscripten workarounds and llvm syntax fixes
[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 = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
33 const Property::Index Layer::CLIPPING_BOX    = 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 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
355 {
356   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
357   {
358     return Actor::GetDefaultPropertyType(index);
359   }
360   else
361   {
362     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
363
364     if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
365     {
366       return DEFAULT_LAYER_PROPERTY_TYPES[index];
367     }
368     else
369     {
370       // index out-of-bounds
371       return Property::NONE;
372     }
373   }
374 }
375
376
377 const std::string& Layer::GetDefaultPropertyName( Property::Index index ) const
378 {
379   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
380   {
381     return Actor::GetDefaultPropertyName(index);
382   }
383   else
384   {
385     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
386
387     if ( ( index >= 0 ) && ( index < DEFAULT_LAYER_PROPERTY_COUNT ) )
388     {
389       return DEFAULT_LAYER_PROPERTY_NAMES[index];
390     }
391     else
392     {
393       // index out-of-bounds
394       static const std::string INVALID_PROPERTY_NAME;
395       return INVALID_PROPERTY_NAME;
396     }
397   }
398 }
399
400 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
401 {
402   Property::Index index = Property::INVALID_INDEX;
403
404   DALI_ASSERT_DEBUG( NULL != mDefaultLayerPropertyLookup );
405
406   // Look for name in current class' default properties
407   DefaultPropertyLookup::const_iterator result = mDefaultLayerPropertyLookup->find( name );
408   if ( mDefaultLayerPropertyLookup->end() != result )
409   {
410     index = result->second;
411   }
412   else
413   {
414     // If not found, check in base class
415     index = Actor::GetDefaultPropertyIndex( name );
416   }
417
418   return index;
419 }
420
421 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
422 {
423   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
424   {
425     Actor::SetDefaultProperty(index, propertyValue);
426   }
427   else
428   {
429     switch(index)
430     {
431       case Dali::Layer::CLIPPING_ENABLE:
432       {
433         mIsClipping = propertyValue.Get<bool>();
434         break;
435       }
436       case Dali::Layer::CLIPPING_BOX:
437       {
438         mClippingBox = propertyValue.Get<Rect<int> >();
439         break;
440       }
441       default:
442       {
443         DALI_LOG_WARNING("Unknown property (%d)\n", index);
444         break;
445       }
446     } // switch(index)
447
448   } // else
449 }
450
451 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
452 {
453   Property::Value ret;
454   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
455   {
456     ret = Actor::GetDefaultProperty(index);
457   }
458   else
459   {
460     switch(index)
461     {
462       case Dali::Layer::CLIPPING_ENABLE:
463       {
464         ret = mIsClipping;
465         break;
466       }
467       case Dali::Layer::CLIPPING_BOX:
468       {
469         ret = mClippingBox;
470         break;
471       }
472       default:
473       {
474         DALI_LOG_WARNING("Unknown property (%d)\n", index);
475         break;
476       }
477     } // switch(index)
478   }
479
480   return ret;
481 }
482
483 bool Layer::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
484 {
485   bool done = false;
486   Layer* layer = dynamic_cast<Layer*>(object);
487
488   if(layer)
489   {
490     if(Dali::Layer::ACTION_RAISE == actionName)
491     {
492       layer->Raise();
493       done = true;
494     }
495     else if(Dali::Layer::ACTION_LOWER == actionName)
496     {
497       layer->Lower();
498       done = true;
499     }
500     else if(Dali::Layer::ACTION_RAISE_TO_TOP == actionName)
501     {
502       layer->RaiseToTop();
503       done = true;
504     }
505     else if(Dali::Layer::ACTION_LOWER_TO_BOTTOM == actionName)
506     {
507       layer->LowerToBottom();
508       done = true;
509     }
510   }
511
512   return done;
513 }
514
515 } // namespace Internal
516
517 } // namespace Dali
518