BoneToolbar.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class BoneToolbar : Toolbar
  7. {
  8. public class BoneToolbarFactory : UxmlFactory<BoneToolbar, BoneToolbarUxmlTraits> {}
  9. public class BoneToolbarUxmlTraits : UxmlTraits {}
  10. public event Action<Tools> SetSkeletonTool = (mode) => {};
  11. public SkinningCache skinningCache { get; private set; }
  12. public BoneToolbar()
  13. {
  14. styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/BoneToolbarStyle.uss"));
  15. }
  16. public void BindElements()
  17. {
  18. var editPose = this.Q<Button>("EditPose");
  19. editPose.clickable.clicked += () => { SetSkeletonTool(Tools.EditPose); };
  20. var editJoints = this.Q<Button>("EditJoints");
  21. editJoints.clickable.clicked += () => { SetSkeletonTool(Tools.EditJoints); };
  22. var createBone = this.Q<Button>("CreateBone");
  23. createBone.clickable.clicked += () => { SetSkeletonTool(Tools.CreateBone); };
  24. var splitBone = this.Q<Button>("SplitBone");
  25. splitBone.clickable.clicked += () => { SetSkeletonTool(Tools.SplitBone); };
  26. }
  27. public void Setup(SkinningCache s)
  28. {
  29. skinningCache = s;
  30. skinningCache.events.skinningModeChanged.AddListener(OnSkinningModeChange);
  31. }
  32. private void OnSkinningModeChange(SkinningMode mode)
  33. {
  34. if (skinningCache.hasCharacter)
  35. {
  36. if (mode == SkinningMode.SpriteSheet)
  37. {
  38. this.Q<Button>("EditJoints").SetEnabled(false);
  39. this.Q<Button>("CreateBone").SetEnabled(false);
  40. this.Q<Button>("SplitBone").SetEnabled(false);
  41. if (skinningCache.GetTool(Tools.EditJoints).isActive
  42. || skinningCache.GetTool(Tools.CreateBone).isActive
  43. || skinningCache.GetTool(Tools.SplitBone).isActive)
  44. SetSkeletonTool(Tools.EditPose);
  45. }
  46. else if (mode == SkinningMode.Character)
  47. {
  48. this.Q<Button>("EditJoints").SetEnabled(true);
  49. this.Q<Button>("CreateBone").SetEnabled(true);
  50. this.Q<Button>("SplitBone").SetEnabled(true);
  51. }
  52. }
  53. }
  54. public void UpdateToggleState()
  55. {
  56. //TODO: Make UI not be aware of BaseTool, Cache, etc. Use Tool enum
  57. var toolButton = this.Q<Button>("EditPose");
  58. SetButtonChecked(toolButton, skinningCache.GetTool(Tools.EditPose).isActive);
  59. toolButton = this.Q<Button>("EditJoints");
  60. SetButtonChecked(toolButton, skinningCache.GetTool(Tools.EditJoints).isActive);
  61. toolButton = this.Q<Button>("CreateBone");
  62. SetButtonChecked(toolButton, skinningCache.GetTool(Tools.CreateBone).isActive);
  63. toolButton = this.Q<Button>("SplitBone");
  64. SetButtonChecked(toolButton, skinningCache.GetTool(Tools.SplitBone).isActive);
  65. OnSkinningModeChange(skinningCache.mode);
  66. }
  67. public static BoneToolbar GenerateFromUXML()
  68. {
  69. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/BoneToolbar.uxml");
  70. var clone = visualTree.CloneTree().Q<BoneToolbar>("BoneToolbar");
  71. clone.BindElements();
  72. return clone;
  73. }
  74. }
  75. }