2 * Copyright(c) 2019 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;
32 /// Creates a Layer object.
34 /// <since_tizen> 3 </since_tizen>
35 public Layer() : this(Interop.Layer.New(), true)
37 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
38 this.SetAnchorPoint(Tizen.NUI.PivotPoint.TopLeft);
39 this.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.AllDimensions);
42 internal Layer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
47 /// Enumeration for the behavior of the layer.
49 /// <since_tizen> 3 </since_tizen>
50 public enum LayerBehavior
53 /// UI control rendering mode (default mode).
54 /// This mode is designed for UI controls that can overlap. In this
55 /// mode renderer order will be respective to the tree hierarchy of
57 /// The rendering order is depth first, so for the following actor tree,
58 /// A will be drawn first, then B, D, E, then C, F. This ensures that
59 /// overlapping actors are drawn as expected (whereas, with breadth first
60 /// traversal, the actors would interleave).<br />
62 /// <since_tizen> 3 </since_tizen>
66 /// Layer will use depth test.
67 /// This mode is designed for a 3 dimensional scene where actors in front
68 /// of other actors will obscure them, i.e. the actors are sorted by the
69 /// distance from the camera.<br />
70 /// When using this mode, a depth test will be used. A depth clear will
71 /// happen for each layer, which means actors in a layer "above" other
72 /// layers will be rendered in front of actors in those layers regardless
73 /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
74 /// Opaque renderers are drawn first and write to the depth buffer. Then
75 /// transparent renderers are drawn with depth test enabled but depth
76 /// write switched off. Transparent renderers are drawn based on their
77 /// distance from the camera. A renderer's DEPTH_INDEX property is used to
78 /// offset the distance to the camera when ordering transparent renderers.
79 /// This is useful if you want to define the draw order of two or more
80 /// transparent renderers that are equal distance from the camera. Unlike
81 /// LAYER_UI, parent-child relationship does not affect rendering order at
84 /// <since_tizen> 3 </since_tizen>
88 internal enum TreeDepthMultiplier
90 TREE_DEPTH_MULTIPLIER = 10000
94 /// Layer behavior, type String (Layer.LayerBehavior).
96 /// <since_tizen> 3 </since_tizen>
97 public Layer.LayerBehavior Behavior
101 return GetBehavior();
110 /// Sets the viewport (in window coordinates), type rectangle.
111 /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
113 /// <since_tizen> 4 </since_tizen>
114 public Rectangle Viewport
120 Rectangle ret = new Rectangle(Interop.Layer.GetClippingBox(SwigCPtr), true);
121 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
126 // Clipping not enabled so return the window size
127 Size2D windowSize = window?.Size;
128 Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
134 Interop.Layer.SetClippingBox(SwigCPtr, Rectangle.getCPtr(value));
135 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
136 ClippingEnabled = true;
141 /// Retrieves and sets the layer's opacity.<br />
143 /// <since_tizen> 3 </since_tizen>
149 var pValue = GetProperty(View.Property.OPACITY);
150 pValue.Get(out temp);
156 var temp = new Tizen.NUI.PropertyValue(value);
157 SetProperty(View.Property.OPACITY, temp);
163 /// Retrieves and sets the layer's visibility.
165 /// <since_tizen> 3 </since_tizen>
166 public bool Visibility
171 var pValue = GetProperty(View.Property.VISIBLE);
172 pValue.Get(out temp);
178 var temp = new Tizen.NUI.PropertyValue(value);
179 SetProperty(View.Property.VISIBLE, temp);
185 /// Get the number of children held by the layer.
187 /// <since_tizen> 3 </since_tizen>
188 public new uint ChildCount
192 return Convert.ToUInt32(Children.Count);
197 /// Gets or sets the layer's name.
199 /// <since_tizen> 3 </since_tizen>
213 /// Queries the depth of the layer.<br />
214 /// 0 is the bottommost layer, higher number is on the top.<br />
216 /// <since_tizen> 3 </since_tizen>
226 /// Internal only property to enable or disable clipping, type boolean.
227 /// By default, this is false, i.e., the viewport of the layer is the entire window.
229 internal bool ClippingEnabled
233 bool ret = Interop.Layer.IsClipping(SwigCPtr);
234 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239 Interop.Layer.SetClipping(SwigCPtr, value);
240 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
245 /// Gets the Layer's ID
248 /// <remarks>Hidden-API</remarks>
249 [EditorBrowsable(EditorBrowsableState.Never)]
259 /// From the Container base class.
262 /// Adds a child view to this layer.
264 /// <seealso cref="Container.Add">
266 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
267 /// <since_tizen> 4 </since_tizen>
268 public override void Add(View child)
272 throw new ArgumentNullException(nameof(child));
275 Container oldParent = child.GetParent();
277 if (oldParent != this)
279 if (oldParent != null)
281 oldParent.Remove(child);
285 child.InternalParent = this;
287 Interop.Actor.Add(SwigCPtr, View.getCPtr(child));
288 if (NDalicPINVOKE.SWIGPendingException.Pending)
289 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296 /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
298 /// <seealso cref="Container.Remove">
300 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
301 /// <since_tizen> 4 </since_tizen>
302 public override void Remove(View child)
306 throw new ArgumentNullException(nameof(child));
308 Interop.Actor.Remove(SwigCPtr, View.getCPtr(child));
309 if (NDalicPINVOKE.SWIGPendingException.Pending)
310 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
312 Children.Remove(child);
313 child.InternalParent = null;
314 OnChildRemoved(child);
318 /// Retrieves a child view by the index.
320 /// <pre>The view has been initialized.</pre>
321 /// <param name="index">The index of the child to retrieve.</param>
322 /// <returns>The view for the given index or empty handle if children not initialized.</returns>
323 /// <since_tizen> 4 </since_tizen>
324 public override View GetChildAt(uint index)
326 if (index < Children.Count)
328 return Children[Convert.ToInt32(index)];
337 /// Get parent of the layer.
339 /// <returns>The view's container</returns>
340 /// <since_tizen> 4 </since_tizen>
341 public override Container GetParent()
347 /// Get the child count of the layer.
349 /// <returns>The child count of the layer.</returns>
350 /// <since_tizen> 4 </since_tizen>
351 [Obsolete("This has been deprecated in API9 and will be removed in API11. Use ChildCount property instead")]
352 public override uint GetChildCount()
354 return Convert.ToUInt32(Children.Count);
358 /// Downcasts a handle to layer handle.
360 /// <exception cref="ArgumentNullException"> Thrown when handle is null. </exception>
361 /// <since_tizen> 3 </since_tizen>
362 /// Do not use this, that will be deprecated. Use as keyword instead.
363 [Obsolete("Do not use this, that will be deprecated. Use as keyword instead.")]
364 [EditorBrowsable(EditorBrowsableState.Never)]
365 public static Layer DownCast(BaseHandle handle)
369 throw new ArgumentNullException(nameof(handle));
371 Layer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
372 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
377 /// Search through this layer's hierarchy for a view with the given unique ID.
379 /// <pre>This layer (the parent) has been initialized.</pre>
380 /// <remarks>The actor itself is also considered in the search.</remarks>
381 /// <param name="id">The id of the child to find</param>
382 /// <returns> A handle to the view if found, or an empty handle if not. </returns>
383 /// <since_tizen> 3 </since_tizen>
384 public View FindChildById(uint id)
386 //to fix memory leak issue, match the handle count with native side.
387 IntPtr cPtr = Interop.Actor.FindChildById(SwigCPtr, id);
388 View ret = this.GetInstanceSafely<View>(cPtr);
389 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
393 internal override View FindCurrentChildById(uint id)
395 return FindChildById(id);
398 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
399 [EditorBrowsable(EditorBrowsableState.Never)]
400 public View FindChildByName(string viewName)
402 //to fix memory leak issue, match the handle count with native side.
403 IntPtr cPtr = Interop.Actor.FindChildByName(SwigCPtr, viewName);
404 View ret = this.GetInstanceSafely<View>(cPtr);
405 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410 /// Increments the depth of the layer.
412 /// <since_tizen> 3 </since_tizen>
415 var parentChildren = window?.LayersChildren;
416 if (parentChildren != null)
418 int currentIdx = parentChildren.IndexOf(this);
420 if (currentIdx >= 0 && currentIdx < parentChildren.Count - 1)
422 var upper = parentChildren[currentIdx + 1];
429 /// Decrements the depth of the layer.
431 /// <since_tizen> 3 </since_tizen>
434 var parentChildren = window?.LayersChildren;
435 if (parentChildren != null)
437 int currentIdx = parentChildren.IndexOf(this);
439 if (currentIdx > 0 && currentIdx < parentChildren.Count)
441 var low = parentChildren[currentIdx - 1];
448 /// Raises the layer to the top.
450 /// <since_tizen> 3 </since_tizen>
451 public void RaiseToTop()
453 var parentChildren = window?.LayersChildren;
455 if (parentChildren != null)
457 parentChildren.Remove(this);
458 parentChildren.Add(this);
460 Interop.Layer.RaiseToTop(SwigCPtr);
461 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
466 /// Lowers the layer to the bottom.
468 /// <since_tizen> 3 </since_tizen>
469 public void LowerToBottom()
471 var parentChildren = window?.LayersChildren;
473 if (parentChildren != null)
475 parentChildren.Remove(this);
476 parentChildren.Insert(0, this);
478 Interop.Layer.LowerToBottom(SwigCPtr);
479 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
484 /// Moves the layer directly above the given layer.<br />
485 /// After the call, this layer's depth will be immediately above target.<br />
487 /// <param name="target">The layer to get on top of.</param>
488 /// <since_tizen> 3 </since_tizen>
489 public void MoveAbove(Layer target)
491 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
492 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
496 /// Moves the layer directly below the given layer.<br />
497 /// After the call, this layer's depth will be immediately below target.<br />
499 /// <param name="target">The layer to get below of.</param>
500 /// <since_tizen> 3 </since_tizen>
501 public void MoveBelow(Layer target)
503 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
504 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
507 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
508 [EditorBrowsable(EditorBrowsableState.Never)]
509 public void SetAnchorPoint(Vector3 anchorPoint)
511 Interop.Actor.SetAnchorPoint(SwigCPtr, Vector3.getCPtr(anchorPoint));
512 if (NDalicPINVOKE.SWIGPendingException.Pending)
513 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
516 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
517 [EditorBrowsable(EditorBrowsableState.Never)]
518 public void SetSize(float width, float height)
520 Interop.ActorInternal.SetSize(SwigCPtr, width, height);
521 if (NDalicPINVOKE.SWIGPendingException.Pending)
522 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
525 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
526 [EditorBrowsable(EditorBrowsableState.Never)]
527 public void SetParentOrigin(Vector3 parentOrigin)
529 Interop.ActorInternal.SetParentOrigin(SwigCPtr, Vector3.getCPtr(parentOrigin));
530 if (NDalicPINVOKE.SWIGPendingException.Pending)
531 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
534 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
535 [EditorBrowsable(EditorBrowsableState.Never)]
536 public void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
538 Interop.Actor.SetResizePolicy(SwigCPtr, (int)policy, (int)dimension);
539 if (NDalicPINVOKE.SWIGPendingException.Pending)
540 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
545 /// This allows the user to specify whether this layer should consume touch (including gestures).
546 /// If set, any layers behind this layer will not be hit-test.
548 /// <param name="consume">Whether the layer should consume touch (including gestures).</param>
549 [EditorBrowsable(EditorBrowsableState.Never)]
550 public void SetTouchConsumed(bool consume)
552 Interop.Layer.SetTouchConsumed(SwigCPtr, consume);
553 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
558 /// This allows the user to specify whether this layer should consume hover.
559 /// If set, any layers behind this layer will not be hit-test.
561 /// <param name="consume">Whether the layer should consume hover</param>
562 [EditorBrowsable(EditorBrowsableState.Never)]
563 public void SetHoverConsumed(bool consume)
565 Interop.Layer.SetHoverConsumed(SwigCPtr, consume);
566 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
569 internal uint GetDepth()
571 var parentChildren = window?.LayersChildren;
572 if (parentChildren != null)
574 int idx = parentChildren.IndexOf(this);
577 return Convert.ToUInt32(idx); ;
582 internal void RaiseAbove(Layer target)
584 var parentChildren = window?.LayersChildren;
585 if (parentChildren != null)
587 int currentIndex = parentChildren.IndexOf(this);
588 int targetIndex = parentChildren.IndexOf(target);
590 if (currentIndex < 0 || targetIndex < 0 ||
591 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
593 NUILog.Error("index should be bigger than 0 and less than children of layer count");
597 // If the currentIndex is less than the target index and the target has the same parent.
598 if (currentIndex < targetIndex)
600 parentChildren.Remove(this);
601 parentChildren.Insert(targetIndex, this);
603 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
604 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
609 internal void LowerBelow(Layer target)
611 var parentChildren = window?.LayersChildren;
613 if (parentChildren != null)
615 int currentIndex = parentChildren.IndexOf(this);
616 int targetIndex = parentChildren.IndexOf(target);
618 if (currentIndex < 0 || targetIndex < 0 ||
619 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
621 NUILog.Error("index should be bigger than 0 and less than children of layer count");
625 // If the currentIndex is not already the 0th index and the target has the same parent.
626 if ((currentIndex != 0) && (targetIndex != -1) &&
627 (currentIndex > targetIndex))
629 parentChildren.Remove(this);
630 parentChildren.Insert(targetIndex, this);
632 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
633 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
638 internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
640 Interop.Layer.SetSortFunction(SwigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
641 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
644 internal bool IsTouchConsumed()
646 bool ret = Interop.Layer.IsTouchConsumed(SwigCPtr);
647 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
650 internal bool IsHoverConsumed()
652 bool ret = Interop.Layer.IsHoverConsumed(SwigCPtr);
653 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
657 internal void AddViewToLayerList(View view)
662 internal void RemoveViewFromLayerList(View view)
664 Children.Remove(view);
667 internal string GetName()
669 string ret = Interop.Actor.GetName(SwigCPtr);
670 if (NDalicPINVOKE.SWIGPendingException.Pending)
671 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
675 internal void SetName(string name)
677 Interop.Actor.SetName(SwigCPtr, name);
678 if (NDalicPINVOKE.SWIGPendingException.Pending)
679 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
682 internal void SetWindow(Window win)
687 internal uint GetId()
689 uint ret = Interop.Actor.GetId(SwigCPtr);
690 if (NDalicPINVOKE.SWIGPendingException.Pending)
691 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
695 /// This will not be public opened.
696 [EditorBrowsable(EditorBrowsableState.Never)]
697 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
699 Interop.Layer.DeleteLayer(swigCPtr);
702 private void SetBehavior(LayerBehavior behavior)
704 Interop.Layer.SetBehavior(SwigCPtr, (int)behavior);
705 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
708 private LayerBehavior GetBehavior()
710 Layer.LayerBehavior ret = (Layer.LayerBehavior)Interop.Layer.GetBehavior(SwigCPtr);
711 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
715 internal class Property
717 internal static readonly int BEHAVIOR = Interop.Layer.BehaviorGet();