ConsoleWindow.cs 17 KB

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