From be64e6ff036ed47d970f7df985917ec30a7e5bd8 Mon Sep 17 00:00:00 2001 From: Neil Sarkar Date: Tue, 11 Aug 2020 13:14:21 -0700 Subject: [PATCH] 0.1.71 - move RepeatView prefab to the beginning, fix indexes on childviews --- Runtime/Views/ChildView.cs | 11 ++++++++++- Runtime/Views/RepeatView.cs | 9 +++++---- Runtime/Views/View.cs | 4 ++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Runtime/Views/ChildView.cs b/Runtime/Views/ChildView.cs index de4a4b9..860897b 100644 --- a/Runtime/Views/ChildView.cs +++ b/Runtime/Views/ChildView.cs @@ -8,7 +8,15 @@ namespace VioletUI { public abstract class ChildView : View where TState : class, IState { + /// + /// Index returns this elements index in the parent RepeatView + /// + /// int protected int Index => index > -1 ? index : index = GetIndex(); + /// + /// Item returns the element associated with this ChildView + /// + /// T Item protected T Item => parent?.Items == null || parent.Items.Count <= Index ? default(T) : parent.Items[Index]; protected T LastItem => LastState == null || parent?.LastItems == null || parent.LastItems.Count <= Index ? default(T) : parent.LastItems[Index]; @@ -39,7 +47,8 @@ int GetIndex() { var t = transform; while(t.parent != null) { if (t.parent.GetComponent>() != null) { - return t.GetSiblingIndex(); + // account for the first item being the prefab + return Math.Max(0, t.GetSiblingIndex()-1); } t = t.parent; } diff --git a/Runtime/Views/RepeatView.cs b/Runtime/Views/RepeatView.cs index 5637210..d93725f 100644 --- a/Runtime/Views/RepeatView.cs +++ b/Runtime/Views/RepeatView.cs @@ -37,6 +37,11 @@ void RenderChildren() { return; } +#if UNITY_EDITOR + var model = PrefabUtility.InstantiatePrefab(ViewPrefab, transform) as GameObject; + model.SetActive(false); +#endif + for (int i = 0; i < Items.Count; i++) { var child = Instantiate(ViewPrefab, transform); var view = child.GetComponent>(); @@ -44,10 +49,6 @@ void RenderChildren() { view.RenderInternal(State, default(TState)); } -#if UNITY_EDITOR - var model = PrefabUtility.InstantiatePrefab(ViewPrefab, transform) as GameObject; - model.SetActive(false); -#endif } } } diff --git a/Runtime/Views/View.cs b/Runtime/Views/View.cs index 3896e82..affdf1f 100644 --- a/Runtime/Views/View.cs +++ b/Runtime/Views/View.cs @@ -69,6 +69,10 @@ internal virtual void RenderInternal(TState state, TState lastState) { Warn($"RenderInternal | gameObject was null"); throw new Bail("gameObject is null"); } + if (!gameObject.activeSelf) { + Verbose($"RenderInternal | bailing since {gameObject.name} is not active"); + throw new Bail($"{gameObject.name} is not active"); + } } catch(MissingReferenceException) { Warn($"RenderInternal | MissingReferenceException when trying to access gameObject"); throw new Bail("gameObject is missing");