ConsoleLogControl.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections;
  3. using SRDebugger.Internal;
  4. using SRDebugger.Services;
  5. using SRF;
  6. using SRF.UI.Layout;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.UI;
  10. namespace SRDebugger.UI.Controls
  11. {
  12. public class ConsoleLogControl : SRMonoBehaviourEx
  13. {
  14. public bool ShowErrors
  15. {
  16. get
  17. {
  18. return this._showErrors;
  19. }
  20. set
  21. {
  22. this._showErrors = value;
  23. this.SetIsDirty();
  24. }
  25. }
  26. public bool ShowWarnings
  27. {
  28. get
  29. {
  30. return this._showWarnings;
  31. }
  32. set
  33. {
  34. this._showWarnings = value;
  35. this.SetIsDirty();
  36. }
  37. }
  38. public bool ShowInfo
  39. {
  40. get
  41. {
  42. return this._showInfo;
  43. }
  44. set
  45. {
  46. this._showInfo = value;
  47. this.SetIsDirty();
  48. }
  49. }
  50. public bool EnableSelection
  51. {
  52. get
  53. {
  54. return this._consoleScrollLayoutGroup.EnableSelection;
  55. }
  56. set
  57. {
  58. this._consoleScrollLayoutGroup.EnableSelection = value;
  59. }
  60. }
  61. public string Filter
  62. {
  63. get
  64. {
  65. return this._filter;
  66. }
  67. set
  68. {
  69. if (this._filter != value)
  70. {
  71. this._filter = value;
  72. this._isDirty = true;
  73. }
  74. }
  75. }
  76. protected override void Awake()
  77. {
  78. base.Awake();
  79. this._consoleScrollLayoutGroup.SelectedItemChanged.AddListener(new UnityAction<object>(this.OnSelectedItemChanged));
  80. Service.Console.Updated += this.ConsoleOnUpdated;
  81. }
  82. protected override void Start()
  83. {
  84. base.Start();
  85. this.SetIsDirty();
  86. base.StartCoroutine(this.ScrollToBottom());
  87. }
  88. private IEnumerator ScrollToBottom()
  89. {
  90. yield return new WaitForEndOfFrame();
  91. yield return new WaitForEndOfFrame();
  92. yield return new WaitForEndOfFrame();
  93. this._scrollPosition = new Vector2?(new Vector2(0f, 0f));
  94. yield break;
  95. }
  96. protected override void OnDestroy()
  97. {
  98. if (Service.Console != null)
  99. {
  100. Service.Console.Updated -= this.ConsoleOnUpdated;
  101. }
  102. base.OnDestroy();
  103. }
  104. private void OnSelectedItemChanged(object arg0)
  105. {
  106. ConsoleEntry obj = arg0 as ConsoleEntry;
  107. if (this.SelectedItemChanged != null)
  108. {
  109. this.SelectedItemChanged(obj);
  110. }
  111. }
  112. protected override void Update()
  113. {
  114. base.Update();
  115. if (this._scrollPosition != null)
  116. {
  117. this._consoleScrollRect.normalizedPosition = this._scrollPosition.Value;
  118. this._scrollPosition = null;
  119. }
  120. if (this._isDirty)
  121. {
  122. this.Refresh();
  123. }
  124. }
  125. private void Refresh()
  126. {
  127. if (this._consoleScrollRect.normalizedPosition.y.ApproxZero())
  128. {
  129. this._scrollPosition = new Vector2?(this._consoleScrollRect.normalizedPosition);
  130. }
  131. this._consoleScrollLayoutGroup.ClearItems();
  132. IReadOnlyList<ConsoleEntry> entries = Service.Console.Entries;
  133. for (int i = 0; i < entries.Count; i++)
  134. {
  135. ConsoleEntry consoleEntry = entries[i];
  136. if ((consoleEntry.LogType == LogType.Error || consoleEntry.LogType == LogType.Exception || consoleEntry.LogType == LogType.Assert) && !this.ShowErrors)
  137. {
  138. if (consoleEntry == this._consoleScrollLayoutGroup.SelectedItem)
  139. {
  140. this._consoleScrollLayoutGroup.SelectedItem = null;
  141. }
  142. }
  143. else if (consoleEntry.LogType == LogType.Warning && !this.ShowWarnings)
  144. {
  145. if (consoleEntry == this._consoleScrollLayoutGroup.SelectedItem)
  146. {
  147. this._consoleScrollLayoutGroup.SelectedItem = null;
  148. }
  149. }
  150. else if (consoleEntry.LogType == LogType.Log && !this.ShowInfo)
  151. {
  152. if (consoleEntry == this._consoleScrollLayoutGroup.SelectedItem)
  153. {
  154. this._consoleScrollLayoutGroup.SelectedItem = null;
  155. }
  156. }
  157. else if (!string.IsNullOrEmpty(this.Filter) && consoleEntry.Message.IndexOf(this.Filter, StringComparison.OrdinalIgnoreCase) < 0)
  158. {
  159. if (consoleEntry == this._consoleScrollLayoutGroup.SelectedItem)
  160. {
  161. this._consoleScrollLayoutGroup.SelectedItem = null;
  162. }
  163. }
  164. else
  165. {
  166. this._consoleScrollLayoutGroup.AddItem(consoleEntry);
  167. }
  168. }
  169. this._isDirty = false;
  170. }
  171. private void SetIsDirty()
  172. {
  173. this._isDirty = true;
  174. }
  175. private void ConsoleOnUpdated(IConsoleService console)
  176. {
  177. this.SetIsDirty();
  178. }
  179. [RequiredField]
  180. [SerializeField]
  181. private VirtualVerticalLayoutGroup _consoleScrollLayoutGroup;
  182. [RequiredField]
  183. [SerializeField]
  184. private ScrollRect _consoleScrollRect;
  185. private bool _isDirty;
  186. private Vector2? _scrollPosition;
  187. private bool _showErrors = true;
  188. private bool _showInfo = true;
  189. private bool _showWarnings = true;
  190. public Action<ConsoleEntry> SelectedItemChanged;
  191. private string _filter;
  192. }
  193. }