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