SpriteBoneInfluenceTool.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.U2D.Layout;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. internal class SpriteBoneInflueceToolController
  8. {
  9. SkinningEvents m_Events;
  10. ISpriteBoneInfluenceToolModel m_Model;
  11. public SpriteBoneInflueceToolController(ISpriteBoneInfluenceToolModel model, SkinningEvents events)
  12. {
  13. m_Events = events;
  14. m_Model = model;
  15. }
  16. public void Activate()
  17. {
  18. m_Events.selectedSpriteChanged.AddListener(OnSpriteSelectionChanged);
  19. m_Events.boneSelectionChanged.AddListener(OnBoneSelectionChanged);
  20. m_Events.boneNameChanged.AddListener(OnBoneNameChanged);
  21. m_Events.skeletonTopologyChanged.AddListener(OnSkeletonTopologyChanged);
  22. m_Events.meshChanged.AddListener(OnMeshChanged);
  23. ShowHideView(true);
  24. OnBoneSelectionChanged();
  25. }
  26. public void Deactivate()
  27. {
  28. m_Events.selectedSpriteChanged.RemoveListener(OnSpriteSelectionChanged);
  29. m_Events.boneSelectionChanged.RemoveListener(OnBoneSelectionChanged);
  30. m_Events.boneNameChanged.RemoveListener(OnBoneNameChanged);
  31. m_Events.skeletonTopologyChanged.RemoveListener(OnSkeletonTopologyChanged);
  32. m_Events.meshChanged.RemoveListener(OnMeshChanged);
  33. ShowHideView(false);
  34. }
  35. private void OnMeshChanged(MeshCache mesh)
  36. {
  37. if (m_Model.view.visible)
  38. m_Model.view.OnMeshChanged();
  39. }
  40. private void OnSpriteSelectionChanged(SpriteCache sprite)
  41. {
  42. if (m_Model.view.visible)
  43. {
  44. m_Model.view.OnSpriteSelectionChanged();
  45. SetViewHeaderText();
  46. }
  47. }
  48. private void OnBoneSelectionChanged()
  49. {
  50. if (m_Model.view.visible)
  51. m_Model.view.OnBoneSelectionChanged();
  52. }
  53. private void OnBoneNameChanged(BoneCache bone)
  54. {
  55. if (m_Model.view.visible)
  56. {
  57. m_Model.view.OnSkeletonChanged();
  58. }
  59. }
  60. private void OnSkeletonTopologyChanged(SkeletonCache skeleton)
  61. {
  62. if (m_Model.view.visible)
  63. m_Model.view.OnSkeletonChanged();
  64. }
  65. public void OnViewCreated()
  66. {
  67. m_Model.view.onAddBone += AddSelectedBoneInfluencetoSprite;
  68. m_Model.view.onRemoveBone += RemoveSelectedBoneInfluenceFromSprite;
  69. m_Model.view.onReordered += OnReorderBoneInfluenceFromSprite;
  70. m_Model.view.onSelectionChanged += SelectBones;
  71. m_Model.view.SetController(this);
  72. ShowHideView(false);
  73. }
  74. private void AddSelectedBoneInfluencetoSprite()
  75. {
  76. var character = m_Model.characterSkeleton;
  77. if (character == null)
  78. return;
  79. var characterPart = m_Model.GetSpriteCharacterPart(m_Model.selectedSprite);
  80. var selectedBones = m_Model.selectedBones;
  81. var characterBones = characterPart.bones.ToList();
  82. foreach (var bone in selectedBones)
  83. {
  84. if (!characterBones.Contains(bone))
  85. characterBones.Add(bone);
  86. }
  87. using (m_Model.UndoScope(TextContent.addBoneInfluence))
  88. {
  89. characterPart.bones = characterBones.ToArray();
  90. m_Events.characterPartChanged.Invoke(characterPart);
  91. m_Model.view.OnSkeletonChanged();
  92. m_Model.view.OnBoneSelectionChanged();
  93. }
  94. }
  95. private void RemoveSelectedBoneInfluenceFromSprite()
  96. {
  97. var character = m_Model.characterSkeleton;
  98. if (character == null)
  99. return;
  100. var characterPart = m_Model.GetSpriteCharacterPart(m_Model.selectedSprite);
  101. var selectedBones = m_Model.selectedBones;
  102. var characterBones = characterPart.bones.ToList();
  103. characterBones.RemoveAll(b => selectedBones.Contains(b));
  104. using (m_Model.UndoScope(TextContent.removeBoneInfluence))
  105. {
  106. characterPart.bones = characterBones.ToArray();
  107. m_Events.characterPartChanged.Invoke(characterPart);
  108. characterPart.sprite.SmoothFill();
  109. m_Events.meshChanged.Invoke(characterPart.sprite.GetMesh());
  110. m_Model.view.OnSkeletonChanged();
  111. m_Model.view.OnBoneSelectionChanged();
  112. }
  113. }
  114. private void OnReorderBoneInfluenceFromSprite(IEnumerable<BoneCache> boneCache)
  115. {
  116. var character = m_Model.characterSkeleton;
  117. if (character != null)
  118. {
  119. var characterPart = m_Model.GetSpriteCharacterPart(m_Model.selectedSprite);
  120. using (m_Model.UndoScope(TextContent.reorderBoneInfluence))
  121. {
  122. characterPart.bones = boneCache.ToArray();
  123. m_Events.characterPartChanged.Invoke(characterPart);
  124. m_Model.view.OnSkeletonChanged();
  125. }
  126. }
  127. else
  128. {
  129. using (m_Model.UndoScope(TextContent.reorderBoneInfluence))
  130. {
  131. m_Model.selectedSprite.GetSkeleton().ReorderBones(boneCache);
  132. m_Events.skeletonTopologyChanged.Invoke(m_Model.selectedSprite.GetSkeleton());
  133. }
  134. }
  135. }
  136. private void SelectBones(IEnumerable<BoneCache> selectedBones)
  137. {
  138. using (m_Model.UndoScope(TextContent.boneSelection))
  139. {
  140. m_Model.selectedBones = selectedBones;
  141. m_Events.boneSelectionChanged.Invoke();
  142. }
  143. }
  144. private void ShowHideView(bool show)
  145. {
  146. m_Model.view.SetHiddenFromLayout(!show);
  147. if (show)
  148. {
  149. m_Model.view.OnSpriteSelectionChanged();
  150. SetViewHeaderText();
  151. }
  152. }
  153. private void SetViewHeaderText()
  154. {
  155. var headerText = m_Model.selectedSprite != null ? m_Model.selectedSprite.name : TextContent.noSpriteSelected;
  156. m_Model.view.headerText = headerText;
  157. }
  158. public BoneCache[] GetSelectedSpriteBoneInfluence()
  159. {
  160. var selectedSprite = m_Model.selectedSprite;
  161. if (selectedSprite != null)
  162. {
  163. if (m_Model.hasCharacter)
  164. {
  165. var characterPart = m_Model.GetSpriteCharacterPart(selectedSprite);
  166. return characterPart.bones.ToArray();
  167. }
  168. else
  169. {
  170. return selectedSprite.GetSkeleton().bones;
  171. }
  172. }
  173. return new BoneCache[0];
  174. }
  175. public int[] GetSelectedBoneForList(IEnumerable<BoneCache> bones)
  176. {
  177. var selectedBones = m_Model.selectedBones;
  178. var spriteBones = GetSelectedSpriteBoneInfluence();
  179. var result = new List<int>();
  180. foreach (var bone in selectedBones)
  181. {
  182. var index = Array.IndexOf(spriteBones, bone);
  183. if (index >= 0)
  184. result.Add(index);
  185. }
  186. return result.ToArray();
  187. }
  188. public bool ShouldEnableAddButton(IEnumerable<BoneCache> bones)
  189. {
  190. if (InCharacterMode())
  191. {
  192. var hasSelectedSprite = m_Model.selectedSprite != null;
  193. var selectedBones = m_Model.selectedBones;
  194. return hasSelectedSprite && selectedBones.FirstOrDefault(x => !bones.Contains(x)) != null;
  195. }
  196. return false;
  197. }
  198. public bool InCharacterMode()
  199. {
  200. return m_Model.hasCharacter && m_Model.skinningMode == SkinningMode.Character;
  201. }
  202. }
  203. internal interface ISpriteBoneInfluenceToolModel
  204. {
  205. ISpriteBoneInfluenceWindow view { get; }
  206. IEnumerable<BoneCache> selectedBones { get; set; }
  207. SpriteCache selectedSprite { get; }
  208. bool hasCharacter { get; }
  209. SkinningMode skinningMode { get; }
  210. SkeletonCache characterSkeleton { get; }
  211. UndoScope UndoScope(string description);
  212. CharacterPartCache GetSpriteCharacterPart(SpriteCache sprite);
  213. }
  214. internal class SpriteBoneInfluenceTool : BaseTool, ISpriteBoneInfluenceToolModel
  215. {
  216. SpriteBoneInflueceToolController m_Controller;
  217. private MeshPreviewBehaviour m_MeshPreviewBehaviour = new MeshPreviewBehaviour();
  218. private SpriteBoneInfluenceWindow m_View;
  219. public SkeletonTool skeletonTool { set; private get; }
  220. public override IMeshPreviewBehaviour previewBehaviour
  221. {
  222. get { return m_MeshPreviewBehaviour; }
  223. }
  224. internal override void OnCreate()
  225. {
  226. m_Controller = new SpriteBoneInflueceToolController(this, skinningCache.events);
  227. }
  228. ISpriteBoneInfluenceWindow ISpriteBoneInfluenceToolModel.view {get { return m_View; } }
  229. IEnumerable<BoneCache> ISpriteBoneInfluenceToolModel.selectedBones
  230. {
  231. get { return skinningCache.skeletonSelection.elements; }
  232. set { skinningCache.skeletonSelection.elements = value.ToArray(); }
  233. }
  234. SpriteCache ISpriteBoneInfluenceToolModel.selectedSprite { get { return skinningCache.selectedSprite; } }
  235. bool ISpriteBoneInfluenceToolModel.hasCharacter { get { return skinningCache.hasCharacter; } }
  236. SkinningMode ISpriteBoneInfluenceToolModel.skinningMode { get { return skinningCache.mode; } }
  237. SkeletonCache ISpriteBoneInfluenceToolModel.characterSkeleton { get { return skinningCache.character != null ? skinningCache.character.skeleton : null; } }
  238. UndoScope ISpriteBoneInfluenceToolModel.UndoScope(string description)
  239. {
  240. return skinningCache.UndoScope(description);
  241. }
  242. protected override void OnActivate()
  243. {
  244. m_Controller.Activate();
  245. if (skeletonTool != null)
  246. skeletonTool.Activate();
  247. }
  248. protected override void OnDeactivate()
  249. {
  250. m_Controller.Deactivate();
  251. if (skeletonTool != null)
  252. skeletonTool.Deactivate();
  253. }
  254. public override void Initialize(LayoutOverlay layout)
  255. {
  256. if (m_View == null)
  257. {
  258. m_View = SpriteBoneInfluenceWindow.CreateFromUXML();
  259. m_Controller.OnViewCreated();
  260. }
  261. layout.rightOverlay.Add(m_View);
  262. }
  263. protected override void OnGUI()
  264. {
  265. m_MeshPreviewBehaviour.showWeightMap = true;
  266. m_MeshPreviewBehaviour.overlaySelected = true;
  267. m_MeshPreviewBehaviour.drawWireframe = true;
  268. skeletonTool.skeletonStyle = SkeletonStyles.WeightMap;
  269. skeletonTool.mode = SkeletonMode.EditPose;
  270. skeletonTool.editBindPose = false;
  271. skeletonTool.DoGUI();
  272. }
  273. public CharacterPartCache GetSpriteCharacterPart(SpriteCache sprite)
  274. {
  275. return sprite.GetCharacterPart();
  276. }
  277. }
  278. }