ConsoleWindow.cs 17 KB

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