[NUI] temporary fix! fix wrong value of screen position in object dump
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layer.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI
22 {
23
24     /// <summary>
25     /// Layers provide a mechanism for overlaying groups of actors on top of each other.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class Layer : Container
29     {
30         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
31
32         internal Layer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Layer_SWIGUpcast(cPtr), cMemoryOwn)
33         {
34             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
35         }
36
37         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Layer obj)
38         {
39             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
40         }
41
42         /// From the Container base class.
43
44         /// <summary>
45         /// Adds a child view to this layer.
46         /// </summary>
47         /// <seealso cref="Container.Add">
48         /// </seealso>
49         /// <since_tizen> 4 </since_tizen>
50         public override void Add(View child)
51         {
52             NDalicPINVOKE.Actor_Add(swigCPtr, View.getCPtr(child));
53             if (NDalicPINVOKE.SWIGPendingException.Pending)
54                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
55
56             Children.Add(child);
57         }
58
59         /// <summary>
60         /// Removes a child view from this layer. If the view was not a child of this layer, this is a no-op.
61         /// </summary>
62         /// <seealso cref="Container.Remove">
63         /// </seealso>
64         /// <since_tizen> 4 </since_tizen>
65         public override void Remove(View child)
66         {
67             NDalicPINVOKE.Actor_Remove(swigCPtr, View.getCPtr(child));
68             if (NDalicPINVOKE.SWIGPendingException.Pending)
69                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
70
71             Children.Remove(child);
72         }
73
74         /// <summary>
75         /// Retrieves a child view by the index.
76         /// </summary>
77         /// <pre>The view has been initialized.</pre>
78         /// <param name="index">The index of the child to retrieve.</param>
79         /// <returns>The view for the given index or empty handle if children not initialized.</returns>
80         /// <since_tizen> 4 </since_tizen>
81         public override View GetChildAt(uint index)
82         {
83             if (index < Children.Count)
84             {
85                 return Children[Convert.ToInt32(index)];
86             }
87             else
88             {
89                 return null;
90             }
91         }
92
93
94         /// <summary>
95         /// Get parent of the layer.
96         /// </summary>
97         /// <returns>The view's container</returns>
98         /// <since_tizen> 4 </since_tizen>
99         public override Container GetParent()
100         {
101             return null;
102         }
103
104         /// <summary>
105         /// Get the child count of the layer.
106         /// </summary>
107         /// <returns>The child count of the layer.</returns>
108         /// <since_tizen> 4 </since_tizen>
109         public override uint GetChildCount()
110         {
111             return Convert.ToUInt32(Children.Count);
112         }
113
114         /// <summary>
115         /// Dispose.
116         /// </summary>
117         /// <since_tizen> 3 </since_tizen>
118         protected override void Dispose(DisposeTypes type)
119         {
120             if(disposed)
121             {
122                 return;
123             }
124
125             if(type == DisposeTypes.Explicit)
126             {
127                 //Called by User
128                 //Release your own managed resources here.
129                 //You should release all of your own disposable objects here.
130             }
131
132             //Release your own unmanaged resources here.
133             //You should not access any managed member here except static instance.
134             //because the execution order of Finalizes is non-deterministic.
135
136             if (swigCPtr.Handle != global::System.IntPtr.Zero)
137             {
138                 if (swigCMemOwn)
139                 {
140                     swigCMemOwn = false;
141                     NDalicPINVOKE.delete_Layer(swigCPtr);
142                 }
143                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
144             }
145
146             base.Dispose(type);
147
148         }
149
150
151         internal class Property
152         {
153             internal static readonly int BEHAVIOR = NDalicPINVOKE.Layer_Property_BEHAVIOR_get();
154         }
155
156         /// <summary>
157         /// Creates a Layer object.
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         public Layer() : this(NDalicPINVOKE.Layer_New(), true)
161         {
162             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163             if(Window.Instance != null)
164             {
165                 this.SetParentOrigin(Tizen.NUI.ParentOrigin.TopLeft);
166                 this.SetAnchorPoint(Tizen.NUI.PivotPoint.TopLeft);
167                 this.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.AllDimensions);
168             }
169         }
170         internal void SetAnchorPoint(Vector3 anchorPoint)
171         {
172             NDalicPINVOKE.Actor_SetAnchorPoint(swigCPtr, Vector3.getCPtr(anchorPoint));
173             if (NDalicPINVOKE.SWIGPendingException.Pending)
174                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
175         }
176         internal void SetResizePolicy(ResizePolicyType policy, DimensionType dimension)
177         {
178             NDalicPINVOKE.Actor_SetResizePolicy(swigCPtr, (int)policy, (int)dimension);
179             if (NDalicPINVOKE.SWIGPendingException.Pending)
180                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
181         }
182         internal void SetParentOrigin(Vector3 origin)
183         {
184             NDalicPINVOKE.Actor_SetParentOrigin(swigCPtr, Vector3.getCPtr(origin));
185             if (NDalicPINVOKE.SWIGPendingException.Pending)
186                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187         }
188
189         /// <summary>
190         /// Downcasts a handle to layer handle.
191         /// </summary>
192         /// <since_tizen> 3 </since_tizen>
193         /// Please do not use! this will be deprecated!
194         /// Instead please use as keyword.
195         [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
196         [EditorBrowsable(EditorBrowsableState.Never)]
197         public static Layer DownCast(BaseHandle handle)
198         {
199             Layer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Layer;
200             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201             return ret;
202         }
203
204         /// <summary>
205         /// Search through this layer's hierarchy for a view with the given unique ID.
206         /// </summary>
207         /// <pre>This layer (the parent) has been initialized.</pre>
208         /// <remarks>The actor itself is also considered in the search.</remarks>
209         /// <param name="id">The id of the child to find</param>
210         /// <returns> A handle to the view if found, or an empty handle if not. </returns>
211         /// <since_tizen> 3 </since_tizen>
212         public View FindChildById(uint id)
213         {
214             IntPtr cPtr = NDalicPINVOKE.Actor_FindChildById(swigCPtr, id);
215             View ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as View;
216
217             if (NDalicPINVOKE.SWIGPendingException.Pending)
218                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
219             return ret;
220         }
221
222         /// <summary>
223         /// Queries the depth of the layer.<br />
224         /// 0 is the bottommost layer, higher number is on the top.<br />
225         /// </summary>
226         /// <since_tizen> 3 </since_tizen>
227         public uint Depth
228         {
229             get
230             {
231                 return GetDepth();
232             }
233         }
234
235         internal uint GetDepth()
236         {
237             var parentChildren = Window.Instance.LayersChildren;
238             if(parentChildren != null)
239             {
240                 int idx = parentChildren.IndexOf(this);
241                 if (idx >= 0)
242                 {
243                     return Convert.ToUInt32(idx); ;
244                 }
245             }
246             return 0u;
247         }
248
249         /// <summary>
250         /// Increments the depth of the layer.
251         /// </summary>
252         /// <since_tizen> 3 </since_tizen>
253         public void Raise()
254         {
255             var parentChildren = Window.Instance.LayersChildren;
256             if (parentChildren != null)
257             {
258                 int currentIdx = parentChildren.IndexOf(this);
259
260                 if (currentIdx != parentChildren.Count - 1)
261                 {
262                     RaiseAbove(parentChildren[currentIdx + 1]);
263
264                     Layer temp = parentChildren[currentIdx + 1];
265                     parentChildren[currentIdx + 1] = this;
266                     parentChildren[currentIdx] = temp;
267                 }
268             }
269         }
270
271         /// <summary>
272         /// Decrements the depth of the layer.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         public void Lower()
276         {
277             var parentChildren = Window.Instance.LayersChildren;
278             if (parentChildren != null)
279             {
280                 int currentIdx = parentChildren.IndexOf(this);
281
282                 if (currentIdx > 0)
283                 {
284                     LowerBelow(parentChildren[currentIdx - 1]);
285
286                     Layer temp = parentChildren[currentIdx - 1];
287                     parentChildren[currentIdx - 1] = this;
288                     parentChildren[currentIdx] = temp;
289
290                 }
291             }
292         }
293
294         internal void RaiseAbove(Layer target)
295         {
296             NDalicPINVOKE.Layer_RaiseAbove(swigCPtr, Layer.getCPtr(target));
297             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
298         }
299
300         internal void LowerBelow(Layer target)
301         {
302             NDalicPINVOKE.Layer_LowerBelow(swigCPtr, Layer.getCPtr(target));
303             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
304         }
305
306         /// <summary>
307         /// Raises the layer to the top.
308         /// </summary>
309         /// <since_tizen> 3 </since_tizen>
310         public void RaiseToTop()
311         {
312             var parentChildren = Window.Instance.LayersChildren;
313
314             if (parentChildren != null)
315             {
316                 parentChildren.Remove(this);
317                 parentChildren.Add(this);
318             }
319             NDalicPINVOKE.Layer_RaiseToTop(swigCPtr);
320             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
321         }
322
323         /// <summary>
324         /// Lowers the layer to the bottom.
325         /// </summary>
326         /// <since_tizen> 3 </since_tizen>
327         public void LowerToBottom()
328         {
329             var parentChildren = Window.Instance.LayersChildren;
330
331             if (parentChildren != null)
332             {
333                 parentChildren.Remove(this);
334                 parentChildren.Insert(0, this);
335             }
336
337             NDalicPINVOKE.Layer_LowerToBottom(swigCPtr);
338             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
339         }
340
341         /// <summary>
342         /// Moves the layer directly above the given layer.<br />
343         /// After the call, this layer's depth will be immediately above target.<br />
344         /// </summary>
345         /// <param name="target">The layer to get on top of.</param>
346         /// <since_tizen> 3 </since_tizen>
347         public void MoveAbove(Layer target)
348         {
349             NDalicPINVOKE.Layer_MoveAbove(swigCPtr, Layer.getCPtr(target));
350             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
351         }
352
353         /// <summary>
354         /// Moves the layer directly below the given layer.<br />
355         /// After the call, this layer's depth will be immediately below target.<br />
356         /// </summary>
357         /// <param name="target">The layer to get below of.</param>
358         /// <since_tizen> 3 </since_tizen>
359         public void MoveBelow(Layer target)
360         {
361             NDalicPINVOKE.Layer_MoveBelow(swigCPtr, Layer.getCPtr(target));
362             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
363         }
364
365         private void SetBehavior(LayerBehavior behavior)
366         {
367             NDalicPINVOKE.Layer_SetBehavior(swigCPtr, (int)behavior);
368             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
369         }
370
371         private LayerBehavior GetBehavior()
372         {
373             Layer.LayerBehavior ret = (Layer.LayerBehavior)NDalicPINVOKE.Layer_GetBehavior(swigCPtr);
374             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375             return ret;
376         }
377
378         internal void SetSortFunction(SWIGTYPE_p_f_r_q_const__Dali__Vector3__float function)
379         {
380             NDalicPINVOKE.Layer_SetSortFunction(swigCPtr, SWIGTYPE_p_f_r_q_const__Dali__Vector3__float.getCPtr(function));
381             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
382         }
383
384         internal void SetTouchConsumed(bool consume)
385         {
386             NDalicPINVOKE.Layer_SetTouchConsumed(swigCPtr, consume);
387             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
388         }
389
390         internal bool IsTouchConsumed()
391         {
392             bool ret = NDalicPINVOKE.Layer_IsTouchConsumed(swigCPtr);
393             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394             return ret;
395         }
396
397         internal void SetHoverConsumed(bool consume)
398         {
399             NDalicPINVOKE.Layer_SetHoverConsumed(swigCPtr, consume);
400             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401         }
402
403         internal bool IsHoverConsumed()
404         {
405             bool ret = NDalicPINVOKE.Layer_IsHoverConsumed(swigCPtr);
406             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
407             return ret;
408         }
409
410         /// <summary>
411         /// Enumeration for the behavior of the layer.
412         /// </summary>
413         /// <since_tizen> 3 </since_tizen>
414         public enum LayerBehavior
415         {
416             /// <summary>
417             /// UI control rendering mode.
418             /// </summary>
419             /// <since_tizen> 3 </since_tizen>
420             Layer2D,
421             /// <summary>
422             /// UI control rendering mode (default mode).
423             /// This mode is designed for UI controls that can overlap. In this
424             /// mode renderer order will be respective to the tree hierarchy of
425             /// Actors.<br />
426             /// The rendering order is depth first, so for the following actor tree,
427             /// A will be drawn first, then B, D, E, then C, F.  This ensures that
428             /// overlapping actors are drawn as expected (whereas, with breadth first
429             /// traversal, the actors would interleave).<br />
430             /// </summary>
431             /// <since_tizen> 3 </since_tizen>
432             LayerUI = Layer2D,
433             /// <summary>
434             /// Layer will use depth test.
435             /// This mode is designed for a 3 dimensional scene where actors in front
436             /// of other actors will obscure them, i.e. the actors are sorted by the
437             /// distance from the camera.<br />
438             /// When using this mode, a depth test will be used. A depth clear will
439             /// happen for each layer, which means actors in a layer "above" other
440             /// layers will be rendered in front of actors in those layers regardless
441             /// of their Z positions (see Layer::Raise() and Layer::Lower()).<br />
442             /// Opaque renderers are drawn first and write to the depth buffer.  Then
443             /// transparent renderers are drawn with depth test enabled but depth
444             /// write switched off.  Transparent renderers are drawn based on their
445             /// distance from the camera.  A renderer's DEPTH_INDEX property is used to
446             /// offset the distance to the camera when ordering transparent renderers.
447             /// This is useful if you want to define the draw order of two or more
448             /// transparent renderers that are equal distance from the camera.  Unlike
449             /// LAYER_UI, parent-child relationship does not affect rendering order at
450             /// all.
451             /// </summary>
452             /// <since_tizen> 3 </since_tizen>
453             Layer3D
454         }
455
456         internal enum TreeDepthMultiplier
457         {
458             TREE_DEPTH_MULTIPLIER = 10000
459         }
460
461         /// <summary>
462         /// Layer behavior, type String (Layer.LayerBehavior).
463         /// </summary>
464         /// <since_tizen> 3 </since_tizen>
465         public Layer.LayerBehavior Behavior
466         {
467             get
468             {
469                 return GetBehavior();
470             }
471             set
472             {
473                 SetBehavior(value);
474             }
475         }
476
477         /// <summary>
478         /// Internal only property to enable or disable clipping, type boolean.
479         /// By default, this is false, i.e., the viewport of the layer is the entire window.
480         /// </summary>
481         internal bool ClippingEnabled
482         {
483             get
484             {
485                 bool ret = NDalicPINVOKE.Layer_IsClipping(swigCPtr);
486                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
487                 return ret;
488             }
489             set
490             {
491                 NDalicPINVOKE.Layer_SetClipping(swigCPtr, value);
492                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
493             }
494         }
495
496         /// <summary>
497         /// Sets the viewport (in window coordinates), type rectangle.
498         /// The contents of the layer will not be visible outside this box, when ViewportEnabled is true.
499         /// </summary>
500         /// <since_tizen> 4 </since_tizen>
501         public Rectangle Viewport
502         {
503             get
504             {
505                 if( ClippingEnabled )
506                 {
507                   Rectangle ret = new Rectangle(NDalicPINVOKE.Layer_GetClippingBox(swigCPtr), true);
508                   if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
509                   return ret;
510                 }
511                 else
512                 {
513                   // Clipping not enabled so return the window size
514                   Size2D windowSize = Window.Instance.Size;
515                   Rectangle ret = new Rectangle(0, 0, windowSize.Width, windowSize.Height);
516                   return ret;
517                 }
518             }
519             set
520             {
521                 NDalicPINVOKE.Layer_SetClippingBox__SWIG_1(swigCPtr, Rectangle.getCPtr(value));
522                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
523                 ClippingEnabled = true;
524             }
525         }
526
527         /// <summary>
528         /// Retrieves and sets the layer's opacity.<br />
529         /// </summary>
530         /// <since_tizen> 3 </since_tizen>
531         public float Opacity
532         {
533             get
534             {
535                 float temp = 0.0f;
536                 GetProperty(View.Property.OPACITY).Get(out temp);
537                 return temp;
538             }
539             set
540             {
541                 SetProperty(View.Property.OPACITY, new Tizen.NUI.PropertyValue(value));
542             }
543         }
544
545         /// <summary>
546         /// Retrieves and sets the layer's visibility.
547         /// </summary>
548         /// <since_tizen> 3 </since_tizen>
549         public bool Visibility
550         {
551             get
552             {
553                 bool temp = false;
554                 GetProperty(View.Property.VISIBLE).Get(out temp);
555                 return temp;
556             }
557             set
558             {
559                 SetProperty(View.Property.VISIBLE, new Tizen.NUI.PropertyValue(value));
560             }
561         }
562
563         /// <summary>
564         /// Get the number of children held by the layer.
565         /// </summary>
566         /// <since_tizen> 3 </since_tizen>
567         public new uint ChildCount
568         {
569             get
570             {
571                 return Convert.ToUInt32(Children.Count);
572             }
573         }
574
575         /// <summary>
576         /// Gets or sets the layer's name.
577         /// </summary>
578         /// <since_tizen> 3 </since_tizen>
579         public string Name
580         {
581             get
582             {
583                 return GetName();
584             }
585             set
586             {
587                 SetName(value);
588             }
589         }
590
591         internal string GetName()
592         {
593             string ret = NDalicPINVOKE.Actor_GetName(swigCPtr);
594             if (NDalicPINVOKE.SWIGPendingException.Pending)
595                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
596             return ret;
597         }
598
599         internal void SetName(string name)
600         {
601             NDalicPINVOKE.Actor_SetName(swigCPtr, name);
602             if (NDalicPINVOKE.SWIGPendingException.Pending)
603                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
604         }
605
606     }
607 }