AnimancerToolsWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. using UnityEngine;
  8. using Object = UnityEngine.Object;
  9. namespace Animancer.Editor.Tools
  10. {
  11. /// <summary>[Editor-Only] [Pro-Only]
  12. /// An <see cref="EditorWindow"/> with various utilities for managing sprites and generating animations.
  13. /// </summary>
  14. /// <remarks>
  15. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/tools">Animancer Tools</see>
  16. /// </remarks>
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Editor.Tools/AnimancerToolsWindow
  18. ///
  19. public sealed partial class AnimancerToolsWindow : EditorWindow
  20. {
  21. /************************************************************************************************************************/
  22. /// <summary>The display name of this window.</summary>
  23. public const string Name = "Animancer Tools";
  24. /// <summary>The singleton instance of this window.</summary>
  25. public static AnimancerToolsWindow Instance { get; private set; }
  26. [SerializeReference] private List<Tool> _Tools;
  27. [SerializeField] private Vector2 _Scroll;
  28. [SerializeField] private int _CurrentTool = -1;
  29. /************************************************************************************************************************/
  30. private SerializedObject _SerializedObject;
  31. private SerializedObject SerializedObject
  32. => _SerializedObject ?? (_SerializedObject = new SerializedObject(this));
  33. /// <summary>Returns the <see cref="SerializedProperty"/> which represents the specified `tool`.</summary>
  34. public SerializedProperty FindSerializedPropertyForTool(Tool tool)
  35. {
  36. var index = _Tools.IndexOf(tool);
  37. var property = SerializedObject.FindProperty(nameof(_Tools));
  38. return property.GetArrayElementAtIndex(index);
  39. }
  40. /************************************************************************************************************************/
  41. private void OnEnable()
  42. {
  43. titleContent = new GUIContent(Name);
  44. Instance = this;
  45. InitializeTools();
  46. Undo.undoRedoPerformed += Repaint;
  47. OnSelectionChange();
  48. }
  49. /************************************************************************************************************************/
  50. private void InitializeTools()
  51. {
  52. if (_Tools == null)
  53. {
  54. _Tools = new List<Tool>();
  55. }
  56. else
  57. {
  58. for (int i = _Tools.Count - 1; i >= 0; i--)
  59. if (_Tools[i] == null)
  60. _Tools.RemoveAt(i);
  61. }
  62. var toolTypes = TypeSelectionButton.GetDerivedTypes(typeof(Tool));
  63. for (int i = 0; i < toolTypes.Count; i++)
  64. {
  65. var toolType = toolTypes[i];
  66. if (IndexOfTool(toolType) >= 0)
  67. continue;
  68. var tool = (Tool)Activator.CreateInstance(toolType);
  69. _Tools.Add(tool);
  70. }
  71. _Tools.Sort();
  72. for (int i = 0; i < _Tools.Count; i++)
  73. _Tools[i].OnEnable(i);
  74. }
  75. /************************************************************************************************************************/
  76. private int IndexOfTool(Type type)
  77. {
  78. for (int i = 0; i < _Tools.Count; i++)
  79. if (_Tools[i].GetType() == type)
  80. return i;
  81. return -1;
  82. }
  83. /************************************************************************************************************************/
  84. private void OnDisable()
  85. {
  86. Undo.undoRedoPerformed -= Repaint;
  87. for (int i = 0; i < _Tools.Count; i++)
  88. _Tools[i].OnDisable();
  89. if (_SerializedObject != null)
  90. {
  91. _SerializedObject.Dispose();
  92. _SerializedObject = null;
  93. }
  94. }
  95. /************************************************************************************************************************/
  96. private void OnSelectionChange()
  97. {
  98. for (int i = 0; i < _Tools.Count; i++)
  99. _Tools[i].OnSelectionChanged();
  100. Repaint();
  101. }
  102. /************************************************************************************************************************/
  103. private void OnGUI()
  104. {
  105. EditorGUIUtility.labelWidth = Mathf.Min(EditorGUIUtility.labelWidth, position.width * 0.5f);
  106. _Scroll = GUILayout.BeginScrollView(_Scroll);
  107. GUILayout.BeginVertical();
  108. GUILayout.EndVertical();
  109. for (int i = 0; i < _Tools.Count; i++)
  110. _Tools[i].DoGUI();
  111. GUILayout.EndScrollView();
  112. }
  113. /************************************************************************************************************************/
  114. /// <summary>Causes this window to redraw its GUI.</summary>
  115. public static new void Repaint() => ((EditorWindow)Instance).Repaint();
  116. /// <summary>Calls <see cref="Undo.RecordObject(Object, string)"/> for this window.</summary>
  117. public static void RecordUndo() => Undo.RecordObject(Instance, Name);
  118. /************************************************************************************************************************/
  119. /// <summary>Calls <see cref="EditorGUI.BeginChangeCheck"/>.</summary>
  120. public static void BeginChangeCheck() => EditorGUI.BeginChangeCheck();
  121. /// <summary>Calls <see cref="EditorGUI.EndChangeCheck"/> and <see cref="RecordUndo"/> if it returned true.</summary>
  122. public static bool EndChangeCheck()
  123. {
  124. if (EditorGUI.EndChangeCheck())
  125. {
  126. RecordUndo();
  127. return true;
  128. }
  129. else return false;
  130. }
  131. /// <summary>Calls <see cref="EndChangeCheck"/> and sets the <c>field = value</c> if it returned true.</summary>
  132. public static bool EndChangeCheck<T>(ref T field, T value)
  133. {
  134. if (EndChangeCheck())
  135. {
  136. field = value;
  137. return true;
  138. }
  139. else return false;
  140. }
  141. /************************************************************************************************************************/
  142. /// <summary>Creates and initializes a new <see cref="ReorderableList"/>.</summary>
  143. public static ReorderableList CreateReorderableList<T>(
  144. List<T> list, string name, ReorderableList.ElementCallbackDelegate drawElementCallback, bool showFooter = false)
  145. {
  146. var reorderableList = new ReorderableList(list, typeof(T))
  147. {
  148. drawHeaderCallback = (area) => GUI.Label(area, name),
  149. drawElementCallback = drawElementCallback,
  150. elementHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing,
  151. };
  152. if (!showFooter)
  153. {
  154. reorderableList.footerHeight = 0;
  155. reorderableList.displayAdd = false;
  156. reorderableList.displayRemove = false;
  157. }
  158. return reorderableList;
  159. }
  160. /************************************************************************************************************************/
  161. /// <summary>Creates and initializes a new <see cref="ReorderableList"/> for <see cref="Sprite"/>s.</summary>
  162. public static ReorderableList CreateReorderableObjectList<T>(
  163. List<T> objects, string name, bool showFooter = false)
  164. where T : Object
  165. {
  166. var reorderableList = CreateReorderableList(objects, name, (area, index, isActive, isFocused) =>
  167. {
  168. area.y = Mathf.Ceil(area.y + EditorGUIUtility.standardVerticalSpacing * 0.5f);
  169. area.height = EditorGUIUtility.singleLineHeight;
  170. BeginChangeCheck();
  171. var obj = (T)EditorGUI.ObjectField(area, objects[index], typeof(T), false);
  172. if (EndChangeCheck())
  173. {
  174. objects[index] = obj;
  175. }
  176. }, showFooter);
  177. if (showFooter)
  178. {
  179. reorderableList.onAddCallback = (list) => list.list.Add(null);
  180. }
  181. return reorderableList;
  182. }
  183. /************************************************************************************************************************/
  184. /// <summary>Creates a new <see cref="ReorderableList"/> for <see cref="string"/>s.</summary>
  185. public static ReorderableList CreateReorderableStringList(
  186. List<string> strings, string name, Func<Rect, int, string> doElementGUI)
  187. {
  188. return CreateReorderableList(strings, name, (area, index, isActive, isFocused) =>
  189. {
  190. area.y = Mathf.Ceil(area.y + EditorGUIUtility.standardVerticalSpacing * 0.5f);
  191. area.height = EditorGUIUtility.singleLineHeight;
  192. BeginChangeCheck();
  193. var str = doElementGUI(area, index);
  194. if (EndChangeCheck())
  195. {
  196. strings[index] = str;
  197. }
  198. });
  199. }
  200. /// <summary>Creates a new <see cref="ReorderableList"/> for <see cref="string"/>s.</summary>
  201. public static ReorderableList CreateReorderableStringList(List<string> strings, string name)
  202. {
  203. return CreateReorderableStringList(strings, name, (area, index) =>
  204. {
  205. return EditorGUI.TextField(area, strings[index]);
  206. });
  207. }
  208. /************************************************************************************************************************/
  209. /// <summary>Opens the <see cref="AnimancerToolsWindow"/>.</summary>
  210. [MenuItem(Strings.AnimancerToolsMenuPath)]
  211. public static void Open() => GetWindow<AnimancerToolsWindow>();
  212. /// <summary>Opens the <see cref="AnimancerToolsWindow"/> showing the specified `tool`.</summary>
  213. public static void Open(Type toolType)
  214. {
  215. var window = GetWindow<AnimancerToolsWindow>();
  216. window._CurrentTool = window.IndexOfTool(toolType);
  217. }
  218. /************************************************************************************************************************/
  219. }
  220. }
  221. #endif