PinEntryControl.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using SRF;
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using UnityEngine.UI;
  9. namespace SRDebugger.UI.Controls
  10. {
  11. public class PinEntryControl : SRMonoBehaviourEx
  12. {
  13. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  14. public event PinEntryControlCallback Complete;
  15. protected override void Awake()
  16. {
  17. base.Awake();
  18. for (int i = 0; i < this.NumberButtons.Length; i++)
  19. {
  20. int number = i;
  21. this.NumberButtons[i].onClick.AddListener(delegate()
  22. {
  23. this.PushNumber(number);
  24. });
  25. }
  26. this.CancelButton.onClick.AddListener(new UnityAction(this.CancelButtonPressed));
  27. this.RefreshState();
  28. }
  29. protected override void Update()
  30. {
  31. base.Update();
  32. if (!this._isVisible)
  33. {
  34. return;
  35. }
  36. if (this._numbers.Count > 0 && (UnityEngine.Input.GetKeyDown(KeyCode.Backspace) || UnityEngine.Input.GetKeyDown(KeyCode.Delete)))
  37. {
  38. this._numbers.PopLast<int>();
  39. this.RefreshState();
  40. }
  41. string inputString = Input.inputString;
  42. for (int i = 0; i < inputString.Length; i++)
  43. {
  44. if (char.IsNumber(inputString, i))
  45. {
  46. int num = (int)char.GetNumericValue(inputString, i);
  47. if (num <= 9 && num >= 0)
  48. {
  49. this.PushNumber(num);
  50. }
  51. }
  52. }
  53. }
  54. public void Show()
  55. {
  56. this.CanvasGroup.alpha = 1f;
  57. CanvasGroup canvasGroup = this.CanvasGroup;
  58. bool flag = true;
  59. this.CanvasGroup.interactable = flag;
  60. canvasGroup.blocksRaycasts = flag;
  61. this._isVisible = true;
  62. }
  63. public void Hide()
  64. {
  65. this.CanvasGroup.alpha = 0f;
  66. CanvasGroup canvasGroup = this.CanvasGroup;
  67. bool flag = false;
  68. this.CanvasGroup.interactable = flag;
  69. canvasGroup.blocksRaycasts = flag;
  70. this._isVisible = false;
  71. }
  72. public void Clear()
  73. {
  74. this._numbers.Clear();
  75. this.RefreshState();
  76. }
  77. public void PlayInvalidCodeAnimation()
  78. {
  79. this.DotAnimator.SetTrigger("Invalid");
  80. }
  81. protected void OnComplete()
  82. {
  83. if (this.Complete != null)
  84. {
  85. this.Complete(new ReadOnlyCollection<int>(this._numbers), false);
  86. }
  87. }
  88. protected void OnCancel()
  89. {
  90. if (this.Complete != null)
  91. {
  92. this.Complete(new int[0], true);
  93. }
  94. }
  95. private void CancelButtonPressed()
  96. {
  97. if (this._numbers.Count > 0)
  98. {
  99. this._numbers.PopLast<int>();
  100. }
  101. else
  102. {
  103. this.OnCancel();
  104. }
  105. this.RefreshState();
  106. }
  107. public void PushNumber(int number)
  108. {
  109. if (this._numbers.Count >= 4)
  110. {
  111. UnityEngine.Debug.LogWarning("[PinEntry] Expected 4 numbers");
  112. return;
  113. }
  114. this._numbers.Add(number);
  115. if (this._numbers.Count >= 4)
  116. {
  117. this.OnComplete();
  118. }
  119. this.RefreshState();
  120. }
  121. private void RefreshState()
  122. {
  123. for (int i = 0; i < this.NumberDots.Length; i++)
  124. {
  125. this.NumberDots[i].isOn = (i < this._numbers.Count);
  126. }
  127. if (this._numbers.Count > 0)
  128. {
  129. this.CancelButtonText.text = "Delete";
  130. }
  131. else
  132. {
  133. this.CancelButtonText.text = ((!this.CanCancel) ? string.Empty : "Cancel");
  134. }
  135. }
  136. private bool _isVisible = true;
  137. private List<int> _numbers = new List<int>(4);
  138. [RequiredField]
  139. public Image Background;
  140. public bool CanCancel = true;
  141. [RequiredField]
  142. public Button CancelButton;
  143. [RequiredField]
  144. public Text CancelButtonText;
  145. [RequiredField]
  146. public CanvasGroup CanvasGroup;
  147. [RequiredField]
  148. public Animator DotAnimator;
  149. public Button[] NumberButtons;
  150. public Toggle[] NumberDots;
  151. [RequiredField]
  152. public Text PromptText;
  153. }
  154. }