2 * Copyright(c) 2018 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 System.Runtime.InteropServices;
26 /// Layers provide a mechanism for overlaying groups of actors on top of each other.
28 /// <since_tizen> 3 </since_tizen>
29 public class Layer : Container
31 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
32 private global::System.IntPtr rootLayoutIntPtr;
33 private global::System.Runtime.InteropServices.HandleRef rootLayoutCPtr;
35 internal Layer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Layer_SWIGUpcast(cPtr), cMemoryOwn)
37 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
38 // Create a root layout (AbsoluteLayout) that is invisible to the user but enables layouts added to this Layer.
39 // Enables layouts added to the Layer to have a parent layout. As parent layout is needed to store measure spec properties.
40 rootLayoutIntPtr = NDalicManualPINVOKE.Window_NewRootLayout();
41 // Store HandleRef used by Add()
42 rootLayoutCPtr = new global::System.Runtime.InteropServices.HandleRef(this, rootLayoutIntPtr);
43 // Add the root layout created above to this layer.
44 NDalicPINVOKE.Actor_Add( swigCPtr, rootLayoutCPtr );
47 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Layer obj)
49 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
52 /// From the Container base class.
55 /// Adds a child view to this layer.
57 /// <seealso cref="Container.Add">
59 /// <since_tizen> 4 </since_tizen>
60 public override void Add(View child)
62 Container oldParent = child.Parent;
63 if (oldParent != this)
65 if (oldParent != null)
67 oldParent.Remove(child);
69 NDalicPINVOKE.Actor_Add( rootLayoutCPtr , View.getCPtr(child));
70 if (NDalicPINVOKE.SWIGPendingException.Pending)
71 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
77 /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
79 /// <seealso cref="Container.Remove">
81 /// <since_tizen> 4 </since_tizen>
82 public override void Remove(View child)
84 NDalicPINVOKE.Actor_Remove( rootLayoutCPtr, View.getCPtr(child));
85 if (NDalicPINVOKE.SWIGPendingException.Pending)
86 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
88 Children.Remove(child);
92 /// Retrieves a child view by the index.
94 /// <pre>The view has been initialized.</pre>
95 /// <param name="index">The index of the child to retrieve.</param>
96 /// <returns>The view for the given index or empty handle if children not initialized.</returns>
97 /// <since_tizen> 4 </since_tizen>
98 public override View GetChildAt(uint index)
100 if (index < Children.Count)
102 return Children[Convert.ToInt32(index)];
111 /// Get parent of the layer.
113 /// <returns>The view's container</returns>
114 /// <since_tizen> 4 </since_tizen>
115 public override Container GetParent()
121 /// Get the child count of the layer.
123 /// <returns>The child count of the layer.</returns>
124 /// <since_tizen> 4 </since_tizen>
125 public override uint GetChildCount()
127 return Convert.ToUInt32(Children.Count);
133 /// <since_tizen> 3 </since_tizen>
134 protected override void Dispose(DisposeTypes type)
141 if(type == DisposeTypes.Explicit)
144 //Release your own managed resources here.
145 //You should release all of your own disposable objects here.
148 //Release your own unmanaged resources here.
149 //You should not access any managed member here except static instance.
150 //because the execution order of Finalizes is non-deterministic.
152 if (swigCPtr.Handle != global::System.IntPtr.Zero)
157 NDalicPINVOKE.delete_Layer(swigCPtr);
159 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
167 internal class Property
169 internal static readonly int BEHAVIOR = NDalicPINVOKE.Layer_Property_BEHAVIOR_get();
173 /// Creates a Layer object.
175 /// <since_tizen> 3 </since_tizen>
176 public Layer() : this(NDalicPINVOKE.Layer_New(), true)
178 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179 if(Window.Instance != null)
181 this.SetAnchorPoint(Tizen.NUI.PivotPoint.TopLeft);
182 this.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.AllDimensions);
185 internal void SetAnchorPoint(Vector3 anchorPoint)
187 NDalicPINVOKE.Actor_SetAnchorPoint(swigCPtr, Vector3.getCPtr(anchorPoint));
188 if (NDalicPINVOKE.SWIGPendingException.Pending)
189 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191 internal void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
193 NDalicPINVOKE.Actor_SetResizePolicy(swigCPtr, (int)policy, (int)dimension);
194 if (NDalicPINVOKE.SWIGPendingException.Pending)
195 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
199 /// Downcasts a handle to layer handle.
201 /// <since_tizen> 3 </since_tizen>
202 /// Please do not use! this will be deprecated!
203 /// Instead please use as keyword.
204 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 public static Layer DownCast(BaseHandle handle)
208 Layer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
209 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214 /// Search through this layer's hierarchy for a view with the given unique ID.
216 /// <pre>This layer (the parent) has been initialized.</pre>
217 /// <remarks>The actor itself is also considered in the search.</remarks>
218 /// <param name="id">The id of the child to find</param>
219 /// <returns> A handle to the view if found, or an empty handle if not. </returns>
220 /// <since_tizen> 3 </since_tizen>
221 public View FindChildById(uint id)
223 //to fix memory leak issue, match the handle count with native side.
224 IntPtr cPtr = NDalicPINVOKE.Actor_FindChildById(swigCPtr, id);
225 HandleRef CPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
226 View ret = Registry.GetManagedBaseHandleFromNativePtr(CPtr.Handle) as View;
227 NDalicPINVOKE.delete_BaseHandle(CPtr);
228 CPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
230 if (NDalicPINVOKE.SWIGPendingException.Pending)
231 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236 /// Queries the depth of the layer.<br />
237 /// 0 is the bottommost layer, higher number is on the top.<br />
239 /// <since_tizen> 3 </since_tizen>
248 internal uint GetDepth()
250 var parentChildren = Window.Instance.LayersChildren;
251 if(parentChildren != null)
253 int idx = parentChildren.IndexOf(this);
256 return Convert.ToUInt32(idx); ;
263 /// Increments the depth of the layer.
265 /// <since_tizen> 3 </since_tizen>
268 var parentChildren = Window.Instance.LayersChildren;
269 if (parentChildren != null)
271 int currentIdx = parentChildren.IndexOf(this);
273 if (currentIdx >= 0 && currentIdx < parentChildren.Count - 1)
275 RaiseAbove(parentChildren[currentIdx + 1]);
277 Layer temp = parentChildren[currentIdx + 1];
278 parentChildren[currentIdx + 1] = this;
279 parentChildren[currentIdx] = temp;
281 NDalicPINVOKE.Layer_Raise(swigCPtr);
282 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
288 /// Decrements the depth of the layer.
290 /// <since_tizen> 3 </since_tizen>
293 var parentChildren = Window.Instance.LayersChildren;
294 if (parentChildren != null)
296 int currentIdx = parentChildren.IndexOf(this);
298 if (currentIdx > 0 && currentIdx < parentChildren.Count)
300 LowerBelow(parentChildren[currentIdx - 1]);
302 Layer temp = parentChildren[currentIdx - 1];
303 parentChildren[currentIdx - 1] = this;
304 parentChildren[currentIdx] = temp;
306 NDalicPINVOKE.Layer_Lower(swigCPtr);
307 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
312 internal void RaiseAbove(Layer target)
314 var parentChildren = Window.Instance.LayersChildren;
315 if (parentChildren != null)
317 int currentIndex = parentChildren.IndexOf(this);
318 int targetIndex = parentChildren.IndexOf(target);
320 if(currentIndex < 0 || targetIndex < 0 ||
321 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
323 NUILog.Error("index should be bigger than 0 and less than children of layer count");
327 // If the currentIndex is less than the target index and the target has the same parent.
328 if (currentIndex < targetIndex)
330 parentChildren.Remove(this);
331 parentChildren.Insert(targetIndex, this);
333 NDalicPINVOKE.Layer_RaiseAbove(swigCPtr, Layer.getCPtr(target));
334 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
339 internal void LowerBelow(Layer target)
341 var parentChildren = Window.Instance.LayersChildren;
343 if (parentChildren != null)
345 int currentIndex = parentChildren.IndexOf(this);
346 int targetIndex = parentChildren.IndexOf(target);
348 if(currentIndex < 0 || targetIndex < 0 ||
349 currentIndex >= parentChildren.Count || targetIndex >= parentChildren.Count)
351 NUILog.Error("index should be bigger than 0 and less than children of layer count");
355 // If the currentIndex is not already the 0th index and the target has the same parent.
356 if ((currentIndex != 0) && (targetIndex != -1) &&
357 (currentIndex > targetIndex))
359 parentChildren.Remove(this);
360 parentChildren.Insert(targetIndex, this);
362 NDalicPINVOKE.Layer_LowerBelow(swigCPtr, Layer.getCPtr(target));
363 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
369 /// Raises the layer to the top.
371 /// <since_tizen> 3 </since_tizen>
372 public void RaiseToTop()
374 var parentChildren = Window.Instance.LayersChildren;
376 if (parentChildren != null)
378 parentChildren.Remove(this);
379 parentChildren.Add(this);
381 NDalicPINVOKE.Layer_RaiseToTop(swigCPtr);
382 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387 /// Lowers the layer to the bottom.
389 /// <since_tizen> 3 </since_tizen>
390 public void LowerToBottom()
392 var parentChildren = Window.Instance.LayersChildren;
394 if (parentChildren != null)
396 parentChildren.Remove(this);
397 parentChildren.Insert(0, this);
399 NDalicPINVOKE.Layer_LowerToBottom(swigCPtr);
400 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
406 /// Moves the layer directly above the given layer.<br />
407 /// After the call, this layer's depth will be immediately above target.<br />
409 /// <param name="target">The layer to get on top of.</param>
410 /// <since_tizen> 3 </since_tizen>
411 public void MoveAbove(Layer target)
413 NDalicPINVOKE.Layer_MoveAbove(swigCPtr, Layer.getCPtr(target));
414 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
418 /// Moves the layer directly below the given layer.<br />
419 /// After the call, this layer's depth will be immediately below target.<br />
421 /// <param name="target">The layer to get below of.</param>
422 /// <since_tizen> 3 </since_tizen>
423 public void MoveBelow(Layer target)
425 NDalicPINVOKE.Layer_MoveBelow(swigCPtr, Layer.getCPtr(target));
426 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429 private void SetBehavior(LayerBehavior behavior)
431 NDalicPINVOKE.Layer_SetBehavior(swigCPtr, (int)behavior);
432 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
435 private LayerBehavior GetBehavior()
437 Layer.LayerBehavior ret = (Layer.LayerBehavior)NDalicPINVOKE.Layer_GetBehavior(swigCPtr);
438 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
442 internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
444 NDalicPINVOKE.Layer_SetSortFunction(swigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
445 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
448 internal void SetTouchConsumed(bool consume)
450 NDalicPINVOKE.Layer_SetTouchConsumed(swigCPtr, consume);
451 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
454 internal bool IsTouchConsumed()
456 bool ret = NDalicPINVOKE.Layer_IsTouchConsumed(swigCPtr);
457 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
461 internal void SetHoverConsumed(bool consume)
463 NDalicPINVOKE.Layer_SetHoverConsumed(swigCPtr, consume);
464 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
467 internal bool IsHoverConsumed()
469 bool ret = NDalicPINVOKE.Layer_IsHoverConsumed(swigCPtr);
470 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
474 internal void AddViewToLayerList( View view )
479 internal void RemoveViewFromLayerList( View view )
481 Children.Remove(view);
485 /// Enumeration for the behavior of the layer.
487 /// <since_tizen> 3 </since_tizen>
488 public enum LayerBehavior
491 /// UI control rendering mode.
493 /// <since_tizen> 3 </since_tizen>
496 /// UI control rendering mode (default mode).
497 /// This mode is designed for UI controls that can overlap. In this
498 /// mode renderer order will be respective to the tree hierarchy of
500 /// The rendering order is depth first, so for the following actor tree,
501 /// A will be drawn first, then B, D, E, then C, F. This ensures that
502 /// overlapping actors are drawn as expected (whereas, with breadth first
503 /// traversal, the actors would interleave).<br />
505 /// <since_tizen> 3 </since_tizen>
508 /// Layer will use depth test.
509 /// This mode is designed for a 3 dimensional scene where actors in front
510 /// of other actors will obscure them, i.e. the actors are sorted by the
511 /// distance from the camera.<br />
512 /// When using this mode, a depth test will be used. A depth clear will
513 /// happen for each layer, which means actors in a layer "above" other
514 /// layers will be rendered in front of actors in those layers regardless
515 /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
516 /// Opaque renderers are drawn first and write to the depth buffer. Then
517 /// transparent renderers are drawn with depth test enabled but depth
518 /// write switched off. Transparent renderers are drawn based on their
519 /// distance from the camera. A renderer's DEPTH_INDEX property is used to
520 /// offset the distance to the camera when ordering transparent renderers.
521 /// This is useful if you want to define the draw order of two or more
522 /// transparent renderers that are equal distance from the camera. Unlike
523 /// LAYER_UI, parent-child relationship does not affect rendering order at
526 /// <since_tizen> 3 </since_tizen>
530 internal enum TreeDepthMultiplier
532 TREE_DEPTH_MULTIPLIER = 10000
536 /// Layer behavior, type String (Layer.LayerBehavior).
538 /// <since_tizen> 3 </since_tizen>
539 public Layer.LayerBehavior Behavior
543 return GetBehavior();
552 /// Internal only property to enable or disable clipping, type boolean.
553 /// By default, this is false, i.e., the viewport of the layer is the entire window.
555 internal bool ClippingEnabled
559 bool ret = NDalicPINVOKE.Layer_IsClipping(swigCPtr);
560 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
565 NDalicPINVOKE.Layer_SetClipping(swigCPtr, value);
566 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
571 /// Sets the viewport (in window coordinates), type rectangle.
572 /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
574 /// <since_tizen> 4 </since_tizen>
575 public Rectangle Viewport
579 if( ClippingEnabled )
581 Rectangle ret = new Rectangle(NDalicPINVOKE.Layer_GetClippingBox(swigCPtr), true);
582 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
587 // Clipping not enabled so return the window size
588 Size2D windowSize = Window.Instance.Size;
589 Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
595 NDalicPINVOKE.Layer_SetClippingBox__SWIG_1(swigCPtr, Rectangle.getCPtr(value));
596 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
597 ClippingEnabled = true;
602 /// Retrieves and sets the layer's opacity.<br />
604 /// <since_tizen> 3 </since_tizen>
610 GetProperty(View.Property.OPACITY).Get(out temp);
615 SetProperty(View.Property.OPACITY, new Tizen.NUI.PropertyValue(value));
620 /// Retrieves and sets the layer's visibility.
622 /// <since_tizen> 3 </since_tizen>
623 public bool Visibility
628 GetProperty(View.Property.VISIBLE).Get(out temp);
633 SetProperty(View.Property.VISIBLE, new Tizen.NUI.PropertyValue(value));
638 /// Get the number of children held by the layer.
640 /// <since_tizen> 3 </since_tizen>
641 public new uint ChildCount
645 return Convert.ToUInt32(Children.Count);
650 /// Gets or sets the layer's name.
652 /// <since_tizen> 3 </since_tizen>
665 internal string GetName()
667 string ret = NDalicPINVOKE.Actor_GetName(swigCPtr);
668 if (NDalicPINVOKE.SWIGPendingException.Pending)
669 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
673 internal void SetName(string name)
675 NDalicPINVOKE.Actor_SetName(swigCPtr, name);
676 if (NDalicPINVOKE.SWIGPendingException.Pending)
677 throw NDalicPINVOKE.SWIGPendingException.Retrieve();