From 23457e8fdbf6f24c86ef32b4b83e8a999f10c25c Mon Sep 17 00:00:00 2001 From: Umar Date: Mon, 4 Sep 2017 21:01:04 +0100 Subject: [PATCH] Scene Graph support in NUI The RSS was increased 0.3% from 519254 to 521274 for 50,000 views. Change-Id: Ib739b406af034e14d8193785b52a5d17f570e4c7 --- Tizen.NUI/src/public/BaseComponents/View.cs | 121 ++++++++++++++++++++++------ Tizen.NUI/src/public/Container.cs | 11 +++ Tizen.NUI/src/public/Layer.cs | 25 +++--- Tizen.NUI/src/public/Window.cs | 16 ++-- 4 files changed, 127 insertions(+), 46 deletions(-) diff --git a/Tizen.NUI/src/public/BaseComponents/View.cs b/Tizen.NUI/src/public/BaseComponents/View.cs index ff714c0..0584dcb 100755 --- a/Tizen.NUI/src/public/BaseComponents/View.cs +++ b/Tizen.NUI/src/public/BaseComponents/View.cs @@ -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); } /// @@ -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); } /// @@ -71,13 +76,14 @@ namespace Tizen.NUI.BaseComponents /// 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; + } } /// @@ -87,10 +93,7 @@ namespace Tizen.NUI.BaseComponents /// 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); } /// @@ -100,23 +103,14 @@ namespace Tizen.NUI.BaseComponents 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; } /// @@ -2064,6 +2058,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(); @@ -2071,6 +2080,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(); @@ -2085,6 +2109,14 @@ namespace Tizen.NUI.BaseComponents /// 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(); @@ -2099,6 +2131,14 @@ namespace Tizen.NUI.BaseComponents /// 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(); @@ -2127,6 +2167,21 @@ namespace Tizen.NUI.BaseComponents /// Will be raised above this view 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(); @@ -2142,7 +2197,23 @@ namespace Tizen.NUI.BaseComponents /// Will be lowered below this view 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(); } @@ -2201,9 +2272,7 @@ namespace Tizen.NUI.BaseComponents ///
The (child) View has been initialized. 
public void Unparent() { - NDalicPINVOKE.Actor_Unparent(swigCPtr); - if (NDalicPINVOKE.SWIGPendingException.Pending) - throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + GetParent()?.Remove(this); } /// diff --git a/Tizen.NUI/src/public/Container.cs b/Tizen.NUI/src/public/Container.cs index 9a96336..a83f187 100755 --- a/Tizen.NUI/src/public/Container.cs +++ b/Tizen.NUI/src/public/Container.cs @@ -15,6 +15,7 @@ */ using System; +using System.Collections.Generic; using Tizen.NUI.BaseComponents; namespace Tizen.NUI @@ -29,6 +30,16 @@ namespace Tizen.NUI public abstract class Container : Animatable { + private List _childViews = new List(); + + public List Children + { + get + { + return _childViews; + } + } + internal Container(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn) { // No un-managed data hence no need to store a native ptr diff --git a/Tizen.NUI/src/public/Layer.cs b/Tizen.NUI/src/public/Layer.cs index 1e8c5af..fd629e3 100755 --- a/Tizen.NUI/src/public/Layer.cs +++ b/Tizen.NUI/src/public/Layer.cs @@ -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); } /// @@ -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); } /// @@ -70,14 +74,14 @@ namespace Tizen.NUI /// The view for the given index or empty handle if children not initialized 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); } /// diff --git a/Tizen.NUI/src/public/Window.cs b/Tizen.NUI/src/public/Window.cs index 69bd48e..7791990 100755 --- a/Tizen.NUI/src/public/Window.cs +++ b/Tizen.NUI/src/public/Window.cs @@ -587,14 +587,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() @@ -634,11 +632,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; } -- 2.7.4