GroupHelpWindow.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace Funly.SkyStudio {
  7. public class GroupHelpWindow : EditorWindow
  8. {
  9. private static GroupHelpWindow _instance;
  10. private static Dictionary<string, ProfileGroupDefinition> m_HelpItems;
  11. private static string m_CurrentHelpKey;
  12. private static SkyProfile m_Profile;
  13. public static void ShowWindow()
  14. {
  15. SharedHelpWindow().ShowUtility();
  16. }
  17. public static GroupHelpWindow SharedHelpWindow() {
  18. if (_instance == null)
  19. {
  20. _instance = CreateInstance<GroupHelpWindow>();
  21. _instance.name = "Sky Help";
  22. _instance.titleContent = new GUIContent("Sky Help");
  23. }
  24. return _instance;
  25. }
  26. public static void SetHelpItem(SkyProfile profile, string propertyKey, bool showWindow) {
  27. m_Profile = profile;
  28. BuildHelpContentIfNecessary();
  29. if (m_HelpItems.ContainsKey(propertyKey) == false) {
  30. return;
  31. }
  32. m_CurrentHelpKey = propertyKey;
  33. if (showWindow)
  34. {
  35. ShowWindow();
  36. }
  37. if (_instance != null)
  38. {
  39. SharedHelpWindow().Repaint();
  40. }
  41. }
  42. public static bool ContainsHelpForKey(string propertyKey) {
  43. BuildHelpContentIfNecessary();
  44. return m_HelpItems.ContainsKey(propertyKey);
  45. }
  46. private void OnInspectorUpdate()
  47. {
  48. // Make sure we only have 1 window open.
  49. if (this != _instance) {
  50. this.Close();
  51. return;
  52. }
  53. Repaint();
  54. }
  55. private void OnGUI()
  56. {
  57. BuildHelpContentIfNecessary();
  58. if (m_CurrentHelpKey == null)
  59. {
  60. EditorGUILayout.HelpBox("No property selected to display help for.", MessageType.Info);
  61. return;
  62. }
  63. RenderHelpItem(m_CurrentHelpKey);
  64. }
  65. private void RenderHelpItem(string helpKey)
  66. {
  67. GUIStyle titleStyle = new GUIStyle(GUI.skin.label);
  68. titleStyle.fontStyle = FontStyle.Bold;
  69. GUIStyle textStyle = new GUIStyle(GUI.skin.label);
  70. textStyle.wordWrap = true;
  71. if (ContainsHelpForKey(helpKey) == false)
  72. {
  73. EditorGUILayout.HelpBox("There is no help entry for property key: " + helpKey, MessageType.Info);
  74. return;
  75. }
  76. ProfileGroupDefinition groupDefinition = m_HelpItems[helpKey];
  77. EditorGUILayout.BeginVertical();
  78. EditorGUILayout.LabelField(groupDefinition.groupName, titleStyle);
  79. EditorGUILayout.Space();
  80. EditorGUILayout.LabelField(groupDefinition.tooltip, textStyle);
  81. string imageName = ImageNameForHelpPropertyKey();
  82. if (imageName != null)
  83. {
  84. EditorGUILayout.BeginHorizontal();
  85. GUILayout.FlexibleSpace();
  86. // TODO - Draw image.
  87. GUILayout.FlexibleSpace();
  88. EditorGUILayout.EndHorizontal();
  89. }
  90. EditorGUILayout.EndVertical();
  91. }
  92. private string ImageNameForHelpPropertyKey()
  93. {
  94. // TODO - support images.
  95. return null;
  96. }
  97. private static void BuildHelpContentIfNecessary() {
  98. if (m_HelpItems == null)
  99. {
  100. BuildHelpContent();
  101. }
  102. }
  103. private static void BuildHelpContent()
  104. {
  105. if (m_Profile == null)
  106. {
  107. Debug.LogError("Can't load help content, since there isn't an active sky profile.");
  108. return;
  109. }
  110. m_HelpItems = m_Profile.GroupDefinitionDictionary();
  111. }
  112. }
  113. }