ConfigInfoLine.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace Unity.RenderStreaming.Editor
  6. {
  7. internal class ConfigInfoLine : VisualElement
  8. {
  9. static class Style
  10. {
  11. const string k_IconFolder = "Packages/com.unity.renderstreaming/Editor/Icon/";
  12. public static readonly Texture ok = EditorGUIUtility.Load(k_IconFolder + "OK.png") as Texture;
  13. public static readonly Texture error = EditorGUIUtility.Load(k_IconFolder + "Error.png") as Texture;
  14. }
  15. private readonly bool m_visibleStatus;
  16. private readonly bool m_skipErrorIcon;
  17. private Func<bool> m_tester;
  18. private Func<bool> m_dependTester;
  19. private bool m_haveFixer;
  20. private bool m_currentStatus;
  21. private bool m_dependStatus;
  22. public ConfigInfoLine(
  23. string label,
  24. string error,
  25. MessageType messageType,
  26. string resolverButtonLabel,
  27. Func<bool> tester,
  28. Action resolver,
  29. Func<bool> dependTester = null,
  30. bool visibleStatus = true,
  31. bool skipErrorIcon = false
  32. )
  33. {
  34. m_visibleStatus = visibleStatus;
  35. m_skipErrorIcon = skipErrorIcon;
  36. m_tester = tester;
  37. m_haveFixer = resolver != null;
  38. m_dependTester = dependTester;
  39. var testLabel = new Label(label) {name = "testLabel"};
  40. var fixer = new Button(resolver) {text = resolverButtonLabel, name = "resolver"};
  41. var testRow = new VisualElement() {name = "testRow"};
  42. testRow.Add(testLabel);
  43. if (m_visibleStatus)
  44. {
  45. var statusOk = new Image {image = Style.ok, name = "statusOK"};
  46. var statusError = new Image {image = Style.error, name = "statusError"};
  47. testRow.Add(statusOk);
  48. testRow.Add(statusError);
  49. }
  50. testRow.Add(fixer);
  51. Add(testRow);
  52. HelpBoxMessageType kind;
  53. switch (messageType)
  54. {
  55. default:
  56. case MessageType.None:
  57. kind = HelpBoxMessageType.None;
  58. break;
  59. case MessageType.Error:
  60. kind = HelpBoxMessageType.Error;
  61. break;
  62. case MessageType.Warning:
  63. kind = HelpBoxMessageType.Warning;
  64. break;
  65. case MessageType.Info:
  66. kind = HelpBoxMessageType.Info;
  67. break;
  68. }
  69. Add(new HelpBox(error, kind));
  70. UpdateDisplay(m_currentStatus, m_haveFixer, m_dependStatus);
  71. }
  72. public void CheckUpdate()
  73. {
  74. bool wellConfigured = m_tester();
  75. bool wellDependConfigured = m_dependTester == null || m_dependTester();
  76. bool changeConfigured = wellConfigured ^ m_currentStatus;
  77. bool changeDependConfigured = wellDependConfigured ^ m_dependStatus;
  78. if (changeConfigured || changeDependConfigured)
  79. {
  80. UpdateDisplay(wellConfigured, m_haveFixer, wellDependConfigured);
  81. m_currentStatus = wellConfigured;
  82. m_dependStatus = wellDependConfigured;
  83. }
  84. }
  85. private void UpdateDisplay(bool statusOK, bool haveFixer, bool dependStatusOK)
  86. {
  87. if (m_visibleStatus)
  88. {
  89. this.Q(name: "statusOK").style.display = statusOK ? DisplayStyle.Flex : DisplayStyle.None;
  90. this.Q(name: "statusError").style.display = !statusOK
  91. ? (m_skipErrorIcon ? DisplayStyle.None : DisplayStyle.Flex)
  92. : DisplayStyle.None;
  93. }
  94. var resolver = this.Q<Button>(name: "resolver");
  95. resolver.style.display = statusOK || !haveFixer ? DisplayStyle.None : DisplayStyle.Flex;
  96. resolver.SetEnabled(dependStatusOK);
  97. this.Q(className: HelpBox.ussClassName).style.display = statusOK ? DisplayStyle.None : DisplayStyle.Flex;
  98. }
  99. }
  100. }