[NUI] Change all CallingConvention to `Cdecl`
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / Layer.cs
1 /*
2  * Copyright(c) 2022 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 using System.Runtime.InteropServices;
21
22 namespace Tizen.NUI
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 Window window;
31         private int layoutCount = 0;
32         private EventHandler<VisibilityChangedEventArgs> visibilityChangedEventHandler;
33         private VisibilityChangedEventCallbackType visibilityChangedEventCallback;
34         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
35         private delegate void VisibilityChangedEventCallbackType(IntPtr data, bool visibility, VisibilityChangeType type);
36
37         /// <summary>
38         /// Creates a Layer object.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         public Layer() : this(Interop.Layer.New(), true)
42         {
43             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
44             this.SetAnchorPoint(Tizen.NUI.PivotPoint.TopLeft);
45             this.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.AllDimensions);
46         }
47
48         internal Layer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
49         {
50         }
51
52         /// <summary>
53         /// Dispose Explicit or Implicit
54         /// </summary>
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         protected override void Dispose(DisposeTypes type)
57         {
58             if (Disposed)
59             {
60                 return;
61             }
62
63             if (visibilityChangedEventCallback != null)
64             {
65                 NUILog.Debug($"[Dispose] visibilityChangedEventCallback");
66
67                 Interop.ActorSignal.VisibilityChangedDisconnect(GetBaseHandleCPtrHandleRef, visibilityChangedEventCallback.ToHandleRef(this));
68                 NDalicPINVOKE.ThrowExceptionIfExistsDebug();
69                 visibilityChangedEventCallback = null;
70             }
71
72             LayoutCount = 0;
73
74             base.Dispose(type);
75         }
76
77         /// <summary>
78         /// Enumeration for the behavior of the layer.
79         /// </summary>
80         /// <since_tizen> 3 </since_tizen>
81         public enum LayerBehavior
82         {
83             /// <summary>
84             /// UI control rendering mode (default mode).
85             /// This mode is designed for UI controls that can overlap. In this
86             /// mode renderer order will be respective to the tree hierarchy of
87             /// Actors.<br />
88             /// The rendering order is depth first, so for the following actor tree,
89             /// A will be drawn first, then B, D, E, then C, F.  This ensures that
90             /// overlapping actors are drawn as expected (whereas, with breadth first
91             /// traversal, the actors would interleave).<br />
92             /// </summary>
93             /// <since_tizen> 3 </since_tizen>
94             LayerUI,
95
96             /// <summary>
97             /// Layer will use depth test.
98             /// This mode is designed for a 3 dimensional scene where actors in front
99             /// of other actors will obscure them, i.e. the actors are sorted by the
100             /// distance from the camera.<br />
101             /// When using this mode, a depth test will be used. A depth clear will
102             /// happen for each layer, which means actors in a layer "above" other
103             /// layers will be rendered in front of actors in those layers regardless
104             /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
105             /// Opaque renderers are drawn first and write to the depth buffer.  Then
106             /// transparent renderers are drawn with depth test enabled but depth
107             /// write switched off.  Transparent renderers are drawn based on their
108             /// distance from the camera.  A renderer's DEPTH_INDEX property is used to
109             /// offset the distance to the camera when ordering transparent renderers.
110             /// This is useful if you want to define the draw order of two or more
111             /// transparent renderers that are equal distance from the camera.  Unlike
112             /// LAYER_UI, parent-child relationship does not affect rendering order at
113             /// all.
114             /// </summary>
115             /// <since_tizen> 3 </since_tizen>
116             Layer3D
117         }
118
119         internal enum TreeDepthMultiplier
120         {
121             TREE_DEPTH_MULTIPLIER = 10000
122         }
123
124         /// <summary>
125         /// Layer behavior, type String (Layer.LayerBehavior).
126         /// </summary>
127         /// <since_tizen> 3 </since_tizen>
128         public Layer.LayerBehavior Behavior
129         {
130             get
131             {
132                 return GetBehavior();
133             }
134             set
135             {
136                 SetBehavior(value);
137             }
138         }
139
140         /// <summary>
141         /// Sets the viewport (in window coordinates), type rectangle.
142         /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
143         /// </summary>
144         /// <since_tizen> 4 </since_tizen>
145         public Rectangle Viewport
146         {
147             get
148             {
149                 if (ClippingEnabled)
150                 {
151                     Rectangle ret = new Rectangle(Interop.Layer.GetClippingBox(SwigCPtr), true);
152                     if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
153                     return ret;
154                 }
155                 else
156                 {
157                     // Clipping not enabled so return the window size
158                     Size2D windowSize = window?.Size;
159                     Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
160                     return ret;
161                 }
162             }
163             set
164             {
165                 Interop.Layer.SetClippingBox(SwigCPtr, Rectangle.getCPtr(value));
166                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167                 ClippingEnabled = true;
168             }
169         }
170
171         /// <summary>
172         /// Retrieves and sets the layer's opacity.<br />
173         /// </summary>
174         /// <since_tizen> 3 </since_tizen>
175         public float Opacity
176         {
177             get
178             {
179                 float temp = 0.0f;
180                 var pValue = GetProperty(View.Property.OPACITY);
181                 pValue.Get(out temp);
182                 pValue.Dispose();
183                 return temp;
184             }
185             set
186             {
187                 var temp = new Tizen.NUI.PropertyValue(value);
188                 SetProperty(View.Property.OPACITY, temp);
189                 temp.Dispose();
190             }
191         }
192
193         /// <summary>
194         /// Retrieves and sets the layer's visibility.
195         /// </summary>
196         /// <since_tizen> 3 </since_tizen>
197         public bool Visibility
198         {
199             get
200             {
201                 bool temp = false;
202                 var pValue = GetProperty(View.Property.VISIBLE);
203                 pValue.Get(out temp);
204                 pValue.Dispose();
205                 return temp;
206             }
207             set
208             {
209                 var temp = new Tizen.NUI.PropertyValue(value);
210                 SetProperty(View.Property.VISIBLE, temp);
211                 temp.Dispose();
212             }
213         }
214
215         /// <summary>
216         /// Get the number of children held by the layer.
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         public new uint ChildCount
220         {
221             get
222             {
223                 return Convert.ToUInt32(Children.Count);
224             }
225         }
226
227         /// <summary>
228         /// Gets or sets the layer's name.
229         /// </summary>
230         /// <since_tizen> 3 </since_tizen>
231         public string Name
232         {
233             get
234             {
235                 return GetName();
236             }
237             set
238             {
239                 SetName(value);
240             }
241         }
242
243         /// <summary>
244         /// Queries the depth of the layer.<br />
245         /// 0 is the bottommost layer, higher number is on the top.<br />
246         /// </summary>
247         /// <since_tizen> 3 </since_tizen>
248         public uint Depth
249         {
250             get
251             {
252                 return GetDepth();
253             }
254         }
255
256         /// <summary>
257         /// Internal only property to enable or disable clipping, type boolean.
258         /// By default, this is false, i.e., the viewport of the layer is the entire window.
259         /// </summary>
260         internal bool ClippingEnabled
261         {
262             get
263             {
264                 bool ret = Interop.Layer.IsClipping(SwigCPtr);
265                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
266                 return ret;
267             }
268             set
269             {
270                 Interop.Layer.SetClipping(SwigCPtr, value);
271                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
272             }
273         }
274
275         /// <summary>
276         /// Gets the Layer's ID
277         /// Readonly
278         /// </summary>
279         /// <remarks>Hidden-API</remarks>
280         [EditorBrowsable(EditorBrowsableState.Never)]
281         public uint ID
282         {
283             get
284             {
285                 return GetId();
286             }
287         }
288
289
290         /// From the Container base class.
291
292         /// <summary>
293         /// Adds a child view to this layer.
294         /// </summary>
295         /// <seealso cref="Container.Add">
296         /// </seealso>
297         /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
298         /// <since_tizen> 4 </since_tizen>
299         public override void Add(View child)
300         {
301             if (null == child)
302             {
303                 throw new ArgumentNullException(nameof(child));
304             }
305
306             Container oldParent = child.GetParent();
307
308             if (oldParent != this)
309             {
310                 if (oldParent != null)
311                 {
312                     oldParent.Remove(child);
313                 }
314                 else
315                 {
316                     child.InternalParent = this;
317                 }
318
319                 LayoutCount += child.LayoutCount;
320
321                 Interop.Actor.Add(SwigCPtr, View.getCPtr(child));
322                 if (NDalicPINVOKE.SWIGPendingException.Pending)
323                     throw NDalicPINVOKE.SWIGPendingException.Retrieve();
324                 Children.Add(child);
325                 OnChildAdded(child);
326             }
327         }
328
329         /// <summary>
330         /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
331         /// </summary>
332         /// <seealso cref="Container.Remove">
333         /// </seealso>
334         /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
335         /// <since_tizen> 4 </since_tizen>
336         public override void Remove(View child)
337         {
338             if (null == child)
339             {
340                 throw new ArgumentNullException(nameof(child));
341             }
342             if (child.GetParent() == null) // Early out if child parent is null.
343                 return;
344
345             if (child.GetParent() != this)
346             {
347                 //throw new System.InvalidOperationException("You have deleted a view that is not a child of this layer.");
348                 Tizen.Log.Error("NUI", "You have deleted a view that is not a child of this layer.");
349                 return;
350             }
351             // If the view had focus, it clears focus.
352             if (child == FocusManager.Instance.GetCurrentFocusView())
353             {
354                 FocusManager.Instance.ClearFocus();
355             }
356
357             Interop.Actor.Remove(SwigCPtr, View.getCPtr(child));
358             if (NDalicPINVOKE.SWIGPendingException.Pending)
359                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
360
361             Children.Remove(child);
362             child.InternalParent = null;
363             OnChildRemoved(child);
364             LayoutCount -= child.LayoutCount;
365         }
366
367         /// <summary>
368         /// Retrieves a child view by the index.
369         /// </summary>
370         /// <pre>The view has been initialized.</pre>
371         /// <param name="index">The index of the child to retrieve.</param>
372         /// <returns>The view for the given index or empty handle if children not initialized.</returns>
373         /// <since_tizen> 4 </since_tizen>
374         public override View GetChildAt(uint index)
375         {
376             if (index < Children.Count)
377             {
378                 return Children[Convert.ToInt32(index)];
379             }
380             else
381             {
382                 return null;
383             }
384         }
385
386         /// <summary>
387         /// Get parent of the layer.
388         /// </summary>
389         /// <returns>The view's container</returns>
390         /// <since_tizen> 4 </since_tizen>
391         public override Container GetParent()
392         {
393             return null;
394         }
395
396         /// <summary>
397         /// Get the child count of the layer.
398         /// </summary>
399         /// <returns>The child count of the layer.</returns>
400         /// <since_tizen> 4 </since_tizen>
401         [Obsolete("This has been deprecated in API9 and will be removed in API11. Use ChildCount property instead")]
402         public override uint GetChildCount()
403         {
404             return Convert.ToUInt32(Children.Count);
405         }
406
407         /// <summary>
408         /// Downcasts a handle to layer handle.
409         /// </summary>
410         /// <exception cref="ArgumentNullException"> Thrown when handle is null. </exception>
411         /// <since_tizen> 3 </since_tizen>
412         /// Do not use this, that will be deprecated. Use as keyword instead.
413         [Obsolete("Do not use this, that will be deprecated. Use as keyword instead.")]
414         [EditorBrowsable(EditorBrowsableState.Never)]
415         public static Layer DownCast(BaseHandle handle)
416         {
417             if (null == handle)
418             {
419                 throw new ArgumentNullException(nameof(handle));
420             }
421             Layer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
422             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
423             return ret;
424         }
425
426         /// <summary>
427         /// Search through this layer's hierarchy for a view with the given unique ID.
428         /// </summary>
429         /// <pre>This layer (the parent) has been initialized.</pre>
430         /// <remarks>The actor itself is also considered in the search.</remarks>
431         /// <param name="id">The id of the child to find</param>
432         /// <returns> A handle to the view if found, or an empty handle if not. </returns>
433         /// <since_tizen> 3 </since_tizen>
434         public View FindChildById(uint id)
435         {
436             //to fix memory leak issue, match the handle count with native side.
437             IntPtr cPtr = Interop.Actor.FindChildById(SwigCPtr, id);
438             View ret = this.GetInstanceSafely<View>(cPtr);
439             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
440             return ret;
441         }
442
443         internal override View FindCurrentChildById(uint id)
444         {
445             return FindChildById(id);
446         }
447
448         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
449         [EditorBrowsable(EditorBrowsableState.Never)]
450         public View FindChildByName(string viewName)
451         {
452             //to fix memory leak issue, match the handle count with native side.
453             IntPtr cPtr = Interop.Actor.FindChildByName(SwigCPtr, viewName);
454             View ret = this.GetInstanceSafely<View>(cPtr);
455             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
456             return ret;
457         }
458
459         /// <summary>
460         /// Increments the depth of the layer.
461         /// </summary>
462         /// <since_tizen> 3 </since_tizen>
463         public void Raise()
464         {
465             var parentChildren = window?.LayersChildren;
466             if (parentChildren != null)
467             {
468                 int currentIdx = parentChildren.IndexOf(this);
469
470                 if (currentIdx >= 0 && currentIdx < parentChildren.Count - 1)
471                 {
472                     var upper = parentChildren[currentIdx + 1];
473                     RaiseAbove(upper);
474                 }
475             }
476         }
477
478         /// <summary>
479         /// Decrements the depth of the layer.
480         /// </summary>
481         /// <since_tizen> 3 </since_tizen>
482         public void Lower()
483         {
484             var parentChildren = window?.LayersChildren;
485             if (parentChildren != null)
486             {
487                 int currentIdx = parentChildren.IndexOf(this);
488                 int idx = window.IsBorderEnabled ? 1 : 0;
489
490                 if (currentIdx > idx && currentIdx < parentChildren.Count)
491                 {
492                     var low = parentChildren[currentIdx - 1];
493                     LowerBelow(low);
494                 }
495             }
496         }
497
498         /// <summary>
499         /// Raises the layer to the top.
500         /// </summary>
501         /// <since_tizen> 3 </since_tizen>
502         public void RaiseToTop()
503         {
504             var parentChildren = window?.LayersChildren;
505
506             if (parentChildren != null)
507             {
508                 parentChildren.Remove(this);
509                 parentChildren.Add(this);
510
511                 Interop.Layer.RaiseToTop(SwigCPtr);
512                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
513             }
514         }
515
516         /// <summary>
517         /// Lowers the layer to the bottom.
518         /// </summary>
519         /// <since_tizen> 3 </since_tizen>
520         public void LowerToBottom()
521         {
522             var parentChildren = window?.LayersChildren;
523
524             if (parentChildren != null)
525             {
526                 parentChildren.Remove(this);
527                 parentChildren.Insert(0, this);
528
529                 Interop.Layer.LowerToBottom(SwigCPtr);
530
531                 if (window.IsBorderEnabled)
532                 {
533                     Layer bottomLayer = window.GetBorderWindowBottomLayer();
534                     parentChildren.Remove(bottomLayer);
535                     parentChildren.Insert(0, bottomLayer);
536                     Interop.Layer.LowerToBottom(Layer.getCPtr(bottomLayer));
537                 }
538                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
539             }
540         }
541
542         /// <summary>
543         /// Moves the layer directly above the given layer.<br />
544         /// After the call, this layer's depth will be immediately above target.<br />
545         /// </summary>
546         /// <param name="target">The layer to get on top of.</param>
547         /// <since_tizen> 3 </since_tizen>
548         public void MoveAbove(Layer target)
549         {
550             RaiseAbove(target);
551         }
552
553         /// <summary>
554         /// Moves the layer directly below the given layer.<br />
555         /// After the call, this layer's depth will be immediately below target.<br />
556         /// </summary>
557         /// <param name="target">The layer to get below of.</param>
558         /// <since_tizen> 3 </since_tizen>
559         public void MoveBelow(Layer target)
560         {
561             LowerBelow(target);
562         }
563
564         /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
565         [EditorBrowsable(EditorBrowsableState.Never)]
566         public void SetAnchorPoint(Vector3 anchorPoint)
567         {
568             Interop.Actor.SetAnchorPoint(SwigCPtr, Vector3.getCPtr(anchorPoint));
569             if (NDalicPINVOKE.SWIGPendingException.Pending)
570                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
571         }
572
573         /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
574         [EditorBrowsable(EditorBrowsableState.Never)]
575         public void SetSize(float width, float height)
576         {
577             Interop.ActorInternal.SetSize(SwigCPtr, width, height);
578             if (NDalicPINVOKE.SWIGPendingException.Pending)
579                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
580         }
581
582         /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
583         [EditorBrowsable(EditorBrowsableState.Never)]
584         public void SetParentOrigin(Vector3 parentOrigin)
585         {
586             Interop.ActorInternal.SetParentOrigin(SwigCPtr, Vector3.getCPtr(parentOrigin));
587             if (NDalicPINVOKE.SWIGPendingException.Pending)
588                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
589         }
590
591         /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
592         [EditorBrowsable(EditorBrowsableState.Never)]
593         public void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
594         {
595             Interop.Actor.SetResizePolicy(SwigCPtr, (int)policy, (int)dimension);
596             if (NDalicPINVOKE.SWIGPendingException.Pending)
597                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
598         }
599
600         /// <summary>
601         /// Inhouse API.
602         /// This allows the user to specify whether this layer should consume touch (including gestures).
603         /// If set, any layers behind this layer will not be hit-test.
604         /// </summary>
605         /// <param name="consume">Whether the layer should consume touch (including gestures).</param>
606         [EditorBrowsable(EditorBrowsableState.Never)]
607         public void SetTouchConsumed(bool consume)
608         {
609             Interop.Layer.SetTouchConsumed(SwigCPtr, consume);
610             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
611         }
612
613         /// <summary>
614         /// Inhouse API.
615         /// This allows the user to specify whether this layer should consume hover.
616         /// If set, any layers behind this layer will not be hit-test.
617         /// </summary>
618         /// <param name="consume">Whether the layer should consume hover</param>
619         [EditorBrowsable(EditorBrowsableState.Never)]
620         public void SetHoverConsumed(bool consume)
621         {
622             Interop.Layer.SetHoverConsumed(SwigCPtr, consume);
623             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
624         }
625
626         /// <summary>
627         /// An event for visibility change which can be used to subscribe or unsubscribe the event handler.<br />
628         /// This signal is emitted when the visible property of this or a parent view is changed.<br />
629         /// </summary>
630         [EditorBrowsable(EditorBrowsableState.Never)]
631         public event EventHandler<VisibilityChangedEventArgs> VisibilityChanged
632         {
633             add
634             {
635                 if (visibilityChangedEventHandler == null)
636                 {
637                     visibilityChangedEventCallback = OnVisibilityChanged;
638                     Interop.ActorSignal.VisibilityChangedConnect(SwigCPtr, visibilityChangedEventCallback.ToHandleRef(this));
639                     NDalicPINVOKE.ThrowExceptionIfExists();
640                 }
641                 visibilityChangedEventHandler += value;
642             }
643
644             remove
645             {
646                 visibilityChangedEventHandler -= value;
647                 if (visibilityChangedEventHandler == null && visibilityChangedEventCallback != null)
648                 {
649                     Interop.ActorSignal.VisibilityChangedDisconnect(SwigCPtr, visibilityChangedEventCallback.ToHandleRef(this));
650                     NDalicPINVOKE.ThrowExceptionIfExists();
651                     visibilityChangedEventCallback = null;
652                 }
653             }
654         }
655
656         /// <summary>
657         /// Event arguments of visibility changed.
658         /// </summary>
659         [EditorBrowsable(EditorBrowsableState.Never)]
660         public class VisibilityChangedEventArgs : EventArgs
661         {
662             private Layer layer;
663             private bool visibility;
664
665             /// <summary>
666             /// The layer whose visibility has changed.
667             /// </summary>
668             [EditorBrowsable(EditorBrowsableState.Never)]
669             public Layer Layer
670             {
671                 get
672                 {
673                     return layer;
674                 }
675                 set
676                 {
677                     layer = value;
678                 }
679             }
680
681             /// <summary>
682             /// Whether the layer is now visible or not.
683             /// </summary>
684             [EditorBrowsable(EditorBrowsableState.Never)]
685             public bool Visibility
686             {
687                 get
688                 {
689                     return visibility;
690                 }
691                 set
692                 {
693                     visibility = value;
694                 }
695             }
696         }
697
698         internal uint GetDepth()
699         {
700             var parentChildren = window?.LayersChildren;
701             if (parentChildren != null)
702             {
703                 int idx = parentChildren.IndexOf(this);
704                 if (idx >= 0)
705                 {
706                     return Convert.ToUInt32(idx); ;
707                 }
708             }
709             return 0u;
710         }
711         internal void RaiseAbove(Layer target)
712         {
713             var parentChildren = window?.LayersChildren;
714             if (parentChildren != null)
715             {
716                 int currentIndex = parentChildren.IndexOf(this);
717                 int targetIndex = parentChildren.IndexOf(target);
718
719                 if (currentIndex < 0 || targetIndex < 0 ||
720                     currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
721                 {
722                     NUILog.Error("index should be bigger than 0 and less than children of layer count");
723                     return;
724                 }
725
726                 // If the currentIndex is less than the target index and the target has the same parent.
727                 if (currentIndex < targetIndex)
728                 {
729                     parentChildren.Remove(this);
730                     parentChildren.Insert(targetIndex, this);
731
732                     Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
733                     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
734                 }
735             }
736         }
737
738         internal void LowerBelow(Layer target)
739         {
740             var parentChildren = window?.LayersChildren;
741
742             if (parentChildren != null)
743             {
744                 int currentIndex = parentChildren.IndexOf(this);
745                 int targetIndex = parentChildren.IndexOf(target);
746
747                 if (currentIndex < 0 || targetIndex < 0 ||
748                     currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
749                 {
750                     NUILog.Error("index should be bigger than 0 and less than children of layer count");
751                     return;
752                 }
753
754                 // If the currentIndex is not already the 0th index and the target has the same parent.
755                 if ((currentIndex != 0) && (targetIndex != -1) &&
756                     (currentIndex > targetIndex))
757                 {
758                     parentChildren.Remove(this);
759                     parentChildren.Insert(targetIndex, this);
760
761                     Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
762                     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
763                 }
764             }
765         }
766
767         internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
768         {
769             Interop.Layer.SetSortFunction(SwigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
770             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
771         }
772
773         internal bool IsTouchConsumed()
774         {
775             bool ret = Interop.Layer.IsTouchConsumed(SwigCPtr);
776             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
777             return ret;
778         }
779         internal bool IsHoverConsumed()
780         {
781             bool ret = Interop.Layer.IsHoverConsumed(SwigCPtr);
782             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
783             return ret;
784         }
785
786         internal void AddViewToLayerList(View view)
787         {
788             Children.Add(view);
789         }
790
791         internal void RemoveViewFromLayerList(View view)
792         {
793             Children.Remove(view);
794         }
795
796         internal string GetName()
797         {
798             string ret = Interop.Actor.GetName(SwigCPtr);
799             if (NDalicPINVOKE.SWIGPendingException.Pending)
800                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
801             return ret;
802         }
803
804         internal void SetName(string name)
805         {
806             Interop.Actor.SetName(SwigCPtr, name);
807             if (NDalicPINVOKE.SWIGPendingException.Pending)
808                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
809         }
810
811         internal void SetWindow(Window win)
812         {
813             if (window == win) return;
814
815             if (window != null)
816             {
817                 window.LayoutController.LayoutCount -= LayoutCount;
818             }
819
820             window = win;
821
822             if (window != null)
823             {
824                 window.LayoutController.LayoutCount += LayoutCount;
825             }
826         }
827
828         internal uint GetId()
829         {
830             uint ret = Interop.Actor.GetId(SwigCPtr);
831             if (NDalicPINVOKE.SWIGPendingException.Pending)
832                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
833             return ret;
834         }
835
836         /// <summary>
837         /// The number of layouts of children views.
838         /// This can be used to set/unset Process callback to calculate Layout.
839         /// </summary>
840         internal int LayoutCount
841         {
842             get
843             {
844                 return layoutCount;
845             }
846
847             set
848             {
849                 if (layoutCount == value) return;
850
851                 if (value < 0) throw new global::System.ArgumentOutOfRangeException(nameof(LayoutCount), "LayoutCount(" + LayoutCount + ") should not be less than zero");
852
853                 if (window != null)
854                 {
855                     int diff = value - layoutCount;
856                     window.LayoutController.LayoutCount += diff;
857                 }
858                 layoutCount = value;
859             }
860         }
861
862         /// This will not be public opened.
863         [EditorBrowsable(EditorBrowsableState.Never)]
864         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
865         {
866             Interop.Layer.DeleteLayer(swigCPtr);
867         }
868
869         private void SetBehavior(LayerBehavior behavior)
870         {
871             Interop.Layer.SetBehavior(SwigCPtr, (int)behavior);
872             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
873         }
874
875         private LayerBehavior GetBehavior()
876         {
877             Layer.LayerBehavior ret = (Layer.LayerBehavior)Interop.Layer.GetBehavior(SwigCPtr);
878             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
879             return ret;
880         }
881
882         internal class Property
883         {
884             internal static readonly int BEHAVIOR = Interop.Layer.BehaviorGet();
885         }
886
887         // Callback for View visibility change signal
888         // type is not used because layer does not have parent so layer's visibility cannot be changed by its parent.
889         private void OnVisibilityChanged(IntPtr data, bool visibility, VisibilityChangeType type)
890         {
891             VisibilityChangedEventArgs e = new VisibilityChangedEventArgs();
892             if (data != IntPtr.Zero)
893             {
894                 e.Layer = Registry.GetManagedBaseHandleFromNativePtr(data) as Layer;
895             }
896             e.Visibility = visibility;
897
898             if (visibilityChangedEventHandler != null)
899             {
900                 visibilityChangedEventHandler(this, e);
901             }
902         }
903     }
904 }