GenerateGeometryPanel.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using UnityEditor.UIElements;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. internal class GenerateGeometryPanel : VisualElement
  8. {
  9. public class GenerateGeometryPanelFactory : UxmlFactory<GenerateGeometryPanel, GenerateGeometryPanelUxmlTraits> {}
  10. public class GenerateGeometryPanelUxmlTraits : UxmlTraits {}
  11. public enum GenerateMode
  12. {
  13. Single,
  14. Multiple
  15. }
  16. private IntegerField m_OutlineDetailField;
  17. private IntegerField m_AlphaToleranceField;
  18. private IntegerField m_SubdivideField;
  19. private Toggle m_Toggle;
  20. public event Action<float, byte, float> onAutoGenerateGeometry;
  21. public event Action<float, byte, float> onAutoGenerateGeometryAll;
  22. public bool generateWeights
  23. {
  24. get { return m_Toggle.value; }
  25. set { m_Toggle.value = value; }
  26. }
  27. public SkinningCache skinningCache { get; set; }
  28. public GenerateGeometryPanel()
  29. {
  30. styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/GenerateGeometryPanelStyle.uss"));
  31. RegisterCallback<MouseDownEvent>((e) => { e.StopPropagation(); });
  32. RegisterCallback<MouseUpEvent>((e) => { e.StopPropagation(); });
  33. }
  34. private void BindElements()
  35. {
  36. var generateButton = this.Q<Button>("GenerateGeometryButton");
  37. generateButton.clickable.clicked += GenerateGeometry;
  38. generateButton.AddManipulator(generateButton.clickable);
  39. var generateAllButton = this.Q<Button>("GenerateGeometryAllButton");
  40. generateAllButton.clickable.clicked += GenerateGeometryAll;
  41. generateAllButton.AddManipulator(generateAllButton.clickable);
  42. m_OutlineDetailField = this.Q<IntegerField>("OutlineDetailField");
  43. m_AlphaToleranceField = this.Q<IntegerField>("AlphaToleranceField");
  44. m_SubdivideField = this.Q<IntegerField>("SubdivideField");
  45. m_Toggle = this.Q<Toggle>("GenerateWeightsField");
  46. var slider = this.Q<Slider>("OutlineDetailSlider");
  47. LinkSliderToFloatField(slider, m_OutlineDetailField, (x) =>
  48. {
  49. GenerateGeomertySettings.outlineDetail = x;
  50. });
  51. m_OutlineDetailField.SetValueWithoutNotify(GenerateGeomertySettings.outlineDetail);
  52. slider.SetValueWithoutNotify(GenerateGeomertySettings.outlineDetail);
  53. slider = this.Q<Slider>("AlphaToleranceSlider");
  54. LinkSliderToFloatField(slider, m_AlphaToleranceField,(x) =>
  55. {
  56. GenerateGeomertySettings.alphaTolerance = x;
  57. });
  58. m_AlphaToleranceField.SetValueWithoutNotify(GenerateGeomertySettings.alphaTolerance);
  59. slider.SetValueWithoutNotify(GenerateGeomertySettings.alphaTolerance);
  60. slider = this.Q<Slider>("SubdivideSlider");
  61. LinkSliderToFloatField(slider, m_SubdivideField,(x) =>
  62. {
  63. GenerateGeomertySettings.subdivide = x;
  64. });
  65. m_SubdivideField.SetValueWithoutNotify(GenerateGeomertySettings.subdivide);
  66. slider.SetValueWithoutNotify(GenerateGeomertySettings.subdivide);
  67. m_Toggle.value = GenerateGeomertySettings.generateWeights;
  68. m_Toggle.RegisterValueChangedCallback((evt) =>
  69. {
  70. GenerateGeomertySettings.generateWeights = evt.newValue;
  71. });
  72. }
  73. private void LinkSliderToFloatField(Slider slider, IntegerField field, Action<int> updatePreferenceAction)
  74. {
  75. slider.RegisterValueChangedCallback((evt) =>
  76. {
  77. if (!evt.newValue.Equals(field.value))
  78. {
  79. var intValue = Mathf.RoundToInt(evt.newValue);
  80. field.SetValueWithoutNotify(intValue);
  81. updatePreferenceAction(intValue);
  82. }
  83. });
  84. field.RegisterValueChangedCallback((evt) =>
  85. {
  86. var newValue = evt.newValue;
  87. if (!newValue.Equals(slider.value))
  88. {
  89. newValue = Math.Min(newValue, (int)slider.highValue);
  90. newValue = Math.Max(newValue, (int)slider.lowValue);
  91. slider.value = newValue;
  92. field.SetValueWithoutNotify(newValue);
  93. updatePreferenceAction(newValue);
  94. }
  95. });
  96. }
  97. public void SetMode(GenerateMode mode)
  98. {
  99. RemoveFromClassList("Multiple");
  100. RemoveFromClassList("Single");
  101. AddToClassList(mode.ToString());
  102. }
  103. public void GenerateGeometry()
  104. {
  105. if (onAutoGenerateGeometry != null)
  106. onAutoGenerateGeometry(m_OutlineDetailField.value, Convert.ToByte(m_AlphaToleranceField.value), m_SubdivideField.value);
  107. }
  108. public void GenerateGeometryAll()
  109. {
  110. if (onAutoGenerateGeometryAll != null)
  111. onAutoGenerateGeometryAll(m_OutlineDetailField.value, Convert.ToByte(m_AlphaToleranceField.value), m_SubdivideField.value);
  112. }
  113. public static GenerateGeometryPanel GenerateFromUXML()
  114. {
  115. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/GenerateGeometryPanel.uxml");
  116. var clone = visualTree.CloneTree().Q<GenerateGeometryPanel>("GenerateGeometryPanel");
  117. clone.BindElements();
  118. return clone;
  119. }
  120. }
  121. }