Skip to content

Commit

Permalink
0.1.71 - move RepeatView prefab to the beginning, fix indexes on chil…
Browse files Browse the repository at this point in the history
…dviews
  • Loading branch information
neilsarkar committed Aug 11, 2020
1 parent 31c2b15 commit be64e6f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Runtime/Views/ChildView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

namespace VioletUI {
public abstract class ChildView<TState, T> : View<TState> where TState : class, IState {
/// <summary>
/// Index returns this elements index in the parent RepeatView
/// </summary>
/// <returns>int</returns>
protected int Index => index > -1 ? index : index = GetIndex();
/// <summary>
/// Item returns the element associated with this ChildView
/// </summary>
/// <returns>T Item</returns>
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];

Expand Down Expand Up @@ -39,7 +47,8 @@ int GetIndex() {
var t = transform;
while(t.parent != null) {
if (t.parent.GetComponent<RepeatView<TState, T>>() != null) {
return t.GetSiblingIndex();
// account for the first item being the prefab
return Math.Max(0, t.GetSiblingIndex()-1);
}
t = t.parent;
}
Expand Down
9 changes: 5 additions & 4 deletions Runtime/Views/RepeatView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ 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<ChildView<TState, T>>();
if (view == null) {continue;}
view.RenderInternal(State, default(TState));
}

#if UNITY_EDITOR
var model = PrefabUtility.InstantiatePrefab(ViewPrefab, transform) as GameObject;
model.SetActive(false);
#endif
}
}
}
4 changes: 4 additions & 0 deletions Runtime/Views/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit be64e6f

Please sign in to comment.