WeightedMaskLayersDefinitionWindow.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 UnityEditor.IMGUI.Controls;
  7. using UnityEngine;
  8. using static Animancer.Editor.AnimancerGUI;
  9. namespace Animancer.Editor
  10. {
  11. /// <summary>An <see cref="TransformTreeWindow{TTarget, TDefinition}"/> for editing spring definitions.</summary>
  12. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/WeightedMaskLayersDefinitionWindow
  13. public class WeightedMaskLayersDefinitionWindow :
  14. TransformTreeWindow<WeightedMaskLayers, WeightedMaskLayersDefinition>
  15. {
  16. /************************************************************************************************************************/
  17. /// <inheritdoc/>
  18. public override IList<Transform> Transforms
  19. => Data.Transforms;
  20. /// <inheritdoc/>
  21. public override WeightedMaskLayersDefinition SourceData
  22. {
  23. get => SourceObject.Definition;
  24. set => SourceObject.Definition = value;
  25. }
  26. /************************************************************************************************************************/
  27. /// <inheritdoc/>
  28. protected override MultiColumnHeaderState.Column[] CreateColumns(float width)
  29. {
  30. const float TransformWidth = 300;
  31. const float GroupWidth = 100;
  32. var groupCount = Data.GroupCount;
  33. var oldColumns = HeaderState?.columns;
  34. var newColumns = new MultiColumnHeaderState.Column[groupCount + 2];
  35. int index;
  36. if (oldColumns == null)
  37. {
  38. var tooltip = "Select which objects to control the weight of";
  39. newColumns[0] = CreateColumn(
  40. "Transform",
  41. tooltip,
  42. TransformWidth);
  43. var includedColumn = newColumns[1] = CreateColumn(
  44. "?",
  45. tooltip,
  46. LineHeight + StandardSpacing);
  47. includedColumn.minWidth = includedColumn.maxWidth = includedColumn.width;
  48. index = 2;
  49. }
  50. else
  51. {
  52. var copyCount = Math.Min(oldColumns.Length, groupCount + 2);
  53. for (int i = 0; i < copyCount; i++)
  54. newColumns[i] = oldColumns[i];
  55. index = copyCount;
  56. }
  57. for (int i = index; i < groupCount + 2; i++)
  58. {
  59. var groupIndex = i - 2;
  60. var name = "Group " + groupIndex.ToStringCached();
  61. var tooltip = "The weights for " + name;
  62. if (groupIndex == 0)
  63. {
  64. name = "Default " + name;
  65. tooltip += " (this group will be applied on startup by default)";
  66. }
  67. newColumns[i] = CreateColumn(
  68. name,
  69. tooltip,
  70. GroupWidth);
  71. }
  72. return newColumns;
  73. }
  74. /************************************************************************************************************************/
  75. /// <inheritdoc/>
  76. protected override Color GetRowColor(TreeViewItem item)
  77. {
  78. if (!TreeView.Transforms.TryGetObject(item.id, out var transform))
  79. return default;
  80. if (Transforms.IndexOf(transform) < 0)
  81. return default;
  82. return GetChainColor(transform, Transforms, 0.15f);
  83. }
  84. /// <summary>Returns a color based on the name of the `transform`'s highest included parent.</summary>
  85. public static Color GetChainColor(Transform transform, IList<Transform> transforms, float alpha)
  86. {
  87. transform = GetChainRoot(transform, transforms);
  88. return GetHashColor(transform.name.GetHashCode(), 1, 1, alpha);
  89. }
  90. /// <summary>Gets the highest parent of `transform` which is included in the `transforms`.</summary>
  91. public static Transform GetChainRoot(Transform transform, IList<Transform> transforms)
  92. {
  93. var parent = transform.parent;
  94. while (parent != null)
  95. {
  96. if (transforms.IndexOf(parent) >= 0)
  97. transform = parent;
  98. parent = parent.parent;
  99. }
  100. return transform;
  101. }
  102. /************************************************************************************************************************/
  103. /// <inheritdoc/>
  104. public override void DrawCellGUI(Rect area, int column, int row, TreeViewItem item, ref bool isSelectionClick)
  105. {
  106. if (!TreeView.Transforms.TryGetObject(item.id, out var transform))
  107. return;
  108. var definitionIndex = GetDefinitionIndex(item.id);
  109. switch (column)
  110. {
  111. case 0:// Transform.
  112. DrawTransformCellGUI(area, transform);
  113. break;
  114. case 1:// Included.
  115. DrawIsIncludedCellGUI(area, item.id, definitionIndex, ref isSelectionClick);
  116. break;
  117. default:// Groups.
  118. DrawWeightGUI(area, item.id, column - 2, definitionIndex);
  119. break;
  120. }
  121. }
  122. /************************************************************************************************************************/
  123. /// <inheritdoc/>
  124. protected override void SetIncluded(
  125. int treeItemID,
  126. int definitionIndex,
  127. bool isIncluded)
  128. {
  129. var data = RecordUndo();
  130. if (isIncluded)
  131. {
  132. if (definitionIndex < 0 &&
  133. TreeView.Transforms.TryGetObject(treeItemID, out var transform))
  134. {
  135. var groupCount = data.GroupCount;
  136. data.AddTransform(transform);
  137. if (groupCount != data.GroupCount)
  138. CreateHeader();
  139. }
  140. }
  141. else
  142. {
  143. if (definitionIndex >= 0)
  144. {
  145. data.RemoveTransform(definitionIndex);
  146. if (data.GroupCount <= 0)
  147. CreateHeader();
  148. }
  149. }
  150. }
  151. /************************************************************************************************************************/
  152. private void DrawWeightGUI(
  153. Rect area,
  154. int treeItemID,
  155. int groupIndex,
  156. int transformIndex)
  157. {
  158. if (transformIndex < 0)
  159. return;
  160. var weight = Data.GetWeight(groupIndex, transformIndex);
  161. if (float.IsNaN(weight))
  162. return;
  163. EditorGUI.BeginChangeCheck();
  164. weight = EditorGUI.FloatField(area, weight);
  165. if (EditorGUI.EndChangeCheck())
  166. {
  167. weight = Mathf.Clamp01(weight);
  168. SetValue(treeItemID, i => RecordUndo().SetWeight(groupIndex, i, weight));
  169. }
  170. }
  171. /************************************************************************************************************************/
  172. private static readonly GUIContent
  173. AddGroupLabel = new(
  174. "Add Group",
  175. "Add another weight group"),
  176. RemoveGroupLabel = new(
  177. "Remove Group",
  178. "Remove the last weight group");
  179. /// <inheritdoc/>
  180. protected override void DoFooterCenterGUI()
  181. {
  182. var hasTransforms = !Data.Transforms.IsNullOrEmpty();
  183. GUI.enabled = hasTransforms;
  184. if (GUILayout.Button(AddGroupLabel))
  185. {
  186. RecordUndo().GroupCount++;
  187. CreateHeader();
  188. }
  189. if (hasTransforms)
  190. GUI.enabled = Data.GroupCount > 1;
  191. if (GUILayout.Button(RemoveGroupLabel))
  192. {
  193. RecordUndo().GroupCount--;
  194. CreateHeader();
  195. }
  196. }
  197. /************************************************************************************************************************/
  198. /// <inheritdoc/>
  199. public override void Apply()
  200. {
  201. base.Apply();
  202. CreateHeader();
  203. SceneView.RepaintAll();
  204. }
  205. /// <inheritdoc/>
  206. public override void Revert()
  207. {
  208. base.Revert();
  209. CreateHeader();
  210. SceneView.RepaintAll();
  211. }
  212. /************************************************************************************************************************/
  213. /// <inheritdoc/>
  214. public override WeightedMaskLayersDefinition RecordUndo(string name = "Animancer")
  215. {
  216. SceneView.RepaintAll();
  217. return base.RecordUndo(name);
  218. }
  219. /************************************************************************************************************************/
  220. }
  221. }
  222. #endif