| | 1 | | using System.Collections; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCL.Builder |
| | 7 | | { |
| | 8 | | public interface IToast |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Show the text |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="instant"></param> |
| | 14 | | void Show(bool instant); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Set the text and changes the size of the toast based on the text |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="text"></param> |
| | 20 | | void SetText(string text); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public class Toast : BaseComponentView, IToast |
| | 24 | | { |
| | 25 | | [Tooltip("Represented in milliseconds")] |
| 17 | 26 | | [SerializeField] internal int msToBeActive = 2000; |
| 17 | 27 | | [SerializeField] internal float sizePerCharacter = 8.8f; |
| | 28 | | [SerializeField] internal Image toastImage; |
| | 29 | | [SerializeField] internal TextMeshProUGUI toastText; |
| | 30 | |
|
| | 31 | | private Coroutine hideCoroutine; |
| | 32 | |
|
| 0 | 33 | | public override void RefreshControl() { } |
| | 34 | |
|
| | 35 | | public void SetText(string text) |
| | 36 | | { |
| 0 | 37 | | toastText.text = text; |
| | 38 | |
|
| 0 | 39 | | Vector2 size = toastImage.rectTransform.sizeDelta; |
| 0 | 40 | | size.x = text.Length * sizePerCharacter; |
| 0 | 41 | | toastImage.rectTransform.sizeDelta = size; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public override void Show(bool instant) |
| | 45 | | { |
| 0 | 46 | | base.Show(instant); |
| 0 | 47 | | if(hideCoroutine != null) |
| 0 | 48 | | StopCoroutine(hideCoroutine); |
| 0 | 49 | | hideCoroutine = StartCoroutine(WaitAndDeactivate()); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | IEnumerator WaitAndDeactivate() |
| | 53 | | { |
| 0 | 54 | | yield return new WaitForSeconds(msToBeActive / 1000f); |
| 0 | 55 | | Hide(false); |
| 0 | 56 | | } |
| | 57 | | } |
| | 58 | | } |