forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteUtilityWindow.cs
More file actions
504 lines (420 loc) · 20.6 KB
/
Copy pathSpriteUtilityWindow.cs
File metadata and controls
504 lines (420 loc) · 20.6 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using UnityEngine;
using UnityEditorInternal;
using UnityEngine.Bindings;
namespace UnityEditor
{
[UIFramework(UIFrameworkUsage.IMGUI)]
[VisibleToOtherModules("UnityEditor.UIBuilderModule")]
internal class SpriteUtilityWindow : EditorWindow
{
protected class Styles
{
public readonly GUIStyle dragdot = "U2D.dragDot";
public readonly GUIStyle dragdotDimmed = "U2D.dragDotDimmed";
public readonly GUIStyle dragdotactive = "U2D.dragDotActive";
public readonly GUIStyle createRect = "U2D.createRect";
public readonly GUIStyle preToolbar = "preToolbar";
public readonly GUIStyle preButton = "preButton";
public readonly GUIStyle preLabel = "preLabel";
public readonly GUIStyle preSlider = "preSlider";
public readonly GUIStyle preSliderThumb = "preSliderThumb";
public readonly GUIStyle preBackground = "preBackground";
public readonly GUIStyle pivotdotactive = "U2D.pivotDotActive";
public readonly GUIStyle pivotdot = "U2D.pivotDot";
public static readonly GUIContent openSpriteEditor = EditorGUIUtility.TrTextContent("Open Sprite Editor");
public static readonly GUIContent install2DPackage = EditorGUIUtility.TrTextContent("Install 2D Sprite Package");
public static readonly GUIContent failedToInstall2DPackageTitle = EditorGUIUtility.TrTextContent("Installation Failed");
public static readonly GUIContent failedToInstall2DPackageContent = EditorGUIUtility.TrTextContent("Failed to install package com.unity.2d.sprite.\nErrorCode: {0}\nMessage: {1}");
public static readonly GUIContent install2DPackageReason = EditorGUIUtility.TrTextContent("The Sprite Editor window is not available because the 2D Sprite package is not installed. Click on the 'Install 2D Sprite Package' button to install the package to edit Sprites in Sprite Editor window.");
public static readonly GUIContent okText = EditorGUIUtility.TrTextContent("OK");
public readonly GUIStyle dragBorderdot = new GUIStyle();
public readonly GUIStyle dragBorderDotActive = new GUIStyle();
public readonly GUIStyle toolbar;
public readonly GUIContent alphaIcon;
public readonly GUIContent RGBIcon;
public readonly GUIStyle notice;
public readonly GUIContent smallMip;
public readonly GUIContent largeMip;
public Styles()
{
toolbar = new GUIStyle(EditorStyles.inspectorBig);
toolbar.margin.top = 0;
toolbar.margin.bottom = 0;
alphaIcon = EditorGUIUtility.IconContent("PreTextureAlpha");
RGBIcon = EditorGUIUtility.IconContent("PreTextureRGB");
preToolbar.border.top = 0;
createRect.border = new RectOffset(3, 3, 3, 3);
notice = new GUIStyle(GUI.skin.label);
notice.alignment = TextAnchor.MiddleCenter;
notice.normal.textColor = Color.yellow;
dragBorderdot.fixedHeight = 5f;
dragBorderdot.fixedWidth = 5f;
dragBorderdot.normal.background = EditorGUIUtility.whiteTexture;
dragBorderDotActive.fixedHeight = dragBorderdot.fixedHeight;
dragBorderDotActive.fixedWidth = dragBorderdot.fixedWidth;
dragBorderDotActive.normal.background = EditorGUIUtility.whiteTexture;
smallMip = EditorGUIUtility.IconContent("PreTextureMipMapLow");
largeMip = EditorGUIUtility.IconContent("PreTextureMipMapHigh");
}
}
protected void InitStyles()
{
if (m_Styles == null)
m_Styles = new Styles();
}
protected Styles m_Styles;
protected const float k_BorderMargin = 10f;
protected const float k_ScrollbarMargin = 16f;
protected const float k_InspectorWindowMargin = 8f;
protected const float k_InspectorWidth = 330f;
protected const float k_MinZoomPercentage = 0.9f;
protected const float k_MaxZoom = 50f;
protected const float k_WheelZoomSpeed = 0.03f;
protected const float k_MouseZoomSpeed = 0.005f;
protected const float k_ToolbarHeight = 17f;
protected Texture2D m_Texture;
protected Texture2D m_TextureAlphaOverride;
protected Rect m_TextureViewRect;
protected Rect m_TextureRect;
protected bool m_ShowAlpha = false;
protected float m_Zoom = -1f;
protected float m_MipLevel = 0;
protected Vector2 m_ScrollPosition = new Vector2();
static LaunchSpriteEditorWindowAfterDomainReload s_LaunchSpriteEditorWindowAfterDomainReload;
protected float GetMinZoom()
{
if (m_Texture == null)
return 1.0f;
// Case 654327: Add k_MaxZoom size to min check to ensure that min zoom is smaller than max zoom
return Mathf.Min(m_TextureViewRect.width / m_Texture.width, m_TextureViewRect.height / m_Texture.height, k_MaxZoom) * k_MinZoomPercentage;
}
protected void HandleZoom()
{
bool zoomMode = Event.current.alt && Event.current.button == 1;
if (zoomMode)
{
EditorGUIUtility.AddCursorRect(m_TextureViewRect, MouseCursor.Zoom);
}
if (
((Event.current.type == EventType.MouseUp || Event.current.type == EventType.MouseDown) && zoomMode) ||
((Event.current.type == EventType.KeyUp || Event.current.type == EventType.KeyDown) && Event.current.keyCode == KeyCode.LeftAlt)
)
{
Repaint();
}
if (Event.current.type == EventType.ScrollWheel || (Event.current.type == EventType.MouseDrag && Event.current.alt && Event.current.button == 1))
{
float zoomMultiplier = 1f - Event.current.delta.y * (Event.current.type == EventType.ScrollWheel ? k_WheelZoomSpeed : -k_MouseZoomSpeed);
// Clamp zoom
float wantedZoom = m_Zoom * zoomMultiplier;
float currentZoom = Mathf.Clamp(wantedZoom, GetMinZoom(), k_MaxZoom);
if (currentZoom != m_Zoom)
{
m_Zoom = currentZoom;
// We need to fix zoomMultiplier if we clamped wantedZoom != currentZoom
if (wantedZoom != currentZoom)
zoomMultiplier /= wantedZoom / currentZoom;
Vector3 textureHalfSize = new Vector2(m_Texture.width, m_Texture.height) * 0.5f;
Vector3 mousePositionWorld = Handles.inverseMatrix.MultiplyPoint3x4(Event.current.mousePosition + m_ScrollPosition);
Vector3 delta = (mousePositionWorld - textureHalfSize) * (zoomMultiplier - 1f);
m_ScrollPosition += (Vector2)Handles.matrix.MultiplyVector(delta);
Event.current.Use();
}
}
}
protected void HandlePanning()
{
// You can pan by holding ALT and using left button or NOT holding ALT and using right button. ALT + right is reserved for zooming.
bool panMode = (!Event.current.alt && Event.current.button > 0 || Event.current.alt && Event.current.button <= 0);
if (panMode && GUIUtility.hotControl == 0)
{
EditorGUIUtility.AddCursorRect(m_TextureViewRect, MouseCursor.Pan);
if (Event.current.type == EventType.MouseDrag)
{
m_ScrollPosition -= Event.current.delta;
Event.current.Use();
}
}
//We need to repaint when entering or exiting the pan mode, so the mouse cursor gets refreshed.
if (
((Event.current.type == EventType.MouseUp || Event.current.type == EventType.MouseDown) && panMode) ||
(Event.current.type == EventType.KeyUp || Event.current.type == EventType.KeyDown) && Event.current.keyCode == KeyCode.LeftAlt
)
{
Repaint();
}
}
// Bounding values for scrollbars. Changes with zoom, because we want min/max scroll to stop at texture edges.
protected Rect maxScrollRect
{
get
{
float halfWidth = m_Texture.width * .5f * m_Zoom;
float halfHeight = m_Texture.height * .5f * m_Zoom;
return new Rect(-halfWidth, -halfHeight, m_TextureViewRect.width + halfWidth * 2f, m_TextureViewRect.height + halfHeight * 2f);
}
}
// Max rect in texture space that can ever be visible
protected Rect maxRect
{
get
{
float marginW = m_TextureViewRect.width * .5f / GetMinZoom();
float marginH = m_TextureViewRect.height * .5f / GetMinZoom();
float left = -marginW;
float top = -marginH;
float width = m_Texture.width + marginW * 2f;
float height = m_Texture.height + marginH * 2f;
return new Rect(left, top, width, height);
}
}
protected void DrawTexturespaceBackground()
{
float size = Mathf.Max(maxRect.width, maxRect.height);
Vector2 offset = new Vector2(maxRect.xMin, maxRect.yMin);
float halfSize = size * .5f;
float alpha = EditorGUIUtility.isProSkin ? 0.15f : 0.08f;
float gridSize = 8f;
SpriteEditorUtility.BeginLines(new Color(0f, 0f, 0f, alpha));
for (float v = 0; v <= size; v += gridSize)
SpriteEditorUtility.DrawLine(new Vector2(-halfSize + v, halfSize + v) + offset, new Vector2(halfSize + v, -halfSize + v) + offset);
SpriteEditorUtility.EndLines();
}
protected void DrawTexture()
{
float mipLevel = Mathf.Min(m_MipLevel, TextureUtil.GetMipmapCount(m_Texture) - 1);
FilterMode oldFilter = m_Texture.filterMode;
TextureUtil.SetFilterModeNoDirty(m_Texture, FilterMode.Point);
if (m_ShowAlpha)
{
// check if we have a valid alpha texture
if (m_TextureAlphaOverride != null)
EditorGUI.DrawTextureTransparent(m_TextureRect, m_TextureAlphaOverride, ScaleMode.StretchToFill, 0, mipLevel);
// else use the original texture and display its alpha
else
EditorGUI.DrawTextureAlpha(m_TextureRect, m_Texture, ScaleMode.StretchToFill, 0, mipLevel);
}
else
EditorGUI.DrawTextureTransparent(m_TextureRect, m_Texture, ScaleMode.StretchToFill, 0, mipLevel);
TextureUtil.SetFilterModeNoDirty(m_Texture, oldFilter);
}
protected void DrawScreenspaceBackground()
{
if (Event.current.type == EventType.Repaint)
m_Styles.preBackground.Draw(m_TextureViewRect, false, false, false, false);
}
protected void HandleScrollbars()
{
Rect horizontalScrollBarPosition = new Rect(m_TextureViewRect.xMin, m_TextureViewRect.yMax, m_TextureViewRect.width, k_ScrollbarMargin);
m_ScrollPosition.x = GUI.HorizontalScrollbar(horizontalScrollBarPosition, m_ScrollPosition.x, m_TextureViewRect.width, maxScrollRect.xMin, maxScrollRect.xMax);
Rect verticalScrollBarPosition = new Rect(m_TextureViewRect.xMax, m_TextureViewRect.yMin, k_ScrollbarMargin, m_TextureViewRect.height);
m_ScrollPosition.y = GUI.VerticalScrollbar(verticalScrollBarPosition, m_ScrollPosition.y, m_TextureViewRect.height, maxScrollRect.yMin, maxScrollRect.yMax);
}
protected void SetupHandlesMatrix()
{
// Offset from top left to center in view space
Vector3 handlesPos = new Vector3(m_TextureRect.x, m_TextureRect.yMax, 0f);
// We flip Y-scale because Unity texture space is bottom-up
Vector3 handlesScale = new Vector3(m_Zoom, -m_Zoom, 1f);
// Handle matrix is for converting between view and texture space coordinates, without taking account the scroll position.
// Scroll position is added separately so we can use it with GUIClip.
Handles.matrix = Matrix4x4.TRS(handlesPos, Quaternion.identity, handlesScale);
}
protected Rect DoAlphaZoomToolbarGUI(Rect area)
{
int mipCount = 1;
if (m_Texture != null)
mipCount = Mathf.Max(mipCount, TextureUtil.GetMipmapCount(m_Texture));
Rect drawArea = new Rect(area.width, 0, 0, area.height);
using (new EditorGUI.DisabledScope(mipCount == 1))
{
drawArea.width = m_Styles.largeMip.image.width;
drawArea.x -= drawArea.width;
GUI.Box(drawArea, m_Styles.largeMip, m_Styles.preLabel);
drawArea.width = EditorGUI.kSliderMinW;
drawArea.x -= drawArea.width;
m_MipLevel = Mathf.Round(GUI.HorizontalSlider(drawArea, m_MipLevel, mipCount - 1, 0, m_Styles.preSlider, m_Styles.preSliderThumb));
drawArea.width = m_Styles.smallMip.image.width;
drawArea.x -= drawArea.width;
GUI.Box(drawArea, m_Styles.smallMip, m_Styles.preLabel);
}
drawArea.width = EditorGUI.kSliderMinW;
drawArea.x -= drawArea.width;
m_Zoom = GUI.HorizontalSlider(drawArea, m_Zoom, GetMinZoom(), k_MaxZoom, m_Styles.preSlider, m_Styles.preSliderThumb);
drawArea.width = EditorGUI.kObjectFieldMiniThumbnailWidth;
drawArea.x -= drawArea.width + EditorGUI.kSpacing;
m_ShowAlpha = GUI.Toggle(drawArea, m_ShowAlpha, m_ShowAlpha ? m_Styles.alphaIcon : m_Styles.RGBIcon, "toolbarButton");
// Returns the area that is not used
return new Rect(area.x, area.y, drawArea.x, area.height);
}
protected void DoTextureGUI()
{
if (m_Texture == null)
return;
// zoom startup init
if (m_Zoom < 0f)
m_Zoom = GetMinZoom();
// Texture rect in view space
m_TextureRect = new Rect(
m_TextureViewRect.width / 2f - (m_Texture.width * m_Zoom / 2f),
m_TextureViewRect.height / 2f - (m_Texture.height * m_Zoom / 2f),
(m_Texture.width * m_Zoom),
(m_Texture.height * m_Zoom)
);
HandleScrollbars();
SetupHandlesMatrix();
DrawScreenspaceBackground();
GUIClip.Push(m_TextureViewRect, -m_ScrollPosition, Vector2.zero, false);
if (Event.current.type == EventType.Repaint)
{
DrawTexturespaceBackground();
DrawTexture();
DrawGizmos();
}
DoTextureGUIExtras();
GUIClip.Pop();
// Handle this after DoTextureGUIExtras in case user wants any event that is handled by Zoom or Panning
HandleZoom();
HandlePanning();
}
protected virtual void DoTextureGUIExtras()
{
}
protected virtual void DrawGizmos()
{
}
protected void SetNewTexture(Texture2D texture)
{
if (texture != m_Texture)
{
m_Texture = texture;
m_Zoom = -1;
m_TextureAlphaOverride = null;
}
}
protected void SetAlphaTextureOverride(Texture2D alphaTexture)
{
if (alphaTexture != m_TextureAlphaOverride)
{
m_TextureAlphaOverride = alphaTexture;
m_Zoom = -1;
}
}
internal override void OnResized()
{
if (m_Texture != null && Event.current != null)
HandleZoom();
}
internal static void DrawToolBarWidget(ref Rect drawRect, ref Rect toolbarRect, Action<Rect> drawAction)
{
toolbarRect.width -= drawRect.width;
if (toolbarRect.width < 0)
drawRect.width += toolbarRect.width;
if (drawRect.width > 0)
drawAction(drawRect);
}
internal static bool DoOpenSpriteEditorWindowUI()
{
var buttonText = showSpriteEditorWindow == null ? Styles.install2DPackage : Styles.openSpriteEditor;
GUILayout.BeginVertical();
var clicked = GUILayout.Button(buttonText);
if (showSpriteEditorWindow == null)
EditorGUILayout.HelpBox(Styles.install2DPackageReason.text, MessageType.Info, true);
GUILayout.EndVertical();
return clicked;
}
[Obsolete("Use SpriteUtility.SetShowSpriteEditorWindowWithObject instead")]
internal static void SetShowSpriteEditorWindow(Func<bool> spriteEditorWindow)
{
if (spriteEditorWindow != null)
showSpriteEditorWindow = (x) => spriteEditorWindow();
}
internal static void SetShowSpriteEditorWindowWithObject(Func<UnityEngine.Object, bool> spriteEditorWindow)
{
if (spriteEditorWindow != null)
showSpriteEditorWindow = spriteEditorWindow;
}
internal static void SetApplySpriteEditorWindow(Action action)
{
if (action != null)
applySpriteEditorWindow = action;
else
applySpriteEditorWindow = () => {};
}
[VisibleToOtherModules("UnityEditor.UIBuilderModule")]
internal static bool ShowSpriteEditorWindow(UnityEngine.Object obj = null)
{
if (showSpriteEditorWindow == null)
{
var installSuccess = InstallSpritePackage();
if (installSuccess)
{
s_LaunchSpriteEditorWindowAfterDomainReload = ScriptableObject.CreateInstance<LaunchSpriteEditorWindowAfterDomainReload>();
s_LaunchSpriteEditorWindowAfterDomainReload.selectedObject = obj;
}
return installSuccess;
}
return showSpriteEditorWindow(obj != null ? obj : Selection.activeObject);
}
static Func<UnityEngine.Object, bool> showSpriteEditorWindow = null;
static bool InstallSpritePackage()
{
if(s_LaunchSpriteEditorWindowAfterDomainReload != null)
DestroyImmediate(s_LaunchSpriteEditorWindowAfterDomainReload);
var addRequest = PackageManager.Client.Add("com.unity.2d.sprite");
while (!addRequest.IsCompleted)
System.Threading.Thread.Sleep(10);
if (addRequest.Status == PackageManager.StatusCode.Failure)
{
var message = String.Format(Styles.failedToInstall2DPackageContent.text, addRequest.Error.errorCode, addRequest.Error.message);
EditorUtility.DisplayDialog(Styles.failedToInstall2DPackageTitle.text, message, Styles.okText.text);
}
return addRequest.Status == PackageManager.StatusCode.Success;
}
internal static void ApplySpriteEditorWindow()
{
applySpriteEditorWindow();
}
static Action applySpriteEditorWindow = () => {};
} // class
internal class LaunchSpriteEditorWindowAfterDomainReload : ScriptableObject
{
[SerializeField]
UnityEngine.Object m_SelectedObject;
public UnityEngine.Object selectedObject
{
get => m_SelectedObject;
set => m_SelectedObject = value;
}
public LaunchSpriteEditorWindowAfterDomainReload()
{
AssemblyReloadEvents.afterAssemblyReload += WaitForEditorApplicaionUpdate;
}
void WaitForEditorApplicaionUpdate()
{
AssemblyReloadEvents.afterAssemblyReload -= WaitForEditorApplicaionUpdate;
// Do this to ensure asset database has finish importing the assets needed by Sprite Editor Window
EditorApplication.update += OpenSpriteEditor;
}
void OpenSpriteEditor()
{
EditorApplication.update -= OpenSpriteEditor;
try
{
SpriteUtilityWindow.ShowSpriteEditorWindow(selectedObject);
}
catch (Exception e)
{
throw e;
}
finally
{
DestroyImmediate(this);
}
}
}
}