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