Remove deprecated APIs in Tizen 3.0
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / layer-impl.cpp
1 /*
2  * Copyright (c) 2018 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
39 typedef Layer::Behavior Behavior;
40
41 DALI_ENUM_TO_STRING_TABLE_BEGIN( BEHAVIOR )
42 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_UI )
43 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_3D )
44 DALI_ENUM_TO_STRING_TABLE_END( BEHAVIOR )
45
46 } // namespace
47
48 namespace Internal
49 {
50
51 namespace
52 {
53
54 // Properties
55
56 //              Name                Type      writable animatable constraint-input  enum for index-checking
57 DALI_PROPERTY_TABLE_BEGIN
58 DALI_PROPERTY( "clippingEnable",    BOOLEAN,    true,    false,   true,   Dali::Layer::Property::CLIPPING_ENABLE )
59 DALI_PROPERTY( "clippingBox",       RECTANGLE,  true,    false,   true,   Dali::Layer::Property::CLIPPING_BOX    )
60 DALI_PROPERTY( "behavior",          STRING,     true,    false,   false,  Dali::Layer::Property::BEHAVIOR        )
61 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, LayerDefaultProperties )
62
63 // Actions
64
65 const char* const ACTION_RAISE =           "raise";
66 const char* const ACTION_LOWER =           "lower";
67 const char* const ACTION_RAISE_TO_TOP =    "raiseToTop";
68 const char* const ACTION_LOWER_TO_BOTTOM = "lowerToBottom";
69
70 BaseHandle Create()
71 {
72   return Dali::Layer::New();
73 }
74
75 TypeRegistration mType( typeid( Dali::Layer ), typeid( Dali::Actor ), Create, LayerDefaultProperties );
76
77 TypeAction a1( mType, ACTION_RAISE, &Layer::DoAction );
78 TypeAction a2( mType, ACTION_LOWER, &Layer::DoAction );
79 TypeAction a3( mType, ACTION_RAISE_TO_TOP, &Layer::DoAction );
80 TypeAction a4( mType, ACTION_LOWER_TO_BOTTOM, &Layer::DoAction );
81
82 } // unnamed namespace
83
84
85 LayerPtr Layer::New()
86 {
87   // create node, nodes are owned by UpdateManager
88   SceneGraph::Layer* layerNode = SceneGraph::Layer::New();
89   OwnerPointer< SceneGraph::Node > transferOwnership( layerNode );
90   AddNodeMessage( Stage::GetCurrent()->GetUpdateManager(), transferOwnership );
91   LayerPtr layer( new Layer( Actor::LAYER, *layerNode ) );
92
93   // Second-phase construction
94   layer->Initialize();
95
96   return layer;
97 }
98
99 LayerPtr Layer::NewRoot( LayerList& layerList, UpdateManager& manager )
100 {
101   // create node, nodes are owned by UpdateManager
102   SceneGraph::Layer* rootLayer = SceneGraph::Layer::New();
103   OwnerPointer< SceneGraph::Layer > transferOwnership( rootLayer );
104   InstallRootMessage( manager, transferOwnership );
105
106   LayerPtr root( new Layer( Actor::ROOT_LAYER, *rootLayer ) );
107
108   // root actor is immediately considered to be on-stage
109   root->mIsOnStage = true;
110
111   // The root actor will not emit a stage connection signal so set the signalled flag here as well
112   root->mOnStageSignalled = true;
113
114   // layer-list must be set for the root layer
115   root->mLayerList = &layerList;
116   layerList.SetRootLayer( &(*root) );
117   layerList.RegisterLayer( *root );
118
119   return root;
120 }
121
122 Layer::Layer( Actor::DerivedType type, const SceneGraph::Layer& layer )
123 : Actor( type, layer ),
124   mLayerList( NULL ),
125   mClippingBox( 0, 0, 0, 0 ),
126   mSortFunction( Layer::ZValue ),
127   mBehavior( Dali::Layer::LAYER_UI ),
128   mIsClipping( false ),
129   mDepthTestDisabled( true ),
130   mTouchConsumed( false ),
131   mHoverConsumed( false )
132 {
133 }
134
135 void Layer::OnInitialize()
136 {
137 }
138
139 Layer::~Layer()
140 {
141 }
142
143 unsigned int Layer::GetDepth() const
144 {
145   return mLayerList ? mLayerList->GetDepth( this ) : 0u;
146 }
147
148 void Layer::Raise()
149 {
150   if ( mLayerList )
151   {
152     mLayerList->RaiseLayer(*this);
153   }
154 }
155
156 void Layer::Lower()
157 {
158   if ( mLayerList )
159   {
160     mLayerList->LowerLayer(*this);
161   }
162 }
163
164 void Layer::RaiseAbove( const Internal::Layer& target )
165 {
166   // cannot raise above ourself, both have to be on stage
167   if( ( this != &target ) && OnStage() && target.OnStage() )
168   {
169     // get parameters depth
170     const uint32_t targetDepth = target.GetDepth();
171     if( GetDepth() < targetDepth )
172     {
173       MoveAbove( target );
174     }
175   }
176 }
177
178 void Layer::LowerBelow( const Internal::Layer& target )
179 {
180   // cannot lower below ourself, both have to be on stage
181   if( ( this != &target ) && OnStage() && target.OnStage() )
182   {
183     // get parameters depth
184     const uint32_t targetDepth = target.GetDepth();
185     if( GetDepth() > targetDepth )
186     {
187       MoveBelow( target );
188     }
189   }
190 }
191
192 void Layer::RaiseToTop()
193 {
194   if ( mLayerList )
195   {
196     mLayerList->RaiseLayerToTop(*this);
197   }
198 }
199
200 void Layer::LowerToBottom()
201 {
202   if ( mLayerList )
203   {
204     mLayerList->LowerLayerToBottom(*this);
205   }
206 }
207
208 void Layer::MoveAbove( const Internal::Layer& target )
209 {
210   // cannot raise above ourself, both have to be on stage
211   if( ( this != &target ) && mLayerList && target.OnStage() )
212   {
213     mLayerList->MoveLayerAbove(*this, target );
214   }
215 }
216
217 void Layer::MoveBelow( const Internal::Layer& target )
218 {
219   // cannot lower below ourself, both have to be on stage
220   if( ( this != &target ) && mLayerList && target.OnStage() )
221   {
222     mLayerList->MoveLayerBelow(*this, target );
223   }
224 }
225
226 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
227 {
228   mBehavior = behavior;
229
230   // Notify update side object.
231   SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
232   // By default, disable depth test for LAYER_UI, and enable for LAYER_3D.
233   SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_UI );
234 }
235
236 void Layer::SetClipping(bool enabled)
237 {
238   if (enabled != mIsClipping)
239   {
240     mIsClipping = enabled;
241
242     // layerNode is being used in a separate thread; queue a message to set the value
243     SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
244   }
245 }
246
247 void Layer::SetClippingBox(int x, int y, int width, int height)
248 {
249   if( ( x != mClippingBox.x ) ||
250       ( y != mClippingBox.y ) ||
251       ( width != mClippingBox.width ) ||
252       ( height != mClippingBox.height ) )
253   {
254     // Clipping box is not animatable; this is the most up-to-date value
255     mClippingBox.Set(x, y, width, height);
256
257     // Convert mClippingBox to GL based coordinates (from bottom-left)
258     ClippingBox clippingBox( mClippingBox );
259
260     StagePtr stage = Stage::GetCurrent();
261     if( stage )
262     {
263       clippingBox.y = static_cast<int32_t>( stage->GetSize().height ) - clippingBox.y - clippingBox.height;
264
265       // layerNode is being used in a separate thread; queue a message to set the value
266       SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
267     }
268   }
269 }
270
271 void Layer::SetDepthTestDisabled( bool disable )
272 {
273   if( disable != mDepthTestDisabled )
274   {
275     mDepthTestDisabled = disable;
276
277     // Send message.
278     // layerNode is being used in a separate thread; queue a message to set the value
279     SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
280   }
281 }
282
283 bool Layer::IsDepthTestDisabled() const
284 {
285   return mDepthTestDisabled;
286 }
287
288 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
289 {
290   if( function != mSortFunction )
291   {
292     mSortFunction = function;
293
294     // layerNode is being used in a separate thread; queue a message to set the value
295     SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
296   }
297 }
298
299 void Layer::SetTouchConsumed( bool consume )
300 {
301   mTouchConsumed = consume;
302 }
303
304 bool Layer::IsTouchConsumed() const
305 {
306   return mTouchConsumed;
307 }
308
309 void Layer::SetHoverConsumed( bool consume )
310 {
311   mHoverConsumed = consume;
312 }
313
314 bool Layer::IsHoverConsumed() const
315 {
316   return mHoverConsumed;
317 }
318
319 void Layer::OnStageConnectionInternal()
320 {
321   if ( !mIsRoot )
322   {
323     DALI_ASSERT_DEBUG( NULL == mLayerList );
324
325     // Find the ordered layer-list
326     // This is different for Layers added via Integration::GetSystemOverlay()
327     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
328     {
329       if( parent->IsLayer() )
330       {
331         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
332         mLayerList = parentLayer->mLayerList;
333       }
334     }
335   }
336
337   DALI_ASSERT_DEBUG( NULL != mLayerList );
338   mLayerList->RegisterLayer( *this );
339 }
340
341 void Layer::OnStageDisconnectionInternal()
342 {
343   mLayerList->UnregisterLayer(*this);
344
345   // mLayerList is only valid when on-stage
346   mLayerList = NULL;
347 }
348
349 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
350 {
351   return static_cast< const SceneGraph::Layer& >( GetNode() ); // we know our node is a layer node
352 }
353
354 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
355 {
356   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
357   {
358     Actor::SetDefaultProperty( index, propertyValue );
359   }
360   else
361   {
362     switch( index )
363     {
364       case Dali::Layer::Property::CLIPPING_ENABLE:
365       {
366         SetClipping( propertyValue.Get<bool>() );
367         break;
368       }
369       case Dali::Layer::Property::CLIPPING_BOX:
370       {
371         Rect<int32_t> clippingBox( propertyValue.Get<Rect<int32_t> >() );
372         SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
373         break;
374       }
375       case Dali::Layer::Property::BEHAVIOR:
376       {
377         Behavior behavior(Dali::Layer::LAYER_UI);
378         if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
379         {
380           SetBehavior( behavior );
381         }
382         break;
383       }
384       default:
385       {
386         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
387         break;
388       }
389     } // switch(index)
390
391   } // else
392 }
393
394 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
395 {
396   Property::Value ret;
397   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
398   {
399     ret = Actor::GetDefaultProperty( index );
400   }
401   else
402   {
403     switch( index )
404     {
405       case Dali::Layer::Property::CLIPPING_ENABLE:
406       {
407         ret = mIsClipping;
408         break;
409       }
410       case Dali::Layer::Property::CLIPPING_BOX:
411       {
412         ret = mClippingBox;
413         break;
414       }
415       case Dali::Layer::Property::BEHAVIOR:
416       {
417         ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
418         break;
419       }
420       default:
421       {
422         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
423         break;
424       }
425     } // switch(index)
426   }
427
428   return ret;
429 }
430
431 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
432 {
433   Property::Value ret;
434   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
435   {
436     ret = Actor::GetDefaultPropertyCurrentValue( index );
437   }
438   else
439   {
440     ret = GetDefaultProperty( index ); // Layer only has event-side properties
441   }
442
443   return ret;
444 }
445
446 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
447 {
448   bool done = false;
449   Layer* layer = dynamic_cast<Layer*>( object );
450
451   if( layer )
452   {
453     if( 0 == actionName.compare( ACTION_RAISE ) )
454     {
455       layer->Raise();
456       done = true;
457     }
458     else if( 0 == actionName.compare( ACTION_LOWER ) )
459     {
460       layer->Lower();
461       done = true;
462     }
463     else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
464     {
465       layer->RaiseToTop();
466       done = true;
467     }
468     else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
469     {
470       layer->LowerToBottom();
471       done = true;
472     }
473   }
474
475   return done;
476 }
477
478 } // namespace Internal
479
480 } // namespace Dali