AnimancerToolsWindow.cs 10 KB

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