PinEntryServiceImpl.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using SRDebugger.Internal;
  5. using SRDebugger.UI.Controls;
  6. using SRF;
  7. using SRF.Service;
  8. using UnityEngine;
  9. namespace SRDebugger.Services.Implementation
  10. {
  11. [Service(typeof(IPinEntryService))]
  12. public class PinEntryServiceImpl : SRServiceBase<IPinEntryService>, IPinEntryService
  13. {
  14. public bool IsShowingKeypad
  15. {
  16. get
  17. {
  18. return this._isVisible;
  19. }
  20. }
  21. public void ShowPinEntry(IList<int> requiredPin, string message, PinEntryCompleteCallback callback, bool allowCancel = true)
  22. {
  23. if (this._isVisible)
  24. {
  25. throw new InvalidOperationException("Pin entry is already in progress");
  26. }
  27. this.VerifyPin(requiredPin);
  28. if (this._pinControl == null)
  29. {
  30. this.Load();
  31. }
  32. if (this._pinControl == null)
  33. {
  34. UnityEngine.Debug.LogWarning("[PinEntry] Pin entry failed loading, executing callback with fail result");
  35. callback(false);
  36. return;
  37. }
  38. this._pinControl.Clear();
  39. this._pinControl.PromptText.text = message;
  40. this._pinControl.CanCancel = allowCancel;
  41. this._callback = callback;
  42. this._requiredPin.Clear();
  43. this._requiredPin.AddRange(requiredPin);
  44. this._pinControl.Show();
  45. this._isVisible = true;
  46. SRDebuggerUtil.EnsureEventSystemExists();
  47. }
  48. [Obsolete]
  49. public void ShowPinEntry(IList<int> requiredPin, string message, PinEntryCompleteCallback callback, bool blockInput, bool allowCancel)
  50. {
  51. this.ShowPinEntry(requiredPin, message, callback, allowCancel);
  52. }
  53. protected override void Awake()
  54. {
  55. base.Awake();
  56. base.CachedTransform.SetParent(Hierarchy.Get("SRDebugger"));
  57. }
  58. private void Load()
  59. {
  60. PinEntryControl pinEntryControl = Resources.Load<PinEntryControl>("SRDebugger/UI/Prefabs/PinEntry");
  61. if (pinEntryControl == null)
  62. {
  63. UnityEngine.Debug.LogError("[PinEntry] Unable to load pin entry prefab");
  64. return;
  65. }
  66. this._pinControl = SRInstantiate.Instantiate<PinEntryControl>(pinEntryControl);
  67. this._pinControl.CachedTransform.SetParent(base.CachedTransform, false);
  68. this._pinControl.Hide();
  69. this._pinControl.Complete += this.PinControlOnComplete;
  70. }
  71. private void PinControlOnComplete(IList<int> result, bool didCancel)
  72. {
  73. bool flag = this._requiredPin.SequenceEqual(result);
  74. if (!didCancel && !flag)
  75. {
  76. this._pinControl.Clear();
  77. this._pinControl.PlayInvalidCodeAnimation();
  78. return;
  79. }
  80. this._isVisible = false;
  81. this._pinControl.Hide();
  82. if (didCancel)
  83. {
  84. this._callback(false);
  85. return;
  86. }
  87. this._callback(flag);
  88. }
  89. private void VerifyPin(IList<int> pin)
  90. {
  91. if (pin.Count != 4)
  92. {
  93. throw new ArgumentException("Pin list must have 4 elements");
  94. }
  95. for (int i = 0; i < pin.Count; i++)
  96. {
  97. if (pin[i] < 0 || pin[i] > 9)
  98. {
  99. throw new ArgumentException("Pin numbers must be >= 0 && <= 9");
  100. }
  101. }
  102. }
  103. private PinEntryCompleteCallback _callback;
  104. private bool _isVisible;
  105. private PinEntryControl _pinControl;
  106. private List<int> _requiredPin = new List<int>(4);
  107. }
  108. }