Merge "Add behaviour for 2D / 3D layers." into devel/new_mesh
[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     clippingBox.y = Stage::GetCurrent()->GetSize().height - clippingBox.y - clippingBox.height;
252
253     // layerNode is being used in a separate thread; queue a message to set the value
254     SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
255   }
256 }
257
258 void Layer::SetDepthTestDisabled( bool disable )
259 {
260   if( disable != mDepthTestDisabled )
261   {
262     mDepthTestDisabled = disable;
263
264     // Send message .....
265     // layerNode is being used in a separate thread; queue a message to set the value
266     SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
267   }
268 }
269
270 bool Layer::IsDepthTestDisabled() const
271 {
272   return mDepthTestDisabled || (mBehavior == Dali::Layer::LAYER_2D);
273 }
274
275 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
276 {
277   if( function != mSortFunction )
278   {
279     mSortFunction = function;
280
281     // layerNode is being used in a separate thread; queue a message to set the value
282     SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
283   }
284 }
285
286 void Layer::SetTouchConsumed( bool consume )
287 {
288   mTouchConsumed = consume;
289 }
290
291 bool Layer::IsTouchConsumed() const
292 {
293   return mTouchConsumed;
294 }
295
296 void Layer::SetHoverConsumed( bool consume )
297 {
298   mHoverConsumed = consume;
299 }
300
301 bool Layer::IsHoverConsumed() const
302 {
303   return mHoverConsumed;
304 }
305
306 SceneGraph::Node* Layer::CreateNode() const
307 {
308   return SceneGraph::Layer::New();
309 }
310
311 void Layer::OnStageConnectionInternal()
312 {
313   if ( !mIsRoot )
314   {
315     DALI_ASSERT_DEBUG( NULL == mLayerList );
316
317     // Find the ordered layer-list
318     // This is different for Layers added via Integration::GetSystemOverlay()
319     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
320     {
321       if( parent->IsLayer() )
322       {
323         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
324         mLayerList = parentLayer->mLayerList;
325       }
326     }
327   }
328
329   DALI_ASSERT_DEBUG( NULL != mLayerList );
330   mLayerList->RegisterLayer( *this );
331 }
332
333 void Layer::OnStageDisconnectionInternal()
334 {
335   mLayerList->UnregisterLayer(*this);
336
337   // mLayerList is only valid when on-stage
338   mLayerList = NULL;
339 }
340
341 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
342 {
343   DALI_ASSERT_DEBUG( mNode != NULL );
344   return dynamic_cast< const SceneGraph::Layer& >( *mNode );
345 }
346
347 unsigned int Layer::GetDefaultPropertyCount() const
348 {
349   return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
350 }
351
352 void Layer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
353 {
354   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
355   indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
356
357   int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
358   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
359   {
360     indices.PushBack( index );
361   }
362 }
363
364 bool Layer::IsDefaultPropertyWritable( Property::Index index ) const
365 {
366   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
367   {
368     return Actor::IsDefaultPropertyWritable( index );
369   }
370
371   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].writable;
372 }
373
374 bool Layer::IsDefaultPropertyAnimatable( Property::Index index ) const
375 {
376   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
377   {
378     return Actor::IsDefaultPropertyAnimatable( index );
379   }
380
381   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].animatable;
382 }
383
384 bool Layer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
385 {
386   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
387   {
388     return Actor::IsDefaultPropertyAConstraintInput( index );
389   }
390
391   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX ].constraintInput;
392 }
393
394 Property::Type Layer::GetDefaultPropertyType( Property::Index index ) const
395 {
396   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
397   {
398     return Actor::GetDefaultPropertyType( index );
399   }
400
401   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
402
403   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
404   {
405     return DEFAULT_PROPERTY_DETAILS[index].type;
406   }
407
408   // index out-of-bounds
409   return Property::NONE;
410 }
411
412 const char* Layer::GetDefaultPropertyName( Property::Index index ) const
413 {
414   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
415   {
416     return Actor::GetDefaultPropertyName( index );
417   }
418
419   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
420   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
421   {
422     return DEFAULT_PROPERTY_DETAILS[index].name;
423   }
424
425   return NULL;
426 }
427
428 Property::Index Layer::GetDefaultPropertyIndex(const std::string& name) const
429 {
430   Property::Index index = Property::INVALID_INDEX;
431
432   // Look for name in current class' default properties
433   for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
434   {
435     const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[i];
436     if( 0 == name.compare( property->name ) ) // dont want to convert rhs to string
437     {
438       index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
439       break;
440     }
441   }
442   if( Property::INVALID_INDEX == index )
443   {
444     // If not found, check in base class
445     index = Actor::GetDefaultPropertyIndex( name );
446   }
447
448   return index;
449 }
450
451 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
452 {
453   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
454   {
455     Actor::SetDefaultProperty( index, propertyValue );
456   }
457   else
458   {
459     switch( index )
460     {
461       case Dali::Layer::Property::CLIPPING_ENABLE:
462       {
463         SetClipping( propertyValue.Get<bool>() );
464         break;
465       }
466       case Dali::Layer::Property::CLIPPING_BOX:
467       {
468         Rect<int> clippingBox( propertyValue.Get<Rect<int> >() );
469         SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
470         break;
471       }
472       case Dali::Layer::Property::BEHAVIOR:
473       {
474         Behavior behavior = Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BehaviorTable, BehaviorTableCount );
475         SetBehavior( behavior );
476         break;
477       }
478       default:
479       {
480         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
481         break;
482       }
483     } // switch(index)
484
485   } // else
486 }
487
488 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
489 {
490   Property::Value ret;
491   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
492   {
493     ret = Actor::GetDefaultProperty( index );
494   }
495   else
496   {
497     switch( index )
498     {
499       case Dali::Layer::Property::CLIPPING_ENABLE:
500       {
501         ret = mIsClipping;
502         break;
503       }
504       case Dali::Layer::Property::CLIPPING_BOX:
505       {
506         ret = mClippingBox;
507         break;
508       }
509       case Dali::Layer::Property::BEHAVIOR:
510       {
511         ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BehaviorTable, BehaviorTableCount );
512         break;
513       }
514       default:
515       {
516         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
517         break;
518       }
519     } // switch(index)
520   }
521
522   return ret;
523 }
524
525 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
526 {
527   bool done = false;
528   Layer* layer = dynamic_cast<Layer*>( object );
529
530   if( layer )
531   {
532     if( 0 == actionName.compare( ACTION_RAISE ) )
533     {
534       layer->Raise();
535       done = true;
536     }
537     else if( 0 == actionName.compare( ACTION_LOWER ) )
538     {
539       layer->Lower();
540       done = true;
541     }
542     else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
543     {
544       layer->RaiseToTop();
545       done = true;
546     }
547     else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
548     {
549       layer->LowerToBottom();
550       done = true;
551     }
552   }
553
554   return done;
555 }
556
557 } // namespace Internal
558
559 } // namespace Dali