暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteFrameModuleView.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using UnityEngine;
  2. namespace UnityEditor.U2D.Sprites
  3. {
  4. internal partial class SpriteFrameModule : SpriteFrameModuleBase
  5. {
  6. private static class SpriteFrameModuleStyles
  7. {
  8. public static readonly GUIContent sliceButtonLabel = EditorGUIUtility.TrTextContent("Slice");
  9. public static readonly GUIContent trimButtonLabel = EditorGUIUtility.TrTextContent("Trim", "Trims selected rectangle (T)");
  10. }
  11. // overrides for SpriteFrameModuleBase
  12. public override void DoMainGUI()
  13. {
  14. base.DoMainGUI();
  15. DrawSpriteRectGizmos();
  16. DrawPotentialSpriteRectGizmos();
  17. if (!spriteEditor.editingDisabled)
  18. {
  19. HandleGizmoMode();
  20. if (containsMultipleSprites)
  21. HandleRectCornerScalingHandles();
  22. HandleBorderCornerScalingHandles();
  23. HandleBorderSidePointScalingSliders();
  24. if (containsMultipleSprites)
  25. HandleRectSideScalingHandles();
  26. HandleBorderSideScalingHandles();
  27. HandlePivotHandle();
  28. if (containsMultipleSprites)
  29. HandleDragging();
  30. spriteEditor.HandleSpriteSelection();
  31. if (containsMultipleSprites)
  32. {
  33. HandleCreate();
  34. HandleDelete();
  35. HandleDuplicate();
  36. }
  37. spriteEditor.spriteRects = m_RectsCache.GetSpriteRects();
  38. }
  39. }
  40. private void DrawPotentialSpriteRectGizmos()
  41. {
  42. if (m_PotentialRects != null && m_PotentialRects.Count > 0)
  43. DrawRectGizmos(m_PotentialRects, Color.red);
  44. }
  45. public override void DoToolbarGUI(Rect toolbarRect)
  46. {
  47. using (new EditorGUI.DisabledScope(!containsMultipleSprites || spriteEditor.editingDisabled || m_TextureDataProvider.GetReadableTexture2D() == null))
  48. {
  49. GUIStyle skin = EditorStyles.toolbarPopup;
  50. Rect drawArea = toolbarRect;
  51. drawArea.width = skin.CalcSize(SpriteFrameModuleStyles.sliceButtonLabel).x;
  52. SpriteUtilityWindow.DrawToolBarWidget(ref drawArea, ref toolbarRect, (adjustedDrawArea) =>
  53. {
  54. if (GUI.Button(adjustedDrawArea, SpriteFrameModuleStyles.sliceButtonLabel, skin))
  55. {
  56. if (SpriteEditorMenu.ShowAtPosition(adjustedDrawArea, this, m_TextureDataProvider))
  57. GUIUtility.ExitGUI();
  58. }
  59. });
  60. using (new EditorGUI.DisabledScope(!hasSelected))
  61. {
  62. drawArea.x += drawArea.width;
  63. drawArea.width = skin.CalcSize(SpriteFrameModuleStyles.trimButtonLabel).x;
  64. SpriteUtilityWindow.DrawToolBarWidget(ref drawArea, ref toolbarRect, (adjustedDrawArea) =>
  65. {
  66. if (GUI.Button(adjustedDrawArea, SpriteFrameModuleStyles.trimButtonLabel, EditorStyles.toolbarButton))
  67. {
  68. TrimAlpha();
  69. Repaint();
  70. }
  71. });
  72. }
  73. }
  74. }
  75. private void HandleRectCornerScalingHandles()
  76. {
  77. if (!hasSelected)
  78. return;
  79. GUIStyle dragDot = styles.dragdot;
  80. GUIStyle dragDotActive = styles.dragdotactive;
  81. var color = Color.white;
  82. Rect rect = new Rect(selectedSpriteRect);
  83. float left = rect.xMin;
  84. float right = rect.xMax;
  85. float top = rect.yMax;
  86. float bottom = rect.yMin;
  87. EditorGUI.BeginChangeCheck();
  88. HandleBorderPointSlider(ref left, ref top, MouseCursor.ResizeUpLeft, false, dragDot, dragDotActive, color);
  89. HandleBorderPointSlider(ref right, ref top, MouseCursor.ResizeUpRight, false, dragDot, dragDotActive, color);
  90. HandleBorderPointSlider(ref left, ref bottom, MouseCursor.ResizeUpRight, false, dragDot, dragDotActive, color);
  91. HandleBorderPointSlider(ref right, ref bottom, MouseCursor.ResizeUpLeft, false, dragDot, dragDotActive, color);
  92. if (EditorGUI.EndChangeCheck())
  93. {
  94. rect.xMin = left;
  95. rect.xMax = right;
  96. rect.yMax = top;
  97. rect.yMin = bottom;
  98. ScaleSpriteRect(rect);
  99. PopulateSpriteFrameInspectorField();
  100. }
  101. }
  102. private void HandleRectSideScalingHandles()
  103. {
  104. if (!hasSelected)
  105. return;
  106. Rect rect = new Rect(selectedSpriteRect);
  107. float left = rect.xMin;
  108. float right = rect.xMax;
  109. float top = rect.yMax;
  110. float bottom = rect.yMin;
  111. Vector2 screenRectTopLeft = Handles.matrix.MultiplyPoint(new Vector3(rect.xMin, rect.yMin));
  112. Vector2 screenRectBottomRight = Handles.matrix.MultiplyPoint(new Vector3(rect.xMax, rect.yMax));
  113. float screenRectWidth = Mathf.Abs(screenRectBottomRight.x - screenRectTopLeft.x);
  114. float screenRectHeight = Mathf.Abs(screenRectBottomRight.y - screenRectTopLeft.y);
  115. EditorGUI.BeginChangeCheck();
  116. left = HandleBorderScaleSlider(left, rect.yMax, screenRectWidth, screenRectHeight, true);
  117. right = HandleBorderScaleSlider(right, rect.yMax, screenRectWidth, screenRectHeight, true);
  118. top = HandleBorderScaleSlider(rect.xMin, top, screenRectWidth, screenRectHeight, false);
  119. bottom = HandleBorderScaleSlider(rect.xMin, bottom, screenRectWidth, screenRectHeight, false);
  120. if (EditorGUI.EndChangeCheck())
  121. {
  122. rect.xMin = left;
  123. rect.xMax = right;
  124. rect.yMax = top;
  125. rect.yMin = bottom;
  126. ScaleSpriteRect(rect);
  127. PopulateSpriteFrameInspectorField();
  128. }
  129. }
  130. private void HandleDragging()
  131. {
  132. if (hasSelected && !MouseOnTopOfInspector())
  133. {
  134. Rect textureBounds = new Rect(0, 0, textureActualWidth, textureActualHeight);
  135. EditorGUI.BeginChangeCheck();
  136. Rect oldRect = selectedSpriteRect;
  137. Rect newRect = SpriteEditorUtility.ClampedRect(SpriteEditorUtility.RoundedRect(SpriteEditorHandles.SliderRect(oldRect)), textureBounds, true);
  138. if (EditorGUI.EndChangeCheck())
  139. {
  140. selectedSpriteRect = newRect;
  141. UpdatePositionField(null);
  142. }
  143. }
  144. }
  145. private void HandleCreate()
  146. {
  147. if (!MouseOnTopOfInspector() && !eventSystem.current.alt)
  148. {
  149. // Create new rects via dragging in empty space
  150. EditorGUI.BeginChangeCheck();
  151. Rect newRect = SpriteEditorHandles.RectCreator(textureActualWidth, textureActualHeight, styles.createRect);
  152. if (EditorGUI.EndChangeCheck() && newRect.width > 0f && newRect.height > 0f)
  153. {
  154. CreateSprite(newRect);
  155. GUIUtility.keyboardControl = 0;
  156. }
  157. }
  158. }
  159. private void HandleDuplicate()
  160. {
  161. IEvent evt = eventSystem.current;
  162. if ((evt.type == EventType.ValidateCommand || evt.type == EventType.ExecuteCommand)
  163. && evt.commandName == EventCommandNames.Duplicate)
  164. {
  165. if (evt.type == EventType.ExecuteCommand)
  166. DuplicateSprite();
  167. evt.Use();
  168. }
  169. }
  170. private void HandleDelete()
  171. {
  172. IEvent evt = eventSystem.current;
  173. if ((evt.type == EventType.ValidateCommand || evt.type == EventType.ExecuteCommand)
  174. && (evt.commandName == EventCommandNames.SoftDelete || evt.commandName == EventCommandNames.Delete))
  175. {
  176. if (evt.type == EventType.ExecuteCommand && hasSelected)
  177. DeleteSprite();
  178. evt.Use();
  179. }
  180. }
  181. }
  182. }