| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | | using DCL.TransactionHUDModel; |
| | 9 | | using UnityEngine.SocialPlatforms.Impl; |
| | 10 | | using Type = DCL.TransactionHUDModel.Type; |
| | 11 | |
|
| | 12 | | public class TransactionHUD : MonoBehaviour, ITransactionHUD |
| | 13 | | { |
| | 14 | | [SerializeField] private GameObject paymentPanel; |
| | 15 | |
|
| | 16 | | [SerializeField] private GameObject signPanel; |
| | 17 | |
|
| | 18 | | [SerializeField] private List<Button> acceptButtons; |
| | 19 | |
|
| | 20 | | [SerializeField] private List<Button> rejectButtons; |
| | 21 | |
|
| | 22 | | [SerializeField] private TMP_Text amountLabel; |
| | 23 | |
|
| | 24 | | [SerializeField] private TMP_Text fromAddressLabel; |
| | 25 | |
|
| | 26 | | [SerializeField] private TMP_Text toAddressLabel; |
| | 27 | |
|
| | 28 | | [SerializeField] private TMP_Text messageLabel; |
| | 29 | |
|
| | 30 | | [SerializeField] private TMP_Text paymentPanelTitle; |
| | 31 | |
|
| | 32 | | [SerializeField] private TMP_Text signPanelTitle; |
| | 33 | |
|
| | 34 | | [SerializeField] private TMP_Text paymentNetworkLabel; |
| | 35 | |
|
| | 36 | | [SerializeField] private TMP_Text signNetworkLabel; |
| | 37 | |
|
| 5 | 38 | | public Model model { get; private set; } = new Model(); |
| | 39 | |
|
| | 40 | | public event Action<ITransactionHUD> OnTransactionAccepted; |
| | 41 | |
|
| | 42 | | public event Action<ITransactionHUD> OnTransactionRejected; |
| | 43 | |
|
| | 44 | | private void OnEnable() |
| | 45 | | { |
| 24 | 46 | | foreach (var acceptButton in acceptButtons) |
| 8 | 47 | | acceptButton.onClick.AddListener(AcceptTransaction); |
| | 48 | |
|
| 24 | 49 | | foreach (var rejectButton in rejectButtons) |
| 8 | 50 | | rejectButton.onClick.AddListener(RejectTransaction); |
| 4 | 51 | | } |
| | 52 | |
|
| | 53 | | public IParcelScene FindScene(string sceneId) |
| | 54 | | { |
| 4 | 55 | | if (DCL.Environment.i.world?.state?.scenesSortedByDistance != null) |
| | 56 | | { |
| 0 | 57 | | foreach (IParcelScene scene in DCL.Environment.i.world.state.scenesSortedByDistance) |
| | 58 | | { |
| 0 | 59 | | if (scene.sceneData.id == sceneId) |
| 0 | 60 | | return scene; |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| 4 | 64 | | return null; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | private void OnDisable() |
| | 68 | | { |
| 24 | 69 | | foreach (var acceptButton in acceptButtons) |
| 8 | 70 | | acceptButton.onClick.RemoveAllListeners(); |
| | 71 | |
|
| 24 | 72 | | foreach (var rejectButton in rejectButtons) |
| 8 | 73 | | rejectButton.onClick.RemoveAllListeners(); |
| 4 | 74 | | } |
| | 75 | |
|
| | 76 | | private static string ShortAddress(string address) |
| | 77 | | { |
| 2 | 78 | | if (address == null) |
| 1 | 79 | | return "Null"; |
| | 80 | |
|
| 1 | 81 | | if (address.Length >= 12) |
| 1 | 82 | | return $"{address.Substring(0, 6)}...{address.Substring(address.Length - 4)}"; |
| | 83 | |
|
| 0 | 84 | | return address; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | private void ShowTransferMessage(Model model) |
| | 88 | | { |
| 1 | 89 | | var scene = FindScene(model.sceneId); |
| | 90 | |
|
| 1 | 91 | | if (scene != null) |
| 0 | 92 | | paymentPanelTitle.text = $"'{scene.GetSceneName()}', {scene.sceneData.basePosition.ToString()} wants to init |
| 1 | 93 | | paymentPanel.SetActive(true); |
| 1 | 94 | | signPanel.SetActive(false); |
| | 95 | |
|
| 1 | 96 | | UserProfile ownUserProfile = UserProfile.GetOwnUserProfile(); |
| 1 | 97 | | fromAddressLabel.text = ShortAddress(ownUserProfile.ethAddress); |
| 1 | 98 | | toAddressLabel.text = ShortAddress(model.toAddress); |
| 1 | 99 | | amountLabel.text = $"{model.amount} {model.currency}"; |
| 1 | 100 | | paymentNetworkLabel.text = KernelConfig.i.Get().network.ToUpper(); |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | private void ShowSignMessage(Model model) |
| | 104 | | { |
| 3 | 105 | | var scene = FindScene(model.sceneId); |
| | 106 | |
|
| 3 | 107 | | if (scene != null) |
| 0 | 108 | | signPanelTitle.text = $"'{scene.GetSceneName()}', {scene.sceneData.basePosition.ToString()} wants to sign th |
| 3 | 109 | | paymentPanel.SetActive(false); |
| 3 | 110 | | signPanel.SetActive(true); |
| 3 | 111 | | signNetworkLabel.text = KernelConfig.i.Get().network.ToUpper(); |
| 3 | 112 | | messageLabel.text = model.message; |
| 3 | 113 | | } |
| | 114 | |
|
| | 115 | | public void Show(Model model) |
| | 116 | | { |
| 4 | 117 | | if (Utils.IsCursorLocked) |
| 0 | 118 | | Utils.UnlockCursor(); |
| | 119 | |
|
| 4 | 120 | | this.model = model; |
| | 121 | |
|
| 4 | 122 | | if (model.requestType == Type.REQUIRE_PAYMENT) |
| 1 | 123 | | ShowTransferMessage(model); |
| | 124 | | else |
| 3 | 125 | | ShowSignMessage(model); |
| 3 | 126 | | } |
| | 127 | |
|
| | 128 | | public void AcceptTransaction() |
| | 129 | | { |
| 0 | 130 | | OnTransactionAccepted?.Invoke(this); |
| 0 | 131 | | Destroy(gameObject); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public void RejectTransaction() |
| | 135 | | { |
| 0 | 136 | | OnTransactionRejected?.Invoke(this); |
| 0 | 137 | | Destroy(gameObject); |
| 0 | 138 | | } |
| | 139 | | } |