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);
469 int idx = window.IsBorderEnabled ? 1 : 0;
471 if (currentIdx > idx && currentIdx < parentChildren.Count)
473 var low = parentChildren[currentIdx - 1];
480 /// Raises the layer to the top.
482 /// <since_tizen> 3 </since_tizen>
483 public void RaiseToTop()
485 var parentChildren = window?.LayersChildren;
487 if (parentChildren != null)
489 parentChildren.Remove(this);
490 parentChildren.Add(this);
492 Interop.Layer.RaiseToTop(SwigCPtr);
493 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
498 /// Lowers the layer to the bottom.
500 /// <since_tizen> 3 </since_tizen>
501 public void LowerToBottom()
503 var parentChildren = window?.LayersChildren;
505 if (parentChildren != null)
507 parentChildren.Remove(this);
508 parentChildren.Insert(0, this);
510 Interop.Layer.LowerToBottom(SwigCPtr);
512 if (window.IsBorderEnabled)
514 Layer bottomLayer = window.GetBorderWindowBottomLayer();
515 parentChildren.Remove(bottomLayer);
516 parentChildren.Insert(0, bottomLayer);
517 Interop.Layer.LowerToBottom(Layer.getCPtr(bottomLayer));
519 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
524 /// Moves the layer directly above the given layer.<br />
525 /// After the call, this layer's depth will be immediately above target.<br />
527 /// <param name="target">The layer to get on top of.</param>
528 /// <since_tizen> 3 </since_tizen>
529 public void MoveAbove(Layer target)
531 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
532 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
536 /// Moves the layer directly below the given layer.<br />
537 /// After the call, this layer's depth will be immediately below target.<br />
539 /// <param name="target">The layer to get below of.</param>
540 /// <since_tizen> 3 </since_tizen>
541 public void MoveBelow(Layer target)
543 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
544 if (NDalicPINVOKE.SWIGPendingException.Pending) 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 SetAnchorPoint(Vector3 anchorPoint)
551 Interop.Actor.SetAnchorPoint(SwigCPtr, Vector3.getCPtr(anchorPoint));
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 SetSize(float width, float height)
560 Interop.ActorInternal.SetSize(SwigCPtr, width, height);
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 SetParentOrigin(Vector3 parentOrigin)
569 Interop.ActorInternal.SetParentOrigin(SwigCPtr, Vector3.getCPtr(parentOrigin));
570 if (NDalicPINVOKE.SWIGPendingException.Pending)
571 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
574 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
575 [EditorBrowsable(EditorBrowsableState.Never)]
576 public void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
578 Interop.Actor.SetResizePolicy(SwigCPtr, (int)policy, (int)dimension);
579 if (NDalicPINVOKE.SWIGPendingException.Pending)
580 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
585 /// This allows the user to specify whether this layer should consume touch (including gestures).
586 /// If set, any layers behind this layer will not be hit-test.
588 /// <param name="consume">Whether the layer should consume touch (including gestures).</param>
589 [EditorBrowsable(EditorBrowsableState.Never)]
590 public void SetTouchConsumed(bool consume)
592 Interop.Layer.SetTouchConsumed(SwigCPtr, consume);
593 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
598 /// This allows the user to specify whether this layer should consume hover.
599 /// If set, any layers behind this layer will not be hit-test.
601 /// <param name="consume">Whether the layer should consume hover</param>
602 [EditorBrowsable(EditorBrowsableState.Never)]
603 public void SetHoverConsumed(bool consume)
605 Interop.Layer.SetHoverConsumed(SwigCPtr, consume);
606 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
609 internal uint GetDepth()
611 var parentChildren = window?.LayersChildren;
612 if (parentChildren != null)
614 int idx = parentChildren.IndexOf(this);
617 return Convert.ToUInt32(idx); ;
622 internal void RaiseAbove(Layer target)
624 var parentChildren = window?.LayersChildren;
625 if (parentChildren != null)
627 int currentIndex = parentChildren.IndexOf(this);
628 int targetIndex = parentChildren.IndexOf(target);
630 if (currentIndex < 0 || targetIndex < 0 ||
631 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
633 NUILog.Error("index should be bigger than 0 and less than children of layer count");
637 // If the currentIndex is less than the target index and the target has the same parent.
638 if (currentIndex < targetIndex)
640 parentChildren.Remove(this);
641 parentChildren.Insert(targetIndex, this);
643 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
644 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
649 internal void LowerBelow(Layer target)
651 var parentChildren = window?.LayersChildren;
653 if (parentChildren != null)
655 int currentIndex = parentChildren.IndexOf(this);
656 int targetIndex = parentChildren.IndexOf(target);
658 if (currentIndex < 0 || targetIndex < 0 ||
659 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
661 NUILog.Error("index should be bigger than 0 and less than children of layer count");
665 // If the currentIndex is not already the 0th index and the target has the same parent.
666 if ((currentIndex != 0) && (targetIndex != -1) &&
667 (currentIndex > targetIndex))
669 parentChildren.Remove(this);
670 parentChildren.Insert(targetIndex, this);
672 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
673 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
678 internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
680 Interop.Layer.SetSortFunction(SwigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
681 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
684 internal bool IsTouchConsumed()
686 bool ret = Interop.Layer.IsTouchConsumed(SwigCPtr);
687 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
690 internal bool IsHoverConsumed()
692 bool ret = Interop.Layer.IsHoverConsumed(SwigCPtr);
693 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
697 internal void AddViewToLayerList(View view)
702 internal void RemoveViewFromLayerList(View view)
704 Children.Remove(view);
707 internal string GetName()
709 string ret = Interop.Actor.GetName(SwigCPtr);
710 if (NDalicPINVOKE.SWIGPendingException.Pending)
711 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
715 internal void SetName(string name)
717 Interop.Actor.SetName(SwigCPtr, name);
718 if (NDalicPINVOKE.SWIGPendingException.Pending)
719 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
722 internal void SetWindow(Window win)
724 if (window == win) return;
728 window.LayoutController.LayoutCount -= LayoutCount;
735 window.LayoutController.LayoutCount += LayoutCount;
739 internal uint GetId()
741 uint ret = Interop.Actor.GetId(SwigCPtr);
742 if (NDalicPINVOKE.SWIGPendingException.Pending)
743 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
748 /// The number of layouts of children views.
749 /// This can be used to set/unset Process callback to calculate Layout.
751 internal int LayoutCount
760 if (layoutCount == value) return;
762 if (value < 0) throw new global::System.ArgumentOutOfRangeException(nameof(LayoutCount), "LayoutCount(" + LayoutCount + ") should not be less than zero");
768 window.LayoutController.LayoutCount = 0;
772 int diff = value - layoutCount;
773 window.LayoutController.LayoutCount += diff;
780 /// This will not be public opened.
781 [EditorBrowsable(EditorBrowsableState.Never)]
782 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
784 Interop.Layer.DeleteLayer(swigCPtr);
787 private void SetBehavior(LayerBehavior behavior)
789 Interop.Layer.SetBehavior(SwigCPtr, (int)behavior);
790 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
793 private LayerBehavior GetBehavior()
795 Layer.LayerBehavior ret = (Layer.LayerBehavior)Interop.Layer.GetBehavior(SwigCPtr);
796 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
800 internal class Property
802 internal static readonly int BEHAVIOR = Interop.Layer.BehaviorGet();