Skip to content

Commit

Permalink
Generate ScreenId enums
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Jul 24, 2020
1 parent a977dd4 commit 7f5696d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
37 changes: 26 additions & 11 deletions Runtime/Navigation/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace VioletUI {
///
/// It is primarily used to navigate between screens, and also exposes the <see cref="OnWillVisit"/> and <seealso cref="OnDidVisit"/> lifecycle events.
/// </summary>
[ExecuteAlways]
public class Navigator : TidyBehaviour {
public ScreenId homeScreen = ScreenId.None;
public bool hasCamera;
Expand All @@ -40,6 +41,7 @@ public class Navigator : TidyBehaviour {
CancellationTokenSource canceler = null;

void Awake() {
if (!Application.isPlaying) { return; }
LoadScreens();
VisitFirstScreen();
}
Expand All @@ -49,7 +51,7 @@ void LoadScreens() {
}

ScreenId screenId = ScreenId.None;
foreach (Screen screen in GetComponentsInChildren<Screen>()) {
foreach (Screen screen in GetComponentsInChildren<Screen>(true)) {
var isValid = Enum.TryParse(screen.name, out screenId);
if (!isValid) {
throw new VioletException($"{screen.name} does not have a valid ScreenId. Make sure this screen is added to MenuBuilder.");
Expand Down Expand Up @@ -162,7 +164,8 @@ protected virtual void VisitFirstScreen() {
ScreenId ScreenToScreenId(Screen screen) {
ScreenId ret;
var slug = screen.name.Replace(" ", "");
var ok = Enum.TryParse<ScreenId>(screen.name.Replace(" ", ""), out ret);
var ok = Enum.TryParse<ScreenId>(slug, out ret);

if (!ok) {
throw new VioletEnumException($"{slug} does not exist in ScreenId. This should be set up automatically by navigator but you can add manually as a workaround.");
}
Expand All @@ -174,6 +177,14 @@ ScreenId ScreenToScreenId(Screen screen) {
[SerializeField, HideInInspector] ScreenId originalHomeScreen;

public void Edit(Screen screen) {
try {
homeScreen = ScreenToScreenId(screen);
} catch (VioletEnumException) {
ScreenIdGenerator.Generate(screen);
Debug.LogWarning($"VioletUI - Couldn't find {screen.name} in the ScreenId enum. This should be fixed if you try your action again. If not, please report a bug.");
return;
}

try {
PrefabUtility.UnpackPrefabInstance(screen.gameObject, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
} catch (ArgumentException) {}
Expand All @@ -182,11 +193,6 @@ public void Edit(Screen screen) {
EditingScreen = screen;
originalHomeScreen = homeScreen;

try {
homeScreen = ScreenToScreenId(screen);
} catch(VioletEnumException) {
print($"Do enum");
}
}

public void FinishEditing(Screen screen = null) {
Expand All @@ -210,12 +216,21 @@ public void AddScreen() {
}

float lastUpdate;
void OnValidate() {
int lastCount;
void Update() {
if (Application.isPlaying) { return; }
// if (transform.childCount == 0) { return; }
// if (Time.time - lastUpdate <= .5f) { return; }
if (transform.childCount == 0) { return; }
if (transform.childCount == lastCount) { return; }
if (Time.time - lastUpdate <= .5f) { return; }
lastCount = transform.childCount;
lastUpdate = Time.time;
print($"Gonna regen enum");
RegenerateEnums();
}

[Button]
void RegenerateEnums() {
var screens = GetComponentsInChildren<Screen>(true);
ScreenIdGenerator.Generate(screens);
}
#endif
}
Expand Down
5 changes: 3 additions & 2 deletions Runtime/Navigation/ScreenId.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)
namespace VioletUI {
public enum ScreenId {
None
None = 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@

namespace VioletUI {
public class ScreenIdGenerator {
static StringBuilder sb;
static StringBuilder sb = new StringBuilder();

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

public static void Generate(Screen[] screens) {
var screenStrings = new List<string>(screens.Length);
foreach (var screen in screens) {
screenStrings.Add(screen.name);
}
Generate(screenStrings);
}

public static void Generate(List<string> screens) {
var newScreens = Filter(screens);
Expand All @@ -29,29 +41,28 @@ static List<string> Filter(List<string> screens) {

static void AddScreens(List<string> screens) {
sb.Clear();
sb.Append("//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)" + Environment.NewLine);
sb.Append("public enum ScreenId {" + Environment.NewLine);
sb.Append("\tNone = 0," + Environment.NewLine);
sb.AppendLine("//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)");
sb.AppendLine("namespace VioletUI {");
sb.AppendLine("\tpublic enum ScreenId {");
sb.AppendLine("\t\tNone = 0,");

// write all existing Enum values - note that unused screens
// will have to be deleted manually. this is to avoid losing references.
foreach (ScreenId screenId in Enum.GetValues(typeof(ScreenId))) {
if (screenId == ScreenId.None) { continue; }
sb.Append($"\t{Enum.GetName(typeof(ScreenId), screenId)} = {(int)screenId},{Environment.NewLine}");
sb.AppendLine($"\t\t{Enum.GetName(typeof(ScreenId), screenId)} = {(int)screenId},");
}

// write any new screen names with incrementing ids
var nextId = Enum.GetValues(typeof(ScreenId)).Cast<int>().Max() + 1;
foreach (var screen in screens) {
sb.Append($"\t{screen} = {nextId++},{Environment.NewLine}");
sb.AppendLine($"\t\t{screen} = {nextId++},");
}

sb.Append("}");

string path = Path.Combine(Application.dataPath, "Scripts", "VioletUI", "ScreenId.cs");
sb.AppendLine("\t}");
sb.AppendLine("}");
string path = Path.GetFullPath("Packages/violetui/Runtime/Navigation/ScreenId.cs");
File.WriteAllText(path, sb.ToString());
AssetDatabase.Refresh();

}

static string sanitize(string s) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f5696d

Please sign in to comment.