Skip to content

Commit

Permalink
Add button to regenerate screenId enum. Make Navigator.Visit return a…
Browse files Browse the repository at this point in the history
… UniTask. Ensure that builds always start on original home screen
  • Loading branch information
neilsarkar committed Dec 23, 2020
1 parent 146bfd6 commit 8cf697f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Editor/ScreenIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ static ScreenIdGenerator() {
Navigator.WantsAddScreens += AddScreens;
Navigator.WantsReplaceScreens -= ReplaceScreens;
Navigator.WantsReplaceScreens += ReplaceScreens;
Navigator.WantsReloadScreens -= ReloadScreens;
Navigator.WantsReloadScreens += ReloadScreens;

// read screens from .json file in case we lost references
var screenIds = ScreenIdSerializer.Deserialize();
Expand All @@ -31,13 +33,17 @@ static ScreenIdGenerator() {
WriteScreenIds(screenIds);
}

public static void ReloadScreens() {
var screenIds = ScreenIdSerializer.Deserialize();
WriteScreenIds(screenIds);
}

public static void ReplaceScreens(VioletScreen[] screens) {
var screenIds = new List<ScreenIdJson>() { new ScreenIdJson("None", 0) };
var nextId = 1;
foreach (var screen in screens) {
var name = VioletScreen.Sanitize(screen.name);
var success = Enum.TryParse<ScreenId>(name, out ScreenId screenId);
// var id = ScreenId.TryParse
screenIds.Add(new ScreenIdJson(
name, success ? (int)screenId : nextId++
));
Expand Down
28 changes: 20 additions & 8 deletions Runtime/Navigation/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void OnDisable() {
/// Visit takes a <see cref="ScreenId"/> and transitions the menu to that scene.
/// </summary>
/// <param name="screenId"></param>
public async void Visit(ScreenId screenId) {
public async UniTask<bool> Visit(ScreenId screenId) {
Violet.LogVerbose($"Visiting {screenId}");
if (!screens.ContainsKey(screenId)) {
throw new VioletException($"Tried to visit {screenId} but it doesn't exist in the current scene. You'll want to add the {screenId} prefab to this scene or to the MenuBuilder prefab. Or change the Home Screen to the screen you want.");
Expand Down Expand Up @@ -99,6 +99,8 @@ public async void Visit(ScreenId screenId) {
canceler.Dispose();
canceler = null;
}

return ok;
}

/// <summary>
Expand All @@ -107,11 +109,11 @@ public async void Visit(ScreenId screenId) {
/// It fires <see cref="OnModalShow" /> prior to setting the screen to active
/// </summary>
/// <param name="screenId">Auto-generated id of screen to show as modal</param>
public void ShowModal(ScreenId screenId) {
public async void ShowModal(ScreenId screenId) {
// we have to call this before setting things to active because
// it causes all input listeners to unsubscribe
OnModalShow?.Invoke(screenId);
screens[screenId].gameObject.SetActive(true);
await screens[screenId].Show(default);
currentModal = screenId;
}

Expand All @@ -120,9 +122,9 @@ public void ShowModal(ScreenId screenId) {
///
/// It fires <see cref="OnModalHide" /> after setting the modal to inactive.
/// </summary>
public void HideModal() {
public async void HideModal() {
if (currentModal == ScreenId.None) { Violet.LogWarning("Called HideModal but there is no current modal - check if HideModal is called twice or called before ShowModal"); return; }
screens[currentModal].gameObject.SetActive(false);
await screens[currentModal].Hide();
OnModalHide?.Invoke(currentModal);
currentModal = ScreenId.None;
}
Expand Down Expand Up @@ -156,7 +158,7 @@ public void Visit(string screenIdString) {
if (!isValid) {
throw new VioletException($"Couldn't find a screen with the id {screenIdString.Replace(" ", "")}. Please check the spelling.");
}
Visit(screenId);
_ = Visit(screenId);
}

public void ShowModal(string screenIdString) {
Expand All @@ -170,7 +172,10 @@ public void ShowModal(string screenIdString) {
}

protected virtual void VisitFirstScreen() {
Visit(homeScreen);
#if !UNITY_EDITOR
homeScreen = originalHomeScreen;
#endif
_ = Visit(homeScreen);
}

ScreenId ScreenToScreenId(VioletScreen screen) {
Expand Down Expand Up @@ -223,9 +228,9 @@ void LoadScreens() {

protected virtual void OnScreenAdded(GameObject gameObject) { }

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

public void Edit(VioletScreen screen) {
originalHomeScreen = homeScreen;
Expand Down Expand Up @@ -255,6 +260,7 @@ public void FinishEditing(VioletScreen screen = null) {
public void DiscardEdits() {
if (EditingScreen == null) { EditingScreen = gameObject.GetComponentInChildren<VioletScreen>(); }
EditingScreen.RevertPrefab();
EditingScreen.gameObject.SetActive(false);
EditingScreen = null;
homeScreen = originalHomeScreen;
}
Expand Down Expand Up @@ -299,9 +305,15 @@ void DeleteUnusedScreens() {
ReplaceScreens();
}

[Button, GUIColor(Violet.r, Violet.g, Violet.b)]
void ReloadScreensFromJson() {
WantsReloadScreens?.Invoke();
}

// TODO: move all of this to the editor assembly
public static Action<VioletScreen[]> WantsAddScreens;
public static Action<VioletScreen[]> WantsReplaceScreens;
public static Action WantsReloadScreens;
void AddScreens() {
WantsAddScreens?.Invoke(GetComponentsInChildren<VioletScreen>(true));
}
Expand Down

0 comments on commit 8cf697f

Please sign in to comment.