-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathWindowBase.cs
252 lines (221 loc) · 8.98 KB
/
WindowBase.cs
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
using System;
using System.Collections;
using RuntimeUnityEditor.Core.Utils;
using RuntimeUnityEditor.Core.Utils.Abstractions;
using UnityEngine;
namespace RuntimeUnityEditor.Core
{
/// <summary>
/// Feature for use with RuntimeUnityEditor that has a GUILayout window. Custom windows can be added with <see cref="RuntimeUnityEditorCore.AddFeature"/>.
/// Consider using <see cref="Window{T}"/> instead of the bare interface.
/// </summary>
public interface IWindow : IFeature
{
/// <summary>
/// Title of the window shown in the title bar and messages related to the window.
/// </summary>
string Title { get; set; }
/// <summary>
/// ID of the GUILayout window. Set to a unique value automatically during initialization.
/// </summary>
int WindowId { get; set; }
/// <summary>
/// Size and position of the window, including borders and title bar.
/// </summary>
Rect WindowRect { get; set; }
/// <summary>
/// Minimum size of the window (width, height).
/// </summary>
Vector2 MinimumSize { get; set; }
/// <summary>
/// Default position of this window.
/// </summary>
ScreenPartition DefaultScreenPosition { get; }
}
/// <summary>
/// Base implementation of <see cref="T:RuntimeUnityEditor.Core.IWindow" />.
/// <typeparamref name="T" /> should be your derived class's Type, e.g. <code>public class MyWindow : Window<MyWindow></code>
/// </summary>
/// <inheritdoc cref="IWindow" />
public abstract class Window<T> : FeatureBase<T>, IWindow where T : Window<T>
{
/// <summary>
/// Default width of tooltips shown inside windows.
/// </summary>
protected const int DefaultTooltipWidth = 400;
/// <summary>
/// Width of tooltips shown inside this window.
/// </summary>
public int TooltipWidth { get; set; } = DefaultTooltipWidth;
// ReSharper disable StaticMemberInGenericType
private static GUIStyle _tooltipStyle;
private static GUIContent _tooltipContent;
private static Texture2D _tooltipBackground;
// ReSharper restore StaticMemberInGenericType
private bool _canShow;
private Rect _initialWindowRect;
private InitSettings.Setting<Rect> _windowRect;
/// <summary>
/// Create a new window instance, should only ever be called once.
/// </summary>
protected Window()
{
DisplayType = FeatureDisplayType.Window;
SettingCategory = "Windows";
}
/// <inheritdoc cref="FeatureBase{T}.AfterInitialized"/>
protected override void AfterInitialized(InitSettings initSettings)
{
base.AfterInitialized(initSettings);
WindowId = base.GetHashCode();
_windowRect = initSettings.RegisterSetting(SettingCategory, DisplayName + " window size", _initialWindowRect, string.Empty);
}
/// <inheritdoc cref="FeatureBase{T}.DisplayName"/>
public override string DisplayName
{
get => _displayName ?? (_displayName = Title ?? base.DisplayName);
set => _displayName = value;
}
/// <inheritdoc cref="FeatureBase{T}.OnGUI"/>
protected override void OnGUI()
{
if (!_canShow) return;
var title = Title;
#if DEBUG
title = $"{title} {WindowRect}";
#endif
WindowRect = GUILayout.Window(WindowId, WindowRect, (GUI.WindowFunction)DrawContentsInt, title);
if (WindowRect.width < MinimumSize.x)
{
var rect = WindowRect;
rect.width = MinimumSize.x;
WindowRect = rect;
}
if (WindowRect.height < MinimumSize.y)
{
var rect = WindowRect;
rect.height = MinimumSize.y;
WindowRect = rect;
}
}
private void DrawContentsInt(int id)
{
int visibleAreaSize = GUI.skin.window.border.top - 4;// 10;
if (GUI.Button(new Rect(WindowRect.width - visibleAreaSize - 2, 2, visibleAreaSize, visibleAreaSize), "X"))
{
Enabled = false;
return;
}
try
{
DrawContents();
DrawTooltip(WindowRect);
}
catch (Exception ex)
{
// Ignore mismatch exceptions caused by virtual lists, there will be an unity error shown anyways
if (!ex.Message.Contains("GUILayout"))
RuntimeUnityEditorCore.Logger.Log(LogLevel.Error, $"[{Title}] GUI crash: {ex}");
}
WindowRect = IMGUIUtils.DragResizeEat(id, WindowRect);
}
private void DrawTooltip(Rect area)
{
if (!string.IsNullOrEmpty(GUI.tooltip))
{
if (_tooltipBackground == null)
{
_tooltipBackground = new Texture2D(1, 1, TextureFormat.ARGB32, false);
_tooltipBackground.SetPixel(0, 0, Color.black);
_tooltipBackground.Apply();
_tooltipStyle = new GUIStyle
{
normal = new GUIStyleState { textColor = Color.white, background = _tooltipBackground },
wordWrap = true,
alignment = TextAnchor.MiddleCenter
};
_tooltipContent = new GUIContent();
}
_tooltipContent.text = GUI.tooltip;
var tooltipWidth = Mathf.Min(TooltipWidth, WindowRect.width);
var height = _tooltipStyle.CalcHeight(_tooltipContent, tooltipWidth) + 10;
var currentEvent = Event.current;
var x = currentEvent.mousePosition.x + tooltipWidth > area.width
? area.width - tooltipWidth
: currentEvent.mousePosition.x;
var y = currentEvent.mousePosition.y + 25 + height > area.height
? currentEvent.mousePosition.y - height
: currentEvent.mousePosition.y + 25;
GUI.Box(new Rect(x, y, tooltipWidth, height), GUI.tooltip, _tooltipStyle);
}
}
/// <inheritdoc cref="FeatureBase{T}.OnVisibleChanged"/>
protected override void OnVisibleChanged(bool visible)
{
// If the taskbar didn't have a chance to initialize yet, wait for a frame. Necessary to calculate free screen space.
if (visible && Taskbar.Instance.Height == 0)
{
// todo more efficient way?
IEnumerator DelayedVisible()
{
yield return null;
base.OnVisibleChanged(Visible);
}
RuntimeUnityEditorCore.PluginObject.AbstractStartCoroutine(DelayedVisible());
}
else
{
base.OnVisibleChanged(visible);
}
}
/// <inheritdoc cref="FeatureBase{T}.VisibleChanged"/>
protected override void VisibleChanged(bool visible)
{
if (visible)
{
if (!WindowManager.IsWindowRectValid(this))
ResetWindowRect();
_canShow = true;
}
}
/// <summary>
/// Discard current window size and position, and set the default ones for this window.
/// </summary>
public void ResetWindowRect()
{
WindowManager.ResetWindowRect(this);
}
/// <summary>
/// Draw contents of the window.
/// This runs inside of <see cref="GUILayout.Window(int,UnityEngine.Rect,UnityEngine.GUI.WindowFunction,string,UnityEngine.GUILayoutOption[])"/>
/// so all <see cref="GUILayout"/> methods can be used to construct the interface.
/// </summary>
protected abstract void DrawContents();
/// <inheritdoc cref="IWindow.Title"/>
public virtual string Title { get; set; }
/// <inheritdoc cref="IWindow.WindowId"/>
public int WindowId { get; set; }
/// <inheritdoc cref="IWindow.WindowRect"/>
public virtual Rect WindowRect
{
get
{
if (_windowRect != null)
return _windowRect.Value;
else
return _initialWindowRect;
}
set
{
if (_windowRect != null)
_windowRect.Value = value;
else
_initialWindowRect = value;
}
}
/// <inheritdoc cref="IWindow.MinimumSize"/>
public Vector2 MinimumSize { get; set; } = new Vector2(100, 100);
/// <inheritdoc cref="IWindow.DefaultScreenPosition" />
public ScreenPartition DefaultScreenPosition { get; protected set; } = ScreenPartition.Default;
}
}