MeshTool.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal partial class MeshTool : BaseTool
  6. {
  7. private MeshCache m_Mesh;
  8. private ISelection<int> m_SelectionOverride;
  9. private SpriteMeshController m_SpriteMeshController;
  10. private SpriteMeshView m_SpriteMeshView;
  11. private RectSelectionTool<int> m_RectSelectionTool = new RectSelectionTool<int>();
  12. private RectVertexSelector m_RectVertexSelector = new RectVertexSelector();
  13. private UnselectTool<int> m_UnselectTool = new UnselectTool<int>();
  14. private ITriangulator m_Triangulator;
  15. public MeshCache mesh
  16. {
  17. get { return m_Mesh; }
  18. }
  19. public SpriteMeshViewMode mode
  20. {
  21. get { return m_SpriteMeshView.mode; }
  22. set { m_SpriteMeshView.mode = value; }
  23. }
  24. public bool disable
  25. {
  26. get { return m_SpriteMeshController.disable; }
  27. set { m_SpriteMeshController.disable = value; }
  28. }
  29. public ISelection<int> selectionOverride
  30. {
  31. get { return m_SelectionOverride; }
  32. set { m_SelectionOverride = value; }
  33. }
  34. public override int defaultControlID
  35. {
  36. get
  37. {
  38. if (m_Mesh == null)
  39. return 0;
  40. return m_RectSelectionTool.controlID;
  41. }
  42. }
  43. private ISelection<int> selection
  44. {
  45. get
  46. {
  47. if(selectionOverride != null)
  48. return selectionOverride;
  49. return skinningCache.vertexSelection;
  50. }
  51. }
  52. internal override void OnCreate()
  53. {
  54. m_SpriteMeshController = new SpriteMeshController();
  55. m_SpriteMeshView = new SpriteMeshView(new GUIWrapper());
  56. m_Triangulator = new Triangulator();
  57. }
  58. protected override void OnActivate()
  59. {
  60. m_SpriteMeshController.disable = false;
  61. m_SelectionOverride = null;
  62. SetupSprite(skinningCache.selectedSprite);
  63. skinningCache.events.selectedSpriteChanged.AddListener(OnSelectedSpriteChanged);
  64. }
  65. protected override void OnDeactivate()
  66. {
  67. skinningCache.events.selectedSpriteChanged.RemoveListener(OnSelectedSpriteChanged);
  68. }
  69. private void OnSelectedSpriteChanged(SpriteCache sprite)
  70. {
  71. SetupSprite(sprite);
  72. }
  73. internal void SetupSprite(SpriteCache sprite)
  74. {
  75. var mesh = sprite.GetMesh();
  76. if (m_Mesh != null
  77. && m_Mesh != mesh
  78. && selection.Count > 0)
  79. selection.Clear();
  80. m_Mesh = mesh;
  81. m_SpriteMeshController.spriteMeshData = m_Mesh;
  82. }
  83. private void SetupGUI()
  84. {
  85. m_SpriteMeshController.spriteMeshView = m_SpriteMeshView;
  86. m_SpriteMeshController.triangulator = m_Triangulator;
  87. m_SpriteMeshController.cacheUndo = skinningCache;
  88. m_RectSelectionTool.cacheUndo = skinningCache;
  89. m_RectSelectionTool.rectSelector = m_RectVertexSelector;
  90. m_RectVertexSelector.selection = selection;
  91. m_UnselectTool.cacheUndo = skinningCache;
  92. m_UnselectTool.selection = selection;
  93. m_SpriteMeshController.frame = new Rect(Vector2.zero, m_Mesh.sprite.textureRect.size);
  94. m_SpriteMeshController.selection = selection;
  95. m_SpriteMeshView.defaultControlID = defaultControlID;
  96. m_RectVertexSelector.spriteMeshData = m_Mesh;
  97. }
  98. protected override void OnGUI()
  99. {
  100. if (m_Mesh == null)
  101. return;
  102. SetupGUI();
  103. var handlesMatrix = Handles.matrix;
  104. Handles.matrix *= m_Mesh.sprite.GetLocalToWorldMatrixFromMode();
  105. BeginPositionOverride();
  106. EditorGUI.BeginChangeCheck();
  107. var guiEnabled = GUI.enabled;
  108. var moveAction = m_SpriteMeshController.spriteMeshView.IsActionHot(MeshEditorAction.MoveEdge) || m_SpriteMeshController.spriteMeshView.IsActionHot(MeshEditorAction.MoveVertex);
  109. GUI.enabled = (!skinningCache.IsOnVisualElement() && guiEnabled) || moveAction;
  110. m_SpriteMeshController.OnGUI();
  111. GUI.enabled = guiEnabled;
  112. if (EditorGUI.EndChangeCheck())
  113. UpdateMesh();
  114. m_RectSelectionTool.OnGUI();
  115. m_UnselectTool.OnGUI();
  116. Handles.matrix = handlesMatrix;
  117. EndPositionOverride();
  118. }
  119. public void BeginPositionOverride()
  120. {
  121. if(m_Mesh != null)
  122. {
  123. m_Mesh.vertexPositionOverride = null;
  124. var skeleton = skinningCache.GetEffectiveSkeleton(m_Mesh.sprite);
  125. Debug.Assert(skeleton != null);
  126. if (skeleton.isPosePreview)
  127. m_Mesh.vertexPositionOverride = m_Mesh.sprite.GetMeshPreview().vertices;
  128. }
  129. }
  130. public void EndPositionOverride()
  131. {
  132. if(m_Mesh != null)
  133. m_Mesh.vertexPositionOverride = null;
  134. }
  135. public void UpdateWeights()
  136. {
  137. InvokeMeshChanged();
  138. }
  139. public void UpdateMesh()
  140. {
  141. InvokeMeshChanged();
  142. }
  143. private void InvokeMeshChanged()
  144. {
  145. if(m_Mesh != null)
  146. skinningCache.events.meshChanged.Invoke(m_Mesh);
  147. }
  148. }
  149. }