123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #if UNITY_EDITOR
- using System;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEditorInternal;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace Animancer.Editor.Tools
- {
-
-
-
-
-
-
-
-
- public sealed partial class AnimancerToolsWindow : EditorWindow
- {
-
-
- public const string Name = "Animancer Tools";
-
- public static AnimancerToolsWindow Instance { get; private set; }
- [SerializeReference] private List<Tool> _Tools;
- [SerializeField] private Vector2 _Scroll;
- [SerializeField] private int _CurrentTool = -1;
-
- private SerializedObject _SerializedObject;
- private SerializedObject SerializedObject
- => _SerializedObject ?? (_SerializedObject = new SerializedObject(this));
-
- public SerializedProperty FindSerializedPropertyForTool(Tool tool)
- {
- var index = _Tools.IndexOf(tool);
- var property = SerializedObject.FindProperty(nameof(_Tools));
- return property.GetArrayElementAtIndex(index);
- }
-
- private void OnEnable()
- {
- titleContent = new GUIContent(Name);
- Instance = this;
- InitializeTools();
- Undo.undoRedoPerformed += Repaint;
- OnSelectionChange();
- }
-
- private void InitializeTools()
- {
- if (_Tools == null)
- {
- _Tools = new List<Tool>();
- }
- else
- {
- for (int i = _Tools.Count - 1; i >= 0; i--)
- if (_Tools[i] == null)
- _Tools.RemoveAt(i);
- }
- var toolTypes = TypeSelectionButton.GetDerivedTypes(typeof(Tool));
- for (int i = 0; i < toolTypes.Count; i++)
- {
- var toolType = toolTypes[i];
- if (IndexOfTool(toolType) >= 0)
- continue;
- var tool = (Tool)Activator.CreateInstance(toolType);
- _Tools.Add(tool);
- }
- _Tools.Sort();
- for (int i = 0; i < _Tools.Count; i++)
- _Tools[i].OnEnable(i);
- }
-
- private int IndexOfTool(Type type)
- {
- for (int i = 0; i < _Tools.Count; i++)
- if (_Tools[i].GetType() == type)
- return i;
- return -1;
- }
-
- private void OnDisable()
- {
- Undo.undoRedoPerformed -= Repaint;
- for (int i = 0; i < _Tools.Count; i++)
- _Tools[i].OnDisable();
- if (_SerializedObject != null)
- {
- _SerializedObject.Dispose();
- _SerializedObject = null;
- }
- }
-
- private void OnSelectionChange()
- {
- for (int i = 0; i < _Tools.Count; i++)
- _Tools[i].OnSelectionChanged();
- Repaint();
- }
-
- private void OnGUI()
- {
- EditorGUIUtility.labelWidth = Mathf.Min(EditorGUIUtility.labelWidth, position.width * 0.5f);
- _Scroll = GUILayout.BeginScrollView(_Scroll);
- GUILayout.BeginVertical();
- GUILayout.EndVertical();
- for (int i = 0; i < _Tools.Count; i++)
- _Tools[i].DoGUI();
- GUILayout.EndScrollView();
- }
-
-
- public static new void Repaint() => ((EditorWindow)Instance).Repaint();
-
- public static void RecordUndo() => Undo.RecordObject(Instance, Name);
-
-
- public static void BeginChangeCheck() => EditorGUI.BeginChangeCheck();
-
- public static bool EndChangeCheck()
- {
- if (EditorGUI.EndChangeCheck())
- {
- RecordUndo();
- return true;
- }
- else return false;
- }
-
- public static bool EndChangeCheck<T>(ref T field, T value)
- {
- if (EndChangeCheck())
- {
- field = value;
- return true;
- }
- else return false;
- }
-
-
- public static ReorderableList CreateReorderableList<T>(
- List<T> list, string name, ReorderableList.ElementCallbackDelegate drawElementCallback, bool showFooter = false)
- {
- var reorderableList = new ReorderableList(list, typeof(T))
- {
- drawHeaderCallback = (area) => GUI.Label(area, name),
- drawElementCallback = drawElementCallback,
- elementHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing,
- };
- if (!showFooter)
- {
- reorderableList.footerHeight = 0;
- reorderableList.displayAdd = false;
- reorderableList.displayRemove = false;
- }
- return reorderableList;
- }
-
-
- public static ReorderableList CreateReorderableObjectList<T>(
- List<T> objects, string name, bool showFooter = false)
- where T : Object
- {
- var reorderableList = CreateReorderableList(objects, name, (area, index, isActive, isFocused) =>
- {
- area.y = Mathf.Ceil(area.y + EditorGUIUtility.standardVerticalSpacing * 0.5f);
- area.height = EditorGUIUtility.singleLineHeight;
- BeginChangeCheck();
- var obj = (T)EditorGUI.ObjectField(area, objects[index], typeof(T), false);
- if (EndChangeCheck())
- {
- objects[index] = obj;
- }
- }, showFooter);
- if (showFooter)
- {
- reorderableList.onAddCallback = (list) => list.list.Add(null);
- }
- return reorderableList;
- }
-
-
- public static ReorderableList CreateReorderableStringList(
- List<string> strings, string name, Func<Rect, int, string> doElementGUI)
- {
- return CreateReorderableList(strings, name, (area, index, isActive, isFocused) =>
- {
- area.y = Mathf.Ceil(area.y + EditorGUIUtility.standardVerticalSpacing * 0.5f);
- area.height = EditorGUIUtility.singleLineHeight;
- BeginChangeCheck();
- var str = doElementGUI(area, index);
- if (EndChangeCheck())
- {
- strings[index] = str;
- }
- });
- }
-
- public static ReorderableList CreateReorderableStringList(List<string> strings, string name)
- {
- return CreateReorderableStringList(strings, name, (area, index) =>
- {
- return EditorGUI.TextField(area, strings[index]);
- });
- }
-
-
- [MenuItem(Strings.AnimancerToolsMenuPath)]
- public static void Open() => GetWindow<AnimancerToolsWindow>();
-
- public static void Open(Type toolType)
- {
- var window = GetWindow<AnimancerToolsWindow>();
- window._CurrentTool = window.IndexOfTool(toolType);
- }
-
- }
- }
- #endif
|