ConsoleWindow.cs 18 KB

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