Skip to content

Commit

Permalink
Rescan popup action (YARC-Official#781)
Browse files Browse the repository at this point in the history
* Actionable Toasts

* On rescan required message, clicking Toast goes to Settings page.

---------

Co-authored-by: Vinay Kapadia <[email protected]>
  • Loading branch information
vinaykapadia and Vinay Kapadia authored May 23, 2024
1 parent a77408f commit f6440f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
7 changes: 6 additions & 1 deletion Assets/Script/Gameplay/GameManager.Loading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using YARG.Gameplay.Player;
using YARG.Menu.Navigation;
using YARG.Menu.Persistent;
using YARG.Menu.Settings;
using YARG.Playback;
using YARG.Player;
using YARG.Replays;
Expand Down Expand Up @@ -133,7 +134,11 @@ private async void Start()

if (_loadState == LoadFailureState.Rescan)
{
ToastManager.ToastWarning("Chart requires a rescan!");
ToastManager.ToastWarning("Chart requires a rescan!", () =>
{
SettingsMenu.Instance.gameObject.SetActive(true);
SettingsMenu.Instance.SelectTabByName("SongManager");
});

global.LoadScene(SceneIndex.Menu);
return;
Expand Down
10 changes: 8 additions & 2 deletions Assets/Script/Menu/Persistent/Toasts/Toast.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using DG.Tweening;
using TMPro;
using UnityEngine;
Expand Down Expand Up @@ -27,8 +28,9 @@ public class Toast : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler,

private bool _canInteract;
private Coroutine _coroutine;
private Action _onClick;

public void Initialize(string type, string message, Sprite icon, Color color)
public void Initialize(string type, string message, Sprite icon, Color color, Action onClick)
{
_background.color = color;

Expand All @@ -39,6 +41,8 @@ public void Initialize(string type, string message, Sprite icon, Color color)
_message.color = Color.white;
_message.text = message;

_onClick = onClick;

_coroutine = StartCoroutine(ToastStartCoroutine());
}

Expand Down Expand Up @@ -96,6 +100,8 @@ public void OnPointerClick(PointerEventData eventData)
return;
}

_onClick?.Invoke();

StopCoroutine(_coroutine);
_coroutine = StartCoroutine(ToastEndCoroutine());
}
Expand Down
39 changes: 23 additions & 16 deletions Assets/Script/Menu/Persistent/Toasts/ToastManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ private readonly struct ToastInfo
{
public readonly ToastType Type;
public readonly string Text;
public readonly Action OnClick;

public ToastInfo(ToastType type, string text)
public ToastInfo(ToastType type, string text, Action onClick)
{
Type = type;
Text = text;
OnClick = onClick;
}
}

Expand All @@ -68,51 +70,56 @@ private void Update()

while (transform.childCount < MAX_TOAST_COUNT && _toastQueue.TryDequeue(out var toast))
{
ShowToast(toast.Type, toast.Text);
ShowToast(toast.Type, toast.Text, toast.OnClick);
}
}

/// <summary>
/// Adds a general message toast to the toast queue.
/// </summary>
/// <param name="text">Text of the toast.</param>
public static void ToastMessage(string text)
=> AddToast(ToastType.General, text);
/// <param name="onClick">Action to perform when the toast is clicked.</param>
public static void ToastMessage(string text, Action onClick = null)
=> AddToast(ToastType.General, text, onClick);

/// <summary>
/// Adds an information message toast to the toast queue.
/// </summary>
/// <param name="text">Text of the toast.</param>
public static void ToastInformation(string text)
=> AddToast(ToastType.Information, text);
/// <param name="onClick">Action to perform when the toast is clicked.</param>
public static void ToastInformation(string text, Action onClick = null)
=> AddToast(ToastType.Information, text, onClick);

/// <summary>
/// Adds a success message toast to the toast queue.
/// </summary>
/// <param name="text">Text of the toast.</param>
public static void ToastSuccess(string text)
=> AddToast(ToastType.Success, text);
/// <param name="onClick">Action to perform when the toast is clicked.</param>
public static void ToastSuccess(string text, Action onClick = null)
=> AddToast(ToastType.Success, text, onClick);

/// <summary>
/// Adds a warning message toast to the toast queue.
/// </summary>
/// <param name="text">Text of the toast.</param>
public static void ToastWarning(string text)
=> AddToast(ToastType.Warning, text);
/// <param name="onClick">Action to perform when the toast is clicked.</param>
public static void ToastWarning(string text, Action onClick = null)
=> AddToast(ToastType.Warning, text, onClick);

/// <summary>
/// Adds an error message toast to the toast queue.
/// </summary>
/// <param name="text">Text of the toast.</param>
public static void ToastError(string text)
=> AddToast(ToastType.Error, text);
/// <param name="onClick">Action to perform when the toast is clicked.</param>
public static void ToastError(string text, Action onClick = null)
=> AddToast(ToastType.Error, text, onClick);

private static void AddToast(ToastType type, string text)
private static void AddToast(ToastType type, string text, Action onClick)
{
_toastQueue.Enqueue(new ToastInfo(type, text));
_toastQueue.Enqueue(new ToastInfo(type, text, onClick));
}

private void ShowToast(ToastType type, string body)
private void ShowToast(ToastType type, string body, Action onClick)
{
// Get properties for this message type
var (text, color, icon) = type switch
Expand All @@ -126,7 +133,7 @@ private void ShowToast(ToastType type, string body)
};

var toast = Instantiate(_toastPrefab, transform);
toast.Initialize(text, body, icon, color);
toast.Initialize(text, body, icon, color, onClick);
}
}
}

0 comments on commit f6440f7

Please sign in to comment.