Skip to content

Commit

Permalink
Bump to 0.1.64 - add WillHide and DidHide, rename Screen to VioletScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Jul 31, 2020
1 parent 151f0b1 commit 95868cb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Editor/ScreenEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void DrawHierarchyItem(int instanceID, Rect rect) {
return;
}

var screen = gameObject.GetComponent<Screen>();
var screen = gameObject.GetComponent<VioletScreen>();
if (screen != null) {
DrawScreen(screen, rect);
return;
Expand All @@ -50,7 +50,7 @@ static void DrawNavigator(Navigator navigator, Rect rect) {
}
}

static void DrawScreen(Screen screen, Rect rect) {
static void DrawScreen(VioletScreen screen, Rect rect) {
var navigator = screen.transform.parent.GetComponent<Navigator>();
if (navigator == null) {
Violet.LogWarning($"Unable to find Navigator for {screen.name} - make sure {screen.transform.parent.name} has a Navigator component");
Expand Down
31 changes: 16 additions & 15 deletions Runtime/Navigation/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Navigator : TidyBehaviour {
#endregion

#region local
[NonSerialized] Dictionary<ScreenId, Screen> screens = new Dictionary<ScreenId, Screen>();
[NonSerialized] Dictionary<ScreenId, VioletScreen> screens = new Dictionary<ScreenId, VioletScreen>();
[NonSerialized] ScreenId lastScreen = ScreenId.None;
[NonSerialized] ScreenId currentScreen = ScreenId.None;
[NonSerialized] ScreenId currentModal = ScreenId.None;
Expand Down Expand Up @@ -137,7 +137,7 @@ protected virtual void VisitFirstScreen() {
Visit(homeScreen);
}

ScreenId ScreenToScreenId(Screen screen) {
ScreenId ScreenToScreenId(VioletScreen screen) {
ScreenId ret;
var slug = ScreenIdGenerator.Sanitize(screen.name);
var ok = Enum.TryParse<ScreenId>(slug, out ret);
Expand All @@ -153,7 +153,7 @@ void LoadScreens() {

ScreenId screenId = ScreenId.None;
screens.Clear();
foreach (Screen screen in GetComponentsInChildren<Screen>(true)) {
foreach (VioletScreen screen in GetComponentsInChildren<VioletScreen>(true)) {
var isValid = Enum.TryParse(ScreenIdGenerator.Sanitize(screen.name), out screenId);
if (!isValid) {
#if UNITY_EDITOR
Expand Down Expand Up @@ -186,10 +186,10 @@ void LoadScreens() {


#if UNITY_EDITOR
[HideInInspector] public Screen EditingScreen;
[HideInInspector] public VioletScreen EditingScreen;
[SerializeField, HideInInspector] ScreenId originalHomeScreen;

public void Edit(Screen screen) {
public void Edit(VioletScreen screen) {
originalHomeScreen = homeScreen;
try {
homeScreen = ScreenToScreenId(screen);
Expand All @@ -205,8 +205,8 @@ public void Edit(Screen screen) {
currentScreen = ScreenToScreenId(screen);
}

public void FinishEditing(Screen screen = null) {
if (EditingScreen == null) { EditingScreen = gameObject.GetComponentInChildren<Screen>(); }
public void FinishEditing(VioletScreen screen = null) {
if (EditingScreen == null) { EditingScreen = gameObject.GetComponentInChildren<VioletScreen>(); }
if (screen == null) { screen = EditingScreen; }
screen.PackPrefab();
screen.gameObject.SetActive(false);
Expand All @@ -216,7 +216,7 @@ public void FinishEditing(Screen screen = null) {

public void AddScreen() {
var gameObject = new GameObject("Rename Me");
var screen = gameObject.AddComponent<Screen>();
var screen = gameObject.AddComponent<VioletScreen>();
var canvas = gameObject.AddComponent<Canvas>();
gameObject.AddComponent<CanvasRenderer>();
gameObject.AddComponent<GraphicRaycaster>();
Expand All @@ -228,11 +228,13 @@ public void AddScreen() {
}

float lastUpdate;
int childCount;
void Update() {
if (Application.isPlaying) { return; }
if (transform.childCount == 0 || transform.childCount == screens.Count) { return; }
if (transform.childCount == 0 || transform.childCount != childCount) { return; }
if (Time.time - lastUpdate <= .5f) { return; }
lastUpdate = Time.time;
childCount = transform.childCount;
Violet.LogVerbose($"{name} reloading screens and regenerating enums. childCount={transform.childCount} screensCount={screens.Count}");
LoadScreens();
RegenerateEnums();
Expand All @@ -242,18 +244,17 @@ void Update() {
[Title("Advanced")]
[ShowIf("Advanced"), Button, GUIColor(Violet.r, Violet.g, Violet.b)]
void Regenerate() {
Violet.Log($"Regenerating enums...");
Violet.LogVerbose($"Regenerating enums...");
RegenerateEnums();
Violet.Log($"Reloading screens...");
Violet.LogVerbose($"Reloading screens...");
LoadScreens();
Violet.Log($"Done.");
Violet.LogVerbose($"Done reloading screens.");
}

void RegenerateEnums() {
var screens = GetComponentsInChildren<Screen>(true);
var screens = GetComponentsInChildren<VioletScreen>(true);
ScreenIdGenerator.Generate(screens);
Violet.Log($"Reloading screens...");
Violet.Log($"Done.");
Violet.LogVerbose($"Done regenerating enums.");
}
#endif
}
Expand Down
14 changes: 7 additions & 7 deletions Runtime/Navigation/ScreenIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace VioletUI {
public class ScreenIdGenerator {
static StringBuilder sb = new StringBuilder();

public static void Generate(Screen screen) {
Generate(new Screen[] {screen});
public static void Generate(VioletScreen screen) {
Generate(new VioletScreen[] {screen});
}

public static void Generate(Screen[] screens) {
public static void Generate(VioletScreen[] screens) {
var screenStrings = new List<string>(screens.Length);
foreach (var screen in screens) {
screenStrings.Add(screen.name);
Expand All @@ -25,9 +25,9 @@ public static void Generate(Screen[] screens) {
}

public static void Generate(List<string> screens) {
var newScreens = Filter(screens);
if (newScreens.Count == 0) { return; }
AddScreens(newScreens);
var newVioletScreens = Filter(screens);
if (newVioletScreens.Count == 0) { return; }
AddVioletScreens(newVioletScreens);
}

static List<string> Filter(List<string> screens) {
Expand All @@ -41,7 +41,7 @@ static List<string> Filter(List<string> screens) {
return ret;
}

static void AddScreens(List<string> screens) {
static void AddVioletScreens(List<string> screens) {
sb.Clear();
sb.AppendLine("//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)");
sb.AppendLine("namespace VioletUI {");
Expand Down
27 changes: 13 additions & 14 deletions Runtime/Navigation/Screen.cs → Runtime/Navigation/VioletScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace VioletUI {
[ExecuteAlways]
public class Screen : TidyBehaviour {
public class VioletScreen : TidyBehaviour {

[Title("Show/Hide")]

Expand All @@ -26,18 +26,20 @@ public class Screen : TidyBehaviour {

Animator animator;

void OnEnable() {
protected virtual void OnEnable() {
animator = gameObject.GetComponent<Animator>();
}

protected virtual void DidShow() { OnShow?.Invoke(); }
protected virtual void DidHide() { OnHide?.Invoke(); }
protected virtual void WillShow() {}
protected virtual void WillHide() {}

internal async UniTask<bool> Show(CancellationToken token = default, string triggerOverride = null) {
gameObject.SetActive(true);
WillShow();

// if (HasGate && Release.Flags[gate]) {
// OnGatedShow?.Invoke();
// } else {
// OnShow?.Invoke();
// }
gameObject.SetActive(true);
DidShow();

var triggerName = triggerOverride == null ? ShowAnimation : triggerOverride;

Expand All @@ -51,7 +53,7 @@ internal async UniTask<bool> Show(CancellationToken token = default, string trig
}

internal async UniTask<bool> Hide(CancellationToken token = default, string triggerOverride = null) {
// uiEvents.lastSelected = EventSystem.current?.currentSelectedGameObject;
WillHide();
var triggerName = triggerOverride == null ? HideAnimation : triggerOverride;

bool ok = true;
Expand All @@ -60,12 +62,9 @@ internal async UniTask<bool> Hide(CancellationToken token = default, string trig
} catch (OperationCanceledException) {
ok = false;
}
// if (HasGate && Release.Flags[gate]) {
// OnGatedHide?.Invoke();
// } else {
// OnHide?.Invoke();
// }

gameObject.SetActive(false);
DidHide();

return ok;
}
Expand Down
File renamed without changes.
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.62",
"version": "0.1.64",
"displayName": "Violet UI",
"description": "State-based rendering with live updates in the Unity Editor",
"unity": "2019.4",
Expand Down

0 comments on commit 95868cb

Please sign in to comment.