-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathContainerWindow.cs
More file actions
466 lines (389 loc) · 16 KB
/
ContainerWindow.cs
File metadata and controls
466 lines (389 loc) · 16 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
// 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 System.Collections.Generic;
using System.Runtime.InteropServices;
namespace UnityEditor
{
[StructLayout(LayoutKind.Sequential)]
internal partial class ContainerWindow : ScriptableObject
{
[SerializeField] MonoReloadableIntPtr m_WindowPtr;
[SerializeField] Rect m_PixelRect;
[SerializeField] int m_ShowMode;
[SerializeField] string m_Title = "";
[SerializeField, UnityEngine.Serialization.FormerlySerializedAs("m_MainView")] View m_RootView;
[SerializeField] Vector2 m_MinSize = new Vector2(120, 80);
[SerializeField] Vector2 m_MaxSize = new Vector2(4000, 4000);
internal bool m_DontSaveToLayout = false;
const float kBorderSize = 4;
const float kTitleHeight = 24;
private int m_ButtonCount;
private float m_TitleBarWidth;
// Fit a container window to the screen.
static internal bool macEditor { get { return Application.platform == RuntimePlatform.OSXEditor; } }
const float kButtonWidth = 13, kButtonHeight = 13, kButtonSpacing = 3, kButtonTop = 0;
private static class Styles
{
// Title Bar Buttons (Non)
public static GUIStyle buttonClose = macEditor ? "WinBtnCloseMac" : "WinBtnClose";
public static GUIStyle buttonMin = macEditor ? "WinBtnMinMac" : "WinBtnClose";
public static GUIStyle buttonMax = macEditor ? "WinBtnMaxMac" : "WinBtnMax";
// Title Bar Button when window is not focused (OSX only)
public static GUIStyle buttonInactive = "WinBtnInactiveMac";
}
public ContainerWindow()
{
m_PixelRect = new Rect(0, 0, 400, 300);
}
internal void __internalAwake()
{
hideFlags = HideFlags.DontSave;
}
internal ShowMode showMode { get { return (ShowMode)m_ShowMode; } }
internal static bool IsPopup(ShowMode mode)
{
return (ShowMode.PopupMenu == mode);
}
internal bool isPopup { get { return IsPopup((ShowMode)m_ShowMode); } }
internal void ShowPopup()
{
ShowPopupWithMode(ShowMode.PopupMenu);
}
internal void ShowTooltip()
{
ShowPopupWithMode(ShowMode.Tooltip);
}
internal void ShowPopupWithMode(ShowMode mode)
{
m_ShowMode = (int)mode;
Internal_Show(m_PixelRect, m_ShowMode, m_MinSize, m_MaxSize);
if (m_RootView)
m_RootView.SetWindowRecurse(this);
Internal_SetTitle(m_Title);
Save();
Internal_BringLiveAfterCreation(false, false);
}
static Color skinBackgroundColor
{
get
{
return EditorGUIUtility.isProSkin ? Color.gray.RGBMultiplied(0.3f).AlphaMultiplied(0.5f) : Color.gray.AlphaMultiplied(0.32f);
}
}
// Show the editor window.
public void Show(ShowMode showMode, bool loadPosition, bool displayImmediately)
{
if (showMode == ShowMode.AuxWindow)
showMode = ShowMode.Utility;
if (showMode == ShowMode.Utility || IsPopup(showMode))
m_DontSaveToLayout = true;
m_ShowMode = (int)showMode;
// Load previous position/size
if (!isPopup)
Load(loadPosition);
Internal_Show(m_PixelRect, m_ShowMode, m_MinSize, m_MaxSize);
// Tell the mainview its now in this window (quick hack to get platform-specific code to move its views to the right window)
if (m_RootView)
m_RootView.SetWindowRecurse(this);
Internal_SetTitle(m_Title);
SetBackgroundColor(skinBackgroundColor);
Internal_BringLiveAfterCreation(displayImmediately, true);
// Window could be killed by now in user callbacks...
if (!this)
return;
// Fit window to screen - needs to be done after bringing the window live
position = FitWindowRectToScreen(m_PixelRect, true, false);
rootView.position = new Rect(0, 0, m_PixelRect.width, m_PixelRect.height);
rootView.Reflow();
// save position right away
Save();
}
public void OnEnable()
{
if (m_RootView)
m_RootView.Initialize(this);
SetBackgroundColor(skinBackgroundColor);
}
public void SetMinMaxSizes(Vector2 min, Vector2 max)
{
m_MinSize = min;
m_MaxSize = max;
Rect r = position;
Rect r2 = r;
r2.width = Mathf.Clamp(r.width, min.x, max.x);
r2.height = Mathf.Clamp(r.height, min.y, max.y);
if (r2.width != r.width || r2.height != r.height)
position = r2;
Internal_SetMinMaxSizes(min, max);
}
internal void InternalCloseWindow()
{
Save();
if (m_RootView)
{
if (m_RootView is GUIView)
((GUIView)m_RootView).RemoveFromAuxWindowList();
DestroyImmediate(m_RootView, true);
m_RootView = null;
}
DestroyImmediate(this, true);
}
public void Close()
{
Save();
// Guard against destroy window or window in the process of being destroyed.
if (this && m_WindowPtr.m_IntPtr != IntPtr.Zero)
InternalClose();
DestroyImmediate(this, true);
}
internal bool IsNotDocked()
{
return ( // hallelujah
(m_ShowMode == (int)ShowMode.Utility || m_ShowMode == (int)ShowMode.AuxWindow) ||
(rootView is SplitView &&
rootView.children.Length == 1 &&
rootView.children[0] is DockArea &&
((DockArea)rootView.children[0]).m_Panes.Count == 1)
);
}
private string NotDockedWindowID()
{
if (IsNotDocked())
{
HostView v = rootView as HostView;
if (v == null)
{
if (rootView is SplitView)
v = (HostView)rootView.children[0];
else
return rootView.GetType().ToString();
}
return (m_ShowMode == (int)ShowMode.Utility || m_ShowMode == (int)ShowMode.AuxWindow) ? v.actualView.GetType().ToString()
: ((DockArea)rootView.children[0]).m_Panes[0].GetType().ToString();
}
return null;
}
public void Save()
{
// only save it if its not docked and its not the MainWindow
if ((m_ShowMode != (int)ShowMode.MainWindow) && IsNotDocked() && !IsZoomed())
{
string ID = NotDockedWindowID();
// save position/size
EditorPrefs.SetFloat(ID + "x", m_PixelRect.x);
EditorPrefs.SetFloat(ID + "y", m_PixelRect.y);
EditorPrefs.SetFloat(ID + "w", m_PixelRect.width);
EditorPrefs.SetFloat(ID + "h", m_PixelRect.height);
}
}
private void Load(bool loadPosition)
{
if ((m_ShowMode != (int)ShowMode.MainWindow) && IsNotDocked())
{
string ID = NotDockedWindowID();
// get position/size
Rect p = m_PixelRect;
if (loadPosition)
{
p.x = EditorPrefs.GetFloat(ID + "x", m_PixelRect.x);
p.y = EditorPrefs.GetFloat(ID + "y", m_PixelRect.y);
}
p.width = Mathf.Max(EditorPrefs.GetFloat(ID + "w", m_PixelRect.width), m_MinSize.x);
p.width = Mathf.Min(p.width, m_MaxSize.x);
p.height = Mathf.Max(EditorPrefs.GetFloat(ID + "h", m_PixelRect.height), m_MinSize.y);
p.height = Mathf.Min(p.height, m_MaxSize.y);
m_PixelRect = p;
}
}
internal void OnResize()
{
if (rootView == null)
return;
rootView.position = new Rect(0, 0, position.width, position.height);
// save position
Save();
}
// The title of the window
public string title
{
get { return m_Title; }
set { m_Title = value; Internal_SetTitle(value); }
}
// Array of all visible ContainerWindows, from frontmost to last
static List<ContainerWindow> s_AllWindows = new List<ContainerWindow>();
public static ContainerWindow[] windows
{
get
{
s_AllWindows.Clear();
GetOrderedWindowList();
return s_AllWindows.ToArray();
}
}
internal void AddToWindowList()
{
s_AllWindows.Add(this);
}
// TODO: Handle title bar height and other things
public Vector2 WindowToScreenPoint(Vector2 windowPoint)
{
Vector2 hmm = Internal_GetTopleftScreenPosition();
return windowPoint + hmm;// + new Vector2 (position.x, position.y) + hmm;
}
public View rootView
{
get { return m_RootView; }
set
{
m_RootView = value;
m_RootView.SetWindowRecurse(this);
m_RootView.position = new Rect(0, 0, position.width, position.height);
m_MinSize = value.minSize;
m_MaxSize = value.maxSize;
}
}
public SplitView rootSplitView
{
get
{
if (m_ShowMode == (int)ShowMode.MainWindow && rootView && rootView.children.Length == 3)
return rootView.children[1] as SplitView;
else
return rootView as SplitView;
}
}
internal string DebugHierarchy()
{
return rootView.DebugHierarchy(0);
}
internal Rect GetDropDownRect(Rect buttonRect, Vector2 minSize, Vector2 maxSize, PopupLocation[] locationPriorityOrder)
{
return PopupLocationHelper.GetDropDownRect(buttonRect, minSize, maxSize, this, locationPriorityOrder);
}
internal Rect GetDropDownRect(Rect buttonRect, Vector2 minSize, Vector2 maxSize)
{
return PopupLocationHelper.GetDropDownRect(buttonRect, minSize, maxSize, this);
}
internal Rect FitPopupWindowRectToScreen(Rect rect, float minimumHeight)
{
const float maxHeight = 900;
float spaceFromBottom = 0f;
if (Application.platform == RuntimePlatform.OSXEditor)
spaceFromBottom = 10f;
float minHeight = minimumHeight + spaceFromBottom;
Rect p = rect;
p.height = Mathf.Min(p.height, maxHeight);
p.height += spaceFromBottom;
p = FitWindowRectToScreen(p, true, true);
float newHeight = Mathf.Max(p.yMax - rect.y, minHeight);
p.y = p.yMax - newHeight;
p.height = newHeight - spaceFromBottom;
return p;
}
public void HandleWindowDecorationEnd(Rect windowPosition)
{
// No Op
}
public void HandleWindowDecorationStart(Rect windowPosition)
{
bool hasTitleBar = (windowPosition.y == 0 && showMode != ShowMode.Utility && !isPopup);
if (!hasTitleBar)
return;
bool hasWindowButtons = Mathf.Abs(windowPosition.xMax - position.width) < 2;
if (hasWindowButtons)
{
GUIStyle close = Styles.buttonClose;
GUIStyle min = Styles.buttonMin;
GUIStyle max = Styles.buttonMax;
if (macEditor && (GUIView.focusedView == null || GUIView.focusedView.window != this))
close = min = max = Styles.buttonInactive;
BeginTitleBarButtons(windowPosition);
if (TitleBarButton(close))
{
Close();
GUIUtility.ExitGUI();
}
if (macEditor && TitleBarButton(min))
{
Minimize();
GUIUtility.ExitGUI();
}
if (TitleBarButton(max))
ToggleMaximize();
}
DragTitleBar(new Rect(0, 0, position.width, kTitleHeight));
}
private void BeginTitleBarButtons(Rect windowPosition)
{
m_ButtonCount = 0;
m_TitleBarWidth = windowPosition.width;
}
private bool TitleBarButton(GUIStyle style)
{
var buttonRect = new Rect(m_TitleBarWidth - kButtonWidth * ++m_ButtonCount - kBorderSize, kButtonTop, kButtonWidth, kButtonHeight);
return GUI.Button(buttonRect, GUIContent.none, style);
}
// Snapping windows
static Vector2 s_LastDragMousePos;
float startDragDpi;
private void DragTitleBar(Rect titleBarRect)
{
int id = GUIUtility.GetControlID(FocusType.Passive);
Event evt = Event.current;
switch (evt.GetTypeForControl(id))
{
case EventType.Repaint:
EditorGUIUtility.AddCursorRect(titleBarRect, MouseCursor.Arrow);
break;
case EventType.MouseDown:
// If the mouse is inside the title bar rect, we say that we're the hot control
if (titleBarRect.Contains(evt.mousePosition) && GUIUtility.hotControl == 0 && evt.button == 0)
{
GUIUtility.hotControl = id;
Event.current.Use();
s_LastDragMousePos = evt.mousePosition;
startDragDpi = GUIUtility.pixelsPerPoint;
Unsupported.SetAllowCursorLock(false, Unsupported.DisallowCursorLockReasons.SizeMove);
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id)
{
GUIUtility.hotControl = 0;
Event.current.Use();
Unsupported.SetAllowCursorLock(true, Unsupported.DisallowCursorLockReasons.SizeMove);
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == id)
{
Vector2 mousePos = evt.mousePosition;
if (startDragDpi != GUIUtility.pixelsPerPoint)
{
// We ignore this mouse event when changing screens in multi monitor setups with
// different dpi scalings as funky things might/will happen
startDragDpi = GUIUtility.pixelsPerPoint;
s_LastDragMousePos = mousePos;
}
else
{
Vector2 movement = mousePos - s_LastDragMousePos;
float minimumDelta = 1.0f / GUIUtility.pixelsPerPoint;
if (Mathf.Abs(movement.x) >= minimumDelta || Mathf.Abs(movement.y) >= minimumDelta)
{
Rect dragPosition = position;
dragPosition.x += movement.x;
dragPosition.y += movement.y;
position = dragPosition;
GUI.changed = true;
}
}
}
break;
}
}
}
} //namespace