123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
-
- namespace SRDebugger.Editor
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using SRF;
- using UnityEngine;
- using UnityEditor;
- using System.ComponentModel;
- using SRF.Helpers;
- #if !DISABLE_SRDEBUGGER
- using Internal;
- using SRDebugger.Services;
- using UI.Controls.Data;
- #endif
- class SROptionsWindow : EditorWindow
- {
- [MenuItem(SRDebugEditorPaths.SROptionsMenuItemPath)]
- public static void Open()
- {
- var window = GetWindow<SROptionsWindow>(false, "SROptions", true);
- window.minSize = new Vector2(100, 100);
- window.Show();
- }
- #if DISABLE_SRDEBUGGER
- private bool _isWorking;
- void OnGUI()
- {
- SRDebugEditor.DrawDisabledWindowGui(ref _isWorking);
- }
- #else
- [Serializable]
- private class CategoryState
- {
- public string Name;
- public bool IsOpen;
- }
- [SerializeField]
- private List<CategoryState> _categoryStates = new List<CategoryState>();
- private Dictionary<Type, Action<OptionDefinition>> _typeLookup;
- private Dictionary<string, List<OptionDefinition>> _options;
- private Vector2 _scrollPosition;
- private bool _queueRefresh;
- private bool _isDirty;
- [NonSerialized] private GUIStyle _divider;
- [NonSerialized] private GUIStyle _foldout;
- private IOptionsService _activeOptionsService;
- public void OnInspectorUpdate()
- {
- if (EditorApplication.isPlaying && (_options == null || _isDirty))
- {
- Populate();
- _queueRefresh = true;
- _isDirty = false;
- }
- else if (!EditorApplication.isPlaying && _options != null)
- {
- Clear();
- _queueRefresh = true;
- }
- if (_queueRefresh)
- {
- Repaint();
- }
- _queueRefresh = false;
- }
- private void OnDisable()
- {
- Clear();
- }
- void PopulateTypeLookup()
- {
- _typeLookup = new Dictionary<Type, Action<OptionDefinition>>()
- {
- {typeof(int), OnGUI_Int},
- {typeof(float), OnGUI_Float},
- {typeof(double), OnGUI_Double},
- {typeof(string), OnGUI_String},
- {typeof(bool), OnGUI_Boolean },
- {typeof(uint), OnGUI_AnyInteger},
- {typeof(ushort), OnGUI_AnyInteger},
- {typeof(short), OnGUI_AnyInteger},
- {typeof(sbyte), OnGUI_AnyInteger},
- {typeof(byte), OnGUI_AnyInteger},
- {typeof(long), OnGUI_AnyInteger},
- };
- }
- void Clear()
- {
- _options = null;
- _isDirty = false;
- if (_activeOptionsService != null)
- {
- _activeOptionsService.OptionsUpdated -= OnOptionsUpdated;
- }
- _activeOptionsService = null;
- }
- void Populate()
- {
- if (_typeLookup == null)
- {
- PopulateTypeLookup();
- }
- if (_activeOptionsService != null)
- {
- _activeOptionsService.OptionsUpdated -= OnOptionsUpdated;
- }
- if (_options != null)
- {
- foreach (KeyValuePair<string, List<OptionDefinition>> kv in _options)
- {
- foreach (var option in kv.Value)
- {
- if (option.IsProperty)
- {
- option.Property.ValueChanged -= OnOptionPropertyValueChanged;
- }
- }
- }
- }
- _options = new Dictionary<string, List<OptionDefinition>>();
-
- foreach (var option in Service.Options.Options)
- {
- List<OptionDefinition> list;
- if (!_options.TryGetValue(option.Category, out list))
- {
- list = new List<OptionDefinition>();
- _options[option.Category] = list;
- }
- list.Add(option);
- if (option.IsProperty)
- {
- option.Property.ValueChanged += OnOptionPropertyValueChanged;
- }
- }
- foreach (var kv in _options)
- {
- kv.Value.Sort((d1, d2) => d1.SortPriority.CompareTo(d2.SortPriority));
- }
- _activeOptionsService = Service.Options;
- _activeOptionsService.OptionsUpdated += OnOptionsUpdated;
- }
- private void OnOptionPropertyValueChanged(PropertyReference property)
- {
- _queueRefresh = true;
- }
- private void OnOptionsUpdated(object sender, EventArgs e)
- {
- _isDirty = true;
- _queueRefresh = true;
- }
- void OnGUI()
- {
- EditorGUILayout.Space();
- if (!EditorApplication.isPlayingOrWillChangePlaymode || _options == null)
- {
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- GUILayout.Label("SROptions can only be edited in play-mode.");
- GUILayout.FlexibleSpace();
- EditorGUILayout.EndHorizontal();
- return;
- }
- if (_divider == null)
- {
- _divider = new GUIStyle(GUI.skin.box);
- _divider.stretchWidth = true;
- _divider.fixedHeight = 2;
- }
- if (_foldout == null)
- {
- _foldout = new GUIStyle(EditorStyles.foldout);
- _foldout.fontStyle = FontStyle.Bold;
- }
- _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);
- foreach (var kv in _options)
- {
- var state = _categoryStates.FirstOrDefault(p => p.Name == kv.Key);
- if (state == null)
- {
- state = new CategoryState()
- {
- Name = kv.Key,
- IsOpen = true
- };
- _categoryStates.Add(state);
- }
-
- state.IsOpen = EditorGUILayout.Foldout(state.IsOpen, kv.Key, _foldout);
- if (!state.IsOpen)
- continue;
- EditorGUILayout.BeginVertical(EditorStyles.inspectorDefaultMargins);
- OnGUI_Category(kv.Value);
- EditorGUILayout.Space();
- EditorGUILayout.EndHorizontal();
- }
- EditorGUILayout.EndScrollView();
- }
- void OnGUI_Category(List<OptionDefinition> options)
- {
- for (var i = 0; i < options.Count; i++)
- {
- var op = options[i];
- if (op.Property != null)
- {
- OnGUI_Property(op);
- } else if (op.Method != null)
- {
- OnGUI_Method(op);
- }
- }
- }
- void OnGUI_Method(OptionDefinition op)
- {
- if (GUILayout.Button(op.Name))
- {
- op.Method.Invoke(null);
- }
- }
- void OnGUI_Property(OptionDefinition op)
- {
- Action<OptionDefinition> method;
- if (op.Property.PropertyType.IsEnum)
- {
- method = OnGUI_Enum;
- }
- else if (!_typeLookup.TryGetValue(op.Property.PropertyType, out method))
- {
- OnGUI_Unsupported(op);
- return;
- }
- if (!op.Property.CanWrite)
- {
- EditorGUI.BeginDisabledGroup(true);
- }
- method(op);
- if (!op.Property.CanWrite)
- {
- EditorGUI.EndDisabledGroup();
- }
- }
- void OnGUI_String(OptionDefinition op)
- {
- EditorGUI.BeginChangeCheck();
- var newValue = EditorGUILayout.TextField(op.Name, (string) op.Property.GetValue());
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_Boolean(OptionDefinition op)
- {
- EditorGUI.BeginChangeCheck();
- var newValue = EditorGUILayout.Toggle(op.Name, (bool) op.Property.GetValue());
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_Enum(OptionDefinition op)
- {
- EditorGUI.BeginChangeCheck();
- var newValue = EditorGUILayout.EnumPopup(op.Name, (Enum)op.Property.GetValue());
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_Int(OptionDefinition op)
- {
- var range = op.Property.GetAttribute<NumberRangeAttribute>();
- int newValue;
- EditorGUI.BeginChangeCheck();
- if (range != null)
- {
- newValue = EditorGUILayout.IntSlider(op.Name, (int)op.Property.GetValue(), (int)range.Min, (int)range.Max);
- }
- else
- {
- newValue = EditorGUILayout.IntField(op.Name, (int) op.Property.GetValue());
- }
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_Float(OptionDefinition op)
- {
- var range = op.Property.GetAttribute<NumberRangeAttribute>();
- float newValue;
- EditorGUI.BeginChangeCheck();
- if (range != null)
- {
- newValue = EditorGUILayout.Slider(op.Name, (float)op.Property.GetValue(), (float)range.Min, (float)range.Max);
- }
- else
- {
- newValue = EditorGUILayout.FloatField(op.Name, (float) op.Property.GetValue());
- }
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_Double(OptionDefinition op)
- {
- var range = op.Property.GetAttribute<NumberRangeAttribute>();
- double newValue;
- EditorGUI.BeginChangeCheck();
- if (range != null && range.Min > float.MinValue && range.Max < float.MaxValue)
- {
- newValue = EditorGUILayout.Slider(op.Name, (float)op.Property.GetValue(), (float)range.Min, (float)range.Max);
- }
- else
- {
- newValue = EditorGUILayout.DoubleField(op.Name, (double) op.Property.GetValue());
- if (range != null)
- {
- if (newValue > range.Max)
- {
- newValue = range.Max;
- } else if (newValue < range.Min)
- {
- newValue = range.Min;
- }
- }
- }
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_AnyInteger(OptionDefinition op)
- {
- NumberControl.ValueRange range;
- if (!NumberControl.ValueRanges.TryGetValue(op.Property.PropertyType, out range))
- {
- Debug.LogError("Unknown integer type: " + op.Property.PropertyType);
- return;
- }
- var userRange = op.Property.GetAttribute<NumberRangeAttribute>();
- EditorGUI.BeginChangeCheck();
- var oldValue = (long)Convert.ChangeType(op.Property.GetValue(), typeof(long));
- var newValue = EditorGUILayout.LongField(op.Name, oldValue);
- if (newValue > range.MaxValue)
- {
- newValue = (long)range.MaxValue;
- } else if (newValue < range.MinValue)
- {
- newValue = (long)range.MinValue;
- }
- if (userRange != null)
- {
- if (newValue > userRange.Max)
- {
- newValue = (long)userRange.Max;
- } else if (newValue < userRange.Min)
- {
- newValue = (long) userRange.Min;
- }
- }
-
- if (EditorGUI.EndChangeCheck())
- {
- op.Property.SetValue(Convert.ChangeType(newValue, op.Property.PropertyType));
- }
- }
- void OnGUI_Unsupported(OptionDefinition op)
- {
- EditorGUILayout.PrefixLabel(op.Name);
- EditorGUILayout.LabelField("Unsupported Type: {0}".Fmt(op.Property.PropertyType));
- }
- #endif
- }
- }
|