forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorWindow.cs
More file actions
996 lines (862 loc) · 34.2 KB
/
Copy pathEditorWindow.cs
File metadata and controls
996 lines (862 loc) · 34.2 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using System.Linq;
using System;
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements;
using UnityEngine.CSSLayout;
namespace UnityEditor
{
// For bindings see: EditorWindow.bindings
public partial class EditorWindow : ScriptableObject
{
[SerializeField]
[HideInInspector]
bool m_AutoRepaintOnSceneChange;
[SerializeField]
[HideInInspector]
Vector2 m_MinSize = new Vector2(100, 100);
[SerializeField]
[HideInInspector]
Vector2 m_MaxSize = new Vector2(4000, 4000);
[SerializeField]
[HideInInspector]
internal GUIContent m_TitleContent;
[SerializeField]
[HideInInspector]
int m_DepthBufferBits = 0;
[SerializeField]
[HideInInspector]
internal Rect m_Pos = new Rect(0, 0, 320, 240);
VisualElement m_RootVisualContainer;
internal
VisualElement rootVisualContainer
{
get
{
if (m_RootVisualContainer == null)
{
m_RootVisualContainer = new VisualElement()
{
name = VisualElementUtils.GetUniqueName("rootVisualContainer"),
pickingMode = PickingMode.Ignore, // do not eat events so IMGUI gets them
persistenceKey = "rootVisualContainer"
};
}
return m_RootVisualContainer;
}
}
[SerializeField]
private SerializableJsonDictionary m_PersistentViewDataDictionary;
private bool m_EnableViewDataPersistence;
internal SerializableJsonDictionary viewDataDictionary
{
get
{
// If persistence is disabled, just don't even create the dictionary. Return null.
if (m_EnableViewDataPersistence && m_PersistentViewDataDictionary == null)
{
string editorPrefFileName = this.GetType().ToString();
m_PersistentViewDataDictionary = EditorWindowPersistentViewData.instance[editorPrefFileName];
}
return m_PersistentViewDataDictionary;
}
}
internal void SavePersistentViewData()
{
if (m_EnableViewDataPersistence && m_PersistentViewDataDictionary != null)
{
string editorPrefFileName = this.GetType().ToString();
EditorWindowPersistentViewData.instance.Save(editorPrefFileName, m_PersistentViewDataDictionary);
}
}
internal ISerializableJsonDictionary GetViewDataDictionary()
{
return viewDataDictionary;
}
// TODO: These should be made public when UIElements is no longer experimental.
internal void DisableViewDataPersistence()
{
m_EnableViewDataPersistence = false;
}
internal void ClearPersistentViewData()
{
string editorPrefFileName = this.GetType().ToString();
EditorWindowPersistentViewData.instance.Clear(editorPrefFileName);
DestroyImmediate(m_PersistentViewDataDictionary);
m_PersistentViewDataDictionary = null;
}
// The GameView rect is in GUI space of the view
Rect m_GameViewRect;
Rect m_GameViewClippedRect;
Vector2 m_GameViewTargetSize;
bool m_DontClearBackground;
#pragma warning disable 649
EventInterests m_EventInterests;
bool m_DisableInputEvents;
// Dockarea we're inside.
[NonSerialized]
internal HostView m_Parent;
// Overlay a notification message over the window.
const double kWarningFadeoutWait = 4;
const double kWarningFadeoutTime = 1;
internal GUIContent m_Notification = null;
Vector2 m_NotificationSize;
internal float m_FadeoutTime = 0;
// Mark the beginning area of all popup windows.
public void BeginWindows()
{
EditorGUIInternal.BeginWindowsForward(1, GetInstanceID());
}
// Close a window group started with EditorWindow::ref::BeginWindows
public void EndWindows()
{
GUI.EndWindows();
}
internal virtual void OnResized() {}
// Does the GUI in this editor window want MouseMove events?
public bool wantsMouseMove
{
get
{
return m_EventInterests.wantsMouseMove;
}
set
{
m_EventInterests.wantsMouseMove = value;
MakeParentsSettingsMatchMe();
}
}
// Does the GUI in this editor window want MouseEnter/LeaveWindow events?
public bool wantsMouseEnterLeaveWindow
{
get
{
return m_EventInterests.wantsMouseEnterLeaveWindow;
}
set
{
m_EventInterests.wantsMouseEnterLeaveWindow = value;
MakeParentsSettingsMatchMe();
}
}
internal void CheckForWindowRepaint()
{
double time = EditorApplication.timeSinceStartup;
if (time < m_FadeoutTime)
return;
if (time > m_FadeoutTime + kWarningFadeoutTime)
{
RemoveNotification();
return;
}
Repaint();
}
internal GUIContent GetLocalizedTitleContent()
{
return GetLocalizedTitleContentFromType(GetType());
}
internal static GUIContent GetLocalizedTitleContentFromType(Type t)
{
EditorWindowTitleAttribute attr = GetEditorWindowTitleAttribute(t);
if (attr != null)
{
string iconName = "";
if (!string.IsNullOrEmpty(attr.icon))
iconName = attr.icon;
else if (attr.useTypeNameAsIconName)
iconName = t.ToString();
if (!string.IsNullOrEmpty(iconName))
{
// This should error msg if icon is not found since icon has been explicitly requested by the user
return EditorGUIUtility.TrTextContentWithIcon(attr.title, iconName);
}
return EditorGUIUtility.TrTextContent(attr.title);
}
// Fallback to type name (Do not localize type name)
return new GUIContent(t.ToString());
}
static EditorWindowTitleAttribute GetEditorWindowTitleAttribute(Type t)
{
object[] attrs = t.GetCustomAttributes(true);
foreach (object attr in attrs)
{
Attribute realAttr = (Attribute)attr;
if (realAttr.TypeId == typeof(EditorWindowTitleAttribute))
{
return (EditorWindowTitleAttribute)attr;
}
}
return null;
}
// Show a notification message.
public void ShowNotification(GUIContent notification)
{
m_Notification = new GUIContent(notification);
if (m_FadeoutTime == 0)
EditorApplication.update += CheckForWindowRepaint;
m_FadeoutTime = (float)(EditorApplication.timeSinceStartup + kWarningFadeoutWait);
}
// Stop showing notification message.
public void RemoveNotification()
{
if (m_FadeoutTime == 0)
return;
EditorApplication.update -= CheckForWindowRepaint;
m_Notification = null;
m_FadeoutTime = 0;
}
internal void DrawNotification()
{
EditorStyles.notificationText.CalcMinMaxWidth(m_Notification, out m_NotificationSize.y, out m_NotificationSize.x);
m_NotificationSize.y = EditorStyles.notificationText.CalcHeight(m_Notification, m_NotificationSize.x);
Vector2 warningSize = m_NotificationSize;
float targetWidth = position.width - EditorStyles.notificationText.margin.horizontal;
float targetHeight = position.height - EditorStyles.notificationText.margin.vertical - 20;
// See if we can fit horizontally. If not, rescale down.
if (targetWidth < m_NotificationSize.x)
{
float scale = targetWidth / m_NotificationSize.x;
warningSize.x *= scale;
warningSize.y = EditorStyles.notificationText.CalcHeight(m_Notification, warningSize.x);
}
if (warningSize.y > targetHeight)
warningSize.y = targetHeight;
Rect r = new Rect((position.width - warningSize.x) * .5f, 20 + (position.height - 20 - warningSize.y) * .7f, warningSize.x, warningSize.y);
double time = EditorApplication.timeSinceStartup;
if (time > m_FadeoutTime)
GUI.color = new Color(1, 1, 1, 1 - (float)((time - m_FadeoutTime) / kWarningFadeoutTime));
GUI.Label(r, GUIContent.none, EditorStyles.notificationBackground);
EditorGUI.DoDropShadowLabel(r, m_Notification, EditorStyles.notificationText, .3f);
}
internal bool dontClearBackground
{
get
{
return m_DontClearBackground;
}
set
{
m_DontClearBackground = value;
if (m_Parent && m_Parent.actualView == this)
m_Parent.backgroundValid = false;
}
}
// Does the window automatically repaint whenever the scene has changed?
public bool autoRepaintOnSceneChange
{
get
{
return m_AutoRepaintOnSceneChange;
}
set
{
m_AutoRepaintOnSceneChange = value;
MakeParentsSettingsMatchMe();
}
}
public bool maximized
{
get
{
return WindowLayout.IsMaximized(this);
}
set
{
bool current = WindowLayout.IsMaximized(this);
if (value != current)
{
if (value)
WindowLayout.Maximize(this);
else
WindowLayout.Unmaximize(this);
}
}
}
internal bool hasFocus { get { return m_Parent && m_Parent.actualView == this; } }
internal bool docked { get { return m_Parent != null && m_Parent.window != null && !m_Parent.window.IsNotDocked(); } }
// This property can be used to stop OS events from being sent to the EditorWindow
internal bool disableInputEvents
{
get { return m_DisableInputEvents; }
set
{
if (m_DisableInputEvents == value)
return;
m_DisableInputEvents = value;
MakeParentsSettingsMatchMe();
}
}
// The EditorWindow which currently has keyboard focus (RO)
static public EditorWindow focusedWindow
{
get
{
HostView view = GUIView.focusedView as HostView;
if (view != null)
return view.actualView;
else
return null;
}
}
// The EditorWindow currently under the mouse cursor (RO)
static public EditorWindow mouseOverWindow
{
get
{
HostView view = GUIView.mouseOverView as HostView;
if (view != null)
return view.actualView;
else
return null;
}
}
internal int GetNumTabs()
{
DockArea da = m_Parent as DockArea;
if (da)
{
return da.m_Panes.Count;
}
return 0;
}
internal bool ShowNextTabIfPossible()
{
DockArea da = m_Parent as DockArea;
if (da)
{
int idx = da.m_Panes.IndexOf(this);
idx = (idx + 1) % da.m_Panes.Count;
if (da.selected != idx)
{
da.selected = idx;
da.Repaint();
return true;
}
}
return false;
}
public void ShowTab()
{
DockArea da = m_Parent as DockArea;
if (da)
{
int idx = da.m_Panes.IndexOf(this);
if (da.selected != idx)
da.selected = idx;
}
Repaint();
}
// Moves keyboard focus to this EditorWindow.
public void Focus()
{
if (m_Parent)
{
ShowTab();
m_Parent.Focus();
}
}
internal void MakeParentsSettingsMatchMe()
{
if (!m_Parent || m_Parent.actualView != this)
return;
m_Parent.SetTitle(GetType().FullName);
m_Parent.autoRepaintOnSceneChange = m_AutoRepaintOnSceneChange;
bool parentChanged = m_Parent.depthBufferBits != m_DepthBufferBits;
m_Parent.depthBufferBits = m_DepthBufferBits;
m_Parent.SetInternalGameViewDimensions(m_GameViewRect, m_GameViewClippedRect, m_GameViewTargetSize);
m_Parent.eventInterests = m_EventInterests;
m_Parent.disableInputEvents = m_DisableInputEvents;
Vector2 parentBorderSizes = new Vector2(m_Parent.borderSize.left + m_Parent.borderSize.right, m_Parent.borderSize.top + m_Parent.borderSize.bottom);
m_Parent.SetMinMaxSizes(minSize + parentBorderSizes, maxSize + parentBorderSizes);
if (parentChanged)
m_Parent.RecreateContext();
}
// Show the EditorWindow as a floating utility window.
public void ShowUtility()
{
ShowWithMode(ShowMode.Utility);
}
// Used for popup style windows.
public void ShowPopup()
{
if (m_Parent == null)
{
ContainerWindow cw = ScriptableObject.CreateInstance<ContainerWindow>();
cw.title = titleContent.text;
HostView host = ScriptableObject.CreateInstance<HostView>();
host.actualView = this;
Rect r = m_Parent.borderSize.Add(new Rect(position.x, position.y, position.width, position.height));
// Order is important here: first set rect of container, then assign main view, then apply various settings, then show.
// Otherwise the rect won't be set until first resize happens.
cw.position = r;
cw.rootView = host;
MakeParentsSettingsMatchMe();
cw.ShowPopup();
}
}
internal void ShowWithMode(ShowMode mode)
{
if (m_Parent == null)
{
SavedGUIState oldState = SavedGUIState.Create();
ContainerWindow cw = ScriptableObject.CreateInstance<ContainerWindow>();
cw.title = titleContent.text;
HostView host = ScriptableObject.CreateInstance<HostView>();
host.actualView = this;
Rect r = m_Parent.borderSize.Add(new Rect(position.x, position.y, position.width, position.height));
// Order is important here: first set rect of container, then assign main view, then apply various settings, then show.
// Otherwise the rect won't be set until first resize happens.
cw.position = r;
cw.rootView = host;
MakeParentsSettingsMatchMe();
cw.Show(mode, true, false);
oldState.ApplyAndForget();
}
}
// Show window with dropdown behaviour (e.g. window is closed when it loses focus) and having
public void ShowAsDropDown(Rect buttonRect, Vector2 windowSize)
{
ShowAsDropDown(buttonRect, windowSize, null);
}
internal void ShowAsDropDown(Rect buttonRect, Vector2 windowSize, PopupLocationHelper.PopupLocation[] locationPriorityOrder)
{
ShowAsDropDown(buttonRect, windowSize, locationPriorityOrder, ShowMode.PopupMenu);
}
// Show as drop down list with custom fit to screen callback
// 'buttonRect' is used for displaying the dropdown below that rect if possible otherwise above
// 'windowSize' is used for setting up initial size
// 'locationPriorityOrder' is for manual popup direction, if null it uses default order: down, up, left or right
internal void ShowAsDropDown(Rect buttonRect, Vector2 windowSize, PopupLocationHelper.PopupLocation[] locationPriorityOrder, ShowMode mode)
{
// Setup position before bringing window live (otherwise the dropshadow on Windows will be placed in 0,0 first frame)
position = ShowAsDropDownFitToScreen(buttonRect, windowSize, locationPriorityOrder);
// Show window and focus
ShowWithMode(mode);
// Fit to screen again now that we have a container window
position = ShowAsDropDownFitToScreen(buttonRect, windowSize, locationPriorityOrder);
// Default to none resizable window
minSize = new Vector2(position.width, position.height);
maxSize = new Vector2(position.width, position.height);
// Focus window
if (focusedWindow != this)
Focus();
// Add after unfreezing display because AuxWindowManager.cpp assumes that aux windows are added after we got/lost- focus calls.
m_Parent.AddToAuxWindowList();
// Dropdown windows should not be saved to layout
m_Parent.window.m_DontSaveToLayout = true;
}
internal Rect ShowAsDropDownFitToScreen(Rect buttonRect, Vector2 windowSize, PopupLocationHelper.PopupLocation[] locationPriorityOrder)
{
if (m_Parent == null)
return new Rect(buttonRect.x, buttonRect.yMax, windowSize.x, windowSize.y);
return m_Parent.window.GetDropDownRect(buttonRect, windowSize, windowSize, locationPriorityOrder);
}
public void Show()
{
Show(false);
}
// Show the EditorWindow.
public void Show(bool immediateDisplay)
{
// If somebody called show on us, set up the neccessary structure for us.
if (m_Parent == null)
CreateNewWindowForEditorWindow(this, true, immediateDisplay);
}
// Show the editor window in the auxiliary window.
public void ShowAuxWindow()
{
ShowWithMode(ShowMode.AuxWindow);
// We ensure Focus change before calling AddToAuxWindowList because the
// AuxWindowManager assumes that focus has been changed before a new window is added.
Focus();
m_Parent.AddToAuxWindowList();
}
// Show modal editor window. Other windows will not be accessible until this one is closed.
internal void ShowModal()
{
ShowWithMode(ShowMode.AuxWindow);
SavedGUIState guiState = SavedGUIState.Create();
MakeModal(m_Parent.window);
guiState.ApplyAndForget();
}
// Returns the first EditorWindow of type /t/ which is currently on the screen.
static EditorWindow GetWindowPrivate(System.Type t, bool utility, string title, bool focus)
{
UnityEngine.Object[] wins = Resources.FindObjectsOfTypeAll(t);
EditorWindow win = wins.Length > 0 ? (EditorWindow)(wins[0]) : null;
if (!win)
{
win = ScriptableObject.CreateInstance(t) as EditorWindow;
if (title != null)
win.titleContent = new GUIContent(title);
if (utility)
win.ShowUtility();
else
win.Show();
}
else if (focus)
{
win.Show(); // For some corner cases in saved layouts, the window can be in an unvisible state.
// Since the caller asked for focus, it's fair to assume he wants it always to be visible. (case 586743)
win.Focus();
}
return win;
}
public static T GetWindow<T>() where T : EditorWindow
{
return GetWindow<T>(false, null, true);
}
public static T GetWindow<T>(bool utility) where T : EditorWindow
{
return GetWindow<T>(utility, null, true);
}
public static T GetWindow<T>(bool utility, string title) where T : EditorWindow
{
return GetWindow<T>(utility, title, true);
}
public static T GetWindow<T>(string title) where T : EditorWindow
{
return GetWindow<T>(title, true);
}
public static T GetWindow<T>(string title, bool focus) where T : EditorWindow
{
return GetWindow<T>(false, title, focus);
}
// Returns the first EditorWindow of type /T/ which is currently on the screen.
public static T GetWindow<T>(bool utility, string title, bool focus) where T : EditorWindow
{
return GetWindow(typeof(T), utility, title, focus) as T;
}
public static T GetWindow<T>(params System.Type[] desiredDockNextTo) where T : EditorWindow
{
return GetWindow<T>(null, true, desiredDockNextTo);
}
public static T GetWindow<T>(string title, params System.Type[] desiredDockNextTo) where T : EditorWindow
{
return GetWindow<T>(title, true, desiredDockNextTo);
}
// Returns the first EditorWindow of type /T/ which is currently on the screen.
public static T GetWindow<T>(string title, bool focus, params System.Type[] desiredDockNextTo) where T : EditorWindow
{
var wins = Resources.FindObjectsOfTypeAll(typeof(T)) as T[];
T win = wins.Length > 0 ? wins[0] : null;
//If the window already exists just focus then return it...
if (win != null)
{
if (focus)
win.Focus();
return win;
}
win = CreateInstance<T>();
if (title != null)
win.titleContent = new GUIContent(title);
//Iterate the desired dock next to types...
foreach (var desired in desiredDockNextTo)
{
var windows = ContainerWindow.windows;
foreach (var w in windows)
{
foreach (var view in w.rootView.allChildren)
{
var dockArea = view as DockArea;
if (dockArea == null) continue;
if (dockArea.m_Panes.Any(pane => pane.GetType() == desired))
{
dockArea.AddTab(win);
return win;
}
}
}
}
win.Show();
return win;
}
// Focuses the first found EditorWindow of specified type if it is open.
public static void FocusWindowIfItsOpen(System.Type t)
{
UnityEngine.Object[] wins = Resources.FindObjectsOfTypeAll(t);
EditorWindow win = wins.Length > 0 ? (wins[0] as EditorWindow) : null;
if (win)
win.Focus();
}
// Focuses the first found EditorWindow of type /T/ if it is open.
public static void FocusWindowIfItsOpen<T>() where T : EditorWindow
{
FocusWindowIfItsOpen(typeof(T));
}
internal void RemoveFromDockArea()
{
DockArea da = m_Parent as DockArea;
if (da)
{
da.RemoveTab(this, true);
}
}
// Returns the first EditorWindow of type /t/ which is currently on the screen.
static EditorWindow GetWindowWithRectPrivate(System.Type t, Rect rect, bool utility, string title)
{
UnityEngine.Object[] wins = Resources.FindObjectsOfTypeAll(t);
EditorWindow win = wins.Length > 0 ? (EditorWindow)(wins[0]) : null;
if (!win)
{
win = ScriptableObject.CreateInstance(t) as EditorWindow;
win.minSize = new Vector2(rect.width, rect.height);
win.maxSize = new Vector2(rect.width, rect.height);
win.position = rect;
if (title != null)
win.titleContent = new GUIContent(title);
if (utility)
win.ShowUtility();
else
win.Show();
}
else
win.Focus();
return win;
}
public static T GetWindowWithRect<T>(Rect rect) where T : EditorWindow
{
return GetWindowWithRect<T>(rect, false, null, true);
}
public static T GetWindowWithRect<T>(Rect rect, bool utility) where T : EditorWindow
{
return GetWindowWithRect<T>(rect, utility, null, true);
}
public static T GetWindowWithRect<T>(Rect rect, bool utility, string title) where T : EditorWindow
{
return GetWindowWithRect<T>(rect, utility, title, true);
}
// Returns the first EditorWindow of type /t/ which is currently on the screen.
public static T GetWindowWithRect<T>(Rect rect, bool utility, string title, bool focus) where T : EditorWindow
{
UnityEngine.Object[] windows = Resources.FindObjectsOfTypeAll(typeof(T));
T window;
if (windows.Length > 0)
{
window = (T)windows[0];
if (focus)
window.Focus();
}
else
{
window = ScriptableObject.CreateInstance<T>();
window.minSize = new Vector2(rect.width, rect.height);
window.maxSize = new Vector2(rect.width, rect.height);
window.position = rect;
if (title != null)
window.titleContent = new GUIContent(title);
if (utility)
window.ShowUtility();
else
window.Show();
}
return window;
}
internal static T GetWindowDontShow<T>() where T : EditorWindow
{
UnityEngine.Object[] windows = Resources.FindObjectsOfTypeAll(typeof(T));
return (windows.Length > 0) ? (T)windows[0] : ScriptableObject.CreateInstance<T>();
}
// Close the editor window.
public void Close()
{
// Ensure to restore normal workspace before destroying. Fix case 406657.
if (WindowLayout.IsMaximized(this))
WindowLayout.Unmaximize(this);
DockArea da = m_Parent as DockArea;
if (da)
{
da.RemoveTab(this, true);
}
else
{
m_Parent.window.Close();
}
UnityEngine.Object.DestroyImmediate(this, true);
}
// Make the window repaint.
public void Repaint()
{
if (m_Parent && m_Parent.actualView == this)
{
m_Parent.Repaint();
}
}
internal void RepaintImmediately()
{
if (m_Parent && m_Parent.actualView == this)
m_Parent.RepaintImmediately();
}
// the minimum size of this window
public Vector2 minSize
{
get
{
return m_MinSize;
}
set
{
m_MinSize = value; MakeParentsSettingsMatchMe();
}
}
// the maximum size of this window
public Vector2 maxSize
{
get
{
return m_MaxSize;
}
set
{
m_MaxSize = value;
MakeParentsSettingsMatchMe();
}
}
// The title of this window (legacy)
[System.Obsolete("Use titleContent instead (it supports setting a title icon as well).")]
public string title
{
get
{
return titleContent.text;
}
set
{
titleContent = EditorGUIUtility.TextContent(value);
}
}
public GUIContent titleContent
{
get
{
return m_TitleContent ?? (m_TitleContent = new GUIContent()); // Ensure m_TitleContent is not null (so we can prevent null checks)
}
set
{
m_TitleContent = value;
if (m_TitleContent != null && m_Parent && m_Parent.window && m_Parent.window.rootView == m_Parent)
m_Parent.window.title = m_TitleContent.text;
}
}
public int depthBufferBits
{
get { return m_DepthBufferBits; }
set { m_DepthBufferBits = value; }
}
internal void SetParentGameViewDimensions(Rect rect, Rect clippedRect, Vector2 targetSize)
{
m_GameViewRect = rect;
m_GameViewClippedRect = clippedRect;
m_GameViewTargetSize = targetSize;
m_Parent.SetInternalGameViewDimensions(m_GameViewRect, m_GameViewClippedRect, m_GameViewTargetSize);
}
[Obsolete("AA is not supported on EditorWindows", false)]
public int antiAlias
{
get { return 1; }
set {}
}
// The position of the window in screen space.
public Rect position
{
get
{
return m_Pos;
}
set
{
m_Pos = value;
// We're setting the position of this editorWindow.
// Only handle this is we have a parent. (unless we're just getting set up)
if (m_Parent)
{
// Figure out if we're the only window here. If we are not, we need to undock us. If we are not, we should just
// move the ContainerWindow that we're inside
DockArea da = m_Parent as DockArea;
if (!da)
{
m_Parent.window.position = value;
}
else if (da.parent && da.m_Panes.Count == 1 && !da.parent.parent) // We should have a DockArea, then a splitView, then null
{
var newPosition = da.borderSize.Add(value);
if (da.background != null)
{
newPosition.y -= da.background.margin.top;
}
da.window.position = newPosition;
}
else
{
// We're docked in a deeper hierarchy, so we need to undock us
da.RemoveTab(this);
// and then create a new window for us...
CreateNewWindowForEditorWindow(this, true, true);
}
}
}
}
// Sends an Event to a window.
public bool SendEvent(Event e)
{
return m_Parent.SendEvent(e);
}
public EditorWindow()
{
m_EnableViewDataPersistence = true;
titleContent.text = GetType().ToString();
}
private void __internalAwake()
{
hideFlags = HideFlags.DontSave; // Can't be HideAndDontSave, as that would make scriptable wizard GUI be disabled
}
// Internal stuff:
// Helper to show this EditorWindow
internal static void CreateNewWindowForEditorWindow(EditorWindow window, bool loadPosition, bool showImmediately)
{
CreateNewWindowForEditorWindow(window, new Vector2(window.position.x, window.position.y), loadPosition, showImmediately);
}
internal static void CreateNewWindowForEditorWindow(EditorWindow window, Vector2 screenPosition, bool loadPosition, bool showImmediately)
{
ContainerWindow cw = ScriptableObject.CreateInstance<ContainerWindow>();
SplitView sw = ScriptableObject.CreateInstance<SplitView>();
cw.rootView = sw;
DockArea da = ScriptableObject.CreateInstance<DockArea>();
sw.AddChild(da);
da.AddTab(window);
Rect r = window.m_Parent.borderSize.Add(new Rect(screenPosition.x, screenPosition.y, window.position.width, window.position.height));
cw.position = r;
sw.position = new Rect(0, 0, r.width, r.height);
window.MakeParentsSettingsMatchMe();
cw.Show(ShowMode.NormalWindow, loadPosition, showImmediately);
//Need this, as show my change the size of the window, due to screen constraints
cw.OnResize();
}
// This is such a hack, but will do for now
[ContextMenu("Add Scene")]
internal void AddSceneTab() {}
[ContextMenu("Add Game")]
internal void AddGameTab() {}
}
[AttributeUsage(AttributeTargets.Class)]
internal class EditorWindowTitleAttribute : System.Attribute
{
public string title { get; set; }
public string icon { get; set; }
public bool useTypeNameAsIconName { get; set; }
}
// makes UIElements opt-in while it's experimental, as a using statement is necessary to call this method
namespace Experimental.UIElements
{
public static class UIElementsEntryPoint
{
public static VisualElement GetRootVisualContainer(this EditorWindow window)
{
return window.rootVisualContainer;
}
}
}
} //namespace