DebugTriggerImpl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using SRDebugger.Internal;
  4. using SRDebugger.UI.Other;
  5. using SRF;
  6. using SRF.Service;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.SceneManagement;
  10. namespace SRDebugger.Services.Implementation
  11. {
  12. [Service(typeof(IDebugTriggerService))]
  13. public class DebugTriggerImpl : SRServiceBase<IDebugTriggerService>, IDebugTriggerService
  14. {
  15. public bool IsEnabled
  16. {
  17. get
  18. {
  19. return this._trigger != null && this._trigger.CachedGameObject.activeSelf;
  20. }
  21. set
  22. {
  23. if (value && this._trigger == null)
  24. {
  25. this.CreateTrigger();
  26. }
  27. if (this._trigger != null)
  28. {
  29. this._trigger.CachedGameObject.SetActive(value);
  30. }
  31. }
  32. }
  33. public PinAlignment Position
  34. {
  35. get
  36. {
  37. return this._position;
  38. }
  39. set
  40. {
  41. if (this._trigger != null)
  42. {
  43. DebugTriggerImpl.SetTriggerPosition(this._trigger.TriggerTransform, value);
  44. }
  45. this._position = value;
  46. }
  47. }
  48. protected override void Awake()
  49. {
  50. base.Awake();
  51. UnityEngine.Object.DontDestroyOnLoad(base.CachedGameObject);
  52. base.CachedTransform.SetParent(Hierarchy.Get("SRDebugger"), true);
  53. base.name = "Trigger";
  54. }
  55. private void CreateTrigger()
  56. {
  57. TriggerRoot triggerRoot = Resources.Load<TriggerRoot>("SRDebugger/UI/Prefabs/Trigger");
  58. if (triggerRoot == null)
  59. {
  60. UnityEngine.Debug.LogError("[SRDebugger] Error loading trigger prefab");
  61. return;
  62. }
  63. this._trigger = SRInstantiate.Instantiate<TriggerRoot>(triggerRoot);
  64. this._trigger.CachedTransform.SetParent(base.CachedTransform, true);
  65. DebugTriggerImpl.SetTriggerPosition(this._trigger.TriggerTransform, this._position);
  66. switch (Settings.Instance.TriggerBehaviour)
  67. {
  68. case Settings.TriggerBehaviours.TripleTap:
  69. this._trigger.TripleTapButton.onClick.AddListener(new UnityAction(this.OnTriggerButtonClick));
  70. this._trigger.TapHoldButton.gameObject.SetActive(false);
  71. break;
  72. case Settings.TriggerBehaviours.TapAndHold:
  73. this._trigger.TapHoldButton.onLongPress.AddListener(new UnityAction(this.OnTriggerButtonClick));
  74. this._trigger.TripleTapButton.gameObject.SetActive(false);
  75. break;
  76. case Settings.TriggerBehaviours.DoubleTap:
  77. this._trigger.TripleTapButton.RequiredTapCount = 2;
  78. this._trigger.TripleTapButton.onClick.AddListener(new UnityAction(this.OnTriggerButtonClick));
  79. this._trigger.TapHoldButton.gameObject.SetActive(false);
  80. break;
  81. default:
  82. throw new Exception("Unhandled TriggerBehaviour");
  83. }
  84. SRDebuggerUtil.EnsureEventSystemExists();
  85. if (DebugTriggerImpl._003C_003Ef__mg_0024cache0 == null)
  86. {
  87. DebugTriggerImpl._003C_003Ef__mg_0024cache0 = new UnityAction<Scene, Scene>(DebugTriggerImpl.OnActiveSceneChanged);
  88. }
  89. SceneManager.activeSceneChanged += DebugTriggerImpl._003C_003Ef__mg_0024cache0;
  90. }
  91. protected override void OnDestroy()
  92. {
  93. if (DebugTriggerImpl._003C_003Ef__mg_0024cache1 == null)
  94. {
  95. DebugTriggerImpl._003C_003Ef__mg_0024cache1 = new UnityAction<Scene, Scene>(DebugTriggerImpl.OnActiveSceneChanged);
  96. }
  97. SceneManager.activeSceneChanged -= DebugTriggerImpl._003C_003Ef__mg_0024cache1;
  98. base.OnDestroy();
  99. }
  100. private static void OnActiveSceneChanged(Scene s1, Scene s2)
  101. {
  102. SRDebuggerUtil.EnsureEventSystemExists();
  103. }
  104. private void OnTriggerButtonClick()
  105. {
  106. SRDebug.Instance.ShowDebugPanel(true);
  107. }
  108. private static void SetTriggerPosition(RectTransform t, PinAlignment position)
  109. {
  110. float x = 0f;
  111. float y = 0f;
  112. float x2 = 0f;
  113. float y2 = 0f;
  114. if (position == PinAlignment.TopLeft || position == PinAlignment.TopRight || position == PinAlignment.TopCenter)
  115. {
  116. y = 1f;
  117. y2 = 1f;
  118. }
  119. else if (position == PinAlignment.BottomLeft || position == PinAlignment.BottomRight || position == PinAlignment.BottomCenter)
  120. {
  121. y = 0f;
  122. y2 = 0f;
  123. }
  124. else if (position == PinAlignment.CenterLeft || position == PinAlignment.CenterRight)
  125. {
  126. y = 0.5f;
  127. y2 = 0.5f;
  128. }
  129. if (position == PinAlignment.TopLeft || position == PinAlignment.BottomLeft || position == PinAlignment.CenterLeft)
  130. {
  131. x = 0f;
  132. x2 = 0f;
  133. }
  134. else if (position == PinAlignment.TopRight || position == PinAlignment.BottomRight || position == PinAlignment.CenterRight)
  135. {
  136. x = 1f;
  137. x2 = 1f;
  138. }
  139. else if (position == PinAlignment.TopCenter || position == PinAlignment.BottomCenter)
  140. {
  141. x = 0.5f;
  142. x2 = 0.5f;
  143. }
  144. t.pivot = new Vector2(x, y);
  145. Vector2 vector = new Vector2(x2, y2);
  146. t.anchorMin = vector;
  147. t.anchorMax = vector;
  148. }
  149. private PinAlignment _position;
  150. private TriggerRoot _trigger;
  151. [CompilerGenerated]
  152. private static UnityAction<Scene, Scene> _003C_003Ef__mg_0024cache0;
  153. [CompilerGenerated]
  154. private static UnityAction<Scene, Scene> _003C_003Ef__mg_0024cache1;
  155. }
  156. }