WeightToolbar.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class WeightToolbar : Toolbar
  7. {
  8. public class CustomUXMLFactor : UxmlFactory<WeightToolbar, UxmlTraits> {}
  9. public event Action<Tools> SetWeightTool = (mode) => {};
  10. public SkinningCache skinningCache { get; set; }
  11. public WeightToolbar()
  12. {
  13. styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/WeightToolbarStyle.uss"));
  14. }
  15. public void EnableBoneInfluenceWidget()
  16. {
  17. this.Q<Button>("BoneInfluenceWidget").SetEnabled(true);
  18. }
  19. public void DisableBoneInfluenceWidget()
  20. {
  21. this.Q<Button>("BoneInfluenceWidget").SetEnabled(false);
  22. }
  23. public void BindElements()
  24. {
  25. var button = this.Q<Button>("AutoGenerateWeight");
  26. button.clickable.clicked += () => SetWeightTool(Tools.GenerateWeights);
  27. button = this.Q<Button>("WeightPainterSlider");
  28. button.clickable.clicked += () => SetWeightTool(Tools.WeightSlider);
  29. button = this.Q<Button>("WeightPainterBrush");
  30. button.clickable.clicked += () => SetWeightTool(Tools.WeightBrush);
  31. button = this.Q<Button>("BoneInfluenceWidget");
  32. button.clickable.clicked += () => SetWeightTool(Tools.BoneInfluence);
  33. }
  34. public static WeightToolbar GenerateFromUXML()
  35. {
  36. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/WeightToolbar.uxml");
  37. var clone = visualTree.CloneTree().Q<WeightToolbar>("WeightToolbar");
  38. clone.BindElements();
  39. return clone;
  40. }
  41. public void UpdateToggleState()
  42. {
  43. //TODO: Make UI not be aware of BaseTool, Cache, etc. Use Tool enum
  44. var button = this.Q<Button>("WeightPainterSlider");
  45. SetButtonChecked(button, skinningCache.GetTool(Tools.WeightSlider).isActive);
  46. button = this.Q<Button>("BoneInfluenceWidget");
  47. SetButtonChecked(button, skinningCache.GetTool(Tools.BoneInfluence).isActive);
  48. button = this.Q<Button>("WeightPainterBrush");
  49. SetButtonChecked(button, skinningCache.GetTool(Tools.WeightBrush).isActive);
  50. button = this.Q<Button>("AutoGenerateWeight");
  51. SetButtonChecked(button, skinningCache.GetTool(Tools.GenerateWeights).isActive);
  52. }
  53. }
  54. }