GenerateWeightsPanel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class GenerateWeightsPanel : VisualElement
  7. {
  8. public class GenerateWeightsPanelFactory : UxmlFactory<GenerateWeightsPanel, GenerateWeightsPanelUxmlTraits> {}
  9. public class GenerateWeightsPanelUxmlTraits : UxmlTraits {}
  10. public event Action onGenerateWeights = () => {};
  11. public event Action onNormalizeWeights = () => {};
  12. public event Action onClearWeights = () => {};
  13. private VisualElement m_AssociateBoneControl;
  14. private Toggle m_AssociateBonesToggle;
  15. Button m_GenerateWeightsButton;
  16. public bool associateBones
  17. {
  18. get { return m_AssociateBoneControl.visible && m_AssociateBonesToggle.value; }
  19. set { m_AssociateBonesToggle.value = value; }
  20. }
  21. public GenerateWeightsPanel()
  22. {
  23. styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/GenerateWeightsPanelStyle.uss"));
  24. if (EditorGUIUtility.isProSkin)
  25. AddToClassList("Dark");
  26. AddToClassList("AssociateBoneEnabled");
  27. RegisterCallback<MouseDownEvent>((e) => { e.StopPropagation(); });
  28. RegisterCallback<MouseUpEvent>((e) => { e.StopPropagation(); });
  29. }
  30. public void BindElements()
  31. {
  32. m_AssociateBoneControl = this.Q<VisualElement>("AssociateBonesControl");
  33. m_GenerateWeightsButton = this.Q<Button>("GenerateWeightsButton");
  34. m_GenerateWeightsButton.clickable.clicked += OnGenerateWeights;
  35. var normalizeWeightsButton = this.Q<Button>("NormalizeWeightsButton");
  36. normalizeWeightsButton.clickable.clicked += OnNormalizeWeights;
  37. var clearWeightsButton = this.Q<Button>("ClearWeightsButton");
  38. clearWeightsButton.clickable.clicked += OnClearWeights;
  39. m_AssociateBonesToggle = this.Q<Toggle>("AssociateBonesField");
  40. }
  41. public string generateButtonText
  42. {
  43. set { m_GenerateWeightsButton.text = value; }
  44. }
  45. public void Update(bool enableAssociateBones)
  46. {
  47. m_AssociateBoneControl.SetHiddenFromLayout(!enableAssociateBones);
  48. if (enableAssociateBones)
  49. {
  50. RemoveFromClassList("AssociateBoneDisabled");
  51. AddToClassList("AssociateBoneEnabled");
  52. }
  53. else
  54. {
  55. RemoveFromClassList("AssociateBoneEnabled");
  56. AddToClassList("AssociateBoneDisabled");
  57. }
  58. }
  59. public void OnGenerateWeights()
  60. {
  61. onGenerateWeights();
  62. }
  63. public void OnNormalizeWeights()
  64. {
  65. onNormalizeWeights();
  66. }
  67. public void OnClearWeights()
  68. {
  69. onClearWeights();
  70. }
  71. public static GenerateWeightsPanel GenerateFromUXML()
  72. {
  73. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/GenerateWeightsPanel.uxml");
  74. var clone = visualTree.CloneTree().Q<GenerateWeightsPanel>("GenerateWeightsPanel");
  75. clone.BindElements();
  76. return clone;
  77. }
  78. }
  79. }