WeightPainterPanel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.UIElements;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. internal class WeightPainterPanel : VisualElement
  9. {
  10. public class WeightPainterPanelFactory : UxmlFactory<WeightPainterPanel, WeightPainterPanelUxmlTraits> {}
  11. public class WeightPainterPanelUxmlTraits : UxmlTraits {}
  12. public static readonly string kNone = "None";
  13. private WeightPainterMode m_PaintMode;
  14. private EnumField m_ModeField;
  15. private Toggle m_NormalizeToggle;
  16. private IntegerField m_HardnessField;
  17. private IntegerField m_StepField;
  18. private IntegerField m_SizeField;
  19. private FloatField m_AmountField;
  20. private Slider m_AmountSlider;
  21. private VisualElement m_BonePopupContainer;
  22. private PopupField<string> m_BonePopup;
  23. private bool m_SliderActive = false;
  24. private WeightInspectorIMGUIPanel m_WeightInspectorPanel;
  25. private UnityEngine.UIElements.PopupWindow m_PopupWindow;
  26. public event Action<int> bonePopupChanged = (s) => {};
  27. public event Action sliderStarted = () => {};
  28. public event Action<float> sliderChanged = (s) => {};
  29. public event Action sliderEnded = () => {};
  30. public event Action weightsChanged = () => {};
  31. public WeightPainterMode paintMode
  32. {
  33. get { return m_PaintMode; }
  34. set
  35. {
  36. if (value == m_PaintMode)
  37. return;
  38. m_PaintMode = value;
  39. if (m_PaintMode == WeightPainterMode.Brush)
  40. {
  41. RemoveFromClassList("SliderMode");
  42. AddToClassList("BrushMode");
  43. }
  44. else
  45. {
  46. RemoveFromClassList("BrushMode");
  47. AddToClassList("SliderMode");
  48. }
  49. }
  50. }
  51. public string title
  52. {
  53. set { m_PopupWindow.text = value; }
  54. }
  55. public WeightEditorMode mode
  56. {
  57. get { return (WeightEditorMode)m_ModeField.value; }
  58. set { m_ModeField.value = value; }
  59. }
  60. public int boneIndex
  61. {
  62. get { return m_BonePopup.index - 1; }
  63. }
  64. public int size
  65. {
  66. get { return m_SizeField.value; }
  67. set { m_SizeField.value = value; }
  68. }
  69. public int hardness
  70. {
  71. get { return m_HardnessField.value; }
  72. set { m_HardnessField.value = value; }
  73. }
  74. public int step
  75. {
  76. get { return m_StepField.value; }
  77. set { m_StepField.value = value; }
  78. }
  79. public bool normalize
  80. {
  81. get { return m_NormalizeToggle.value; }
  82. set { m_NormalizeToggle.value = value; }
  83. }
  84. public float amount
  85. {
  86. get { return m_AmountField.value; }
  87. set { m_AmountField.value = value; }
  88. }
  89. public WeightPainterPanel()
  90. {
  91. styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/WeightPainterPanelStyle.uss"));
  92. if (EditorGUIUtility.isProSkin)
  93. AddToClassList("Dark");
  94. paintMode = WeightPainterMode.Brush;
  95. AddToClassList("BrushMode");
  96. RegisterCallback<MouseDownEvent>((e) => { e.StopPropagation(); });
  97. RegisterCallback<MouseUpEvent>((e) => { e.StopPropagation(); });
  98. }
  99. public void BindElements()
  100. {
  101. m_ModeField = this.Q<EnumField>("ModeField");
  102. m_BonePopupContainer = this.Q<VisualElement>("BoneEnumPopup");
  103. m_NormalizeToggle = this.Q<Toggle>("NormalizeToggle");
  104. m_SizeField = this.Q<IntegerField>("SizeField");
  105. m_HardnessField = this.Q<IntegerField>("HardnessField");
  106. m_StepField = this.Q<IntegerField>("StepField");
  107. m_AmountSlider = this.Q<Slider>("AmountSlider");
  108. m_AmountField = this.Q<FloatField>("AmountField");
  109. m_AmountField.isDelayed = true;
  110. m_WeightInspectorPanel = this.Q<WeightInspectorIMGUIPanel>("WeightsInspector");
  111. m_PopupWindow = this.Q<UnityEngine.UIElements.PopupWindow>();
  112. LinkSliderToIntegerField(this.Q<Slider>("HardnessSlider"), m_HardnessField);
  113. LinkSliderToIntegerField(this.Q<Slider>("StepSlider"), m_StepField);
  114. m_ModeField.RegisterValueChangedCallback((evt) =>
  115. {
  116. SetupMode();
  117. });
  118. m_AmountSlider.RegisterValueChangedCallback((evt) =>
  119. {
  120. if (!evt.Equals(m_AmountField.value))
  121. m_AmountField.value = (float)Math.Round((double)evt.newValue, 2);
  122. if (m_SliderActive)
  123. sliderChanged?.Invoke(m_AmountField.value);
  124. });
  125. m_AmountSlider.RegisterCallback<MouseCaptureEvent>(evt =>
  126. {
  127. m_SliderActive = true;
  128. sliderStarted?.Invoke();
  129. }, TrickleDown.TrickleDown);
  130. m_AmountSlider.RegisterCallback<MouseCaptureOutEvent>(evt =>
  131. {
  132. m_SliderActive = false;
  133. sliderEnded?.Invoke();
  134. m_AmountSlider.value = 0;
  135. }, TrickleDown.TrickleDown);
  136. m_AmountField.RegisterValueChangedCallback((evt) =>
  137. {
  138. var newValue = Mathf.Clamp(evt.newValue, m_AmountSlider.lowValue, m_AmountSlider.highValue);
  139. if (focusController.focusedElement == m_AmountField && !newValue.Equals(m_AmountSlider.value))
  140. {
  141. sliderStarted();
  142. sliderChanged(newValue);
  143. sliderEnded();
  144. Focus();
  145. m_AmountField.value = 0f;
  146. m_AmountSlider.value = 0f;
  147. }
  148. });
  149. m_WeightInspectorPanel.weightsChanged += () => weightsChanged();
  150. }
  151. public void SetActive(bool active)
  152. {
  153. this.Q("Amount").SetEnabled(active);
  154. }
  155. private void SetupMode()
  156. {
  157. var boneElement = this.Q<VisualElement>("Bone");
  158. boneElement.SetHiddenFromLayout(mode == WeightEditorMode.Smooth);
  159. SetupAmountSlider();
  160. }
  161. private void SetupAmountSlider()
  162. {
  163. if (paintMode == WeightPainterMode.Slider)
  164. {
  165. if (mode == WeightEditorMode.Smooth)
  166. {
  167. m_AmountSlider.lowValue = 0.0f;
  168. m_AmountSlider.highValue = 8.0f;
  169. }
  170. else
  171. {
  172. m_AmountSlider.lowValue = -1.0f;
  173. m_AmountSlider.highValue = 1.0f;
  174. }
  175. }
  176. }
  177. private void LinkSliderToIntegerField(Slider slider, IntegerField field)
  178. {
  179. slider.RegisterValueChangedCallback((evt) =>
  180. {
  181. if (!evt.newValue.Equals(field.value))
  182. field.value = Mathf.RoundToInt(evt.newValue);
  183. });
  184. field.RegisterValueChangedCallback((evt) =>
  185. {
  186. if (!evt.newValue.Equals((int)slider.value))
  187. slider.value = evt.newValue;
  188. });
  189. }
  190. public void UpdateWeightInspector(ISpriteMeshData spriteMeshData, string[] boneNames, ISelection<int> selection, ICacheUndo cacheUndo)
  191. {
  192. m_WeightInspectorPanel.weightInspector.spriteMeshData = spriteMeshData;
  193. m_WeightInspectorPanel.weightInspector.boneNames = ModuleUtility.ToGUIContentArray(boneNames);
  194. m_WeightInspectorPanel.weightInspector.selection = selection;
  195. m_WeightInspectorPanel.weightInspector.cacheUndo = cacheUndo;
  196. }
  197. public void UpdatePanel(string[] boneNames)
  198. {
  199. SetupMode();
  200. UpdateBonePopup(boneNames);
  201. }
  202. private void UpdateBonePopup(string[] names)
  203. {
  204. VisualElement boneElement = null;
  205. if (m_ModeField != null && mode == WeightEditorMode.Smooth)
  206. {
  207. boneElement = this.Q<VisualElement>("Bone");
  208. boneElement.SetHiddenFromLayout(false);
  209. }
  210. if (m_BonePopup != null)
  211. {
  212. m_BonePopupContainer.Remove(m_BonePopup);
  213. }
  214. m_BonePopup = new PopupField<string>(new List<string>(names), 0);
  215. m_BonePopup.name = "BonePopupField";
  216. m_BonePopup.label = TextContent.bone;
  217. m_BonePopup.tooltip = TextContent.boneToolTip;
  218. m_BonePopup.RegisterValueChangedCallback((evt) =>
  219. {
  220. bonePopupChanged(boneIndex);
  221. });
  222. m_BonePopupContainer.Add(m_BonePopup);
  223. if (boneElement != null)
  224. {
  225. boneElement.SetHiddenFromLayout(true);
  226. }
  227. }
  228. internal void SetBoneSelectionByName(string boneName)
  229. {
  230. if (m_BonePopup != null)
  231. m_BonePopup.value = boneName;
  232. }
  233. public static WeightPainterPanel GenerateFromUXML()
  234. {
  235. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/WeightPainterPanel.uxml");
  236. var clone = visualTree.CloneTree().Q<WeightPainterPanel>("WeightPainterPanel");
  237. // EnumField can only get type of Enum from the current running assembly when defined through UXML
  238. // Manually create the EnumField here
  239. var mode = clone.Q<VisualElement>("Mode");
  240. var modeField = new EnumField(WeightEditorMode.AddAndSubtract);
  241. modeField.name = "ModeField";
  242. modeField.label = TextContent.mode;
  243. modeField.tooltip = TextContent.modeTooltip;
  244. mode.Add(modeField);
  245. clone.BindElements();
  246. return clone;
  247. }
  248. }
  249. }