SkeletonTool.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using UnityEditor.U2D.Layout;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class SkeletonTool : BaseTool
  7. {
  8. [SerializeField]
  9. private SkeletonController m_SkeletonController;
  10. private SkeletonToolView m_SkeletonToolView;
  11. private RectBoneSelector m_RectBoneSelector = new RectBoneSelector();
  12. private RectSelectionTool<BoneCache> m_RectSelectionTool = new RectSelectionTool<BoneCache>();
  13. private UnselectTool<BoneCache> m_UnselectTool = new UnselectTool<BoneCache>();
  14. public bool enableBoneInspector { get; set; }
  15. public SkeletonMode mode
  16. {
  17. get { return m_SkeletonController.view.mode; }
  18. set { m_SkeletonController.view.mode = value; }
  19. }
  20. public bool editBindPose
  21. {
  22. get { return m_SkeletonController.editBindPose; }
  23. set { m_SkeletonController.editBindPose = value; }
  24. }
  25. public ISkeletonStyle skeletonStyle
  26. {
  27. get { return m_SkeletonController.styleOverride; }
  28. set { m_SkeletonController.styleOverride = value; }
  29. }
  30. public override int defaultControlID
  31. {
  32. get { return 0; }
  33. }
  34. public BoneCache hoveredBone
  35. {
  36. get { return m_SkeletonController.hoveredBone; }
  37. }
  38. public SkeletonCache skeleton
  39. {
  40. get { return m_SkeletonController.skeleton; }
  41. private set { m_SkeletonController.skeleton = value; }
  42. }
  43. internal override void OnCreate()
  44. {
  45. m_SkeletonController = new SkeletonController();
  46. m_SkeletonController.view = new SkeletonView(new GUIWrapper());
  47. m_SkeletonController.view.InvalidID = 0;
  48. m_SkeletonController.selection = skinningCache.skeletonSelection;
  49. m_SkeletonToolView = new SkeletonToolView();
  50. m_SkeletonToolView.onBoneNameChanged += BoneNameChanged;
  51. m_SkeletonToolView.onBoneDepthChanged += BoneDepthChanged;
  52. m_RectBoneSelector.selection = skinningCache.skeletonSelection;
  53. m_RectSelectionTool.rectSelector = m_RectBoneSelector;
  54. m_RectSelectionTool.cacheUndo = skinningCache;
  55. m_RectSelectionTool.onSelectionUpdate += () =>
  56. {
  57. skinningCache.events.boneSelectionChanged.Invoke();
  58. };
  59. m_UnselectTool.cacheUndo = skinningCache;
  60. m_UnselectTool.selection = skinningCache.skeletonSelection;
  61. m_UnselectTool.onUnselect += () =>
  62. {
  63. skinningCache.events.boneSelectionChanged.Invoke();
  64. };
  65. }
  66. public override void Initialize(LayoutOverlay layout)
  67. {
  68. m_SkeletonToolView.Initialize(layout);
  69. }
  70. protected override void OnActivate()
  71. {
  72. SetupSkeleton(skinningCache.GetEffectiveSkeleton(skinningCache.selectedSprite));
  73. UpdateBoneInspector();
  74. skinningCache.events.boneSelectionChanged.AddListener(BoneSelectionChanged);
  75. skinningCache.events.selectedSpriteChanged.AddListener(SelectedSpriteChanged);
  76. skinningCache.events.skinningModeChanged.AddListener(SkinningModeChanged);
  77. skinningCache.events.boneDepthChanged.AddListener(BoneDataChanged);
  78. skinningCache.events.boneNameChanged.AddListener(BoneDataChanged);
  79. skeletonStyle = null;
  80. }
  81. protected override void OnDeactivate()
  82. {
  83. m_SkeletonToolView.Hide();
  84. m_SkeletonController.Reset();
  85. skinningCache.events.boneSelectionChanged.RemoveListener(BoneSelectionChanged);
  86. skinningCache.events.selectedSpriteChanged.RemoveListener(SelectedSpriteChanged);
  87. skinningCache.events.skinningModeChanged.RemoveListener(SkinningModeChanged);
  88. skinningCache.events.boneDepthChanged.RemoveListener(BoneDataChanged);
  89. skinningCache.events.boneNameChanged.RemoveListener(BoneDataChanged);
  90. skeletonStyle = null;
  91. }
  92. void BoneDataChanged(BoneCache bone)
  93. {
  94. if(m_SkeletonToolView.target == bone)
  95. m_SkeletonToolView.Update(bone.name, Mathf.RoundToInt(bone.depth));
  96. }
  97. private void SelectedSpriteChanged(SpriteCache sprite)
  98. {
  99. SetupSkeleton(skinningCache.GetEffectiveSkeleton(sprite));
  100. }
  101. private void BoneSelectionChanged()
  102. {
  103. UpdateBoneInspector();
  104. }
  105. private void UpdateBoneInspector()
  106. {
  107. var selectedBone = skinningCache.skeletonSelection.activeElement;
  108. var selectionCount = skinningCache.skeletonSelection.Count;
  109. m_SkeletonToolView.Hide();
  110. if (enableBoneInspector && selectedBone != null && selectionCount == 1)
  111. {
  112. m_SkeletonToolView.Update(selectedBone.name, Mathf.RoundToInt(selectedBone.depth));
  113. m_SkeletonToolView.Show(selectedBone);
  114. }
  115. }
  116. private void SkinningModeChanged(SkinningMode mode)
  117. {
  118. SetupSkeleton(skinningCache.GetEffectiveSkeleton(skinningCache.selectedSprite));
  119. }
  120. private void SetupSkeleton(SkeletonCache sk)
  121. {
  122. m_RectBoneSelector.bones = null;
  123. skeleton = sk;
  124. if (skeleton != null)
  125. m_RectBoneSelector.bones = skeleton.bones;
  126. }
  127. protected override void OnGUI()
  128. {
  129. m_SkeletonController.view.defaultControlID = 0;
  130. if (skeleton != null && mode != SkeletonMode.Disabled)
  131. {
  132. m_RectSelectionTool.OnGUI();
  133. m_SkeletonController.view.defaultControlID = m_RectSelectionTool.controlID;
  134. }
  135. m_SkeletonController.OnGUI();
  136. m_UnselectTool.OnGUI();
  137. }
  138. private void BoneNameChanged(BoneCache selectedBone, string name)
  139. {
  140. if (selectedBone != null)
  141. {
  142. if (string.Compare(selectedBone.name, name) == 0)
  143. return;
  144. if(string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
  145. m_SkeletonToolView.Update(selectedBone.name, Mathf.RoundToInt(selectedBone.depth));
  146. else
  147. {
  148. using (skinningCache.UndoScope(TextContent.boneName))
  149. {
  150. selectedBone.name = name;
  151. skinningCache.events.boneNameChanged.Invoke(selectedBone);
  152. }
  153. }
  154. }
  155. }
  156. private void BoneDepthChanged(BoneCache selectedBone, int depth)
  157. {
  158. if (selectedBone != null)
  159. {
  160. if (Mathf.RoundToInt(selectedBone.depth) == depth)
  161. return;
  162. using (skinningCache.UndoScope(TextContent.boneDepth))
  163. {
  164. selectedBone.depth = depth;
  165. skinningCache.events.boneDepthChanged.Invoke(selectedBone);
  166. }
  167. }
  168. }
  169. }
  170. }