ConsoleWindow.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using BansheeEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Displays a list of log messages.
  10. /// </summary>
  11. public class ConsoleWindow : EditorWindow
  12. {
  13. /// <summary>
  14. /// Filter type that determines what kind of messages are shown in the console.
  15. /// </summary>
  16. [Flags]
  17. private enum EntryFilter
  18. {
  19. Info = 0x01, Warning = 0x02, Error = 0x04, All = Info | Warning | Error
  20. }
  21. private const int ENTRY_HEIGHT = 33;
  22. private static int sSelectedElementIdx = -1;
  23. private GUIListView<ConsoleGUIEntry, ConsoleEntryData> listView;
  24. private List<ConsoleEntryData> entries = new List<ConsoleEntryData>();
  25. private EntryFilter filter = EntryFilter.All;
  26. /// <summary>
  27. /// Opens the console window.
  28. /// </summary>
  29. [MenuItem("Windows/Console", ButtonModifier.CtrlAlt, ButtonCode.C, 6000)]
  30. private static void OpenConsoleWindow()
  31. {
  32. OpenWindow<ConsoleWindow>();
  33. }
  34. /// <inheritdoc/>
  35. protected override LocString GetDisplayName()
  36. {
  37. return new LocEdString("Console");
  38. }
  39. private void OnInitialize()
  40. {
  41. Width = 300;
  42. GUILayoutY layout = GUI.AddLayoutY();
  43. listView = new GUIListView<ConsoleGUIEntry, ConsoleEntryData>(Width, Height, ENTRY_HEIGHT, layout);
  44. Debug.OnAdded += OnEntryAdded;
  45. // TODO - Add buttons to filter info/warning/error
  46. // TODO - Add button to clear console
  47. // TODO - Add button that splits the window vertically and displays details about an entry + callstack
  48. // TODO - On entry double-click open VS at that line
  49. // TODO - On callstack entry double-click open VS at that line
  50. for (int i = 0; i < 10; i++)
  51. Debug.Log("DUMMY ENTRY #" + i);
  52. }
  53. private void OnEditorUpdate()
  54. {
  55. listView.Update();
  56. }
  57. private void OnDestroy()
  58. {
  59. Debug.OnAdded -= OnEntryAdded;
  60. }
  61. /// <inheritdoc/>
  62. protected override void WindowResized(int width, int height)
  63. {
  64. listView.SetSize(width, height);
  65. base.WindowResized(width, height);
  66. }
  67. /// <summary>
  68. /// Triggered when a new entry is added in the debug log.
  69. /// </summary>
  70. /// <param name="type">Type of the message.</param>
  71. /// <param name="message">Message string.</param>
  72. private void OnEntryAdded(DebugMessageType type, string message)
  73. {
  74. // Check if compiler message, otherwise parse it normally
  75. LogEntryData logEntry = ScriptCodeManager.ParseCompilerMessage(message);
  76. if (logEntry == null)
  77. logEntry = Debug.ParseLogMessage(message);
  78. ConsoleEntryData newEntry = new ConsoleEntryData();
  79. newEntry.type = type;
  80. newEntry.callstack = logEntry.callstack;
  81. newEntry.message = logEntry.message;
  82. entries.Add(newEntry);
  83. if (DoesFilterMatch(type))
  84. listView.AddEntry(newEntry);
  85. }
  86. /// <summary>
  87. /// Changes the filter that controls what type of messages are displayed in the console.
  88. /// </summary>
  89. /// <param name="filter">Flags that control which type of messages should be displayed.</param>
  90. private void SetFilter(EntryFilter filter)
  91. {
  92. if (this.filter == filter)
  93. return;
  94. listView.Clear();
  95. foreach (var entry in entries)
  96. {
  97. if (DoesFilterMatch(entry.type))
  98. listView.AddEntry(entry);
  99. }
  100. this.filter = filter;
  101. }
  102. /// <summary>
  103. /// Checks if the currently active entry filter matches the provided type (i.e. the entry with the type should be
  104. /// displayed).
  105. /// </summary>
  106. /// <param name="type">Type of the entry to check.</param>
  107. /// <returns>True if the entry with the specified type should be displayed in the console.</returns>
  108. private bool DoesFilterMatch(DebugMessageType type)
  109. {
  110. switch (type)
  111. {
  112. case DebugMessageType.Info:
  113. return filter.HasFlag(EntryFilter.Info);
  114. case DebugMessageType.Warning:
  115. return filter.HasFlag(EntryFilter.Warning);
  116. case DebugMessageType.Error:
  117. return filter.HasFlag(EntryFilter.Error);
  118. }
  119. return false;
  120. }
  121. /// <summary>
  122. /// Removes all entries from the console.
  123. /// </summary>
  124. private void Clear()
  125. {
  126. listView.Clear();
  127. entries.Clear();
  128. }
  129. /// <summary>
  130. /// Contains data for a single entry in the console.
  131. /// </summary>
  132. private class ConsoleEntryData : GUIListViewData
  133. {
  134. public DebugMessageType type;
  135. public string message;
  136. public CallStackEntry[] callstack;
  137. }
  138. /// <summary>
  139. /// Contains GUI elements used for displaying a single entry in the console.
  140. /// </summary>
  141. private class ConsoleGUIEntry : GUIListViewEntry<ConsoleEntryData>
  142. {
  143. private const int CALLER_LABEL_HEIGHT = 11;
  144. private const int MESSAGE_HEIGHT = ENTRY_HEIGHT - CALLER_LABEL_HEIGHT;
  145. private static readonly Color BG_COLOR = Color.DarkGray;
  146. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  147. private GUIPanel overlay;
  148. private GUIPanel main;
  149. private GUIPanel underlay;
  150. private GUILabel messageLabel;
  151. private GUILabel functionLabel;
  152. private GUITexture background;
  153. private int entryIdx;
  154. private string file;
  155. private int line;
  156. /// <inheritdoc/>
  157. public override void BuildGUI()
  158. {
  159. main = Layout.AddPanel(0, 1, 1, GUIOption.FixedHeight(ENTRY_HEIGHT));
  160. overlay = main.AddPanel(-1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  161. underlay = main.AddPanel(1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  162. GUILayoutY mainLayout = main.AddLayoutY();
  163. GUILayoutY overlayLayout = overlay.AddLayoutY();
  164. GUILayoutY underlayLayout = underlay.AddLayoutY();
  165. messageLabel = new GUILabel(new LocEdString(""), EditorStyles.MultiLineLabel, GUIOption.FixedHeight(MESSAGE_HEIGHT));
  166. functionLabel = new GUILabel(new LocEdString(""), GUIOption.FixedHeight(CALLER_LABEL_HEIGHT));
  167. mainLayout.AddElement(messageLabel);
  168. mainLayout.AddElement(functionLabel);
  169. background = new GUITexture(Builtin.WhiteTexture, GUIOption.FixedHeight(ENTRY_HEIGHT));
  170. underlayLayout.AddElement(background);
  171. GUIButton button = new GUIButton(new LocEdString(""), EditorStyles.Blank, GUIOption.FixedHeight(ENTRY_HEIGHT));
  172. overlayLayout.AddElement(button);
  173. button.OnClick += OnClicked;
  174. button.OnDoubleClick += OnDoubleClicked;
  175. }
  176. /// <inheritdoc/>
  177. public override void UpdateContents(int index, ConsoleEntryData data)
  178. {
  179. if (index != sSelectedElementIdx)
  180. {
  181. if (index%2 != 0)
  182. {
  183. background.Visible = true;
  184. background.SetTint(BG_COLOR);
  185. }
  186. else
  187. {
  188. background.Visible = false;
  189. }
  190. }
  191. else
  192. {
  193. background.Visible = true;
  194. background.SetTint(SELECTION_COLOR);
  195. }
  196. messageLabel.SetContent(new LocEdString(data.message));
  197. string method = "";
  198. if (data.callstack != null && data.callstack.Length > 0)
  199. {
  200. file = data.callstack[0].file;
  201. line = data.callstack[0].line;
  202. if (string.IsNullOrEmpty(data.callstack[0].method))
  203. method = "\tat " + file + ":" + line;
  204. else
  205. method = "\t" + data.callstack[0].method + " at " + file + ":" + line;
  206. }
  207. else
  208. {
  209. file = "";
  210. line = 0;
  211. }
  212. functionLabel.SetContent(new LocEdString(method));
  213. entryIdx = index;
  214. }
  215. /// <summary>
  216. /// Triggered when the entry is selected.
  217. /// </summary>
  218. private void OnClicked()
  219. {
  220. sSelectedElementIdx = entryIdx;
  221. RefreshEntries();
  222. }
  223. /// <summary>
  224. /// Triggered when the entry is double-clicked.
  225. /// </summary>
  226. private void OnDoubleClicked()
  227. {
  228. if(!string.IsNullOrEmpty(file))
  229. CodeEditor.OpenFile(file, line);
  230. }
  231. }
  232. }
  233. }