2
0

ConsoleWindow.cs 20 KB

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