Newer
Older
HoloAnatomy / Assets / HoloToolkit / Utilities / Scripts / Editor / AutoConfigureWindow.cs
SURFACEBOOK2\jackwynne on 25 May 2018 6 KB v1
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3.  
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8.  
  9. namespace HoloToolkit.Unity
  10. {
  11. /// <summary>
  12. /// Base class for auto configuration build windows.
  13. /// </summary>
  14. public abstract class AutoConfigureWindow<TSetting> : EditorWindow
  15. {
  16. #region Member Fields
  17.  
  18. private Dictionary<TSetting, bool> values = new Dictionary<TSetting, bool>();
  19. private Dictionary<TSetting, string> names = new Dictionary<TSetting, string>();
  20. private Dictionary<TSetting, string> descriptions = new Dictionary<TSetting, string>();
  21.  
  22. private string statusMessage = string.Empty;
  23. private Vector2 scrollPosition = Vector2.zero;
  24. private GUIStyle wrapStyle;
  25.  
  26. #endregion // Member Fields
  27.  
  28. #region Internal Methods
  29.  
  30. private void SettingToggle(TSetting setting)
  31. {
  32. EditorGUI.BeginChangeCheck();
  33.  
  34. // Draw and update setting flag
  35. values[setting] = GUILayout.Toggle(values[setting], new GUIContent(names[setting]));
  36.  
  37. if (EditorGUI.EndChangeCheck())
  38. {
  39. OnGuiChanged();
  40. }
  41.  
  42. // If this control is the one under the mouse, update the status message
  43. if (Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
  44. {
  45. StatusMessage = descriptions[setting];
  46. Repaint();
  47. }
  48. }
  49.  
  50. /// <summary>
  51. /// Gets or sets the status message displayed at the bottom of the window.
  52. /// </summary>
  53. private string StatusMessage { get { return statusMessage; } set { statusMessage = value; } }
  54.  
  55. /// <summary>
  56. /// Checks all the checkboxes in the window.
  57. /// </summary>
  58. private void SelectAll()
  59. {
  60. var keys = values.Keys.ToArray();
  61. for (int iKey = 0; iKey < keys.Length; iKey++)
  62. {
  63. Values[keys[iKey]] = true;
  64. }
  65. }
  66.  
  67. /// <summary>
  68. /// Unchecks all the checkboxes in the window.
  69. /// </summary>
  70. private void SelectNone()
  71. {
  72. var keys = values.Keys.ToArray();
  73. for (int iKey = 0; iKey < keys.Length; iKey++)
  74. {
  75. Values[keys[iKey]] = false;
  76. }
  77. }
  78.  
  79. #endregion // Internal Methods
  80.  
  81. #region Overridables / Event Triggers
  82.  
  83. /// <summary>
  84. /// Called when settings should be applied.
  85. /// </summary>
  86. protected abstract void ApplySettings();
  87.  
  88. /// <summary>
  89. /// Called when settings should be loaded.
  90. /// </summary>
  91. protected abstract void LoadSettings();
  92.  
  93. /// <summary>
  94. /// Called when string names and descriptions should be loaded.
  95. /// </summary>
  96. protected abstract void LoadStrings();
  97.  
  98. /// <summary>
  99. /// Called when a toggle has been flipped and a change has been detected.
  100. /// </summary>
  101. protected abstract void OnGuiChanged();
  102.  
  103. #endregion // Overridables / Event Triggers
  104.  
  105. #region Overrides / Event Handlers
  106.  
  107. /// <summary>
  108. /// Called when the window is created.
  109. /// </summary>
  110. protected virtual void Awake()
  111. {
  112. wrapStyle = new GUIStyle("label")
  113. {
  114. wordWrap = true,
  115. richText = true
  116. };
  117. }
  118.  
  119. protected virtual void OnEnable()
  120. {
  121. LoadStrings();
  122. LoadSettings();
  123. }
  124.  
  125. /// <summary>
  126. /// Renders the GUI
  127. /// </summary>
  128. protected virtual void OnGUI()
  129. {
  130. // Begin Settings Section
  131. GUILayout.BeginVertical(EditorStyles.helpBox);
  132.  
  133. // Individual Settings
  134. var keys = values.Keys.ToArray();
  135. for (int iKey = 0; iKey < keys.Length; iKey++)
  136. {
  137. SettingToggle(keys[iKey]);
  138. }
  139.  
  140. // End Settings Section
  141. GUILayout.EndVertical();
  142.  
  143. // Status box area
  144. GUILayout.BeginVertical(EditorStyles.helpBox);
  145. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  146. GUILayout.Label(statusMessage, wrapStyle);
  147. GUILayout.EndScrollView();
  148. GUILayout.EndVertical();
  149.  
  150. // Buttons
  151. GUILayout.BeginHorizontal(EditorStyles.miniButtonRight);
  152. bool selectAllClicked = GUILayout.Button("Select All");
  153. bool selectNoneClicked = GUILayout.Button("Select None");
  154. bool applyClicked = GUILayout.Button("Apply");
  155. GUILayout.EndHorizontal();
  156.  
  157. // Clicked?
  158. if (selectAllClicked)
  159. {
  160. SelectAll();
  161. }
  162.  
  163. if (selectNoneClicked)
  164. {
  165. SelectNone();
  166. }
  167.  
  168. if (applyClicked)
  169. {
  170. ApplySettings();
  171. }
  172. }
  173.  
  174. #endregion // Overrides / Event Handlers
  175.  
  176. #region Protected Properties
  177.  
  178. /// <summary>
  179. /// Gets the descriptions of the settings.
  180. /// </summary>
  181. protected Dictionary<TSetting, string> Descriptions
  182. {
  183. get
  184. {
  185. return descriptions;
  186. }
  187.  
  188. set
  189. {
  190. descriptions = value;
  191. }
  192. }
  193.  
  194. /// <summary>
  195. /// Gets the names of the settings.
  196. /// </summary>
  197. protected Dictionary<TSetting, string> Names
  198. {
  199. get
  200. {
  201. return names;
  202. }
  203.  
  204. set
  205. {
  206. names = value;
  207. }
  208. }
  209.  
  210. /// <summary>
  211. /// Gets the values of the settings.
  212. /// </summary>
  213. protected Dictionary<TSetting, bool> Values
  214. {
  215. get
  216. {
  217. return values;
  218. }
  219.  
  220. set
  221. {
  222. values = value;
  223. }
  224. }
  225.  
  226. #endregion // Protected Properties
  227. }
  228. }