2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
24 /// Layers provide a mechanism for overlaying groups of actors on top of each other.
26 /// <since_tizen> 3 </since_tizen>
27 public class Layer : Container
29 private Window window;
30 private int layoutCount = 0;
33 /// Creates a Layer object.
35 /// <since_tizen> 3 </since_tizen>
36 public Layer() : this(Interop.Layer.New(), true)
38 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
39 this.SetAnchorPoint(Tizen.NUI.PivotPoint.TopLeft);
40 this.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.AllDimensions);
43 internal Layer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
48 /// Dispose Explicit or Implicit
50 [EditorBrowsable(EditorBrowsableState.Never)]
51 protected override void Dispose(DisposeTypes type)
64 /// Enumeration for the behavior of the layer.
66 /// <since_tizen> 3 </since_tizen>
67 public enum LayerBehavior
70 /// UI control rendering mode (default mode).
71 /// This mode is designed for UI controls that can overlap. In this
72 /// mode renderer order will be respective to the tree hierarchy of
74 /// The rendering order is depth first, so for the following actor tree,
75 /// A will be drawn first, then B, D, E, then C, F. This ensures that
76 /// overlapping actors are drawn as expected (whereas, with breadth first
77 /// traversal, the actors would interleave).<br />
79 /// <since_tizen> 3 </since_tizen>
83 /// Layer will use depth test.
84 /// This mode is designed for a 3 dimensional scene where actors in front
85 /// of other actors will obscure them, i.e. the actors are sorted by the
86 /// distance from the camera.<br />
87 /// When using this mode, a depth test will be used. A depth clear will
88 /// happen for each layer, which means actors in a layer "above" other
89 /// layers will be rendered in front of actors in those layers regardless
90 /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
91 /// Opaque renderers are drawn first and write to the depth buffer. Then
92 /// transparent renderers are drawn with depth test enabled but depth
93 /// write switched off. Transparent renderers are drawn based on their
94 /// distance from the camera. A renderer's DEPTH_INDEX property is used to
95 /// offset the distance to the camera when ordering transparent renderers.
96 /// This is useful if you want to define the draw order of two or more
97 /// transparent renderers that are equal distance from the camera. Unlike
98 /// LAYER_UI, parent-child relationship does not affect rendering order at
101 /// <since_tizen> 3 </since_tizen>
105 internal enum TreeDepthMultiplier
107 TREE_DEPTH_MULTIPLIER = 10000
111 /// Layer behavior, type String (Layer.LayerBehavior).
113 /// <since_tizen> 3 </since_tizen>
114 public Layer.LayerBehavior Behavior
118 return GetBehavior();
127 /// Sets the viewport (in window coordinates), type rectangle.
128 /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
130 /// <since_tizen> 4 </since_tizen>
131 public Rectangle Viewport
137 Rectangle ret = new Rectangle(Interop.Layer.GetClippingBox(SwigCPtr), true);
138 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
143 // Clipping not enabled so return the window size
144 Size2D windowSize = window?.Size;
145 Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
151 Interop.Layer.SetClippingBox(SwigCPtr, Rectangle.getCPtr(value));
152 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
153 ClippingEnabled = true;
158 /// Retrieves and sets the layer's opacity.<br />
160 /// <since_tizen> 3 </since_tizen>
166 var pValue = GetProperty(View.Property.OPACITY);
167 pValue.Get(out temp);
173 var temp = new Tizen.NUI.PropertyValue(value);
174 SetProperty(View.Property.OPACITY, temp);
180 /// Retrieves and sets the layer's visibility.
182 /// <since_tizen> 3 </since_tizen>
183 public bool Visibility
188 var pValue = GetProperty(View.Property.VISIBLE);
189 pValue.Get(out temp);
195 var temp = new Tizen.NUI.PropertyValue(value);
196 SetProperty(View.Property.VISIBLE, temp);
202 /// Get the number of children held by the layer.
204 /// <since_tizen> 3 </since_tizen>
205 public new uint ChildCount
209 return Convert.ToUInt32(Children.Count);
214 /// Gets or sets the layer's name.
216 /// <since_tizen> 3 </since_tizen>
230 /// Queries the depth of the layer.<br />
231 /// 0 is the bottommost layer, higher number is on the top.<br />
233 /// <since_tizen> 3 </since_tizen>
243 /// Internal only property to enable or disable clipping, type boolean.
244 /// By default, this is false, i.e., the viewport of the layer is the entire window.
246 internal bool ClippingEnabled
250 bool ret = Interop.Layer.IsClipping(SwigCPtr);
251 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256 Interop.Layer.SetClipping(SwigCPtr, value);
257 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
262 /// Gets the Layer's ID
265 /// <remarks>Hidden-API</remarks>
266 [EditorBrowsable(EditorBrowsableState.Never)]
276 /// From the Container base class.
279 /// Adds a child view to this layer.
281 /// <seealso cref="Container.Add">
283 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
284 /// <since_tizen> 4 </since_tizen>
285 public override void Add(View child)
289 throw new ArgumentNullException(nameof(child));
292 Container oldParent = child.GetParent();
294 if (oldParent != this)
296 if (oldParent != null)
298 oldParent.Remove(child);
302 child.InternalParent = this;
305 LayoutCount += child.LayoutCount;
307 Interop.Actor.Add(SwigCPtr, View.getCPtr(child));
308 if (NDalicPINVOKE.SWIGPendingException.Pending)
309 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
316 /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
318 /// <seealso cref="Container.Remove">
320 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
321 /// <since_tizen> 4 </since_tizen>
322 public override void Remove(View child)
326 throw new ArgumentNullException(nameof(child));
328 if (child.GetParent() == null) // Early out if child parent is null.
331 if (child.GetParent() != this)
333 //throw new System.InvalidOperationException("You have deleted a view that is not a child of this layer.");
334 Tizen.Log.Error("NUI", "You have deleted a view that is not a child of this layer.");
337 Interop.Actor.Remove(SwigCPtr, View.getCPtr(child));
338 if (NDalicPINVOKE.SWIGPendingException.Pending)
339 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
341 Children.Remove(child);
342 child.InternalParent = null;
343 OnChildRemoved(child);
344 LayoutCount -= child.LayoutCount;
348 /// Retrieves a child view by the index.
350 /// <pre>The view has been initialized.</pre>
351 /// <param name="index">The index of the child to retrieve.</param>
352 /// <returns>The view for the given index or empty handle if children not initialized.</returns>
353 /// <since_tizen> 4 </since_tizen>
354 public override View GetChildAt(uint index)
356 if (index < Children.Count)
358 return Children[Convert.ToInt32(index)];
367 /// Get parent of the layer.
369 /// <returns>The view's container</returns>
370 /// <since_tizen> 4 </since_tizen>
371 public override Container GetParent()
377 /// Get the child count of the layer.
379 /// <returns>The child count of the layer.</returns>
380 /// <since_tizen> 4 </since_tizen>
381 [Obsolete("Deprecated in API9, will be removed in API11. Please use ChildCount property instead!")]
382 public override uint GetChildCount()
384 return Convert.ToUInt32(Children.Count);
388 /// Downcasts a handle to layer handle.
390 /// <exception cref="ArgumentNullException"> Thrown when handle is null. </exception>
391 /// <since_tizen> 3 </since_tizen>
392 /// Please do not use! this will be deprecated!
393 /// Instead please use as keyword.
394 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
395 [EditorBrowsable(EditorBrowsableState.Never)]
396 public static Layer DownCast(BaseHandle handle)
400 throw new ArgumentNullException(nameof(handle));
402 Layer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
403 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408 /// Search through this layer's hierarchy for a view with the given unique ID.
410 /// <pre>This layer (the parent) has been initialized.</pre>
411 /// <remarks>The actor itself is also considered in the search.</remarks>
412 /// <param name="id">The id of the child to find</param>
413 /// <returns> A handle to the view if found, or an empty handle if not. </returns>
414 /// <since_tizen> 3 </since_tizen>
415 public View FindChildById(uint id)
417 //to fix memory leak issue, match the handle count with native side.
418 IntPtr cPtr = Interop.Actor.FindChildById(SwigCPtr, id);
419 View ret = this.GetInstanceSafely<View>(cPtr);
420 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
424 internal override View FindCurrentChildById(uint id)
426 return FindChildById(id);
429 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
430 [EditorBrowsable(EditorBrowsableState.Never)]
431 public View FindChildByName(string viewName)
433 //to fix memory leak issue, match the handle count with native side.
434 IntPtr cPtr = Interop.Actor.FindChildByName(SwigCPtr, viewName);
435 View ret = this.GetInstanceSafely<View>(cPtr);
436 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
441 /// Increments the depth of the layer.
443 /// <since_tizen> 3 </since_tizen>
446 var parentChildren = window?.LayersChildren;
447 if (parentChildren != null)
449 int currentIdx = parentChildren.IndexOf(this);
451 if (currentIdx >= 0 && currentIdx < parentChildren.Count - 1)
453 var upper = parentChildren[currentIdx + 1];
460 /// Decrements the depth of the layer.
462 /// <since_tizen> 3 </since_tizen>
465 var parentChildren = window?.LayersChildren;
466 if (parentChildren != null)
468 int currentIdx = parentChildren.IndexOf(this);
470 if (currentIdx > 0 && currentIdx < parentChildren.Count)
472 var low = parentChildren[currentIdx - 1];
479 /// Raises the layer to the top.
481 /// <since_tizen> 3 </since_tizen>
482 public void RaiseToTop()
484 var parentChildren = window?.LayersChildren;
486 if (parentChildren != null)
488 parentChildren.Remove(this);
489 parentChildren.Add(this);
491 Interop.Layer.RaiseToTop(SwigCPtr);
492 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
497 /// Lowers the layer to the bottom.
499 /// <since_tizen> 3 </since_tizen>
500 public void LowerToBottom()
502 var parentChildren = window?.LayersChildren;
504 if (parentChildren != null)
506 parentChildren.Remove(this);
507 parentChildren.Insert(0, this);
509 Interop.Layer.LowerToBottom(SwigCPtr);
510 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
515 /// Moves the layer directly above the given layer.<br />
516 /// After the call, this layer's depth will be immediately above target.<br />
518 /// <param name="target">The layer to get on top of.</param>
519 /// <since_tizen> 3 </since_tizen>
520 public void MoveAbove(Layer target)
522 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
523 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
527 /// Moves the layer directly below the given layer.<br />
528 /// After the call, this layer's depth will be immediately below target.<br />
530 /// <param name="target">The layer to get below of.</param>
531 /// <since_tizen> 3 </since_tizen>
532 public void MoveBelow(Layer target)
534 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
535 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
538 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
539 [EditorBrowsable(EditorBrowsableState.Never)]
540 public void SetAnchorPoint(Vector3 anchorPoint)
542 Interop.Actor.SetAnchorPoint(SwigCPtr, Vector3.getCPtr(anchorPoint));
543 if (NDalicPINVOKE.SWIGPendingException.Pending)
544 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
547 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
548 [EditorBrowsable(EditorBrowsableState.Never)]
549 public void SetSize(float width, float height)
551 Interop.ActorInternal.SetSize(SwigCPtr, width, height);
552 if (NDalicPINVOKE.SWIGPendingException.Pending)
553 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
556 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
557 [EditorBrowsable(EditorBrowsableState.Never)]
558 public void SetParentOrigin(Vector3 parentOrigin)
560 Interop.ActorInternal.SetParentOrigin(SwigCPtr, Vector3.getCPtr(parentOrigin));
561 if (NDalicPINVOKE.SWIGPendingException.Pending)
562 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
565 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
566 [EditorBrowsable(EditorBrowsableState.Never)]
567 public void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
569 Interop.Actor.SetResizePolicy(SwigCPtr, (int)policy, (int)dimension);
570 if (NDalicPINVOKE.SWIGPendingException.Pending)
571 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
576 /// This allows the user to specify whether this layer should consume touch (including gestures).
577 /// If set, any layers behind this layer will not be hit-test.
579 /// <param name="consume">Whether the layer should consume touch (including gestures).</param>
580 [EditorBrowsable(EditorBrowsableState.Never)]
581 public void SetTouchConsumed(bool consume)
583 Interop.Layer.SetTouchConsumed(SwigCPtr, consume);
584 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
589 /// This allows the user to specify whether this layer should consume hover.
590 /// If set, any layers behind this layer will not be hit-test.
592 /// <param name="consume">Whether the layer should consume hover</param>
593 [EditorBrowsable(EditorBrowsableState.Never)]
594 public void SetHoverConsumed(bool consume)
596 Interop.Layer.SetHoverConsumed(SwigCPtr, consume);
597 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
600 internal uint GetDepth()
602 var parentChildren = window?.LayersChildren;
603 if (parentChildren != null)
605 int idx = parentChildren.IndexOf(this);
608 return Convert.ToUInt32(idx); ;
613 internal void RaiseAbove(Layer target)
615 var parentChildren = window?.LayersChildren;
616 if (parentChildren != null)
618 int currentIndex = parentChildren.IndexOf(this);
619 int targetIndex = parentChildren.IndexOf(target);
621 if (currentIndex < 0 || targetIndex < 0 ||
622 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
624 NUILog.Error("index should be bigger than 0 and less than children of layer count");
628 // If the currentIndex is less than the target index and the target has the same parent.
629 if (currentIndex < targetIndex)
631 parentChildren.Remove(this);
632 parentChildren.Insert(targetIndex, this);
634 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
635 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
640 internal void LowerBelow(Layer target)
642 var parentChildren = window?.LayersChildren;
644 if (parentChildren != null)
646 int currentIndex = parentChildren.IndexOf(this);
647 int targetIndex = parentChildren.IndexOf(target);
649 if (currentIndex < 0 || targetIndex < 0 ||
650 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
652 NUILog.Error("index should be bigger than 0 and less than children of layer count");
656 // If the currentIndex is not already the 0th index and the target has the same parent.
657 if ((currentIndex != 0) && (targetIndex != -1) &&
658 (currentIndex > targetIndex))
660 parentChildren.Remove(this);
661 parentChildren.Insert(targetIndex, this);
663 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
664 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
669 internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
671 Interop.Layer.SetSortFunction(SwigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
672 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
675 internal bool IsTouchConsumed()
677 bool ret = Interop.Layer.IsTouchConsumed(SwigCPtr);
678 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
681 internal bool IsHoverConsumed()
683 bool ret = Interop.Layer.IsHoverConsumed(SwigCPtr);
684 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
688 internal void AddViewToLayerList(View view)
693 internal void RemoveViewFromLayerList(View view)
695 Children.Remove(view);
698 internal string GetName()
700 string ret = Interop.Actor.GetName(SwigCPtr);
701 if (NDalicPINVOKE.SWIGPendingException.Pending)
702 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
706 internal void SetName(string name)
708 Interop.Actor.SetName(SwigCPtr, name);
709 if (NDalicPINVOKE.SWIGPendingException.Pending)
710 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
713 internal void SetWindow(Window win)
715 if (window == win) return;
719 window.LayoutController.LayoutCount -= LayoutCount;
726 window.LayoutController.LayoutCount += LayoutCount;
730 internal uint GetId()
732 uint ret = Interop.Actor.GetId(SwigCPtr);
733 if (NDalicPINVOKE.SWIGPendingException.Pending)
734 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
739 /// The number of layouts of children views.
740 /// This can be used to set/unset Process callback to calculate Layout.
742 internal int LayoutCount
751 if (layoutCount == value) return;
753 if (value < 0) throw new global::System.ArgumentOutOfRangeException(nameof(LayoutCount), "LayoutCount(" + LayoutCount + ") should not be less than zero");
755 int diff = value - layoutCount;
760 window.LayoutController.LayoutCount += diff;
765 /// This will not be public opened.
766 [EditorBrowsable(EditorBrowsableState.Never)]
767 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
769 Interop.Layer.DeleteLayer(swigCPtr);
772 private void SetBehavior(LayerBehavior behavior)
774 Interop.Layer.SetBehavior(SwigCPtr, (int)behavior);
775 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
778 private LayerBehavior GetBehavior()
780 Layer.LayerBehavior ret = (Layer.LayerBehavior)Interop.Layer.GetBehavior(SwigCPtr);
781 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
785 internal class Property
787 internal static readonly int BEHAVIOR = Interop.Layer.BehaviorGet();