forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUILayoutUtility.cs
More file actions
504 lines (450 loc) · 21.3 KB
/
GUILayoutUtility.cs
File metadata and controls
504 lines (450 loc) · 21.3 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 System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security;
using UnityEngineInternal;
using UnityEngine.Bindings;
using UnityEngine.Scripting;
namespace UnityEngine
{
// Utility functions for implementing and extending the GUILayout class.
public partial class GUILayoutUtility
{
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal sealed class LayoutCache
{
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal GUILayoutGroup topLevel = new GUILayoutGroup();
internal GenericStack layoutGroups = new GenericStack();
internal GUILayoutGroup windows = new GUILayoutGroup();
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal LayoutCache()
{
layoutGroups.Push(topLevel);
}
internal LayoutCache(LayoutCache other)
{
topLevel = other.topLevel;
layoutGroups = other.layoutGroups;
windows = other.windows;
}
}
// TODO: Clean these up after a while
static readonly Dictionary<int, LayoutCache> s_StoredLayouts = new Dictionary<int, LayoutCache>();
static readonly Dictionary<int, LayoutCache> s_StoredWindows = new Dictionary<int, LayoutCache>();
internal static LayoutCache current = new LayoutCache();
internal static readonly Rect kDummyRect = new Rect(0, 0, 1, 1);
internal static void CleanupRoots()
{
// See GUI.CleanupRoots
s_SpaceStyle = null;
s_StoredLayouts.Clear();
s_StoredWindows.Clear();
current = new LayoutCache();
}
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal static LayoutCache SelectIDList(int instanceID, bool isWindow)
{
Dictionary<int, LayoutCache> store = isWindow ? s_StoredWindows : s_StoredLayouts;
LayoutCache cache;
if (store.TryGetValue(instanceID, out cache) == false)
{
// Debug.Log ("Creating ID " +instanceID + " " + Event.current.type);
cache = new LayoutCache();
store[instanceID] = cache;
}
else
{
// Debug.Log ("reusing ID " +instanceID + " " + Event.current.type);
}
current.topLevel = cache.topLevel;
current.layoutGroups = cache.layoutGroups;
current.windows = cache.windows;
return cache;
}
// Set up the internal GUILayouting
// Called by the main GUI class automatically (from GUI.Begin)
internal static void Begin(int instanceID)
{
LayoutCache cache = SelectIDList(instanceID, false);
// Make a vertical group to encompass the whole thing
if (Event.current.type == EventType.Layout)
{
current.topLevel = cache.topLevel = new GUILayoutGroup();
current.layoutGroups.Clear();
current.layoutGroups.Push(current.topLevel);
current.windows = cache.windows = new GUILayoutGroup();
}
else
{
current.topLevel = cache.topLevel;
current.layoutGroups = cache.layoutGroups;
current.windows = cache.windows;
}
}
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal static void BeginContainer(LayoutCache cache)
{
// Make a vertical group to encompass the whole thing
if (Event.current.type == EventType.Layout)
{
current.topLevel = cache.topLevel = new GUILayoutGroup();
current.layoutGroups.Clear();
current.layoutGroups.Push(current.topLevel);
current.windows = cache.windows = new GUILayoutGroup();
}
else
{
current.topLevel = cache.topLevel;
current.layoutGroups = cache.layoutGroups;
current.windows = cache.windows;
}
}
internal static void BeginWindow(int windowID, GUIStyle style, GUILayoutOption[] options)
{
LayoutCache cache = SelectIDList(windowID, true);
// Make a vertical group to encompass the whole thing
if (Event.current.type == EventType.Layout)
{
current.topLevel = cache.topLevel = new GUILayoutGroup();
current.topLevel.style = style;
current.topLevel.windowID = windowID;
if (options != null)
current.topLevel.ApplyOptions(options);
current.layoutGroups.Clear();
current.layoutGroups.Push(current.topLevel);
current.windows = cache.windows = new GUILayoutGroup();
}
else
{
current.topLevel = cache.topLevel;
current.layoutGroups = cache.layoutGroups;
current.windows = cache.windows;
}
}
// TODO: actually make these check...
[Obsolete("BeginGroup has no effect and will be removed", false)]
public static void BeginGroup(string GroupName) {}
[Obsolete("EndGroup has no effect and will be removed", false)]
public static void EndGroup(string groupName) {}
internal static void Layout()
{
if (current.topLevel.windowID == -1)
{
// Normal GUILayout.whatever -outside beginArea calls.
// Here we go over all entries and calculate their sizes
current.topLevel.CalcWidth();
current.topLevel.SetHorizontal(0, Mathf.Min(Screen.width / GUIUtility.pixelsPerPoint, current.topLevel.maxWidth));
current.topLevel.CalcHeight();
current.topLevel.SetVertical(0, Mathf.Min(Screen.height / GUIUtility.pixelsPerPoint, current.topLevel.maxHeight));
LayoutFreeGroup(current.windows);
}
else
{
LayoutSingleGroup(current.topLevel);
LayoutFreeGroup(current.windows);
}
}
// Global layout function. Called from EditorWindows (differs from game view in that they use the full window size and try to stretch GUI
internal static void LayoutFromEditorWindow()
{
if (current.topLevel != null)
{
current.topLevel.CalcWidth();
current.topLevel.SetHorizontal(0, Screen.width / GUIUtility.pixelsPerPoint);
current.topLevel.CalcHeight();
current.topLevel.SetVertical(0, Screen.height / GUIUtility.pixelsPerPoint);
// UNCOMMENT ME TO DEBUG THE EditorWindow ROOT LAYOUT RESULTS
// Debug.Log (current.topLevel);
// Layout all beginarea parts...
LayoutFreeGroup(current.windows);
}
else
{
Debug.LogError("GUILayout state invalid. Verify that all layout begin/end calls match.");
}
}
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal static void LayoutFromContainer(float w, float h)
{
if (current.topLevel != null)
{
current.topLevel.CalcWidth();
current.topLevel.SetHorizontal(0, w);
current.topLevel.CalcHeight();
current.topLevel.SetVertical(0, h);
// UNCOMMENT ME TO DEBUG THE EditorWindow ROOT LAYOUT RESULTS
// Debug.Log (current.topLevel);
// Layout all beginarea parts...
LayoutFreeGroup(current.windows);
}
else
{
Debug.LogError("GUILayout state invalid. Verify that all layout begin/end calls match.");
}
}
// Global layout function. Calculates all sizes of all windows etc & assigns.
// After this call everything has a properly calculated size
// Called by Unity automatically.
// Is public so we can access it from editor inspectors, but not supported by public stuff
internal static float LayoutFromInspector(float width)
{
if (current.topLevel != null && current.topLevel.windowID == -1)
{
// Here we go over all entries and calculate their sizes
current.topLevel.CalcWidth();
current.topLevel.SetHorizontal(0, width);
current.topLevel.CalcHeight();
current.topLevel.SetVertical(0, Mathf.Min(Screen.height / GUIUtility.pixelsPerPoint, current.topLevel.maxHeight));
// UNCOMMENT ME TO DEBUG THE INSPECTOR
// Debug.Log (current.topLevel);
float height = current.topLevel.minHeight;
// Layout all beginarea parts...
// TODO: NOT SURE HOW THIS WORKS IN AN INSPECTOR
LayoutFreeGroup(current.windows);
return height;
}
if (current.topLevel != null)
LayoutSingleGroup(current.topLevel);
return 0;
}
internal static void LayoutFreeGroup(GUILayoutGroup toplevel)
{
foreach (GUILayoutGroup i in toplevel.entries)
{
LayoutSingleGroup(i);
}
toplevel.ResetCursor();
}
static void LayoutSingleGroup(GUILayoutGroup i)
{
if (!i.isWindow)
{
// CalcWidth knocks out minWidth with the calculated sizes from its children. Normally, this is fine, but since we're in a fixed-size area,
// we want to maintain that
float origMinWidth = i.minWidth;
float origMaxWidth = i.maxWidth;
// Figure out the group's min & maxWidth.
i.CalcWidth();
// Make it as wide as possible, but the Rect supplied takes precedence...
i.SetHorizontal(i.rect.x, Mathf.Clamp(i.maxWidth, origMinWidth, origMaxWidth));
// Do the same preservation for CalcHeight...
float origMinHeight = i.minHeight;
float origMaxHeight = i.maxHeight;
i.CalcHeight();
// Make it as high as possible, but the Rect supplied takes precedence...
i.SetVertical(i.rect.y, Mathf.Clamp(i.maxHeight, origMinHeight, origMaxHeight));
}
else
{
// Figure out the group's min & maxWidth.
i.CalcWidth();
Rect winRect = Internal_GetWindowRect(i.windowID);
// Make it as wide as possible, but the Rect supplied takes precedence...
i.SetHorizontal(winRect.x, Mathf.Clamp(winRect.width, i.minWidth, i.maxWidth));
i.CalcHeight();
// Make it as high as possible, but the Rect supplied takes precedence...
i.SetVertical(winRect.y, Mathf.Clamp(winRect.height, i.minHeight, i.maxHeight));
// If GUILayout did any resizing, make sure the window reflects this.
Internal_MoveWindow(i.windowID, i.rect);
}
}
[SecuritySafeCritical]
static GUILayoutGroup CreateGUILayoutGroupInstanceOfType(Type LayoutType)
{
if (!typeof(GUILayoutGroup).IsAssignableFrom(LayoutType))
throw new ArgumentException("LayoutType needs to be of type GUILayoutGroup");
return (GUILayoutGroup)Activator.CreateInstance(LayoutType);
}
// Generic helper - use this when creating a layoutgroup. It will make sure everything is wired up correctly.
internal static GUILayoutGroup BeginLayoutGroup(GUIStyle style, GUILayoutOption[] options, Type layoutType)
{
GUILayoutGroup g;
switch (Event.current.type)
{
case EventType.Used:
case EventType.Layout:
g = CreateGUILayoutGroupInstanceOfType(layoutType);
g.style = style;
if (options != null)
g.ApplyOptions(options);
current.topLevel.Add(g);
break;
default:
g = current.topLevel.GetNext() as GUILayoutGroup;
if (g == null)
throw new ArgumentException("GUILayout: Mismatched LayoutGroup." + Event.current.type);
g.ResetCursor();
GUIDebugger.LogLayoutGroupEntry(g.rect, g.margin, g.style, g.isVertical);
break;
}
current.layoutGroups.Push(g);
current.topLevel = g;
return g;
}
// The matching end for BeginLayoutGroup
internal static void EndLayoutGroup()
{
if (current.layoutGroups.Count == 0
|| Event.current == null
)
{
Debug.LogError("EndLayoutGroup: BeginLayoutGroup must be called first.");
return;
}
if (Event.current.type != EventType.Layout && Event.current.type != EventType.Used)
GUIDebugger.LogLayoutEndGroup();
current.layoutGroups.Pop();
if (0 < current.layoutGroups.Count)
current.topLevel = (GUILayoutGroup)current.layoutGroups.Peek();
else
current.topLevel = new GUILayoutGroup();
}
// Generic helper - use this when creating a layout group. It will make sure everything is wired up correctly.
internal static GUILayoutGroup BeginLayoutArea(GUIStyle style, Type layoutType)
{
GUILayoutGroup g;
switch (Event.current.type)
{
case EventType.Used:
case EventType.Layout:
g = CreateGUILayoutGroupInstanceOfType(layoutType);
g.style = style;
current.windows.Add(g);
break;
default:
g = current.windows.GetNext() as GUILayoutGroup;
if (g == null)
throw new ArgumentException("GUILayout: Mismatched LayoutGroup." + Event.current.type);
g.ResetCursor();
GUIDebugger.LogLayoutGroupEntry(g.rect, g.margin, g.style, g.isVertical);
break;
}
current.layoutGroups.Push(g);
current.topLevel = g;
return g;
}
// Trampoline for Editor stuff
internal static GUILayoutGroup DoBeginLayoutArea(GUIStyle style, Type layoutType)
{
return BeginLayoutArea(style, layoutType);
}
internal static GUILayoutGroup topLevel => current.topLevel;
public static Rect GetRect(GUIContent content, GUIStyle style) { return DoGetRect(content, style, null); }
// Reserve layout space for a rectangle for displaying some contents with a specific style.
public static Rect GetRect(GUIContent content, GUIStyle style, params GUILayoutOption[] options) { return DoGetRect(content, style, options); }
static Rect DoGetRect(GUIContent content, GUIStyle style, GUILayoutOption[] options)
{
GUIUtility.CheckOnGUI();
switch (Event.current.type)
{
case EventType.Layout:
if (style.isHeightDependantOnWidth)
{
current.topLevel.Add(new GUIWordWrapSizer(style, content, options));
}
else
{
Vector2 sizeConstraints = new Vector2(0, 0);
if (options != null)
{
foreach (var option in options)
{
switch (option.type)
{
case GUILayoutOption.Type.maxHeight:
sizeConstraints.y = (float)option.value;
break;
case GUILayoutOption.Type.maxWidth:
sizeConstraints.x = (float)option.value;
break;
}
}
}
Vector2 size = style.CalcSizeWithConstraints(content, sizeConstraints);
current.topLevel.Add(new GUILayoutEntry(size.x, size.x, size.y, size.y, style, options));
}
return kDummyRect;
case EventType.Used:
return kDummyRect;
default:
var entry = current.topLevel.GetNext();
GUIDebugger.LogLayoutEntry(entry.rect, entry.margin, entry.style);
return entry.rect;
}
}
public static Rect GetRect(float width, float height) { return DoGetRect(width, width, height, height, GUIStyle.none, null); }
public static Rect GetRect(float width, float height, GUIStyle style) {return DoGetRect(width, width, height, height, style, null); }
public static Rect GetRect(float width, float height, params GUILayoutOption[] options) {return DoGetRect(width, width, height, height, GUIStyle.none, options); }
// Reserve layout space for a rectangle with a fixed content area.
public static Rect GetRect(float width, float height, GUIStyle style, params GUILayoutOption[] options)
{return DoGetRect(width, width, height, height, style, options); }
public static Rect GetRect(float minWidth, float maxWidth, float minHeight, float maxHeight)
{ return DoGetRect(minWidth, maxWidth, minHeight, maxHeight, GUIStyle.none, null); }
public static Rect GetRect(float minWidth, float maxWidth, float minHeight, float maxHeight, GUIStyle style)
{ return DoGetRect(minWidth, maxWidth, minHeight, maxHeight, style, null); }
public static Rect GetRect(float minWidth, float maxWidth, float minHeight, float maxHeight, params GUILayoutOption[] options)
{ return DoGetRect(minWidth, maxWidth, minHeight, maxHeight, GUIStyle.none, options); }
// Reserve layout space for a flexible rect.
public static Rect GetRect(float minWidth, float maxWidth, float minHeight, float maxHeight, GUIStyle style, params GUILayoutOption[] options)
{ return DoGetRect(minWidth, maxWidth, minHeight, maxHeight, style, options); }
static Rect DoGetRect(float minWidth, float maxWidth, float minHeight, float maxHeight, GUIStyle style, GUILayoutOption[] options)
{
switch (Event.current.type)
{
case EventType.Layout:
current.topLevel.Add(new GUILayoutEntry(minWidth, maxWidth, minHeight, maxHeight, style, options));
return kDummyRect;
case EventType.Used:
return kDummyRect;
default:
return current.topLevel.GetNext().rect;
}
}
// Get the rectangle last used by GUILayout for a control.
public static Rect GetLastRect()
{
switch (Event.current.type)
{
case EventType.Layout:
case EventType.Used:
return kDummyRect;
default:
return current.topLevel.GetLast();
}
}
public static Rect GetAspectRect(float aspect) { return DoGetAspectRect(aspect, null); }
public static Rect GetAspectRect(float aspect, GUIStyle style) { return DoGetAspectRect(aspect, null); }
public static Rect GetAspectRect(float aspect, params GUILayoutOption[] options) { return DoGetAspectRect(aspect, options); }
// Reserve layout space for a rectangle with a specific aspect ratio.
public static Rect GetAspectRect(float aspect, GUIStyle style, params GUILayoutOption[] options) { return DoGetAspectRect(aspect, options); }
private static Rect DoGetAspectRect(float aspect, GUILayoutOption[] options)
{
switch (Event.current.type)
{
case EventType.Layout:
current.topLevel.Add(new GUIAspectSizer(aspect, options));
return kDummyRect;
case EventType.Used:
return kDummyRect;
default:
return current.topLevel.GetNext().rect;
}
}
// Style used by space elements so we can do special handling of spaces.
internal static GUIStyle spaceStyle
{
get
{
if (s_SpaceStyle == null) s_SpaceStyle = new GUIStyle();
s_SpaceStyle.stretchWidth = false;
return s_SpaceStyle;
}
}
static GUIStyle s_SpaceStyle;
}
}