(Feat) Add Copy Code button
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using DG.Tweening;
|
||||
using Ashwild.Player;
|
||||
using Ashwild.Settings;
|
||||
|
||||
@@ -35,6 +36,12 @@ namespace Ashwild.UI
|
||||
[SerializeField] private TMP_Text roomCodeLabel;
|
||||
[Tooltip("Shown in place of the code when no online session is active.")]
|
||||
[SerializeField] private string offlinePlaceholder = "—";
|
||||
[Tooltip("Copies the current session code to the system clipboard when clicked.")]
|
||||
[SerializeField] private Button copyCodeButton;
|
||||
[Tooltip("Flashed in place of the code for a moment after a successful copy.")]
|
||||
[SerializeField] private string copyFeedback = "Copié !";
|
||||
[Tooltip("How long the copy confirmation stays on screen before the code reappears.")]
|
||||
[SerializeField] private float copyFeedbackDuration = 1.2f;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -45,6 +52,12 @@ namespace Ashwild.UI
|
||||
/// </summary>
|
||||
private GameUIManager Game => UIManager.Instance as GameUIManager;
|
||||
|
||||
/// <summary>
|
||||
/// Pending tween that restores the room code label after the copy confirmation; killed
|
||||
/// before being restarted and on teardown so it never fires on a destroyed label.
|
||||
/// </summary>
|
||||
private Tween copyFeedbackTween;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
@@ -60,6 +73,15 @@ namespace Ashwild.UI
|
||||
if (inviteButton != null) inviteButton.onClick.AddListener(() => Game?.InviteFriend());
|
||||
if (quitButton != null) quitButton.onClick.AddListener(() => Game?.QuitToMenu());
|
||||
if (settingsButton != null) settingsButton.onClick.AddListener(() => UIManager.Instance.OpenPanel<SettingsPanel>());
|
||||
if (copyCodeButton != null) copyCodeButton.onClick.AddListener(CopySessionCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Kills the copy-feedback tween so it never restores the label after the panel is gone.
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
copyFeedbackTween?.Kill();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -86,10 +108,33 @@ namespace Ashwild.UI
|
||||
/// </summary>
|
||||
private void RefreshRoomCode()
|
||||
{
|
||||
copyFeedbackTween?.Kill();
|
||||
|
||||
bool hasCode = !string.IsNullOrEmpty(PlayerEvents.SessionCode);
|
||||
|
||||
if (roomCodeLabel != null)
|
||||
roomCodeLabel.text = hasCode ? PlayerEvents.SessionCode : offlinePlaceholder;
|
||||
|
||||
if (copyCodeButton != null)
|
||||
copyCodeButton.interactable = hasCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the live session code to the system clipboard and briefly flashes a confirmation
|
||||
/// in the room-code label. Does nothing when no online session is active.
|
||||
/// </summary>
|
||||
private void CopySessionCode()
|
||||
{
|
||||
string code = PlayerEvents.SessionCode;
|
||||
if (string.IsNullOrEmpty(code)) return;
|
||||
|
||||
GUIUtility.systemCopyBuffer = code;
|
||||
|
||||
if (roomCodeLabel == null) return;
|
||||
|
||||
string code = PlayerEvents.SessionCode;
|
||||
roomCodeLabel.text = string.IsNullOrEmpty(code) ? offlinePlaceholder : code;
|
||||
copyFeedbackTween?.Kill();
|
||||
roomCodeLabel.text = copyFeedback;
|
||||
copyFeedbackTween = DOVirtual.DelayedCall(copyFeedbackDuration, () => roomCodeLabel.text = code);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user