Skip to content

Commit

Permalink
Revert to generating screenId in package path
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Aug 6, 2020
1 parent 4e866e3 commit d8cb53b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
11 changes: 7 additions & 4 deletions Runtime/Navigation/ScreenIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ static void AddVioletScreens(List<string> screens) {
}
sb.AppendLine("\t}");
sb.AppendLine("}");
string path = $"{packagePath()}/Runtime/Navigation/ScreenId.cs";
File.WriteAllText(path, sb.ToString());
File.WriteAllText(packagePath(), sb.ToString());
Violet.Log("Regenerating the ScreenId enum, this may take a second to recompile.");
AssetDatabase.Refresh();
}
Expand All @@ -72,10 +71,14 @@ public static string Sanitize(string s) {
return s.Replace(" ", "");
}

static string packagePath() {
return $"{packageBasePath()}/Runtime/Navigation/ScreenId.cs";
}

static string scriptPath() {
return null;
return "Assets/Menus/ScreenId.cs";
}
static string packagePath() {
static string packageBasePath() {
foreach (var path in packagePaths) {
var paths = Directory.GetDirectories(path, "*violetui*", SearchOption.TopDirectoryOnly);
if (paths.Length > 0) { return paths[0];}
Expand Down
1 change: 1 addition & 0 deletions Runtime/RenderShortcut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static class RenderShortcut {

[Shortcut("VioletRenderShortcut", KeyCode.Semicolon, ShortcutModifiers.Action)]
public static void Press() {
Violet.LogVerbose("Rendering from shortcut press");
OnPress?.Invoke();
}
}
Expand Down
10 changes: 9 additions & 1 deletion Runtime/StateMonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ void Update() {
Singleton = this;
}

protected override void OnDestroy() {
base.OnDestroy();
RenderShortcut.OnPress -= Render;
}

[Button("Render (shortcut: cmd+;)"), GUIColor(Violet.r, Violet.g, Violet.b)]
void Render() {
if (State == null) { Violet.LogWarning($"state is null for {name}");}
if (State == null) {
Violet.LogWarning($"state is null for {name}");
return;
}
State.TriggerChange();
CopyState();
}
Expand Down
32 changes: 26 additions & 6 deletions Runtime/Views/ChildView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
using Dispatch;
using Sirenix.OdinInspector;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor.Experimental.SceneManagement;
#endif

namespace VioletUI {
public abstract class ChildView<TState, T> : View<TState> where TState : class, IState {
protected int Index => transform.GetSiblingIndex();
protected int Index => index > -1 ? index : index = GetIndex();
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 All @@ -16,9 +19,6 @@ protected virtual void Render(T item, int index, TState state) {}

internal override void OnShowInternal() {
parent = gameObject.GetComponentInParent<RepeatView<TState, T>>();
if (parent == null) {
throw new VioletException($"ChildView {gameObject.name} does not have a RepeatView as a parent. Make sure the parent has a component attached that inherits from RepeatView.");
}
}

internal override void RenderInternal(TState state, TState lastState) {
Expand All @@ -28,18 +28,38 @@ internal override void RenderInternal(TState state, TState lastState) {

internal override bool IsDirtyInternal(TState state, TState lastState) {
base.IsDirtyInternal(state, lastState);
if (parent == null || parent.Items == null) { throw new Bail(); }
if (!IsDirty(Item, LastItem)) { throw new Bail(); }
if (parent == null || parent.Items == null) { throw new Bail($"Parent was null - parent={parent} parent.Items={parent?.Items}"); }
if (!IsDirty(Item, LastItem)) { throw new Bail($"IsDirty returned false - Item={Item} LastItem={LastItem}"); }

return true;
}

int index = -1;
int GetIndex() {
var t = transform;
while(t.parent != null) {
if (t.parent.GetComponent<RepeatView<TState, T>>() != null) {
return t.GetSiblingIndex();
}
t = t.parent;
}

#if UNITY_EDITOR
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage == null) {
throw new VioletException($"ChildView {gameObject.name} does not have a RepeatView as a parent. Make sure the parent has a component attached that inherits from RepeatView.");
}
#endif
return -1;
}

#if UNITY_EDITOR
public override void Update() {
base.Update();
if (Application.isPlaying) { return; }

parent = gameObject.GetComponentInParent<RepeatView<TState, T>>();

}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Views/RepeatView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal override void RenderInternal(TState state, TState lastState) {

internal override bool IsDirtyInternal(TState state, TState lastState) {
base.IsDirtyInternal(state, lastState);
if (lastState != null && Items.Count == LastItems?.Count) { throw new Bail(); }
if (ViewPrefab == null) { throw new Bail(); }
if (lastState != null && Items.Count == LastItems?.Count) { throw new Bail($"No change in item count - {Items.Count}=={LastItems?.Count} "); }
if (ViewPrefab == null) { throw new Bail($"ViewPrefab is null"); }
return true;
}

Expand Down
25 changes: 15 additions & 10 deletions Runtime/Views/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public abstract class View<TState> : TidyBehaviour where TState : class, IState
protected abstract TState LastState { get; }

/// <summary>
/// OnShow is called when the gameObject becomes active in editor or playmode
/// OnShow is called when the gameObject becomes active in editor or playmode, prior to first Render.
/// </summary>
protected virtual void OnShow() { }
/// <summary>
/// OnHide is called when the gameObject becomes inactive in editor or playmode
/// OnHide is called when the gameObject becomes inactive in editor or playmode, after last Render.
/// </summary>
protected virtual void OnHide() { }
/// <summary>
Expand All @@ -37,7 +37,7 @@ protected virtual void OnHide() { }
protected virtual void Render(TState state) { }

/// <summary>
/// dispatcher allows you to send Actions that
/// Dispatcher allows you to send Actions that change the State
/// </summary>
protected virtual Dispatcher<TState> Dispatcher {
get {
Expand All @@ -52,26 +52,31 @@ protected virtual Dispatcher<TState> Dispatcher {
protected void LogWarning(string s) { Violet.LogWarning(s); }
protected void LogError(string s) { Violet.LogError(s); }

// convenience accessor
RectTransform rectTransform;

// Internal methods are so that callers don't have to remember to call base. at the beginning of their implementations
internal virtual void OnShowInternal() {}
internal virtual void OnShowInternal() {
rectTransform = GetComponent<RectTransform>();
}
internal virtual void OnHideInternal() {}
internal virtual bool IsDirtyInternal(TState state, TState lastState) {
if (!IsDirty(state, lastState)) { throw new Bail(); }
if (!IsDirty(state, lastState)) { throw new Bail("IsDirty=false"); }
return true;
}
internal virtual void RenderInternal(TState state, TState lastState) {
try {
if (gameObject == null) {
Warn($"RenderInternal | gameObject was null");
throw new Bail();
throw new Bail("gameObject is null");
}
} catch(MissingReferenceException) {
Warn($"RenderInternal | MissingReferenceException when trying to access gameObject");
throw new Bail();
throw new Bail("gameObject is missing");
}

if (lastState != null) {
if (!IsDirtyInternal(state, lastState) ) { throw new Bail(); }
if (!IsDirtyInternal(state, lastState) ) { throw new Bail("IsDirtyInternal=false"); }
}

Render(state);
Expand All @@ -80,9 +85,9 @@ internal virtual void RenderInternal(TState state, TState lastState) {
void RenderWrapper(TState state, TState lastState) {
try {
RenderInternal(state, lastState);
} catch (Bail) {
} catch (Bail e) {
try {
Verbose($"{gameObject.name} bailed from render");
Verbose($"{gameObject.name} bailed from render - {e.Message}");
} catch (MissingReferenceException) {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.neilsarkar.violetui",
"version": "0.1.65",
"version": "0.1.66",
"displayName": "Violet UI",
"description": "State-based rendering with live updates in the Unity Editor",
"unity": "2019.4",
Expand Down

0 comments on commit d8cb53b

Please sign in to comment.