Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateWeightsPanel.cs 3.3KB

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