InfoTabController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using SRF.UI;
  2. namespace SRDebugger.UI.Tabs
  3. {
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using Controls;
  7. using Services;
  8. using SRF;
  9. using SRF.Service;
  10. using UnityEngine;
  11. public class InfoTabController : SRMonoBehaviourEx
  12. {
  13. public const char Tick = '\u2713';
  14. public const char Cross = '\u00D7';
  15. public const string NameColor = "#BCBCBC";
  16. private Dictionary<string, InfoBlock> _infoBlocks = new Dictionary<string, InfoBlock>();
  17. [RequiredField] public InfoBlock InfoBlockPrefab;
  18. [RequiredField] public RectTransform LayoutContainer;
  19. [RequiredField] public FlashGraphic ToggleButton;
  20. private bool _updateEveryFrame;
  21. protected override void OnEnable()
  22. {
  23. base.OnEnable();
  24. InternalRefresh();
  25. if (_updateEveryFrame)
  26. {
  27. ToggleButton.FlashAndHoldUntilNextPress();
  28. }
  29. }
  30. public void Refresh()
  31. {
  32. ToggleButton.Flash(); // flash to disable any "press and hold" that is going on
  33. _updateEveryFrame = false;
  34. InternalRefresh();
  35. }
  36. protected override void Update()
  37. {
  38. if (_updateEveryFrame)
  39. {
  40. InternalRefresh();
  41. }
  42. }
  43. public void ActivateRefreshEveryFrame()
  44. {
  45. ToggleButton.FlashAndHoldUntilNextPress();
  46. _updateEveryFrame = true;
  47. InternalRefresh();
  48. }
  49. private void InternalRefresh()
  50. {
  51. var s = SRServiceManager.GetService<ISystemInformationService>();
  52. foreach (var category in s.GetCategories())
  53. {
  54. if (!_infoBlocks.ContainsKey(category))
  55. {
  56. var block = CreateBlock(category);
  57. _infoBlocks.Add(category, block);
  58. }
  59. }
  60. foreach (var kv in _infoBlocks)
  61. {
  62. FillInfoBlock(kv.Value, s.GetInfo(kv.Key));
  63. }
  64. }
  65. private void FillInfoBlock(InfoBlock block, IList<InfoEntry> info)
  66. {
  67. var sb = new StringBuilder();
  68. var maxTitleLength = 0;
  69. foreach (var systemInfo in info)
  70. {
  71. if (systemInfo.Title.Length > maxTitleLength)
  72. {
  73. maxTitleLength = systemInfo.Title.Length;
  74. }
  75. }
  76. maxTitleLength += 2;
  77. var first = true;
  78. foreach (var i in info)
  79. {
  80. if (first)
  81. {
  82. first = false;
  83. }
  84. else
  85. {
  86. sb.AppendLine();
  87. }
  88. sb.Append("<color=");
  89. sb.Append(NameColor);
  90. sb.Append(">");
  91. sb.Append(i.Title);
  92. sb.Append(": ");
  93. sb.Append("</color>");
  94. for (var j = i.Title.Length; j <= maxTitleLength; ++j)
  95. {
  96. sb.Append(' ');
  97. }
  98. if (i.Value is bool)
  99. {
  100. sb.Append((bool) i.Value ? Tick : Cross);
  101. }
  102. else
  103. {
  104. sb.Append(i.Value);
  105. }
  106. }
  107. block.Content.text = sb.ToString();
  108. }
  109. private InfoBlock CreateBlock(string title)
  110. {
  111. var block = SRInstantiate.Instantiate(InfoBlockPrefab);
  112. block.Title.text = title;
  113. block.CachedTransform.SetParent(LayoutContainer, false);
  114. return block;
  115. }
  116. }
  117. }