-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathEditorGUIUtility.cs
More file actions
559 lines (457 loc) · 20.6 KB
/
Copy pathEditorGUIUtility.cs
File metadata and controls
559 lines (457 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
using UnityEngine;
using System;
using System.Collections.Generic;
using UnityEngine.ProBuilder;
namespace UnityEditor.ProBuilder.UI
{
/// <summary>
/// Generic GUI utility methods used in ProBuilder windows.
/// </summary>
static class EditorGUIUtility
{
internal static class Styles
{
private static GUIStyle s_Command;
public static GUIStyle command => s_Command ??= "command";
public static GUIContent[] selectModeIcons;
static Texture2D s_ObjectIcon;
public static Texture2D ObjectIcon => s_ObjectIcon ??= IconUtility.GetIcon("Modes/Mode_Object");
static Texture2D s_VertexIcon;
public static Texture2D VertexIcon => s_VertexIcon ??= IconUtility.GetIcon("Modes/Mode_Vertex");
static Texture2D s_EdgeIcon;
public static Texture2D EdgeIcon => s_EdgeIcon ??= IconUtility.GetIcon("Modes/Mode_Edge");
static Texture2D s_FaceIcon;
public static Texture2D FaceIcon => s_FaceIcon ??= IconUtility.GetIcon("Modes/Mode_Face");
public static void Init()
{
selectModeIcons = new GUIContent[]
{
ObjectIcon != null
? new GUIContent(s_ObjectIcon, "Object Selection")
: new GUIContent("OBJ", "Object Selection"),
VertexIcon != null
? new GUIContent(s_VertexIcon, "Vertex Selection")
: new GUIContent("VRT", "Vertex Selection"),
EdgeIcon != null
? new GUIContent(s_EdgeIcon, "Edge Selection")
: new GUIContent("EDG", "Edge Selection"),
FaceIcon != null
? new GUIContent(s_FaceIcon, "Face Selection")
: new GUIContent("FCE", "Face Selection"),
};
}
internal static void ResetForPlayMode()
{
selectModeIcons = null;
s_ObjectIcon = null;
s_VertexIcon = null;
s_EdgeIcon = null;
s_FaceIcon = null;
}
}
static readonly Color TOOL_SETTINGS_COLOR = UnityEditor.EditorGUIUtility.isProSkin
? Color.green
: new Color(.2f, .2f, .2f, .2f);
static GUIStyle _splitStyle;
static GUIStyle SplitStyle
{
get
{
if (_splitStyle == null)
{
_splitStyle = new GUIStyle();
_splitStyle.normal.background = UnityEditor.EditorGUIUtility.whiteTexture;
_splitStyle.margin = new RectOffset(6, 6, 0, 0);
}
return _splitStyle;
}
}
static GUIStyle _centeredGreyMiniLabel;
public static GUIStyle CenteredGreyMiniLabel
{
get
{
if (_centeredGreyMiniLabel == null)
{
_centeredGreyMiniLabel = new GUIStyle(UnityEditor.EditorStyles.label);
_centeredGreyMiniLabel.normal.textColor = Color.gray;
_centeredGreyMiniLabel.alignment = TextAnchor.MiddleCenter;
}
return _centeredGreyMiniLabel;
}
}
static GUIStyle _solidBackgroundStyle;
public static GUIStyle solidBackgroundStyle
{
get
{
if (_solidBackgroundStyle == null)
{
_solidBackgroundStyle = new GUIStyle();
_solidBackgroundStyle.normal.background = UnityEditor.EditorGUIUtility.whiteTexture;
}
return _solidBackgroundStyle;
}
}
static GUIStyle _buttonNoBackgroundSmallMarginStyle = null;
public static GUIStyle ButtonNoBackgroundSmallMarginStyle
{
get
{
if (_buttonNoBackgroundSmallMarginStyle == null)
{
_buttonNoBackgroundSmallMarginStyle = new GUIStyle();
_buttonNoBackgroundSmallMarginStyle.margin = new RectOffset(0, 0, 0, 0);
_buttonNoBackgroundSmallMarginStyle.alignment = TextAnchor.MiddleCenter;
_buttonNoBackgroundSmallMarginStyle.padding = new RectOffset(2, 2, 2, 2);
}
return _buttonNoBackgroundSmallMarginStyle;
}
}
static GUIContent _guiContent = null;
public static GUIContent TempContent(string label, string tooltip = null, Texture2D icon = null)
{
if (_guiContent == null)
_guiContent = new GUIContent();
_guiContent.text = label;
_guiContent.tooltip = tooltip;
_guiContent.image = icon;
return _guiContent;
}
static Stack<bool> s_GuiEnabled = new Stack<bool>();
static Stack<Color> s_ContentColor = new Stack<Color>();
static Stack<Color> s_BackgroundColor = new Stack<Color>();
public static void PushGUIEnabled(bool enabled)
{
s_GuiEnabled.Push(GUI.enabled);
GUI.enabled = enabled;
}
public static void PopGUIEnabled()
{
GUI.enabled = s_GuiEnabled.Pop();
}
public static void PushGUIContentColor(Color color)
{
s_ContentColor.Push(GUI.color);
GUI.contentColor = color;
}
public static void PopGUIContentColor()
{
GUI.contentColor = s_ContentColor.Pop();
}
public static void PushBackgroundColor(Color color)
{
s_BackgroundColor.Push(GUI.backgroundColor);
GUI.backgroundColor = color;
}
public static void PopBackgroundColor()
{
GUI.backgroundColor = s_BackgroundColor.Pop();
}
/**
* Draws a horizontal line and inserts a GUILayout.Space(2).
* @param lines How many lines to draw. Typically 1 or 2 suffice.
*/
public static void DrawSeparator(int lines, Color color)
{
Color old = UnityEngine.GUI.backgroundColor;
UnityEngine.GUI.backgroundColor = color;
DrawSeparator(lines);
UnityEngine.GUI.backgroundColor = old;
}
public static void DrawSeparator(int lines)
{
GUILayout.Box("", SplitStyle, GUILayout.MaxHeight(2));
for (int i = 1; i < lines; i++)
{
GUILayout.Space(2);
GUILayout.Box("", SplitStyle, GUILayout.MaxHeight(2));
}
}
static Dictionary<GUIStyle, GUIStyle> onStyles = new Dictionary<GUIStyle, GUIStyle>();
public static GUIStyle GetOnStyle(GUIStyle style)
{
GUIStyle on;
if (onStyles.TryGetValue(style, out on))
return on;
on = new GUIStyle(style);
on.normal.textColor = on.onNormal.textColor;
on.normal.background = on.onNormal.background;
onStyles.Add(style, on);
return on;
}
static Dictionary<GUIStyle, GUIStyle> activeStyles = new Dictionary<GUIStyle, GUIStyle>();
public static GUIStyle GetActiveStyle(GUIStyle style)
{
GUIStyle activeStyle;
if (activeStyles.TryGetValue(style, out activeStyle))
return activeStyle;
activeStyle = new GUIStyle(style);
activeStyle.normal.textColor = activeStyle.active.textColor;
activeStyle.normal.background = activeStyle.active.background;
activeStyles.Add(style, activeStyle);
return activeStyle;
}
/**
* Draw a solid color block at rect.
*/
public static void DrawSolidColor(Rect rect, Color col)
{
Color old = UnityEngine.GUI.backgroundColor;
UnityEngine.GUI.backgroundColor = col;
UnityEngine.GUI.Box(rect, "", SplitStyle);
UnityEngine.GUI.backgroundColor = old;
}
const int FieldBoxWidth = 64;
public static float FloatFieldConstrained(GUIContent content, float value, int width)
{
GUILayout.BeginHorizontal();
GUILayout.Label(content, GUILayout.MaxWidth(width - FieldBoxWidth));
value = UnityEditor.EditorGUILayout.FloatField("", value, GUILayout.MaxWidth(FieldBoxWidth - 4));
GUILayout.EndHorizontal();
return value;
}
public static int IntFieldConstrained(GUIContent content, int value, int width)
{
GUILayout.BeginHorizontal();
GUILayout.Label(content, GUILayout.MaxWidth(width - FieldBoxWidth));
value = UnityEditor.EditorGUILayout.IntField("", value, GUILayout.MaxWidth(FieldBoxWidth - 4));
GUILayout.EndHorizontal();
return value;
}
static GUIContent slider_guicontent = new GUIContent("", "");
public static float FreeSlider(string content, float value, float min, float max)
{
slider_guicontent.text = content;
return FreeSlider(slider_guicontent, value, min, max);
}
/**
* Similar to EditorGUILayoutUtility.Slider, except this allows for values outside of the min/max bounds via the float field.
*/
public static float FreeSlider(GUIContent content, float value, float min, float max)
{
float pixelsPerPoint = 1f;
pixelsPerPoint = UnityEditor.EditorGUIUtility.pixelsPerPoint;
float PAD = 8f / pixelsPerPoint;
float sliderHeight = UnityEditor.EditorGUIUtility.singleLineHeight;
const float MIN_LABEL_WIDTH = 0f;
const float MAX_LABEL_WIDTH = 128f;
const float MIN_FIELD_WIDTH = 48f;
Rect rect = EditorGUILayout.GetSliderRect(true);
float y = rect.y;
float labelWidth = content != null ? Mathf.Max(MIN_LABEL_WIDTH, Mathf.Min(GUI.skin.label.CalcSize(content).x + PAD, MAX_LABEL_WIDTH)) : 0f;
float remaining = ((Screen.width / pixelsPerPoint) - (PAD * 2f)) - labelWidth;
float sliderWidth = remaining - (MIN_FIELD_WIDTH + PAD);
float floatWidth = MIN_FIELD_WIDTH;
Rect labelRect = new Rect(PAD, y + 2f, labelWidth, sliderHeight);
Rect sliderRect = new Rect(labelRect.x + labelWidth, y + 1f, sliderWidth, sliderHeight);
Rect floatRect = new Rect(sliderRect.x + sliderRect.width + PAD, y + 1f, floatWidth, sliderHeight);
if (content != null)
GUI.Label(labelRect, content);
EditorGUI.BeginChangeCheck();
int controlID = GUIUtility.GetControlID(FocusType.Passive, sliderRect);
float tmp = value;
tmp = GUI.Slider(sliderRect, tmp, 0f, min, max, GUI.skin.horizontalSlider, (!EditorGUI.showMixedValue) ? GUI.skin.horizontalSliderThumb : "SliderMixed", true, controlID);
if (EditorGUI.EndChangeCheck())
value = Event.current.control ? 1f * Mathf.Round(tmp / 1f) : tmp;
value = EditorGUI.FloatField(floatRect, value);
return value;
}
public static int FreeSliderWithRange(string content, int value, int min, int max, ref int uiMin, ref int uiMax, ref bool expanded)
{
slider_guicontent.text = content;
return FreeSliderWithRange(slider_guicontent, value, min, max, ref uiMin, ref uiMax, ref expanded);
}
/**
* Similar to EditorGUILayoutUtility.Slider, except this allows for values outside of the uiMin/uiMax bounds via the int field.
* Contrary to the FreeSlider however it has a hard range defined with min and max.
*/
public static int FreeSliderWithRange(GUIContent content, int value, int min, int max, ref int uiMin, ref int uiMax, ref bool expanded)
{
float pixelsPerPoint = 1f;
pixelsPerPoint = UnityEditor.EditorGUIUtility.pixelsPerPoint;
float PAD = 6f / pixelsPerPoint;
const float SLIDER_HEIGHT = 16f;
const float MIN_LABEL_WIDTH = 0f;
const float MAX_LABEL_WIDTH = 128f;
const float MIN_FIELD_WIDTH = 48f;
GUILayoutUtility.GetRect(UnityEditor.EditorGUIUtility.currentViewWidth / pixelsPerPoint, 18);
Rect previousRect = GUILayoutUtility.GetLastRect();
float y = previousRect.y;
float labelWidth = content != null ? Mathf.Max(MIN_LABEL_WIDTH, Mathf.Min(GUI.skin.label.CalcSize(content).x + PAD, MAX_LABEL_WIDTH)) : 0f;
float remaining = ((Screen.width / pixelsPerPoint) - (PAD * 4f)) - labelWidth;
float sliderWidth = remaining - (MIN_FIELD_WIDTH + PAD);
float intWidth = MIN_FIELD_WIDTH;
float indentOffset = EditorGUI.indentLevel * 15f;
Rect labelRect = new Rect(PAD, y + 2f, labelWidth, SLIDER_HEIGHT);
Rect sliderRect = new Rect(labelRect.x + labelWidth + 2*PAD, y + 1f, sliderWidth, SLIDER_HEIGHT);
Rect intRect = new Rect(sliderRect.x + sliderRect.width + PAD, y + 1f, intWidth, SLIDER_HEIGHT);
Rect totalRect = GUILayoutUtility.GetRect(1, UnityEditor.EditorGUIUtility.singleLineHeight);
Rect foldoutRect = new Rect(labelRect.xMax - PAD, labelRect.y, 15, totalRect.height);
if (content != null)
GUI.Label(labelRect, content);
EditorGUI.BeginChangeCheck();
int controlID = GUIUtility.GetControlID(FocusType.Passive, sliderRect);
float tmp = value;
float tmpUIMin = uiMin;
float tmpUIMax = uiMax;
tmp = GUI.Slider(sliderRect, tmp, 0f, tmpUIMin, tmpUIMax, GUI.skin.horizontalSlider, (!EditorGUI.showMixedValue) ? GUI.skin.horizontalSliderThumb : "SliderMixed", true, controlID);
if (EditorGUI.EndChangeCheck())
value = (int) (Event.current.control ? 1 * Mathf.Round(tmp / 1f) : tmp);
value = EditorGUI.DelayedIntField(intRect, value);
if (value > uiMax)
uiMax = value;
if (value < uiMin)
uiMin = value;
expanded = EditorGUI.Foldout(foldoutRect, expanded, GUIContent.none);
if (expanded)
{
Rect rangeLabelRect = new Rect(sliderRect.x, sliderRect.yMax, sliderRect.width / 2, intRect.height);
Rect minRect = new Rect(intRect.x - (intRect.width + indentOffset + PAD), sliderRect.yMax + 2f, intRect.width, intRect.height);
Rect maxRect = new Rect(intRect.x, sliderRect.yMax + 2f, intRect.width, intRect.height);
EditorGUI.PrefixLabel(rangeLabelRect, new GUIContent("Range:"));
uiMin = UnityEditor.EditorGUI.DelayedIntField(minRect, uiMin);
uiMin = UnityEngine.ProBuilder.Math.Clamp(uiMin, min, uiMax);
if (value < uiMin)
value = uiMin;
uiMax = UnityEditor.EditorGUI.DelayedIntField(maxRect, uiMax);
uiMax = UnityEngine.ProBuilder.Math.Clamp(uiMax, uiMin + 1, max);
if (value > uiMax)
value = uiMax;
}
return UnityEngine.ProBuilder.Math.Clamp(value, min, max);
}
public static bool ToolSettingsGUI(string text,
string description,
bool showSettings,
Func<ProBuilderMesh[], ActionResult> action,
Action gui,
ProBuilderMesh[] selection)
{
return ToolSettingsGUI(text, description, showSettings, action, gui, true, selection);
}
public static bool ToolSettingsGUI(string text,
string description,
bool showSettings,
Func<ProBuilderMesh[], ActionResult> action,
Action gui,
bool enabled,
ProBuilderMesh[] selection)
{
if (enabled)
{
GUILayout.BeginHorizontal();
if (GUILayout.Button(new GUIContent(text, description), UnityEditor.EditorStyles.miniButtonLeft))
action(selection);
if (GUILayout.Button(showSettings ? "-" : "+", UnityEditor.EditorStyles.miniButtonRight, GUILayout.MaxWidth(24)))
showSettings = !showSettings;
GUILayout.EndHorizontal();
if (showSettings)
{
GUILayout.BeginVertical(EditorStyles.sceneTextBox);
gui();
GUILayout.EndVertical();
GUILayout.Space(4);
}
}
else
{
if (GUILayout.Button(new GUIContent(text, description), UnityEditor.EditorStyles.miniButton))
action(selection);
}
return showSettings;
}
/**
* Similar to EditorGUILayout.Slider, except that this won't return clamped values
* unless the user modifies the value.
*/
public static float Slider(GUIContent content, float value, float min, float max)
{
float tmp = value;
EditorGUI.BeginChangeCheck();
tmp = UnityEditor.EditorGUILayout.Slider(content, value, min, max);
if (EditorGUI.EndChangeCheck())
return tmp;
else
return value;
}
static Rect sceneLabelRect = new Rect(0f, 0f, 0f, 0f);
static Color SceneLabelBackgroundColor = new Color(.12f, .12f, .12f, 1f);
static GUIStyle sceneBoldLabel
{
get
{
if (_sceneBoldLabel == null)
{
_sceneBoldLabel = new GUIStyle(UnityEditor.EditorStyles.boldLabel);
_sceneBoldLabel.normal.textColor = Color.white;
}
return _sceneBoldLabel;
}
}
static GUIStyle _sceneBoldLabel = null;
[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
Styles.ResetForPlayMode();
_splitStyle = null;
_centeredGreyMiniLabel = null;
_solidBackgroundStyle = null;
_buttonNoBackgroundSmallMarginStyle = null;
_sceneBoldLabel = null;
sceneLabelRect = new Rect(0f, 0f, 0f, 0f);
}
/**
* Draw a label in the scene view with a solid color background.
*/
public static void SceneLabel(string text, Vector2 position, bool center = true)
{
GUIContent gc = TempContent(text);
float width = UnityEditor.EditorStyles.boldLabel.CalcSize(gc).x;
float height = UnityEditor.EditorStyles.label.CalcHeight(gc, width) + 4;
sceneLabelRect.x = center ? position.x - width * .5f : position.x;
sceneLabelRect.y = center ? position.y - height * .5f : position.y - height;
sceneLabelRect.width = width;
sceneLabelRect.height = height;
DrawSolidColor(sceneLabelRect, SceneLabelBackgroundColor);
GUI.Label(sceneLabelRect, gc, sceneBoldLabel);
}
public static SelectMode DoElementModeToolbar(Rect rect, SelectMode mode)
{
Styles.Init();
EditorGUI.BeginChangeCheck();
var textureMode = mode.ContainsFlag(SelectMode.TextureVertex | SelectMode.TextureEdge | SelectMode.TextureFace);
int currentSelectionMode = -1;
switch (mode)
{
case SelectMode.Vertex:
case SelectMode.TextureVertex:
currentSelectionMode = 1;
break;
case SelectMode.Edge:
case SelectMode.TextureEdge:
currentSelectionMode = 2;
break;
case SelectMode.Face:
case SelectMode.TextureFace:
currentSelectionMode = 3;
break;
default:
currentSelectionMode = -1;
break;
}
currentSelectionMode = GUI.Toolbar(rect, currentSelectionMode, Styles.selectModeIcons, Styles.command);
if (EditorGUI.EndChangeCheck())
{
mode = currentSelectionMode switch
{
1 => textureMode ? SelectMode.TextureVertex : SelectMode.Vertex,
2 => textureMode ? SelectMode.TextureEdge : SelectMode.Edge,
3 => textureMode ? SelectMode.TextureFace : SelectMode.Face,
_ => mode
};
}
return mode;
}
}
}