[Tizen] Scene Graph support in NUI 08/153508/1
authorUmar <m.umar@partner.samsung.com>
Mon, 4 Sep 2017 20:01:04 +0000 (21:01 +0100)
committerJinho, Lee <jeano.lee@samsung.com>
Thu, 28 Sep 2017 14:08:45 +0000 (23:08 +0900)
The RSS was increased 0.3% from 519254 to 521274 for 50,000 views.
Change-Id: Ib739b406af034e14d8193785b52a5d17f570e4c7

Tizen.NUI/src/public/BaseComponents/View.cs
Tizen.NUI/src/public/Container.cs
Tizen.NUI/src/public/Layer.cs
Tizen.NUI/src/public/Window.cs

index 1002e87..286bcf9 100755 (executable)
@@ -27,6 +27,7 @@ namespace Tizen.NUI.BaseComponents
     {
         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
 
+
         internal View(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.View_SWIGUpcast(cPtr), cMemoryOwn)
         {
             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
@@ -50,6 +51,8 @@ namespace Tizen.NUI.BaseComponents
             NDalicPINVOKE.Actor_Add(swigCPtr, View.getCPtr(child));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            Children.Add(child);
         }
 
         /// <summary>
@@ -62,6 +65,8 @@ namespace Tizen.NUI.BaseComponents
             NDalicPINVOKE.Actor_Remove(swigCPtr, View.getCPtr(child));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            Children.Remove(child);
         }
 
         /// <summary>
@@ -71,13 +76,14 @@ namespace Tizen.NUI.BaseComponents
         /// </seealso>
         public override View GetChildAt(uint index)
         {
-            IntPtr cPtr = NDalicPINVOKE.Actor_GetChildAt(swigCPtr, index);
-
-            View ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as View;
-
-            if (NDalicPINVOKE.SWIGPendingException.Pending)
-                throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-            return ret ?? null;
+            if (index < Children.Count)
+            {
+                return Children[Convert.ToInt32(index)];
+            }
+            else
+            {
+                return null;
+            }
         }
 
         /// <summary>
@@ -87,10 +93,7 @@ namespace Tizen.NUI.BaseComponents
         /// </seealso>
         protected override uint GetChildCount()
         {
-            uint ret = NDalicPINVOKE.Actor_GetChildCount(swigCPtr);
-            if (NDalicPINVOKE.SWIGPendingException.Pending)
-                throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-            return ret;
+            return Convert.ToUInt32(Children.Count);
         }
 
         /// <summary>
@@ -99,23 +102,14 @@ namespace Tizen.NUI.BaseComponents
         /// <seealso cref="Container::GetParent()">
         protected override Container GetParent()
         {
-            Container ret;
             IntPtr cPtr = NDalicPINVOKE.Actor_GetParent(swigCPtr);
 
             BaseHandle basehandle = Registry.GetManagedBaseHandleFromNativePtr(cPtr);
 
-            if(basehandle is Layer)
-            {
-                ret = basehandle as Layer;
-            }
-            else
-            {
-                ret = basehandle as View;
-            }
-
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-            return ret;
+
+            return basehandle as Container;
         }
 
         [Obsolete("This is temporal API. Currently Parent returns View but Container class has been introduced so 'View Parent' will be changed 'Container Parent' later soon, then this will be removed")]
