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;
20 using Tizen.NUI.Binding;
25 /// Layers provide a mechanism for overlaying groups of actors on top of each other.
27 /// <since_tizen> 3 </since_tizen>
28 public class Layer : Container
30 private Window window;
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(Interop.Layer.Upcast(cPtr), cMemoryOwn)
48 /// Enumeration for the behavior of the layer.
50 /// <since_tizen> 3 </since_tizen>
51 public enum LayerBehavior
54 /// UI control rendering mode (default mode).
55 /// This mode is designed for UI controls that can overlap. In this
56 /// mode renderer order will be respective to the tree hierarchy of
58 /// The rendering order is depth first, so for the following actor tree,
59 /// A will be drawn first, then B, D, E, then C, F. This ensures that
60 /// overlapping actors are drawn as expected (whereas, with breadth first
61 /// traversal, the actors would interleave).<br />
63 /// <since_tizen> 3 </since_tizen>
67 /// Deprecated in API6; Will be removed in API9. Please use LayerUI instead.
69 /// <since_tizen> 3 </since_tizen>
70 [Obsolete("Deprecated in API6; Will be removed in API9. Please use LayerUI instead.")]
71 [EditorBrowsable(EditorBrowsableState.Never)]
75 /// Layer will use depth test.
76 /// This mode is designed for a 3 dimensional scene where actors in front
77 /// of other actors will obscure them, i.e. the actors are sorted by the
78 /// distance from the camera.<br />
79 /// When using this mode, a depth test will be used. A depth clear will
80 /// happen for each layer, which means actors in a layer "above" other
81 /// layers will be rendered in front of actors in those layers regardless
82 /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
83 /// Opaque renderers are drawn first and write to the depth buffer. Then
84 /// transparent renderers are drawn with depth test enabled but depth
85 /// write switched off. Transparent renderers are drawn based on their
86 /// distance from the camera. A renderer's DEPTH_INDEX property is used to
87 /// offset the distance to the camera when ordering transparent renderers.
88 /// This is useful if you want to define the draw order of two or more
89 /// transparent renderers that are equal distance from the camera. Unlike
90 /// LAYER_UI, parent-child relationship does not affect rendering order at
93 /// <since_tizen> 3 </since_tizen>
97 internal enum TreeDepthMultiplier
99 TREE_DEPTH_MULTIPLIER = 10000
103 /// Layer behavior, type String (Layer.LayerBehavior).
105 /// <since_tizen> 3 </since_tizen>
106 public Layer.LayerBehavior Behavior
110 return GetBehavior();
119 /// Sets the viewport (in window coordinates), type rectangle.
120 /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
122 /// <since_tizen> 4 </since_tizen>
123 public Rectangle Viewport
129 Rectangle ret = new Rectangle(Interop.Layer.GetClippingBox(SwigCPtr), true);
130 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
135 // Clipping not enabled so return the window size
136 Size2D windowSize = window?.Size;
137 Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
143 Interop.Layer.SetClippingBox(SwigCPtr, Rectangle.getCPtr(value));
144 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
145 ClippingEnabled = true;
150 /// Retrieves and sets the layer's opacity.<br />
152 /// <since_tizen> 3 </since_tizen>
158 var pValue = GetProperty(View.Property.OPACITY);
159 pValue.Get(out temp);
165 var temp = new Tizen.NUI.PropertyValue(value);
166 SetProperty(View.Property.OPACITY, temp);
172 /// Retrieves and sets the layer's visibility.
174 /// <since_tizen> 3 </since_tizen>
175 public bool Visibility
180 var pValue = GetProperty(View.Property.VISIBLE);
181 pValue.Get(out temp);
187 var temp = new Tizen.NUI.PropertyValue(value);
188 SetProperty(View.Property.VISIBLE, temp);
194 /// Get the number of children held by the layer.
196 /// <since_tizen> 3 </since_tizen>
197 public new uint ChildCount
201 return Convert.ToUInt32(Children.Count);
206 /// Gets or sets the layer's name.
208 /// <since_tizen> 3 </since_tizen>
222 /// Queries the depth of the layer.<br />
223 /// 0 is the bottommost layer, higher number is on the top.<br />
225 /// <since_tizen> 3 </since_tizen>
235 /// Internal only property to enable or disable clipping, type boolean.
236 /// By default, this is false, i.e., the viewport of the layer is the entire window.
238 internal bool ClippingEnabled
242 bool ret = Interop.Layer.IsClipping(SwigCPtr);
243 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
248 Interop.Layer.SetClipping(SwigCPtr, value);
249 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
253 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
254 [EditorBrowsable(EditorBrowsableState.Never)]
255 public ResourceDictionary XamlResources
259 return Application.Current.XamlResources;
263 Application.Current.XamlResources = value;
267 /// From the Container base class.
270 /// Adds a child view to this layer.
272 /// <seealso cref="Container.Add">
274 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
275 /// <since_tizen> 4 </since_tizen>
276 public override void Add(View child)
280 throw new ArgumentNullException(nameof(child));
283 Container oldParent = child.GetParent();
285 if (oldParent != this)
287 if (oldParent != null)
289 oldParent.Remove(child);
293 child.InternalParent = this;
295 Interop.Actor.Add( SwigCPtr , View.getCPtr(child));
296 if (NDalicPINVOKE.SWIGPendingException.Pending)
297 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
299 BindableObject.SetInheritedBindingContext(child, this?.BindingContext);
304 /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
306 /// <seealso cref="Container.Remove">
308 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
309 /// <since_tizen> 4 </since_tizen>
310 public override void Remove(View child)
314 throw new ArgumentNullException(nameof(child));
316 Interop.Actor.Remove( SwigCPtr, View.getCPtr(child));
317 if (NDalicPINVOKE.SWIGPendingException.Pending)
318 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
320 Children.Remove(child);
321 child.InternalParent = null;
325 /// Retrieves a child view by the index.
327 /// <pre>The view has been initialized.</pre>
328 /// <param name="index">The index of the child to retrieve.</param>
329 /// <returns>The view for the given index or empty handle if children not initialized.</returns>
330 /// <since_tizen> 4 </since_tizen>
331 public override View GetChildAt(uint index)
333 if (index < Children.Count)
335 return Children[Convert.ToInt32(index)];
344 /// Get parent of the layer.
346 /// <returns>The view's container</returns>
347 /// <since_tizen> 4 </since_tizen>
348 public override Container GetParent()
354 /// Get the child count of the layer.
356 /// <returns>The child count of the layer.</returns>
357 /// <since_tizen> 4 </since_tizen>
358 // [Obsolete("Deprecated in API9, will be removed in API11. Please use ChildCount property instead!")]
359 public override uint GetChildCount()
361 return Convert.ToUInt32(Children.Count);
365 /// Downcasts a handle to layer handle.
367 /// <exception cref="ArgumentNullException"> Thrown when handle is null. </exception>
368 /// <since_tizen> 3 </since_tizen>
369 /// Please do not use! this will be deprecated!
370 /// Instead please use as keyword.
371 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
372 [EditorBrowsable(EditorBrowsableState.Never)]
373 public static Layer DownCast(BaseHandle handle)
377 throw new ArgumentNullException(nameof(handle));
379 Layer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
380 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
385 /// Search through this layer's hierarchy for a view with the given unique ID.
387 /// <pre>This layer (the parent) has been initialized.</pre>
388 /// <remarks>The actor itself is also considered in the search.</remarks>
389 /// <param name="id">The id of the child to find</param>
390 /// <returns> A handle to the view if found, or an empty handle if not. </returns>
391 /// <since_tizen> 3 </since_tizen>
392 public View FindChildById(uint id)
394 //to fix memory leak issue, match the handle count with native side.
395 IntPtr cPtr = Interop.Actor.FindChildById(SwigCPtr, id);
396 View ret = this.GetInstanceSafely<View>(cPtr);
397 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401 internal override View FindCurrentChildById(uint id)
403 return FindChildById(id);
406 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
407 [EditorBrowsable(EditorBrowsableState.Never)]
408 public View FindChildByName(string viewName)
410 //to fix memory leak issue, match the handle count with native side.
411 IntPtr cPtr = Interop.Actor.FindChildByName(SwigCPtr, viewName);
412 View ret = this.GetInstanceSafely<View>(cPtr);
413 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
418 /// Increments the depth of the layer.
420 /// <since_tizen> 3 </since_tizen>
423 var parentChildren = window?.LayersChildren;
424 if (parentChildren != null)
426 int currentIdx = parentChildren.IndexOf(this);
428 if (currentIdx >= 0 && currentIdx < parentChildren.Count - 1)
430 var upper = parentChildren[currentIdx + 1];
437 /// Decrements the depth of the layer.
439 /// <since_tizen> 3 </since_tizen>
442 var parentChildren = window?.LayersChildren;
443 if (parentChildren != null)
445 int currentIdx = parentChildren.IndexOf(this);
447 if (currentIdx > 0 && currentIdx < parentChildren.Count)
449 var low = parentChildren[currentIdx - 1];
456 /// Raises the layer to the top.
458 /// <since_tizen> 3 </since_tizen>
459 public void RaiseToTop()
461 var parentChildren = window?.LayersChildren;
463 if (parentChildren != null)
465 parentChildren.Remove(this);
466 parentChildren.Add(this);
468 Interop.Layer.RaiseToTop(SwigCPtr);
469 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
474 /// Lowers the layer to the bottom.
476 /// <since_tizen> 3 </since_tizen>
477 public void LowerToBottom()
479 var parentChildren = window?.LayersChildren;
481 if (parentChildren != null)
483 parentChildren.Remove(this);
484 parentChildren.Insert(0, this);
486 Interop.Layer.LowerToBottom(SwigCPtr);
487 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
492 /// Moves the layer directly above the given layer.<br />
493 /// After the call, this layer's depth will be immediately above target.<br />
495 /// <param name="target">The layer to get on top of.</param>
496 /// <since_tizen> 3 </since_tizen>
497 public void MoveAbove(Layer target)
499 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
500 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
504 /// Moves the layer directly below the given layer.<br />
505 /// After the call, this layer's depth will be immediately below target.<br />
507 /// <param name="target">The layer to get below of.</param>
508 /// <since_tizen> 3 </since_tizen>
509 public void MoveBelow(Layer target)
511 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
512 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
515 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Layer obj)
517 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
520 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
521 [EditorBrowsable(EditorBrowsableState.Never)]
522 public void SetAnchorPoint(Vector3 anchorPoint)
524 Interop.Actor.SetAnchorPoint(SwigCPtr, Vector3.getCPtr(anchorPoint));
525 if (NDalicPINVOKE.SWIGPendingException.Pending)
526 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
529 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
530 [EditorBrowsable(EditorBrowsableState.Never)]
531 public void SetSize(float width, float height)
533 Interop.ActorInternal.SetSize(SwigCPtr, width, height);
534 if (NDalicPINVOKE.SWIGPendingException.Pending)
535 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 SetParentOrigin(Vector3 parentOrigin)
542 Interop.ActorInternal.SetParentOrigin(SwigCPtr, Vector3.getCPtr(parentOrigin));
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 SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
551 Interop.Actor.SetResizePolicy(SwigCPtr, (int)policy, (int)dimension);
552 if (NDalicPINVOKE.SWIGPendingException.Pending)
553 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
558 /// This allows the user to specify whether this layer should consume touch (including gestures).
559 /// If set, any layers behind this layer will not be hit-test.
561 /// <param name="consume">Whether the layer should consume touch (including gestures).</param>
562 [EditorBrowsable(EditorBrowsableState.Never)]
563 public void SetTouchConsumed(bool consume)
565 Interop.Layer.SetTouchConsumed(SwigCPtr, consume);
566 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
571 /// This allows the user to specify whether this layer should consume hover.
572 /// If set, any layers behind this layer will not be hit-test.
574 /// <param name="consume">Whether the layer should consume hover</param>
575 [EditorBrowsable(EditorBrowsableState.Never)]
576 public void SetHoverConsumed(bool consume)
578 Interop.Layer.SetHoverConsumed(SwigCPtr, consume);
579 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
582 internal uint GetDepth()
584 var parentChildren = window?.LayersChildren;
585 if (parentChildren != null)
587 int idx = parentChildren.IndexOf(this);
590 return Convert.ToUInt32(idx); ;
595 internal void RaiseAbove(Layer target)
597 var parentChildren = window?.LayersChildren;
598 if (parentChildren != null)
600 int currentIndex = parentChildren.IndexOf(this);
601 int targetIndex = parentChildren.IndexOf(target);
603 if (currentIndex < 0 || targetIndex < 0 ||
604 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
606 NUILog.Error("index should be bigger than 0 and less than children of layer count");
610 // If the currentIndex is less than the target index and the target has the same parent.
611 if (currentIndex < targetIndex)
613 parentChildren.Remove(this);
614 parentChildren.Insert(targetIndex, this);
616 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
617 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
622 internal void LowerBelow(Layer target)
624 var parentChildren = window?.LayersChildren;
626 if (parentChildren != null)
628 int currentIndex = parentChildren.IndexOf(this);
629 int targetIndex = parentChildren.IndexOf(target);
631 if (currentIndex < 0 || targetIndex < 0 ||
632 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
634 NUILog.Error("index should be bigger than 0 and less than children of layer count");
638 // If the currentIndex is not already the 0th index and the target has the same parent.
639 if ((currentIndex != 0) && (targetIndex != -1) &&
640 (currentIndex > targetIndex))
642 parentChildren.Remove(this);
643 parentChildren.Insert(targetIndex, this);
645 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
646 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
651 internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
653 Interop.Layer.SetSortFunction(SwigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
654 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
657 internal bool IsTouchConsumed()
659 bool ret = Interop.Layer.IsTouchConsumed(SwigCPtr);
660 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
663 internal bool IsHoverConsumed()
665 bool ret = Interop.Layer.IsHoverConsumed(SwigCPtr);
666 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
670 internal void AddViewToLayerList(View view)
675 internal void RemoveViewFromLayerList(View view)
677 Children.Remove(view);
680 internal string GetName()
682 string ret = Interop.Actor.GetName(SwigCPtr);
683 if (NDalicPINVOKE.SWIGPendingException.Pending)
684 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
688 internal void SetName(string name)
690 Interop.Actor.SetName(SwigCPtr, name);
691 if (NDalicPINVOKE.SWIGPendingException.Pending)
692 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
695 internal void SetWindow(Window win)
700 /// This will not be public opened.
701 [EditorBrowsable(EditorBrowsableState.Never)]
702 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
704 Interop.Layer.DeleteLayer(swigCPtr);
707 private void SetBehavior(LayerBehavior behavior)
709 Interop.Layer.SetBehavior(SwigCPtr, (int)behavior);
710 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
713 private LayerBehavior GetBehavior()
715 Layer.LayerBehavior ret = (Layer.LayerBehavior)Interop.Layer.GetBehavior(SwigCPtr);
716 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
720 internal class Property
722 internal static readonly int BEHAVIOR = Interop.Layer.BehaviorGet();