SpriteFrameModuleView.cs 7.8 KB

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