DebugTriggerImpl.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. namespace SRDebugger.Services.Implementation
  2. {
  3. using System;
  4. using Internal;
  5. using SRF;
  6. using SRF.Service;
  7. using UI.Other;
  8. using UnityEngine;
  9. [Service(typeof (IDebugTriggerService))]
  10. public class DebugTriggerImpl : SRServiceBase<IDebugTriggerService>, IDebugTriggerService
  11. {
  12. private PinAlignment _position;
  13. private TriggerRoot _trigger;
  14. private IConsoleService _consoleService;
  15. private bool _showErrorNotification;
  16. public bool IsEnabled
  17. {
  18. get { return _trigger != null && _trigger.CachedGameObject.activeSelf; }
  19. set
  20. {
  21. // Create trigger if it does not yet exist
  22. if (value && _trigger == null)
  23. {
  24. CreateTrigger();
  25. }
  26. if (_trigger != null)
  27. {
  28. _trigger.CachedGameObject.SetActive(value);
  29. }
  30. }
  31. }
  32. public bool ShowErrorNotification
  33. {
  34. get
  35. {
  36. return _showErrorNotification;
  37. }
  38. set
  39. {
  40. if (_showErrorNotification == value) return;
  41. _showErrorNotification = value;
  42. if (_trigger == null) return;
  43. if(_showErrorNotification)
  44. {
  45. _consoleService = SRServiceManager.GetService<IConsoleService>();
  46. _consoleService.Error += OnError;
  47. }
  48. else
  49. {
  50. _consoleService.Error -= OnError;
  51. _consoleService = null;
  52. }
  53. }
  54. }
  55. public PinAlignment Position
  56. {
  57. get { return _position; }
  58. set
  59. {
  60. if (_trigger != null)
  61. {
  62. SetTriggerPosition(_trigger.TriggerTransform, value);
  63. }
  64. _position = value;
  65. }
  66. }
  67. protected override void Awake()
  68. {
  69. base.Awake();
  70. DontDestroyOnLoad(CachedGameObject);
  71. CachedTransform.SetParent(Hierarchy.Get("SRDebugger"), true);
  72. ShowErrorNotification = Settings.Instance.ErrorNotification;
  73. name = "Trigger";
  74. }
  75. private void OnError(IConsoleService console)
  76. {
  77. if (_trigger != null)
  78. {
  79. _trigger.ErrorNotifier.ShowErrorWarning();
  80. }
  81. }
  82. private void CreateTrigger()
  83. {
  84. var prefab = Resources.Load<TriggerRoot>(SRDebugPaths.TriggerPrefabPath);
  85. if (prefab == null)
  86. {
  87. Debug.LogError("[SRDebugger] Error loading trigger prefab");
  88. return;
  89. }
  90. _trigger = SRInstantiate.Instantiate(prefab);
  91. _trigger.CachedTransform.SetParent(CachedTransform, true);
  92. SetTriggerPosition(_trigger.TriggerTransform, _position);
  93. switch (Settings.Instance.TriggerBehaviour)
  94. {
  95. case Settings.TriggerBehaviours.TripleTap:
  96. {
  97. _trigger.TripleTapButton.onClick.AddListener(OnTriggerButtonClick);
  98. _trigger.TapHoldButton.gameObject.SetActive(false);
  99. break;
  100. }
  101. case Settings.TriggerBehaviours.TapAndHold:
  102. {
  103. _trigger.TapHoldButton.onLongPress.AddListener(OnTriggerButtonClick);
  104. _trigger.TripleTapButton.gameObject.SetActive(false);
  105. break;
  106. }
  107. case Settings.TriggerBehaviours.DoubleTap:
  108. {
  109. _trigger.TripleTapButton.RequiredTapCount = 2;
  110. _trigger.TripleTapButton.onClick.AddListener(OnTriggerButtonClick);
  111. _trigger.TapHoldButton.gameObject.SetActive(false);
  112. break;
  113. }
  114. default:
  115. throw new Exception("Unhandled TriggerBehaviour");
  116. }
  117. SRDebuggerUtil.EnsureEventSystemExists();
  118. UnityEngine.SceneManagement.SceneManager.activeSceneChanged += OnActiveSceneChanged;
  119. if (_showErrorNotification)
  120. {
  121. _consoleService = SRServiceManager.GetService<IConsoleService>();
  122. _consoleService.Error += OnError;
  123. }
  124. }
  125. protected override void OnDestroy()
  126. {
  127. UnityEngine.SceneManagement.SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  128. if (_consoleService != null)
  129. {
  130. _consoleService.Error -= OnError;
  131. }
  132. base.OnDestroy();
  133. }
  134. private static void OnActiveSceneChanged(UnityEngine.SceneManagement.Scene s1, UnityEngine.SceneManagement.Scene s2)
  135. {
  136. SRDebuggerUtil.EnsureEventSystemExists();
  137. }
  138. private void OnTriggerButtonClick()
  139. {
  140. if (_trigger.ErrorNotifier.IsVisible)
  141. {
  142. // Open into console if there is an error.
  143. SRDebug.Instance.ShowDebugPanel(DefaultTabs.Console);
  144. }
  145. else
  146. {
  147. SRDebug.Instance.ShowDebugPanel();
  148. }
  149. }
  150. private static void SetTriggerPosition(RectTransform t, PinAlignment position)
  151. {
  152. var pivotX = 0f;
  153. var pivotY = 0f;
  154. var posX = 0f;
  155. var posY = 0f;
  156. if (position == PinAlignment.TopLeft || position == PinAlignment.TopRight || position == PinAlignment.TopCenter)
  157. {
  158. pivotY = 1f;
  159. posY = 1f;
  160. }
  161. else if (position == PinAlignment.BottomLeft || position == PinAlignment.BottomRight || position == PinAlignment.BottomCenter)
  162. {
  163. pivotY = 0f;
  164. posY = 0f;
  165. } else if (position == PinAlignment.CenterLeft || position == PinAlignment.CenterRight)
  166. {
  167. pivotY = 0.5f;
  168. posY = 0.5f;
  169. }
  170. if (position == PinAlignment.TopLeft || position == PinAlignment.BottomLeft || position == PinAlignment.CenterLeft)
  171. {
  172. pivotX = 0f;
  173. posX = 0f;
  174. }
  175. else if (position == PinAlignment.TopRight || position == PinAlignment.BottomRight || position == PinAlignment.CenterRight)
  176. {
  177. pivotX = 1f;
  178. posX = 1f;
  179. } else if (position == PinAlignment.TopCenter || position == PinAlignment.BottomCenter)
  180. {
  181. pivotX = 0.5f;
  182. posX = 0.5f;
  183. }
  184. t.pivot = new Vector2(pivotX, pivotY);
  185. t.anchorMax = t.anchorMin = new Vector2(posX, posY);
  186. }
  187. }
  188. }