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(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 /// Layer will use depth test.
68 /// This mode is designed for a 3 dimensional scene where actors in front
69 /// of other actors will obscure them, i.e. the actors are sorted by the
70 /// distance from the camera.<br />
71 /// When using this mode, a depth test will be used. A depth clear will
72 /// happen for each layer, which means actors in a layer "above" other
73 /// layers will be rendered in front of actors in those layers regardless
74 /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
75 /// Opaque renderers are drawn first and write to the depth buffer. Then
76 /// transparent renderers are drawn with depth test enabled but depth
77 /// write switched off. Transparent renderers are drawn based on their
78 /// distance from the camera. A renderer's DEPTH_INDEX property is used to
79 /// offset the distance to the camera when ordering transparent renderers.
80 /// This is useful if you want to define the draw order of two or more
81 /// transparent renderers that are equal distance from the camera. Unlike
82 /// LAYER_UI, parent-child relationship does not affect rendering order at
85 /// <since_tizen> 3 </since_tizen>
89 internal enum TreeDepthMultiplier
91 TREE_DEPTH_MULTIPLIER = 10000
95 /// Layer behavior, type String (Layer.LayerBehavior).
97 /// <since_tizen> 3 </since_tizen>
98 public Layer.LayerBehavior Behavior
102 return GetBehavior();
111 /// Sets the viewport (in window coordinates), type rectangle.
112 /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
114 /// <since_tizen> 4 </since_tizen>
115 public Rectangle Viewport
121 Rectangle ret = new Rectangle(Interop.Layer.GetClippingBox(SwigCPtr), true);
122 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
127 // Clipping not enabled so return the window size
128 Size2D windowSize = window?.Size;
129 Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
135 Interop.Layer.SetClippingBox(SwigCPtr, Rectangle.getCPtr(value));
136 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
137 ClippingEnabled = true;
142 /// Retrieves and sets the layer's opacity.<br />
144 /// <since_tizen> 3 </since_tizen>
150 var pValue = GetProperty(View.Property.OPACITY);
151 pValue.Get(out temp);
157 var temp = new Tizen.NUI.PropertyValue(value);
158 SetProperty(View.Property.OPACITY, temp);
164 /// Retrieves and sets the layer's visibility.
166 /// <since_tizen> 3 </since_tizen>
167 public bool Visibility
172 var pValue = GetProperty(View.Property.VISIBLE);
173 pValue.Get(out temp);
179 var temp = new Tizen.NUI.PropertyValue(value);
180 SetProperty(View.Property.VISIBLE, temp);
186 /// Get the number of children held by the layer.
188 /// <since_tizen> 3 </since_tizen>
189 public new uint ChildCount
193 return Convert.ToUInt32(Children.Count);
198 /// Gets or sets the layer's name.
200 /// <since_tizen> 3 </since_tizen>
214 /// Queries the depth of the layer.<br />
215 /// 0 is the bottommost layer, higher number is on the top.<br />
217 /// <since_tizen> 3 </since_tizen>
227 /// Internal only property to enable or disable clipping, type boolean.
228 /// By default, this is false, i.e., the viewport of the layer is the entire window.
230 internal bool ClippingEnabled
234 bool ret = Interop.Layer.IsClipping(SwigCPtr);
235 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
240 Interop.Layer.SetClipping(SwigCPtr, value);
241 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
245 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
246 [EditorBrowsable(EditorBrowsableState.Never)]
247 public ResourceDictionary XamlResources
251 return Application.Current.XamlResources;
255 Application.Current.XamlResources = value;
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();
291 BindableObject.SetInheritedBindingContext(child, this?.BindingContext);
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;
317 /// Retrieves a child view by the index.
319 /// <pre>The view has been initialized.</pre>
320 /// <param name="index">The index of the child to retrieve.</param>
321 /// <returns>The view for the given index or empty handle if children not initialized.</returns>
322 /// <since_tizen> 4 </since_tizen>
323 public override View GetChildAt(uint index)
325 if (index < Children.Count)
327 return Children[Convert.ToInt32(index)];
336 /// Get parent of the layer.
338 /// <returns>The view's container</returns>
339 /// <since_tizen> 4 </since_tizen>
340 public override Container GetParent()
346 /// Get the child count of the layer.
348 /// <returns>The child count of the layer.</returns>
349 /// <since_tizen> 4 </since_tizen>
350 [Obsolete("Deprecated in API9, will be removed in API11. Please use ChildCount property instead!")]
351 public override uint GetChildCount()
353 return Convert.ToUInt32(Children.Count);
357 /// Downcasts a handle to layer handle.
359 /// <exception cref="ArgumentNullException"> Thrown when handle is null. </exception>
360 /// <since_tizen> 3 </since_tizen>
361 /// Please do not use! this will be deprecated!
362 /// Instead please use as keyword.
363 [Obsolete("Please do not use! This will be deprecated! Please 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 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Layer obj)
509 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
512 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
513 [EditorBrowsable(EditorBrowsableState.Never)]
514 public void SetAnchorPoint(Vector3 anchorPoint)
516 Interop.Actor.SetAnchorPoint(SwigCPtr, Vector3.getCPtr(anchorPoint));
517 if (NDalicPINVOKE.SWIGPendingException.Pending)
518 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
521 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
522 [EditorBrowsable(EditorBrowsableState.Never)]
523 public void SetSize(float width, float height)
525 Interop.ActorInternal.SetSize(SwigCPtr, width, height);
526 if (NDalicPINVOKE.SWIGPendingException.Pending)
527 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
530 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
531 [EditorBrowsable(EditorBrowsableState.Never)]
532 public void SetParentOrigin(Vector3 parentOrigin)
534 Interop.ActorInternal.SetParentOrigin(SwigCPtr, Vector3.getCPtr(parentOrigin));
535 if (NDalicPINVOKE.SWIGPendingException.Pending)
536 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
539 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
540 [EditorBrowsable(EditorBrowsableState.Never)]
541 public void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
543 Interop.Actor.SetResizePolicy(SwigCPtr, (int)policy, (int)dimension);
544 if (NDalicPINVOKE.SWIGPendingException.Pending)
545 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
550 /// This allows the user to specify whether this layer should consume touch (including gestures).
551 /// If set, any layers behind this layer will not be hit-test.
553 /// <param name="consume">Whether the layer should consume touch (including gestures).</param>
554 [EditorBrowsable(EditorBrowsableState.Never)]
555 public void SetTouchConsumed(bool consume)
557 Interop.Layer.SetTouchConsumed(SwigCPtr, consume);
558 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
563 /// This allows the user to specify whether this layer should consume hover.
564 /// If set, any layers behind this layer will not be hit-test.
566 /// <param name="consume">Whether the layer should consume hover</param>
567 [EditorBrowsable(EditorBrowsableState.Never)]
568 public void SetHoverConsumed(bool consume)
570 Interop.Layer.SetHoverConsumed(SwigCPtr, consume);
571 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
574 internal uint GetDepth()
576 var parentChildren = window?.LayersChildren;
577 if (parentChildren != null)
579 int idx = parentChildren.IndexOf(this);
582 return Convert.ToUInt32(idx); ;
587 internal void RaiseAbove(Layer target)
589 var parentChildren = window?.LayersChildren;
590 if (parentChildren != null)
592 int currentIndex = parentChildren.IndexOf(this);
593 int targetIndex = parentChildren.IndexOf(target);
595 if (currentIndex < 0 || targetIndex < 0 ||
596 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
598 NUILog.Error("index should be bigger than 0 and less than children of layer count");
602 // If the currentIndex is less than the target index and the target has the same parent.
603 if (currentIndex < targetIndex)
605 parentChildren.Remove(this);
606 parentChildren.Insert(targetIndex, this);
608 Interop.Layer.MoveAbove(SwigCPtr, Layer.getCPtr(target));
609 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
614 internal void LowerBelow(Layer target)
616 var parentChildren = window?.LayersChildren;
618 if (parentChildren != null)
620 int currentIndex = parentChildren.IndexOf(this);
621 int targetIndex = parentChildren.IndexOf(target);
623 if (currentIndex < 0 || targetIndex < 0 ||
624 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
626 NUILog.Error("index should be bigger than 0 and less than children of layer count");
630 // If the currentIndex is not already the 0th index and the target has the same parent.
631 if ((currentIndex != 0) && (targetIndex != -1) &&
632 (currentIndex > targetIndex))
634 parentChildren.Remove(this);
635 parentChildren.Insert(targetIndex, this);
637 Interop.Layer.MoveBelow(SwigCPtr, Layer.getCPtr(target));
638 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
643 internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
645 Interop.Layer.SetSortFunction(SwigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
646 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
649 internal bool IsTouchConsumed()
651 bool ret = Interop.Layer.IsTouchConsumed(SwigCPtr);
652 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
655 internal bool IsHoverConsumed()
657 bool ret = Interop.Layer.IsHoverConsumed(SwigCPtr);
658 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
662 internal void AddViewToLayerList(View view)
667 internal void RemoveViewFromLayerList(View view)
669 Children.Remove(view);
672 internal string GetName()
674 string ret = Interop.Actor.GetName(SwigCPtr);
675 if (NDalicPINVOKE.SWIGPendingException.Pending)
676 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
680 internal void SetName(string name)
682 Interop.Actor.SetName(SwigCPtr, name);
683 if (NDalicPINVOKE.SWIGPendingException.Pending)
684 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
687 internal void SetWindow(Window win)
692 /// This will not be public opened.
693 [EditorBrowsable(EditorBrowsableState.Never)]
694 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
696 Interop.Layer.DeleteLayer(swigCPtr);
699 private void SetBehavior(LayerBehavior behavior)
701 Interop.Layer.SetBehavior(SwigCPtr, (int)behavior);
702 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
705 private LayerBehavior GetBehavior()
707 Layer.LayerBehavior ret = (Layer.LayerBehavior)Interop.Layer.GetBehavior(SwigCPtr);
708 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
712 internal class Property
714 internal static readonly int BEHAVIOR = Interop.Layer.BehaviorGet();