ConsoleLogControl.cs 5.2 KB

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