ConsoleEntryView.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. namespace SRDebugger.UI.Controls
  2. {
  3. using System;
  4. using Services;
  5. using SRF;
  6. using SRF.UI;
  7. using SRF.UI.Layout;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. [RequireComponent(typeof (RectTransform))]
  11. public class ConsoleEntryView : SRMonoBehaviourEx, IVirtualView
  12. {
  13. public const string ConsoleBlobInfo = "Console_Info_Blob";
  14. public const string ConsoleBlobWarning = "Console_Warning_Blob";
  15. public const string ConsoleBlobError = "Console_Error_Blob";
  16. private int _count;
  17. private bool _hasCount;
  18. private ConsoleEntry _prevData;
  19. private RectTransform _rectTransform;
  20. [RequiredField] public Text Count;
  21. [RequiredField] public CanvasGroup CountContainer;
  22. [RequiredField] public StyleComponent ImageStyle;
  23. [RequiredField] public Text Message;
  24. [RequiredField] public Text StackTrace;
  25. public void SetDataContext(object data)
  26. {
  27. var msg = data as ConsoleEntry;
  28. if (msg == null)
  29. {
  30. throw new Exception("Data should be a ConsoleEntry");
  31. }
  32. // Always check for updates on "Count", as it can change
  33. if (msg.Count > 1)
  34. {
  35. if (!_hasCount)
  36. {
  37. CountContainer.alpha = 1f;
  38. _hasCount = true;
  39. }
  40. if (msg.Count != _count)
  41. {
  42. Count.text = Internal.SRDebuggerUtil.GetNumberString(msg.Count, 999, "999+");
  43. _count = msg.Count;
  44. }
  45. }
  46. else if (_hasCount)
  47. {
  48. CountContainer.alpha = 0f;
  49. _hasCount = false;
  50. }
  51. // Only update everything else if data context has changed, not just for an update
  52. if (msg == _prevData)
  53. {
  54. return;
  55. }
  56. _prevData = msg;
  57. Message.text = msg.MessagePreview;
  58. StackTrace.text = msg.StackTracePreview;
  59. if (string.IsNullOrEmpty(StackTrace.text))
  60. {
  61. Message.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 2,
  62. _rectTransform.rect.height - 4);
  63. }
  64. else
  65. {
  66. Message.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 12,
  67. _rectTransform.rect.height - 14);
  68. }
  69. switch (msg.LogType)
  70. {
  71. case LogType.Log:
  72. ImageStyle.StyleKey = ConsoleBlobInfo;
  73. break;
  74. case LogType.Warning:
  75. ImageStyle.StyleKey = ConsoleBlobWarning;
  76. break;
  77. case LogType.Exception:
  78. case LogType.Assert:
  79. case LogType.Error:
  80. ImageStyle.StyleKey = ConsoleBlobError;
  81. break;
  82. }
  83. }
  84. protected override void Awake()
  85. {
  86. base.Awake();
  87. _rectTransform = CachedTransform as RectTransform;
  88. CountContainer.alpha = 0f;
  89. Message.supportRichText = Settings.Instance.RichTextInConsole;
  90. }
  91. }
  92. }