DebugPanelServiceImpl.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Diagnostics;
  3. using SRDebugger.Internal;
  4. using SRDebugger.UI;
  5. using SRF;
  6. using SRF.Service;
  7. using UnityEngine;
  8. namespace SRDebugger.Services.Implementation
  9. {
  10. [Service(typeof(IDebugPanelService))]
  11. public class DebugPanelServiceImpl : ScriptableObject, IDebugPanelService
  12. {
  13. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  14. public event Action<IDebugPanelService, bool> VisibilityChanged;
  15. public DebugPanelRoot RootObject
  16. {
  17. get
  18. {
  19. return this._debugPanelRootObject;
  20. }
  21. }
  22. public bool IsLoaded
  23. {
  24. get
  25. {
  26. return this._debugPanelRootObject != null;
  27. }
  28. }
  29. public bool IsVisible
  30. {
  31. get
  32. {
  33. return this.IsLoaded && this._isVisible;
  34. }
  35. set
  36. {
  37. if (this._isVisible == value)
  38. {
  39. return;
  40. }
  41. if (value)
  42. {
  43. if (!this.IsLoaded)
  44. {
  45. this.Load();
  46. }
  47. SRDebuggerUtil.EnsureEventSystemExists();
  48. this._debugPanelRootObject.CanvasGroup.alpha = 1f;
  49. this._debugPanelRootObject.CanvasGroup.interactable = true;
  50. this._debugPanelRootObject.CanvasGroup.blocksRaycasts = true;
  51. this._cursorWasVisible = new bool?(Cursor.visible);
  52. this._cursorLockMode = new CursorLockMode?(Cursor.lockState);
  53. if (Settings.Instance.AutomaticallyShowCursor)
  54. {
  55. Cursor.visible = true;
  56. Cursor.lockState = CursorLockMode.None;
  57. }
  58. }
  59. else
  60. {
  61. if (this.IsLoaded)
  62. {
  63. this._debugPanelRootObject.CanvasGroup.alpha = 0f;
  64. this._debugPanelRootObject.CanvasGroup.interactable = false;
  65. this._debugPanelRootObject.CanvasGroup.blocksRaycasts = false;
  66. }
  67. if (this._cursorWasVisible != null)
  68. {
  69. Cursor.visible = this._cursorWasVisible.Value;
  70. this._cursorWasVisible = null;
  71. }
  72. if (this._cursorLockMode != null)
  73. {
  74. Cursor.lockState = this._cursorLockMode.Value;
  75. this._cursorLockMode = null;
  76. }
  77. }
  78. this._isVisible = value;
  79. if (this.VisibilityChanged != null)
  80. {
  81. this.VisibilityChanged(this, this._isVisible);
  82. }
  83. }
  84. }
  85. public DefaultTabs? ActiveTab
  86. {
  87. get
  88. {
  89. if (this._debugPanelRootObject == null)
  90. {
  91. return null;
  92. }
  93. return this._debugPanelRootObject.TabController.ActiveTab;
  94. }
  95. }
  96. public void OpenTab(DefaultTabs tab)
  97. {
  98. if (!this.IsVisible)
  99. {
  100. this.IsVisible = true;
  101. }
  102. this._debugPanelRootObject.TabController.OpenTab(tab);
  103. }
  104. public void Unload()
  105. {
  106. if (this._debugPanelRootObject == null)
  107. {
  108. return;
  109. }
  110. this.IsVisible = false;
  111. this._debugPanelRootObject.CachedGameObject.SetActive(false);
  112. UnityEngine.Object.Destroy(this._debugPanelRootObject.CachedGameObject);
  113. this._debugPanelRootObject = null;
  114. }
  115. private void Load()
  116. {
  117. DebugPanelRoot debugPanelRoot = Resources.Load<DebugPanelRoot>("SRDebugger/UI/Prefabs/DebugPanel");
  118. if (debugPanelRoot == null)
  119. {
  120. UnityEngine.Debug.LogError("[SRDebugger] Error loading debug panel prefab");
  121. return;
  122. }
  123. this._debugPanelRootObject = SRInstantiate.Instantiate<DebugPanelRoot>(debugPanelRoot);
  124. this._debugPanelRootObject.name = "Panel";
  125. UnityEngine.Object.DontDestroyOnLoad(this._debugPanelRootObject);
  126. this._debugPanelRootObject.CachedTransform.SetParent(Hierarchy.Get("SRDebugger"), true);
  127. SRDebuggerUtil.EnsureEventSystemExists();
  128. }
  129. private DebugPanelRoot _debugPanelRootObject;
  130. private bool _isVisible;
  131. private bool? _cursorWasVisible;
  132. private CursorLockMode? _cursorLockMode;
  133. }
  134. }