SROptionsWindow.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. 
  2. namespace SRDebugger.Editor
  3. {
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using SRF;
  8. using UnityEngine;
  9. using UnityEditor;
  10. using System.ComponentModel;
  11. using SRF.Helpers;
  12. #if !DISABLE_SRDEBUGGER
  13. using Internal;
  14. using SRDebugger.Services;
  15. using UI.Controls.Data;
  16. #endif
  17. class SROptionsWindow : EditorWindow
  18. {
  19. [MenuItem(SRDebugEditorPaths.SROptionsMenuItemPath)]
  20. public static void Open()
  21. {
  22. var window = GetWindow<SROptionsWindow>(false, "SROptions", true);
  23. window.minSize = new Vector2(100, 100);
  24. window.Show();
  25. }
  26. #if DISABLE_SRDEBUGGER
  27. private bool _isWorking;
  28. void OnGUI()
  29. {
  30. SRDebugEditor.DrawDisabledWindowGui(ref _isWorking);
  31. }
  32. #else
  33. [Serializable]
  34. private class CategoryState
  35. {
  36. public string Name;
  37. public bool IsOpen;
  38. }
  39. [SerializeField]
  40. private List<CategoryState> _categoryStates = new List<CategoryState>();
  41. private Dictionary<Type, Action<OptionDefinition>> _typeLookup;
  42. private Dictionary<string, List<OptionDefinition>> _options;
  43. private Vector2 _scrollPosition;
  44. private bool _queueRefresh;
  45. private bool _isDirty;
  46. [NonSerialized] private GUIStyle _divider;
  47. [NonSerialized] private GUIStyle _foldout;
  48. private IOptionsService _activeOptionsService;
  49. public void OnInspectorUpdate()
  50. {
  51. if (EditorApplication.isPlaying && (_options == null || _isDirty))
  52. {
  53. Populate();
  54. _queueRefresh = true;
  55. _isDirty = false;
  56. }
  57. else if (!EditorApplication.isPlaying && _options != null)
  58. {
  59. Clear();
  60. _queueRefresh = true;
  61. }
  62. if (_queueRefresh)
  63. {
  64. Repaint();
  65. }
  66. _queueRefresh = false;
  67. }
  68. private void OnDisable()
  69. {
  70. Clear();
  71. }
  72. void PopulateTypeLookup()
  73. {
  74. _typeLookup = new Dictionary<Type, Action<OptionDefinition>>()
  75. {
  76. {typeof(int), OnGUI_Int},
  77. {typeof(float), OnGUI_Float},
  78. {typeof(double), OnGUI_Double},
  79. {typeof(string), OnGUI_String},
  80. {typeof(bool), OnGUI_Boolean },
  81. {typeof(uint), OnGUI_AnyInteger},
  82. {typeof(ushort), OnGUI_AnyInteger},
  83. {typeof(short), OnGUI_AnyInteger},
  84. {typeof(sbyte), OnGUI_AnyInteger},
  85. {typeof(byte), OnGUI_AnyInteger},
  86. {typeof(long), OnGUI_AnyInteger},
  87. };
  88. }
  89. void Clear()
  90. {
  91. _options = null;
  92. _isDirty = false;
  93. if (_activeOptionsService != null)
  94. {
  95. _activeOptionsService.OptionsUpdated -= OnOptionsUpdated;
  96. }
  97. _activeOptionsService = null;
  98. }
  99. void Populate()
  100. {
  101. if (_typeLookup == null)
  102. {
  103. PopulateTypeLookup();
  104. }
  105. if (_activeOptionsService != null)
  106. {
  107. _activeOptionsService.OptionsUpdated -= OnOptionsUpdated;
  108. }
  109. if (_options != null)
  110. {
  111. foreach (KeyValuePair<string, List<OptionDefinition>> kv in _options)
  112. {
  113. foreach (var option in kv.Value)
  114. {
  115. if (option.IsProperty)
  116. {
  117. option.Property.ValueChanged -= OnOptionPropertyValueChanged;
  118. }
  119. }
  120. }
  121. }
  122. _options = new Dictionary<string, List<OptionDefinition>>();
  123. foreach (var option in Service.Options.Options)
  124. {
  125. List<OptionDefinition> list;
  126. if (!_options.TryGetValue(option.Category, out list))
  127. {
  128. list = new List<OptionDefinition>();
  129. _options[option.Category] = list;
  130. }
  131. list.Add(option);
  132. if (option.IsProperty)
  133. {
  134. option.Property.ValueChanged += OnOptionPropertyValueChanged;
  135. }
  136. }
  137. foreach (var kv in _options)
  138. {
  139. kv.Value.Sort((d1, d2) => d1.SortPriority.CompareTo(d2.SortPriority));
  140. }
  141. _activeOptionsService = Service.Options;
  142. _activeOptionsService.OptionsUpdated += OnOptionsUpdated;
  143. }
  144. private void OnOptionPropertyValueChanged(PropertyReference property)
  145. {
  146. _queueRefresh = true;
  147. }
  148. private void OnOptionsUpdated(object sender, EventArgs e)
  149. {
  150. _isDirty = true;
  151. _queueRefresh = true;
  152. }
  153. void OnGUI()
  154. {
  155. EditorGUILayout.Space();
  156. if (!EditorApplication.isPlayingOrWillChangePlaymode || _options == null)
  157. {
  158. EditorGUILayout.BeginHorizontal();
  159. GUILayout.FlexibleSpace();
  160. GUILayout.Label("SROptions can only be edited in play-mode.");
  161. GUILayout.FlexibleSpace();
  162. EditorGUILayout.EndHorizontal();
  163. return;
  164. }
  165. if (_divider == null)
  166. {
  167. _divider = new GUIStyle(GUI.skin.box);
  168. _divider.stretchWidth = true;
  169. _divider.fixedHeight = 2;
  170. }
  171. if (_foldout == null)
  172. {
  173. _foldout = new GUIStyle(EditorStyles.foldout);
  174. _foldout.fontStyle = FontStyle.Bold;
  175. }
  176. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);
  177. foreach (var kv in _options)
  178. {
  179. var state = _categoryStates.FirstOrDefault(p => p.Name == kv.Key);
  180. if (state == null)
  181. {
  182. state = new CategoryState()
  183. {
  184. Name = kv.Key,
  185. IsOpen = true
  186. };
  187. _categoryStates.Add(state);
  188. }
  189. state.IsOpen = EditorGUILayout.Foldout(state.IsOpen, kv.Key, _foldout);
  190. if (!state.IsOpen)
  191. continue;
  192. EditorGUILayout.BeginVertical(EditorStyles.inspectorDefaultMargins);
  193. OnGUI_Category(kv.Value);
  194. EditorGUILayout.Space();
  195. EditorGUILayout.EndHorizontal();
  196. }
  197. EditorGUILayout.EndScrollView();
  198. }
  199. void OnGUI_Category(List<OptionDefinition> options)
  200. {
  201. for (var i = 0; i < options.Count; i++)
  202. {
  203. var op = options[i];
  204. if (op.Property != null)
  205. {
  206. OnGUI_Property(op);
  207. } else if (op.Method != null)
  208. {
  209. OnGUI_Method(op);
  210. }
  211. }
  212. }
  213. void OnGUI_Method(OptionDefinition op)
  214. {
  215. if (GUILayout.Button(op.Name))
  216. {
  217. op.Method.Invoke(null);
  218. }
  219. }
  220. void OnGUI_Property(OptionDefinition op)
  221. {
  222. Action<OptionDefinition> method;
  223. if (op.Property.PropertyType.IsEnum)
  224. {
  225. method = OnGUI_Enum;
  226. }
  227. else if (!_typeLookup.TryGetValue(op.Property.PropertyType, out method))
  228. {
  229. OnGUI_Unsupported(op);
  230. return;
  231. }
  232. if (!op.Property.CanWrite)
  233. {
  234. EditorGUI.BeginDisabledGroup(true);
  235. }
  236. method(op);
  237. if (!op.Property.CanWrite)
  238. {
  239. EditorGUI.EndDisabledGroup();
  240. }
  241. }
  242. void OnGUI_String(OptionDefinition op)
  243. {
  244. EditorGUI.BeginChangeCheck();
  245. var newValue = EditorGUILayout.TextField(op.Name, (string) op.Property.GetValue());
  246. if (EditorGUI.EndChangeCheck())
  247. {
  248. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  249. }
  250. }
  251. void OnGUI_Boolean(OptionDefinition op)
  252. {
  253. EditorGUI.BeginChangeCheck();
  254. var newValue = EditorGUILayout.Toggle(op.Name, (bool) op.Property.GetValue());
  255. if (EditorGUI.EndChangeCheck())
  256. {
  257. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  258. }
  259. }
  260. void OnGUI_Enum(OptionDefinition op)
  261. {
  262. EditorGUI.BeginChangeCheck();
  263. var newValue = EditorGUILayout.EnumPopup(op.Name, (Enum)op.Property.GetValue());
  264. if (EditorGUI.EndChangeCheck())
  265. {
  266. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  267. }
  268. }
  269. void OnGUI_Int(OptionDefinition op)
  270. {
  271. var range = op.Property.GetAttribute<NumberRangeAttribute>();
  272. int newValue;
  273. EditorGUI.BeginChangeCheck();
  274. if (range != null)
  275. {
  276. newValue = EditorGUILayout.IntSlider(op.Name, (int)op.Property.GetValue(), (int)range.Min, (int)range.Max);
  277. }
  278. else
  279. {
  280. newValue = EditorGUILayout.IntField(op.Name, (int) op.Property.GetValue());
  281. }
  282. if (EditorGUI.EndChangeCheck())
  283. {
  284. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  285. }
  286. }
  287. void OnGUI_Float(OptionDefinition op)
  288. {
  289. var range = op.Property.GetAttribute<NumberRangeAttribute>();
  290. float newValue;
  291. EditorGUI.BeginChangeCheck();
  292. if (range != null)
  293. {
  294. newValue = EditorGUILayout.Slider(op.Name, (float)op.Property.GetValue(), (float)range.Min, (float)range.Max);
  295. }
  296. else
  297. {
  298. newValue = EditorGUILayout.FloatField(op.Name, (float) op.Property.GetValue());
  299. }
  300. if (EditorGUI.EndChangeCheck())
  301. {
  302. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  303. }
  304. }
  305. void OnGUI_Double(OptionDefinition op)
  306. {
  307. var range = op.Property.GetAttribute<NumberRangeAttribute>();
  308. double newValue;
  309. EditorGUI.BeginChangeCheck();
  310. if (range != null && range.Min > float.MinValue && range.Max < float.MaxValue)
  311. {
  312. newValue = EditorGUILayout.Slider(op.Name, (float)op.Property.GetValue(), (float)range.Min, (float)range.Max);
  313. }
  314. else
  315. {
  316. newValue = EditorGUILayout.DoubleField(op.Name, (double) op.Property.GetValue());
  317. if (range != null)
  318. {
  319. if (newValue > range.Max)
  320. {
  321. newValue = range.Max;
  322. } else if (newValue < range.Min)
  323. {
  324. newValue = range.Min;
  325. }
  326. }
  327. }
  328. if (EditorGUI.EndChangeCheck())
  329. {
  330. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  331. }
  332. }
  333. void OnGUI_AnyInteger(OptionDefinition op)
  334. {
  335. NumberControl.ValueRange range;
  336. if (!NumberControl.ValueRanges.TryGetValue(op.Property.PropertyType, out range))
  337. {
  338. Debug.LogError("Unknown integer type: " + op.Property.PropertyType);
  339. return;
  340. }
  341. var userRange = op.Property.GetAttribute<NumberRangeAttribute>();
  342. EditorGUI.BeginChangeCheck();
  343. var oldValue = (long)Convert.ChangeType(op.Property.GetValue(), typeof(long));
  344. var newValue = EditorGUILayout.LongField(op.Name, oldValue);
  345. if (newValue > range.MaxValue)
  346. {
  347. newValue = (long)range.MaxValue;
  348. } else if (newValue < range.MinValue)
  349. {
  350. newValue = (long)range.MinValue;
  351. }
  352. if (userRange != null)
  353. {
  354. if (newValue > userRange.Max)
  355. {
  356. newValue = (long)userRange.Max;
  357. } else if (newValue < userRange.Min)
  358. {
  359. newValue = (long) userRange.Min;
  360. }
  361. }
  362. if (EditorGUI.EndChangeCheck())
  363. {
  364. op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
  365. }
  366. }
  367. void OnGUI_Unsupported(OptionDefinition op)
  368. {
  369. EditorGUILayout.PrefixLabel(op.Name);
  370. EditorGUILayout.LabelField("Unsupported Type: {0}".Fmt(op.Property.PropertyType));
  371. }
  372. #endif
  373. }
  374. }