SpriteEditorUtility.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEvent = UnityEngine.Event;
  4. namespace UnityEditor.U2D.Sprites
  5. {
  6. static internal class SpriteEditorUtility
  7. {
  8. public static Vector2 GetPivotValue(SpriteAlignment alignment, Vector2 customOffset)
  9. {
  10. switch (alignment)
  11. {
  12. case SpriteAlignment.BottomLeft:
  13. return new Vector2(0f, 0f);
  14. case SpriteAlignment.BottomCenter:
  15. return new Vector2(0.5f, 0f);
  16. case SpriteAlignment.BottomRight:
  17. return new Vector2(1f, 0f);
  18. case SpriteAlignment.LeftCenter:
  19. return new Vector2(0f, 0.5f);
  20. case SpriteAlignment.Center:
  21. return new Vector2(0.5f, 0.5f);
  22. case SpriteAlignment.RightCenter:
  23. return new Vector2(1f, 0.5f);
  24. case SpriteAlignment.TopLeft:
  25. return new Vector2(0f, 1f);
  26. case SpriteAlignment.TopCenter:
  27. return new Vector2(0.5f, 1f);
  28. case SpriteAlignment.TopRight:
  29. return new Vector2(1f, 1f);
  30. case SpriteAlignment.Custom:
  31. return customOffset;
  32. }
  33. return Vector2.zero;
  34. }
  35. public static Rect RoundedRect(Rect rect)
  36. {
  37. return new Rect(
  38. Mathf.RoundToInt(rect.xMin),
  39. Mathf.RoundToInt(rect.yMin),
  40. Mathf.RoundToInt(rect.width),
  41. Mathf.RoundToInt(rect.height)
  42. );
  43. }
  44. public static Rect RoundToInt(Rect r)
  45. {
  46. r.xMin = Mathf.RoundToInt(r.xMin);
  47. r.yMin = Mathf.RoundToInt(r.yMin);
  48. r.xMax = Mathf.RoundToInt(r.xMax);
  49. r.yMax = Mathf.RoundToInt(r.yMax);
  50. return r;
  51. }
  52. public static Rect ClampedRect(Rect rect, Rect clamp, bool maintainSize)
  53. {
  54. Rect r = new Rect(rect);
  55. if (maintainSize)
  56. {
  57. Vector2 center = rect.center;
  58. if (center.x + Mathf.Abs(rect.width) * .5f > clamp.xMax)
  59. center.x = clamp.xMax - rect.width * .5f;
  60. if (center.x - Mathf.Abs(rect.width) * .5f < clamp.xMin)
  61. center.x = clamp.xMin + rect.width * .5f;
  62. if (center.y + Mathf.Abs(rect.height) * .5f > clamp.yMax)
  63. center.y = clamp.yMax - rect.height * .5f;
  64. if (center.y - Mathf.Abs(rect.height) * .5f < clamp.yMin)
  65. center.y = clamp.yMin + rect.height * .5f;
  66. r.center = center;
  67. }
  68. else
  69. {
  70. if (r.width > 0f)
  71. {
  72. r.xMin = Mathf.Max(rect.xMin, clamp.xMin);
  73. r.xMax = Mathf.Min(rect.xMax, clamp.xMax);
  74. }
  75. else
  76. {
  77. r.xMin = Mathf.Min(rect.xMin, clamp.xMax);
  78. r.xMax = Mathf.Max(rect.xMax, clamp.xMin);
  79. }
  80. if (r.height > 0f)
  81. {
  82. r.yMin = Mathf.Max(rect.yMin, clamp.yMin);
  83. r.yMax = Mathf.Min(rect.yMax, clamp.yMax);
  84. }
  85. else
  86. {
  87. r.yMin = Mathf.Min(rect.yMin, clamp.yMax);
  88. r.yMax = Mathf.Max(rect.yMax, clamp.yMin);
  89. }
  90. }
  91. r.width = Mathf.Abs(r.width);
  92. r.height = Mathf.Abs(r.height);
  93. return r;
  94. }
  95. public static void DrawBox(Rect position)
  96. {
  97. Vector3[] points = new Vector3[5];
  98. int i = 0;
  99. points[i++] = new Vector3(position.xMin, position.yMin, 0f);
  100. points[i++] = new Vector3(position.xMax, position.yMin, 0f);
  101. points[i++] = new Vector3(position.xMax, position.yMax, 0f);
  102. points[i++] = new Vector3(position.xMin, position.yMax, 0f);
  103. DrawLine(points[0], points[1]);
  104. DrawLine(points[1], points[2]);
  105. DrawLine(points[2], points[3]);
  106. DrawLine(points[3], points[0]);
  107. }
  108. public static void DrawLine(Vector3 p1, Vector3 p2)
  109. {
  110. Assert.IsTrue(UnityEvent.current.type == EventType.Repaint);
  111. GL.Vertex(p1);
  112. GL.Vertex(p2);
  113. }
  114. public static void BeginLines(Color color)
  115. {
  116. Assert.IsTrue(UnityEvent.current.type == EventType.Repaint);
  117. HandleUtility.ApplyWireMaterial();
  118. GL.PushMatrix();
  119. GL.MultMatrix(Handles.matrix);
  120. GL.Begin(GL.LINES);
  121. GL.Color(color);
  122. }
  123. public static void EndLines()
  124. {
  125. Assert.IsTrue(UnityEvent.current.type == EventType.Repaint);
  126. GL.End();
  127. GL.PopMatrix();
  128. }
  129. public static void FourIntFields(Vector2 rectSize, GUIContent label, GUIContent labelX, GUIContent labelY, GUIContent labelZ, GUIContent labelW, ref int x, ref int y, ref int z, ref int w)
  130. {
  131. Rect rect = GUILayoutUtility.GetRect(rectSize.x, rectSize.y);
  132. Rect labelRect = rect;
  133. labelRect.width = EditorGUIUtility.labelWidth;
  134. labelRect.height = EditorGUI.kSingleLineHeight;
  135. GUI.Label(labelRect, label);
  136. Rect fieldRect = rect;
  137. fieldRect.width -= EditorGUIUtility.labelWidth;
  138. fieldRect.height = EditorGUI.kSingleLineHeight;
  139. fieldRect.x += EditorGUIUtility.labelWidth;
  140. fieldRect.width /= 2;
  141. fieldRect.width -= EditorGUI.kSpacingSubLabel;
  142. float oldLabelWidth = EditorGUIUtility.labelWidth;
  143. EditorGUIUtility.labelWidth = EditorGUI.kMiniLabelW;
  144. GUI.SetNextControlName("FourIntFields_x");
  145. x = EditorGUI.IntField(fieldRect, labelX, x);
  146. fieldRect.x += fieldRect.width + EditorGUI.kSpacing;
  147. GUI.SetNextControlName("FourIntFields_y");
  148. y = EditorGUI.IntField(fieldRect, labelY, y);
  149. fieldRect.y += EditorGUI.kSingleLineHeight + EditorGUI.kVerticalSpacingMultiField;
  150. fieldRect.x -= fieldRect.width + EditorGUI.kSpacing;
  151. GUI.SetNextControlName("FourIntFields_z");
  152. z = EditorGUI.IntField(fieldRect, labelZ, z);
  153. fieldRect.x += fieldRect.width + EditorGUI.kSpacing;
  154. GUI.SetNextControlName("FourIntFields_w");
  155. w = EditorGUI.IntField(fieldRect, labelW, w);
  156. EditorGUIUtility.labelWidth = oldLabelWidth;
  157. }
  158. }
  159. }