-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUISprite.cs
352 lines (330 loc) · 8.06 KB
/
UISprite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using UnityEngine;
[AddComponentMenu("NGUI/UI/NGUI Sprite")]
[ExecuteInEditMode]
public class UISprite : UIBasicSprite
{
[HideInInspector]
[SerializeField]
private UIAtlas mAtlas;
[HideInInspector]
[SerializeField]
private string mSpriteName;
[HideInInspector]
[SerializeField]
private bool mFillCenter = true;
[NonSerialized]
protected UISpriteData mSprite;
[NonSerialized]
private bool mSpriteSet;
public override Material material => (!(mAtlas != null)) ? null : mAtlas.spriteMaterial;
public UIAtlas atlas
{
get
{
return mAtlas;
}
set
{
if (mAtlas != value)
{
RemoveFromPanel();
mAtlas = value;
mSpriteSet = false;
mSprite = null;
if (string.IsNullOrEmpty(mSpriteName) && mAtlas != null && mAtlas.spriteList.Count > 0)
{
SetAtlasSprite(mAtlas.spriteList[0]);
mSpriteName = mSprite.name;
}
if (!string.IsNullOrEmpty(mSpriteName))
{
string spriteName = mSpriteName;
mSpriteName = string.Empty;
this.spriteName = spriteName;
MarkAsChanged();
}
}
}
}
public string spriteName
{
get
{
return mSpriteName;
}
set
{
if (string.IsNullOrEmpty(value))
{
if (!string.IsNullOrEmpty(mSpriteName))
{
mSpriteName = string.Empty;
mSprite = null;
mChanged = true;
mSpriteSet = false;
}
}
else if (mSpriteName != value)
{
mSpriteName = value;
mSprite = null;
mChanged = true;
mSpriteSet = false;
}
}
}
public bool isValid => GetAtlasSprite() != null;
[Obsolete("Use 'centerType' instead")]
public bool fillCenter
{
get
{
return centerType != AdvancedType.Invisible;
}
set
{
if (value != (centerType != AdvancedType.Invisible))
{
centerType = (value ? AdvancedType.Sliced : AdvancedType.Invisible);
MarkAsChanged();
}
}
}
public override Vector4 border
{
get
{
UISpriteData atlasSprite = GetAtlasSprite();
if (atlasSprite == null)
{
return base.border;
}
return new Vector4((float)atlasSprite.borderLeft, (float)atlasSprite.borderBottom, (float)atlasSprite.borderRight, (float)atlasSprite.borderTop);
}
}
public override float pixelSize => (!(mAtlas != null)) ? 1f : mAtlas.pixelSize;
public override int minWidth
{
get
{
if (type == Type.Sliced || type == Type.Advanced)
{
Vector4 vector = border * pixelSize;
int num = Mathf.RoundToInt(vector.x + vector.z);
UISpriteData atlasSprite = GetAtlasSprite();
if (atlasSprite != null)
{
num += atlasSprite.paddingLeft + atlasSprite.paddingRight;
}
return Mathf.Max(base.minWidth, ((num & 1) != 1) ? num : (num + 1));
}
return base.minWidth;
}
}
public override int minHeight
{
get
{
if (type == Type.Sliced || type == Type.Advanced)
{
Vector4 vector = border * pixelSize;
int num = Mathf.RoundToInt(vector.y + vector.w);
UISpriteData atlasSprite = GetAtlasSprite();
if (atlasSprite != null)
{
num += atlasSprite.paddingTop + atlasSprite.paddingBottom;
}
return Mathf.Max(base.minHeight, ((num & 1) != 1) ? num : (num + 1));
}
return base.minHeight;
}
}
public override Vector4 drawingDimensions
{
get
{
Vector2 pivotOffset = base.pivotOffset;
float num = (0f - pivotOffset.x) * (float)mWidth;
float num2 = (0f - pivotOffset.y) * (float)mHeight;
float num3 = num + (float)mWidth;
float num4 = num2 + (float)mHeight;
if (GetAtlasSprite() != null && mType != Type.Tiled)
{
int paddingLeft = mSprite.paddingLeft;
int paddingBottom = mSprite.paddingBottom;
int num5 = mSprite.paddingRight;
int num6 = mSprite.paddingTop;
int num7 = mSprite.width + paddingLeft + num5;
int num8 = mSprite.height + paddingBottom + num6;
float num9 = 1f;
float num10 = 1f;
if (num7 > 0 && num8 > 0 && (mType == Type.Simple || mType == Type.Filled))
{
if ((num7 & 1) != 0)
{
num5++;
}
if ((num8 & 1) != 0)
{
num6++;
}
num9 = 1f / (float)num7 * (float)mWidth;
num10 = 1f / (float)num8 * (float)mHeight;
}
if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
{
num += (float)num5 * num9;
num3 -= (float)paddingLeft * num9;
}
else
{
num += (float)paddingLeft * num9;
num3 -= (float)num5 * num9;
}
if (mFlip == Flip.Vertically || mFlip == Flip.Both)
{
num2 += (float)num6 * num10;
num4 -= (float)paddingBottom * num10;
}
else
{
num2 += (float)paddingBottom * num10;
num4 -= (float)num6 * num10;
}
}
Vector4 vector = (!(mAtlas != null)) ? Vector4.zero : (border * pixelSize);
float num11 = vector.x + vector.z;
float num12 = vector.y + vector.w;
float x = Mathf.Lerp(num, num3 - num11, mDrawRegion.x);
float y = Mathf.Lerp(num2, num4 - num12, mDrawRegion.y);
float z = Mathf.Lerp(num + num11, num3, mDrawRegion.z);
float w = Mathf.Lerp(num2 + num12, num4, mDrawRegion.w);
return new Vector4(x, y, z, w);
}
}
public override bool premultipliedAlpha => mAtlas != null && mAtlas.premultipliedAlpha;
public UISpriteData GetAtlasSprite()
{
if (!mSpriteSet)
{
mSprite = null;
}
if (mSprite == null && mAtlas != null)
{
if (!string.IsNullOrEmpty(mSpriteName))
{
UISpriteData sprite = mAtlas.GetSprite(mSpriteName);
if (sprite == null)
{
return null;
}
SetAtlasSprite(sprite);
}
if (mSprite == null && mAtlas.spriteList.Count > 0)
{
UISpriteData uISpriteData = mAtlas.spriteList[0];
if (uISpriteData == null)
{
return null;
}
SetAtlasSprite(uISpriteData);
if (mSprite == null)
{
Debug.LogError(mAtlas.name + " seems to have a null sprite!");
return null;
}
mSpriteName = mSprite.name;
}
}
return mSprite;
}
protected void SetAtlasSprite(UISpriteData sp)
{
mChanged = true;
mSpriteSet = true;
if (sp != null)
{
mSprite = sp;
mSpriteName = mSprite.name;
}
else
{
mSpriteName = ((mSprite == null) ? string.Empty : mSprite.name);
mSprite = sp;
}
}
public override void MakePixelPerfect()
{
if (isValid)
{
base.MakePixelPerfect();
if (mType != Type.Tiled)
{
UISpriteData atlasSprite = GetAtlasSprite();
if (atlasSprite != null)
{
Texture mainTexture = this.mainTexture;
if (!(mainTexture == null) && (mType == Type.Simple || mType == Type.Filled || !atlasSprite.hasBorder) && mainTexture != null)
{
int num = Mathf.RoundToInt(pixelSize * (float)(atlasSprite.width + atlasSprite.paddingLeft + atlasSprite.paddingRight));
int num2 = Mathf.RoundToInt(pixelSize * (float)(atlasSprite.height + atlasSprite.paddingTop + atlasSprite.paddingBottom));
if ((num & 1) == 1)
{
num++;
}
if ((num2 & 1) == 1)
{
num2++;
}
base.width = num;
base.height = num2;
}
}
}
}
}
protected override void OnInit()
{
if (!mFillCenter)
{
mFillCenter = true;
centerType = AdvancedType.Invisible;
}
base.OnInit();
}
protected override void OnUpdate()
{
base.OnUpdate();
if (mChanged || !mSpriteSet)
{
mSpriteSet = true;
mSprite = null;
mChanged = true;
}
}
public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
{
Texture mainTexture = this.mainTexture;
if (!(mainTexture == null))
{
if (mSprite == null)
{
mSprite = atlas.GetSprite(spriteName);
}
if (mSprite != null)
{
Rect rect = new Rect((float)mSprite.x, (float)mSprite.y, (float)mSprite.width, (float)mSprite.height);
Rect rect2 = new Rect((float)(mSprite.x + mSprite.borderLeft), (float)(mSprite.y + mSprite.borderTop), (float)(mSprite.width - mSprite.borderLeft - mSprite.borderRight), (float)(mSprite.height - mSprite.borderBottom - mSprite.borderTop));
rect = NGUIMath.ConvertToTexCoords(rect, mainTexture.width, mainTexture.height);
rect2 = NGUIMath.ConvertToTexCoords(rect2, mainTexture.width, mainTexture.height);
int size = verts.size;
Fill(verts, uvs, cols, rect, rect2);
if (onPostFill != null)
{
onPostFill(this, size, verts, uvs, cols);
}
}
}
}
}