Revert "[NUI] Fix sibling issue (#20)"
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layer.cs
1 /*
2  * Copyright(c) 2017 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 using System;
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI
22 {
23
24     /// <summary>
25     /// Layers provide a mechanism for overlaying groups of actors on top of each other.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class Layer : Container
29     {
30         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
31
32         internal Layer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Layer_SWIGUpcast(cPtr), cMemoryOwn)
33         {
34             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
35         }
36
37         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Layer obj)
38         {
39             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
40         }
41
42         /// From the Container base class.
43
44         /// <summary>
45         /// Adds a child view to this layer.
46         /// </summary>
47         /// <seealso cref="Container.Add">
48         /// </seealso>
49         /// <since_tizen> 4 </since_tizen>
50         public override void Add(View child)
51         {
52             NDalicPINVOKE.Actor_Add(swigCPtr, View.getCPtr(child));
53             if (NDalicPINVOKE.SWIGPendingException.Pending)
54                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
55
56             Children.Add(child);
57         }
58
59         /// <summary>
60         /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
61         /// </summary>
62         /// <seealso cref="Container.Remove">
63         /// </seealso>
64         /// <since_tizen> 4 </since_tizen>
65         public override void Remove(View child)
66         {
67             NDalicPINVOKE.Actor_Remove(swigCPtr, View.getCPtr(child));
68             if (NDalicPINVOKE.SWIGPendingException.Pending)
69                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
70
71             Children.Remove(child);
72         }
73
74         /// <summary>
75         /// Retrieves a child view by the index.
76         /// </summary>
77         /// <pre>The view has been initialized.</pre>
78         /// <param name="index">The index of the child to retrieve.</param>
79         /// <returns>The view for the given index or empty handle if children not initialized.</returns>
80         /// <since_tizen> 4 </since_tizen>
81         public override View GetChildAt(uint index)
82         {
83             if (index < Children.Count)
84             {
85                 return Children[Convert.ToInt32(index)];
86             }
87             else
88             {
89                 return null;
90             }
91         }
92
93
94         /// <summary>
95         /// Get parent of the layer.
96         /// </summary>
97         /// <returns>The view's container</returns>
98         /// <since_tizen> 4 </since_tizen>
99         public override Container GetParent()
100         {
101             return null;
102         }
103
104         /// <summary>
105         /// Get the child count of the layer.
106         /// </summary>
107         /// <returns>The child count of the layer.</returns>
108         /// <since_tizen> 4 </since_tizen>
109         public override uint GetChildCount()
110         {
111             return Convert.ToUInt32(Children.Count);
112         }
113
114         /// <summary>
115         /// Dispose.
116         /// </summary>
117         /// <since_tizen> 3 </since_tizen>
118         protected override void Dispose(DisposeTypes type)
119         {
120             if(disposed)
121             {
122                 return;
123             }
124
125             if(type == DisposeTypes.Explicit)
126             {
127                 //Called by User
128                 //Release your own managed resources here.
129                 //You should release all of your own disposable objects here.
130             }
131
132             //Release your own unmanaged resources here.
133             //You should not access any managed member here except static instance.
134             //because the execution order of Finalizes is non-deterministic.
135
136             if (swigCPtr.Handle != global::System.IntPtr.Zero)
137             {
138                 if (swigCMemOwn)
139                 {
140                     swigCMemOwn = false;
141                     NDalicPINVOKE.delete_Layer(swigCPtr);
142                 }
143                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
144             }
145
146             base.Dispose(type);
147
148         }
149
150
151         internal class Property
152         {
153             internal static readonly int BEHAVIOR = NDalicPINVOKE.Layer_Property_BEHAVIOR_get();
154         }
155
156         /// <summary>
157         /// Creates a Layer object.
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         public Layer() : this(NDalicPINVOKE.Layer_New(), true)
161         {
162             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163             if(Window.Instance != null)
164             {
165                 this.SetAnchorPoint(Tizen.NUI.PivotPoint.TopLeft);
166                 this.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.AllDimensions);
167             }
168         }
169         internal void SetAnchorPoint(Vector3 anchorPoint)
170         {
171             NDalicPINVOKE.Actor_SetAnchorPoint(swigCPtr, Vector3.getCPtr(anchorPoint));
172             if (NDalicPINVOKE.SWIGPendingException.Pending)
173                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174         }
175         internal void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
176         {
177             NDalicPINVOKE.Actor_SetResizePolicy(swigCPtr, (int)policy, (int)dimension);
178             if (NDalicPINVOKE.SWIGPendingException.Pending)
179                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
180         }
181
182         /// <summary>
183         /// Downcasts a handle to layer handle.
184         /// </summary>
185         /// <since_tizen> 3 </since_tizen>
186         /// Please do not use! this will be deprecated!
187         /// Instead please use as keyword.
188         [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
189         [EditorBrowsable(EditorBrowsableState.Never)]
190         public static Layer DownCast(BaseHandle handle)
191         {
192             Layer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
193             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
194             return ret;
195         }
196
197         /// <summary>
198         /// Search through this layer's hierarchy for a view with the given unique ID.
199         /// </summary>
200         /// <pre>This layer (the parent) has been initialized.</pre>
201         /// <remarks>The actor itself is also considered in the search.</remarks>
202         /// <param name="id">The id of the child to find</param>
203         /// <returns> A handle to the view if found, or an empty handle if not. </returns>
204         /// <since_tizen> 3 </since_tizen>
205         public View FindChildById(uint id)
206         {
207             IntPtr cPtr = NDalicPINVOKE.Actor_FindChildById(swigCPtr, id);
208             View ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as View;
209
210             if (NDalicPINVOKE.SWIGPendingException.Pending)
211                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212             return ret;
213         }
214
215         /// <summary>
216         /// Queries the depth of the layer.<br />
217         /// 0 is the bottommost layer, higher number is on the top.<br />
218         /// </summary>
219         /// <since_tizen> 3 </since_tizen>
220         public uint Depth
221         {
222             get
223             {
224                 return GetDepth();
225             }
226         }
227
228         internal uint GetDepth()
229         {
230             var parentChildren = Window.Instance.LayersChildren;
231             if(parentChildren != null)
232             {
233                 int idx = parentChildren.IndexOf(this);
234                 if (idx >= 0)
235                 {
236                     return Convert.ToUInt32(idx); ;
237                 }
238             }
239             return 0u;
240         }
241
242         /// <summary>
243         /// Increments the depth of the layer.
244         /// </summary>
245         /// <since_tizen> 3 </since_tizen>
246         public void Raise()
247         {
248             var parentChildren = Window.Instance.LayersChildren;
249             if (parentChildren != null)
250             {
251                 int currentIdx = parentChildren.IndexOf(this);
252
253                 if (currentIdx != parentChildren.Count - 1)
254                 {
255                     RaiseAbove(parentChildren[currentIdx + 1]);
256
257                     Layer temp = parentChildren[currentIdx + 1];
258                     parentChildren[currentIdx + 1] = this;
259                     parentChildren[currentIdx] = temp;
260                 }
261             }
262         }
263
264         /// <summary>
265         /// Decrements the depth of the layer.
266         /// </summary>
267         /// <since_tizen> 3 </since_tizen>
268         public void Lower()
269         {
270             var parentChildren = Window.Instance.LayersChildren;
271             if (parentChildren != null)
272             {
273                 int currentIdx = parentChildren.IndexOf(this);
274
275                 if (currentIdx > 0)
276                 {
277                     LowerBelow(parentChildren[currentIdx - 1]);
278
279                     Layer temp = parentChildren[currentIdx - 1];
280                     parentChildren[currentIdx - 1] = this;
281                     parentChildren[currentIdx] = temp;
282
283                 }
284             }
285         }
286
287         internal void RaiseAbove(Layer target)
288         {
289             NDalicPINVOKE.Layer_RaiseAbove(swigCPtr, Layer.getCPtr(target));
290             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
291         }
292
293         internal void LowerBelow(Layer target)
294         {
295             NDalicPINVOKE.Layer_LowerBelow(swigCPtr, Layer.getCPtr(target));
296             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
297         }
298
299         /// <summary>
300         /// Raises the layer to the top.
301         /// </summary>
302         /// <since_tizen> 3 </since_tizen>
303         public void RaiseToTop()
304         {
305             var parentChildren = Window.Instance.LayersChildren;
306
307             if (parentChildren != null)
308             {
309                 parentChildren.Remove(this);
310                 parentChildren.Add(this);
311             }
312             NDalicPINVOKE.Layer_RaiseToTop(swigCPtr);
313             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
314         }
315
316         /// <summary>
317         /// Lowers the layer to the bottom.
318         /// </summary>
319         /// <since_tizen> 3 </since_tizen>
320         public void LowerToBottom()
321         {
322             var parentChildren = Window.Instance.LayersChildren;
323
324             if (parentChildren != null)
325             {
326                 parentChildren.Remove(this);
327                 parentChildren.Insert(0, this);
328             }
329
330             NDalicPINVOKE.Layer_LowerToBottom(swigCPtr);
331             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
332         }
333
334         /// <summary>
335         /// Moves the layer directly above the given layer.<br />
336         /// After the call, this layer's depth will be immediately above target.<br />
337         /// </summary>
338         /// <param name="target">The layer to get on top of.</param>
339         /// <since_tizen> 3 </since_tizen>
340         public void MoveAbove(Layer target)
341         {
342             NDalicPINVOKE.Layer_MoveAbove(swigCPtr, Layer.getCPtr(target));
343             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
344         }
345
346         /// <summary>
347         /// Moves the layer directly below the given layer.<br />
348         /// After the call, this layer's depth will be immediately below target.<br />
349         /// </summary>
350         /// <param name="target">The layer to get below of.</param>
351         /// <since_tizen> 3 </since_tizen>
352         public void MoveBelow(Layer target)
353         {
354             NDalicPINVOKE.Layer_MoveBelow(swigCPtr, Layer.getCPtr(target));
355             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
356         }
357
358         private void SetBehavior(LayerBehavior behavior)
359         {
360             NDalicPINVOKE.Layer_SetBehavior(swigCPtr, (int)behavior);
361             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
362         }
363
364         private LayerBehavior GetBehavior()
365         {
366             Layer.LayerBehavior ret = (Layer.LayerBehavior)NDalicPINVOKE.Layer_GetBehavior(swigCPtr);
367             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
368             return ret;
369         }
370
371         internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
372         {
373             NDalicPINVOKE.Layer_SetSortFunction(swigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
374             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375         }
376
377         internal void SetTouchConsumed(bool consume)
378         {
379             NDalicPINVOKE.Layer_SetTouchConsumed(swigCPtr, consume);
380             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
381         }
382
383         internal bool IsTouchConsumed()
384         {
385             bool ret = NDalicPINVOKE.Layer_IsTouchConsumed(swigCPtr);
386             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387             return ret;
388         }
389
390         internal void SetHoverConsumed(bool consume)
391         {
392             NDalicPINVOKE.Layer_SetHoverConsumed(swigCPtr, consume);
393             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394         }
395
396         internal bool IsHoverConsumed()
397         {
398             bool ret = NDalicPINVOKE.Layer_IsHoverConsumed(swigCPtr);
399             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400             return ret;
401         }
402
403         /// <summary>
404         /// Enumeration for the behavior of the layer.
405         /// </summary>
406         /// <since_tizen> 3 </since_tizen>
407         public enum LayerBehavior
408         {
409             /// <summary>
410             /// UI control rendering mode.
411             /// </summary>
412             /// <since_tizen> 3 </since_tizen>
413             Layer2D,
414             /// <summary>
415             /// UI control rendering mode (default mode).
416             /// This mode is designed for UI controls that can overlap. In this
417             /// mode renderer order will be respective to the tree hierarchy of
418             /// Actors.<br />
419             /// The rendering order is depth first, so for the following actor tree,
420             /// A will be drawn first, then B, D, E, then C, F.  This ensures that
421             /// overlapping actors are drawn as expected (whereas, with breadth first
422             /// traversal, the actors would interleave).<br />
423             /// </summary>
424             /// <since_tizen> 3 </since_tizen>
425             LayerUI = Layer2D,
426             /// <summary>
427             /// Layer will use depth test.
428             /// This mode is designed for a 3 dimensional scene where actors in front
429             /// of other actors will obscure them, i.e. the actors are sorted by the
430             /// distance from the camera.<br />
431             /// When using this mode, a depth test will be used. A depth clear will
432             /// happen for each layer, which means actors in a layer "above" other
433             /// layers will be rendered in front of actors in those layers regardless
434             /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
435             /// Opaque renderers are drawn first and write to the depth buffer.  Then
436             /// transparent renderers are drawn with depth test enabled but depth
437             /// write switched off.  Transparent renderers are drawn based on their
438             /// distance from the camera.  A renderer's DEPTH_INDEX property is used to
439             /// offset the distance to the camera when ordering transparent renderers.
440             /// This is useful if you want to define the draw order of two or more
441             /// transparent renderers that are equal distance from the camera.  Unlike
442             /// LAYER_UI, parent-child relationship does not affect rendering order at
443             /// all.
444             /// </summary>
445             /// <since_tizen> 3 </since_tizen>
446             Layer3D
447         }
448
449         internal enum TreeDepthMultiplier
450         {
451             TREE_DEPTH_MULTIPLIER = 10000
452         }
453
454         /// <summary>
455         /// Layer behavior, type String (Layer.LayerBehavior).
456         /// </summary>
457         /// <since_tizen> 3 </since_tizen>
458         public Layer.LayerBehavior Behavior
459         {
460             get
461             {
462                 return GetBehavior();
463             }
464             set
465             {
466                 SetBehavior(value);
467             }
468         }
469
470         /// <summary>
471         /// Internal only property to enable or disable clipping, type boolean.
472         /// By default, this is false, i.e., the viewport of the layer is the entire window.
473         /// </summary>
474         internal bool ClippingEnabled
475         {
476             get
477             {
478                 bool ret = NDalicPINVOKE.Layer_IsClipping(swigCPtr);
479                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
480                 return ret;
481             }
482             set
483             {
484                 NDalicPINVOKE.Layer_SetClipping(swigCPtr, value);
485                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
486             }
487         }
488
489         /// <summary>
490         /// Sets the viewport (in window coordinates), type rectangle.
491         /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
492         /// </summary>
493         /// <since_tizen> 4 </since_tizen>
494         public Rectangle Viewport
495         {
496             get
497             {
498                 if( ClippingEnabled )
499                 {
500                   Rectangle ret = new Rectangle(NDalicPINVOKE.Layer_GetClippingBox(swigCPtr), true);
501                   if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
502                   return ret;
503                 }
504                 else
505                 {
506                   // Clipping not enabled so return the window size
507                   Size2D windowSize = Window.Instance.Size;
508                   Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
509                   return ret;
510                 }
511             }
512             set
513             {
514                 NDalicPINVOKE.Layer_SetClippingBox__SWIG_1(swigCPtr, Rectangle.getCPtr(value));
515                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
516                 ClippingEnabled = true;
517             }
518         }
519
520         /// <summary>
521         /// Retrieves and sets the layer's opacity.<br />
522         /// </summary>
523         /// <since_tizen> 3 </since_tizen>
524         public float Opacity
525         {
526             get
527             {
528                 float temp = 0.0f;
529                 GetProperty(View.Property.OPACITY).Get(out temp);
530                 return temp;
531             }
532             set
533             {
534                 SetProperty(View.Property.OPACITY, new Tizen.NUI.PropertyValue(value));
535             }
536         }
537
538         /// <summary>
539         /// Retrieves and sets the layer's visibility.
540         /// </summary>
541         /// <since_tizen> 3 </since_tizen>
542         public bool Visibility
543         {
544             get
545             {
546                 bool temp = false;
547                 GetProperty(View.Property.VISIBLE).Get(out temp);
548                 return temp;
549             }
550             set
551             {
552                 SetProperty(View.Property.VISIBLE, new Tizen.NUI.PropertyValue(value));
553             }
554         }
555
556         /// <summary>
557         /// Get the number of children held by the layer.
558         /// </summary>
559         /// <since_tizen> 3 </since_tizen>
560         public new uint ChildCount
561         {
562             get
563             {
564                 return Convert.ToUInt32(Children.Count);
565             }
566         }
567
568         /// <summary>
569         /// Gets or sets the layer's name.
570         /// </summary>
571         /// <since_tizen> 3 </since_tizen>
572         public string Name
573         {
574             get
575             {
576                 return GetName();
577             }
578             set
579             {
580                 SetName(value);
581             }
582         }
583
584         internal string GetName()
585         {
586             string ret = NDalicPINVOKE.Actor_GetName(swigCPtr);
587             if (NDalicPINVOKE.SWIGPendingException.Pending)
588                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
589             return ret;
590         }
591
592         internal void SetName(string name)
593         {
594             NDalicPINVOKE.Actor_SetName(swigCPtr, name);
595             if (NDalicPINVOKE.SWIGPendingException.Pending)
596                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
597         }
598
599     }
600 }