@@ -2198,6 +2192,21 @@ namespace Tizen.NUI.BaseComponents
 
         internal void Raise()
         {
+            var parentChildren = Parent?.Children;
+
+            if (parentChildren != null)
+            {
+                int currentIndex = parentChildren.IndexOf(this);
+
+                // If the view is not already the last item in the list.
+                if (currentIndex != parentChildren.Count -1)
+                {
+                    View temp = parentChildren[currentIndex + 1];
+                    parentChildren[currentIndex + 1] = this;
+                    parentChildren[currentIndex] = temp;
+                }
+            }
+
             NDalicPINVOKE.Raise(swigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2205,6 +2214,21 @@ namespace Tizen.NUI.BaseComponents
 
         internal void Lower()
         {
+            var parentChildren = Parent?.Children;
+
+            if (parentChildren != null)
+            {
+                int currentIndex = parentChildren.IndexOf(this);
+
+                // If the view is not already the first item in the list.
+                if (currentIndex > 0)
+                {
+                    View temp = parentChildren[currentIndex - 1];
+                    parentChildren[currentIndex - 1] = this;
+                    parentChildren[currentIndex] = temp;
+                }
+            }
+
             NDalicPINVOKE.Lower(swigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2219,6 +2243,14 @@ namespace Tizen.NUI.BaseComponents
         /// </remarks>
         public void RaiseToTop()
         {
+            var parentChildren = Parent?.Children;
+
+            if (parentChildren != null)
+            {
+                parentChildren.Remove(this);
+                parentChildren.Add(this);
+            }
+
             NDalicPINVOKE.RaiseToTop(swigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2233,6 +2265,14 @@ namespace Tizen.NUI.BaseComponents
         /// </remarks>
         public void LowerToBottom()
         {
+            var parentChildren = Parent?.Children;
+
+            if (parentChildren != null)
+            {
+                parentChildren.Remove(this);
+                parentChildren.Insert(0, this);
+            }
+
             NDalicPINVOKE.LowerToBottom(swigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2261,6 +2301,21 @@ namespace Tizen.NUI.BaseComponents
         /// <param name="target">Will be raised above this view.</param>
         internal void RaiseAbove(View target)
         {
+            var parentChildren = Parent?.Children;
+
+            if (parentChildren != null)
+            {
+                int currentIndex = parentChildren.IndexOf(this);
+                int targetIndex = parentChildren.IndexOf(target);
+
+                // If the currentIndex is less than the target index and the target has the same parent.
+                if (currentIndex < targetIndex)
+                {
+                    parentChildren.Remove(this);
+                    parentChildren.Insert(targetIndex, this);
+                }
+            }
+
             NDalicPINVOKE.RaiseAbove(swigCPtr, View.getCPtr(target));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2276,7 +2331,23 @@ namespace Tizen.NUI.BaseComponents
         /// <param name="target">Will be lowered below this view.</param>
         internal void LowerBelow(View target)
         {
-            NDalicPINVOKE.RaiseAbove(swigCPtr, View.getCPtr(target));
+            var parentChildren = Parent?.Children;
+
+            if (parentChildren != null)
+            {
+                int currentIndex = parentChildren.IndexOf(this);
+                int targetIndex = parentChildren.IndexOf(target);
+
+                // If the currentIndex is not already the 0th index and the target has the same parent.
+                if ((currentIndex != 0) && (targetIndex != -1) &&
+                    (currentIndex > targetIndex))
+                {
+                    parentChildren.Remove(this);
+                    parentChildren.Insert(targetIndex, this);
+                }
+            }
+
+            NDalicPINVOKE.LowerBelow(swigCPtr, View.getCPtr(target));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
@@ -2335,9 +2406,7 @@ namespace Tizen.NUI.BaseComponents
         /// <pre>The (child) view has been initialized. </pre>
         public void Unparent()
         {
-            NDalicPINVOKE.Actor_Unparent(swigCPtr);
-            if (NDalicPINVOKE.SWIGPendingException.Pending)
-                throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            GetParent()?.Remove(this);
         }
 
         /// <summary>
index 1078d6a..a83f187 100755 (executable)
-/** Copyright (c) 2017 Samsung Electronics Co., Ltd.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-*/
-
-using System;
-using Tizen.NUI.BaseComponents;
-
-namespace Tizen.NUI
-{
-    /// <summary>
-    ///
-    /// The Container is an abstract class to be inherited from by classes that desire to have views
-    /// added to them.
-    ///
-    /// </summary>
-
-    public abstract class Container : Animatable
-    {
-
-        internal Container(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
-        {
-            // No un-managed data hence no need to store a native ptr
-        }
-
-        protected override void Dispose(DisposeTypes type)
-        {
-            if (disposed)
-            {
-                return;
-            }
-
-            base.Dispose(type);
-        }
-
-
-        /// <summary>
-        /// Adds a child view to this Container.
-        /// </summary>
-        /// <pre>This Container (the parent) has been initialized. The child view has been initialized. The child view is not the same as the parent view.</pre>
-        /// <post>The child will be referenced by its parent. This means that the child will be kept alive, even if the handle passed into this method is reset or destroyed.</post>
-        /// <remarks>If the child already has a parent, it will be removed from the old parent and reparented to this view. This may change child's position, color, scale, etc. as it now inherits them from this view.</remarks>
-        /// <param name="view">The child view to add.</param>
-        public abstract void Add( View view );
-
-        /// <summary>
-        /// Removes a child view from this view. If the view was not a child of this view, this is a no-op.
-        /// </summary>
-        /// <pre>This view (the parent) has been initialized. The child view is not the same as the parent view.</pre>
-        /// <param name="child">The child.</param>
-        public abstract void Remove( View view );
-
-        /// <summary>
-        /// Retrieves the child view by the index.
-        /// </summary>
-        /// <pre>The view has been initialized.</pre>
-        /// <param name="index">The index of the child to retrieve.</param>
-        /// <returns>The view for the given index or empty handle if children are not initialized.</returns>
-        public abstract View GetChildAt( uint index );
-
-        /// <summary>
-        /// Gets the parent of this container.
-        /// </summary>
-        /// <pre>The child container has been initialized.</pre>
-        /// <returns>The parent container.</returns>
-        protected abstract Container GetParent();
-
-        /// <summary>
-        /// Gets the number of children for this container.
-        /// </summary>
-        /// <pre>The container has been initialized.</pre>
-        /// <returns>The number of children.</returns>
-        protected abstract UInt32 GetChildCount();
-
-        /// <summary>
-        /// Gets the parent container.
-        /// Read only
-        /// </summary>
-        /// <pre>The child container has been initialized.</pre>
-        /// <returns>The parent container.</returns>
-        public Container Parent
-        {
-            get
-            {
-                return GetParent();
-            }
-        }
-
-        /// <summary>
-        /// Gets the number of children for this container.
-        /// Read only
-        /// </summary>
-        /// <pre>The container has been initialized.</pre>
-        /// <returns>The number of children.</returns>
-        public uint ChildCount
-        {
-            get
-            {
-                return GetChildCount();
-            }
-        }
-    }
+/** Copyright (c) 2017 Samsung Electronics Co., Ltd.\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*\r
+*/\r
+\r
+using System;\r
+using System.Collections.Generic;\r
+using Tizen.NUI.BaseComponents;\r
+\r
+namespace Tizen.NUI\r
+{\r
+    /// <summary>\r
+    ///\r
+    /// Container is an abstract class to be inherited from by classes that desire to have Views\r
+    /// added to them.\r
+    ///\r
+    /// </summary>\r
+\r
+    public abstract class Container : Animatable\r
+    {\r
+\r
+        private List<View> _childViews = new List<View>();\r
+\r
+        public List<View> Children\r
+        {\r
+            get\r
+            {\r
+                return _childViews;\r
+            }\r
+        }\r
+\r
+        internal Container(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)\r
+        {\r
+            // No un-managed data hence no need to store a native ptr\r
+        }\r
+\r
+        protected override void Dispose(DisposeTypes type)\r
+        {\r
+            if (disposed)\r
+            {\r
+                return;\r
+            }\r
+\r
+            base.Dispose(type);\r
+        }\r
+\r
+\r
+        /// <summary>\r
+        /// Adds a child view to this Container.\r
+        /// </summary>\r
+        /// <pre>This Container(the parent) has been initialized. The child view has been initialized. The child view is not the same as the parent view.</pre>\r
+        /// <post>The child will be referenced by its parent. This means that the child will be kept alive, even if the handle passed into this method is reset or destroyed.</post>\r
+        /// <remarks>If the child already has a parent, it will be removed from old parent and reparented to this view. This may change child's position, color, scale etc as it now inherits them from this view.</remarks>\r
+        /// <param name="view">The child view to add</param>\r
+        public abstract void Add( View view );\r
+\r
+        /// <summary>\r
+        /// Removes a child View from this View. If the view was not a child of this view, this is a no-op.\r
+        /// </summary>\r
+        /// <pre>This View(the parent) has been initialized. The child view is not the same as the parent view.</pre>\r
+        /// <param name="child">The child</param>\r
+        public abstract void Remove( View view );\r
+\r
+        /// <summary>\r
+        /// Retrieves child view by index.\r
+        /// </summary>\r
+        /// <pre>The View has been initialized.</pre>\r
+        /// <param name="index">The index of the child to retrieve</param>\r
+        /// <returns>The view for the given index or empty handle if children not initialized</returns>\r
+        public abstract View GetChildAt( uint index );\r
+\r
+        /// <summary>\r
+        /// Get the parent of this container\r
+        /// </summary>\r
+        /// <pre>The child container has been initialized.</pre>\r
+        /// <returns>The parent container</returns>\r
+        protected abstract Container GetParent();\r
+\r
+        /// <summary>\r
+        /// Get the number of children for this container\r
+        /// </summary>\r
+        /// <pre>The container has been initialized.</pre>\r
+        /// <returns>number of children</returns>\r
+        protected abstract UInt32 GetChildCount();\r
+\r
+        /// <summary>\r
+        /// Get the parent Container\r
+        /// Read only\r
+        /// </summary>\r
+        /// <pre>The child container has been initialized.</pre>\r
+        /// <returns>The parent container</returns>\r
+        public Container Parent\r
+        {\r
+            get\r
+            {\r
+                return GetParent();\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
+        /// Get the number of children for this container\r
+        /// Read only\r
+        /// </summary>\r
+        /// <pre>The container has been initialized.</pre>\r
+        /// <returns>number of children</returns>\r
+        public uint ChildCount\r
+        {\r
+            get\r
+            {\r
+                return GetChildCount();\r
+            }\r
+        }\r
+    }\r
 } // namespace Tizen.NUI
\ No newline at end of file
index cc9dd25..e62d647 100755 (executable)
@@ -48,6 +48,8 @@ namespace Tizen.NUI
             NDalicPINVOKE.Actor_Add(swigCPtr, View.getCPtr(child));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            Children.Add(child);
         }
 
         /// <summary>
@@ -60,6 +62,8 @@ namespace Tizen.NUI
             NDalicPINVOKE.Actor_Remove(swigCPtr, View.getCPtr(child));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            Children.Remove(child);
         }
 
         /// <summary>
@@ -70,14 +74,14 @@ namespace Tizen.NUI
         /// <returns>The view for the given index or empty handle if children not initialized.</returns>
         public override View GetChildAt(uint index)
         {
-            System.IntPtr cPtr = NDalicPINVOKE.Actor_GetChildAt(swigCPtr, index);
-
-            View ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as View;
-
-            if (NDalicPINVOKE.SWIGPendingException.Pending)
-                throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-
-            return ret ?? null;
+            if (index < Children.Count)
+            {
+                return Children[Convert.ToInt32(index)];
+            }
+            else
+            {
+                return null;
+            }
         }
 
 
@@ -88,10 +92,7 @@ namespace Tizen.NUI
 
         protected override uint GetChildCount()
         {
-            uint ret = NDalicPINVOKE.Actor_GetChildCount(swigCPtr);
-            if (NDalicPINVOKE.SWIGPendingException.Pending)
-                throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-            return ret;
+            return Convert.ToUInt32(Children.Count);
         }
 
         protected override void Dispose(DisposeTypes type)
index 220424e..5c39f3f 100755 (executable)
@@ -600,14 +600,12 @@ namespace Tizen.NUI
 
         public void Add(View view)
         {
-            NDalicPINVOKE.Stage_Add(stageCPtr, View.getCPtr(view));
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            GetRootLayer()?.Add(view);
         }
 
         public void Remove(View view)
         {
-            NDalicPINVOKE.Stage_Remove(stageCPtr, View.getCPtr(view));
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            GetRootLayer()?.Remove(view);
         }
 
         internal Vector2 GetSize()
@@ -647,11 +645,13 @@ namespace Tizen.NUI
 
         internal Layer GetRootLayer()
         {
-            if (_rootLayer == null)
+            // Window.IsInstalled() is actually true only when called from event thread and
+            // Core has been initialized, not when Stage is ready.
+            if (_rootLayer == null && Window.IsInstalled())
+            {
                 _rootLayer = new Layer(NDalicPINVOKE.Stage_GetRootLayer(stageCPtr), true);
-
-
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+                if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            }
             return _rootLayer;
         }