ConsoleWindow.cs 17 KB

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