ConsoleWindow.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 TITLE_HEIGHT = 21;
  22. private const int ENTRY_HEIGHT = 39;
  23. private static int sSelectedElementIdx = -1;
  24. private GUIListView<ConsoleGUIEntry, ConsoleEntryData> listView;
  25. private List<ConsoleEntryData> entries = new List<ConsoleEntryData>();
  26. private List<ConsoleEntryData> filteredEntries = new List<ConsoleEntryData>();
  27. private EntryFilter filter = EntryFilter.All;
  28. private GUIScrollArea detailsArea;
  29. private int dbg_disable = 0;
  30. /// <summary>
  31. /// Opens the console window.
  32. /// </summary>
  33. [MenuItem("Windows/Console", ButtonModifier.CtrlAlt, ButtonCode.C, 6000)]
  34. private static void OpenConsoleWindow()
  35. {
  36. OpenWindow<ConsoleWindow>();
  37. }
  38. /// <inheritdoc/>
  39. protected override LocString GetDisplayName()
  40. {
  41. return new LocEdString("Console");
  42. }
  43. private void OnInitialize()
  44. {
  45. Width = 300;
  46. GUILayoutY layout = GUI.AddLayoutY();
  47. GUILayoutX titleLayout = layout.AddLayoutX();
  48. GUIToggle infoBtn = new GUIToggle(new GUIContent(EditorBuiltin.GetLogIcon(LogIcon.Info, 16)), EditorStyles.Button);
  49. GUIToggle warningBtn = new GUIToggle(new GUIContent(EditorBuiltin.GetLogIcon(LogIcon.Warning, 16)), EditorStyles.Button);
  50. GUIToggle errorBtn = new GUIToggle(new GUIContent(EditorBuiltin.GetLogIcon(LogIcon.Error, 16)), EditorStyles.Button);
  51. GUIToggle detailsBtn = new GUIToggle(new LocEdString("Show details"), EditorStyles.Button);
  52. GUIButton clearBtn = new GUIButton(new LocEdString("Clear"));
  53. titleLayout.AddElement(infoBtn);
  54. titleLayout.AddElement(warningBtn);
  55. titleLayout.AddElement(errorBtn);
  56. titleLayout.AddFlexibleSpace();
  57. titleLayout.AddElement(detailsBtn);
  58. titleLayout.AddElement(clearBtn);
  59. infoBtn.Value = filter.HasFlag(EntryFilter.Info);
  60. warningBtn.Value = filter.HasFlag(EntryFilter.Warning);
  61. errorBtn.Value = filter.HasFlag(EntryFilter.Error);
  62. infoBtn.OnToggled += x =>
  63. {
  64. if (x)
  65. SetFilter(filter | EntryFilter.Info);
  66. else
  67. SetFilter(filter & ~EntryFilter.Info);
  68. };
  69. warningBtn.OnToggled += x =>
  70. {
  71. if (x)
  72. SetFilter(filter | EntryFilter.Warning);
  73. else
  74. SetFilter(filter & ~EntryFilter.Warning);
  75. };
  76. errorBtn.OnToggled += x =>
  77. {
  78. if (x)
  79. SetFilter(filter | EntryFilter.Error);
  80. else
  81. SetFilter(filter & ~EntryFilter.Error);
  82. };
  83. detailsBtn.OnToggled += ToggleDetailsPanel;
  84. clearBtn.OnClick += Clear;
  85. GUILayoutX mainLayout = layout.AddLayoutX();
  86. listView = new GUIListView<ConsoleGUIEntry, ConsoleEntryData>(Width, Height - TITLE_HEIGHT, ENTRY_HEIGHT, mainLayout);
  87. detailsArea = new GUIScrollArea();
  88. mainLayout.AddElement(detailsArea);
  89. detailsArea.Active = false;
  90. Debug.OnAdded += OnEntryAdded;
  91. // DEBUG ONLY
  92. for (int i = 0; i < 10; i++)
  93. Debug.Log("DUMMY ENTRY #" + i);
  94. }
  95. private void OnEditorUpdate()
  96. {
  97. listView.Update();
  98. dbg_disable++;
  99. }
  100. private void OnDestroy()
  101. {
  102. Debug.OnAdded -= OnEntryAdded;
  103. }
  104. /// <inheritdoc/>
  105. protected override void WindowResized(int width, int height)
  106. {
  107. listView.SetSize(width, height);
  108. base.WindowResized(width, height);
  109. }
  110. /// <summary>
  111. /// Triggered when a new entry is added in the debug log.
  112. /// </summary>
  113. /// <param name="type">Type of the message.</param>
  114. /// <param name="message">Message string.</param>
  115. private void OnEntryAdded(DebugMessageType type, string message)
  116. {
  117. if (dbg_disable > 1)
  118. return;
  119. // Check if compiler message, otherwise parse it normally
  120. LogEntryData logEntry = ScriptCodeManager.ParseCompilerMessage(message);
  121. if (logEntry == null)
  122. logEntry = Debug.ParseLogMessage(message);
  123. ConsoleEntryData newEntry = new ConsoleEntryData();
  124. newEntry.type = type;
  125. newEntry.callstack = logEntry.callstack;
  126. newEntry.message = logEntry.message;
  127. entries.Add(newEntry);
  128. if (DoesFilterMatch(type))
  129. {
  130. listView.AddEntry(newEntry);
  131. filteredEntries.Add(newEntry);
  132. }
  133. }
  134. /// <summary>
  135. /// Changes the filter that controls what type of messages are displayed in the console.
  136. /// </summary>
  137. /// <param name="filter">Flags that control which type of messages should be displayed.</param>
  138. private void SetFilter(EntryFilter filter)
  139. {
  140. if (this.filter == filter)
  141. return;
  142. this.filter = filter;
  143. listView.Clear();
  144. filteredEntries.Clear();
  145. foreach (var entry in entries)
  146. {
  147. if (DoesFilterMatch(entry.type))
  148. {
  149. listView.AddEntry(entry);
  150. filteredEntries.Add(entry);
  151. }
  152. }
  153. sSelectedElementIdx = -1;
  154. }
  155. /// <summary>
  156. /// Checks if the currently active entry filter matches the provided type (i.e. the entry with the type should be
  157. /// displayed).
  158. /// </summary>
  159. /// <param name="type">Type of the entry to check.</param>
  160. /// <returns>True if the entry with the specified type should be displayed in the console.</returns>
  161. private bool DoesFilterMatch(DebugMessageType type)
  162. {
  163. switch (type)
  164. {
  165. case DebugMessageType.Info:
  166. return filter.HasFlag(EntryFilter.Info);
  167. case DebugMessageType.Warning:
  168. return filter.HasFlag(EntryFilter.Warning);
  169. case DebugMessageType.Error:
  170. return filter.HasFlag(EntryFilter.Error);
  171. }
  172. return false;
  173. }
  174. /// <summary>
  175. /// Removes all entries from the console.
  176. /// </summary>
  177. private void Clear()
  178. {
  179. listView.Clear();
  180. entries.Clear();
  181. filteredEntries.Clear();
  182. sSelectedElementIdx = -1;
  183. }
  184. /// <summary>
  185. /// Shows or hides the details panel.
  186. /// </summary>
  187. /// <param name="show">True to show, false to hide.</param>
  188. private void ToggleDetailsPanel(bool show)
  189. {
  190. detailsArea.Active = show;
  191. if (show)
  192. RefreshDetailsPanel();
  193. }
  194. /// <summary>
  195. /// Updates the contents of the details panel according to the currently selected element.
  196. /// </summary>
  197. private void RefreshDetailsPanel()
  198. {
  199. detailsArea.Layout.Clear();
  200. if (sSelectedElementIdx != -1)
  201. {
  202. ConsoleEntryData entry = filteredEntries[sSelectedElementIdx];
  203. LocString message = new LocEdString(entry.message);
  204. GUILabel messageLabel = new GUILabel(message);
  205. detailsArea.Layout.AddElement(messageLabel);
  206. detailsArea.Layout.AddSpace(10);
  207. if (entry.callstack != null)
  208. {
  209. foreach (var call in entry.callstack)
  210. {
  211. string callMessage;
  212. if (string.IsNullOrEmpty(call.method))
  213. callMessage = "\tat " + call.file + ":" + call.line;
  214. else
  215. callMessage = "\t" + call.method + " at " + call.file + ":" + call.line;
  216. GUIButton callBtn = new GUIButton(new LocEdString(callMessage));
  217. detailsArea.Layout.AddElement(callBtn);
  218. CallStackEntry hoistedCall = call;
  219. callBtn.OnClick += () =>
  220. {
  221. CodeEditor.OpenFile(hoistedCall.file, hoistedCall.line);
  222. };
  223. }
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Contains data for a single entry in the console.
  229. /// </summary>
  230. private class ConsoleEntryData : GUIListViewData
  231. {
  232. public DebugMessageType type;
  233. public string message;
  234. public CallStackEntry[] callstack;
  235. }
  236. /// <summary>
  237. /// Contains GUI elements used for displaying a single entry in the console.
  238. /// </summary>
  239. private class ConsoleGUIEntry : GUIListViewEntry<ConsoleEntryData>
  240. {
  241. private const int CALLER_LABEL_HEIGHT = 11;
  242. private const int PADDING = 3;
  243. private const int MESSAGE_HEIGHT = ENTRY_HEIGHT - CALLER_LABEL_HEIGHT - PADDING * 2;
  244. private static readonly Color BG_COLOR = Color.DarkGray;
  245. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  246. private GUIPanel overlay;
  247. private GUIPanel main;
  248. private GUIPanel underlay;
  249. private GUITexture icon;
  250. private GUILabel messageLabel;
  251. private GUILabel functionLabel;
  252. private GUITexture background;
  253. private int entryIdx;
  254. private string file;
  255. private int line;
  256. /// <inheritdoc/>
  257. public override void BuildGUI()
  258. {
  259. main = Layout.AddPanel(0, 1, 1, GUIOption.FixedHeight(ENTRY_HEIGHT));
  260. overlay = main.AddPanel(-1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  261. underlay = main.AddPanel(1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  262. GUILayoutX mainLayout = main.AddLayoutX();
  263. GUILayoutY overlayLayout = overlay.AddLayoutY();
  264. GUILayoutY underlayLayout = underlay.AddLayoutY();
  265. icon = new GUITexture(null);
  266. messageLabel = new GUILabel(new LocEdString(""), EditorStyles.MultiLineLabel, GUIOption.FixedHeight(MESSAGE_HEIGHT));
  267. functionLabel = new GUILabel(new LocEdString(""), GUIOption.FixedHeight(CALLER_LABEL_HEIGHT));
  268. GUILayoutY iconLayout = mainLayout.AddLayoutY();
  269. mainLayout.AddSpace(PADDING);
  270. iconLayout.AddFlexibleSpace();
  271. iconLayout.AddElement(icon);
  272. iconLayout.AddFlexibleSpace();
  273. GUILayoutY messageLayout = mainLayout.AddLayoutY();
  274. messageLayout.AddElement(messageLabel);
  275. messageLayout.AddElement(functionLabel);
  276. mainLayout.AddSpace(PADDING);
  277. background = new GUITexture(Builtin.WhiteTexture, GUIOption.FixedHeight(ENTRY_HEIGHT));
  278. underlayLayout.AddElement(background);
  279. GUIButton button = new GUIButton(new LocEdString(""), EditorStyles.Blank, GUIOption.FixedHeight(ENTRY_HEIGHT));
  280. overlayLayout.AddElement(button);
  281. button.OnClick += OnClicked;
  282. button.OnDoubleClick += OnDoubleClicked;
  283. }
  284. /// <inheritdoc/>
  285. public override void UpdateContents(int index, ConsoleEntryData data)
  286. {
  287. if (index != sSelectedElementIdx)
  288. {
  289. if (index%2 != 0)
  290. {
  291. background.Visible = true;
  292. background.SetTint(BG_COLOR);
  293. }
  294. else
  295. {
  296. background.Visible = false;
  297. }
  298. }
  299. else
  300. {
  301. background.Visible = true;
  302. background.SetTint(SELECTION_COLOR);
  303. }
  304. switch (data.type)
  305. {
  306. case DebugMessageType.Info:
  307. icon.SetTexture(EditorBuiltin.GetLogIcon(LogIcon.Info, 32));
  308. break;
  309. case DebugMessageType.Warning:
  310. icon.SetTexture(EditorBuiltin.GetLogIcon(LogIcon.Warning, 32));
  311. break;
  312. case DebugMessageType.Error:
  313. icon.SetTexture(EditorBuiltin.GetLogIcon(LogIcon.Error, 32));
  314. break;
  315. }
  316. messageLabel.SetContent(new LocEdString(data.message));
  317. string method = "";
  318. if (data.callstack != null && data.callstack.Length > 0)
  319. {
  320. file = data.callstack[0].file;
  321. line = data.callstack[0].line;
  322. if (string.IsNullOrEmpty(data.callstack[0].method))
  323. method = "\tat " + file + ":" + line;
  324. else
  325. method = "\t" + data.callstack[0].method + " at " + file + ":" + line;
  326. }
  327. else
  328. {
  329. file = "";
  330. line = 0;
  331. }
  332. functionLabel.SetContent(new LocEdString(method));
  333. entryIdx = index;
  334. }
  335. /// <summary>
  336. /// Triggered when the entry is selected.
  337. /// </summary>
  338. private void OnClicked()
  339. {
  340. sSelectedElementIdx = entryIdx;
  341. ConsoleWindow window = GetWindow<ConsoleWindow>();
  342. window.RefreshDetailsPanel();
  343. RefreshEntries();
  344. }
  345. /// <summary>
  346. /// Triggered when the entry is double-clicked.
  347. /// </summary>
  348. private void OnDoubleClicked()
  349. {
  350. if(!string.IsNullOrEmpty(file))
  351. CodeEditor.OpenFile(file, line);
  352. }
  353. }
  354. }
  355. }