forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternalEditorGUI.cs
More file actions
322 lines (285 loc) · 13.5 KB
/
Copy pathInternalEditorGUI.cs
File metadata and controls
322 lines (285 loc) · 13.5 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
// NOTE:
// This file should only contain internal functions of the EditorGUI class
//
namespace UnityEditor
{
public sealed partial class EditorGUI
{
static int s_DropdownButtonHash = "DropdownButton".GetHashCode();
static int s_MouseDeltaReaderHash = "MouseDeltaReader".GetHashCode();
internal static bool Button(Rect position, GUIContent content)
{
return Button(position, content, EditorStyles.miniButton);
}
// We need an EditorGUI.Button that only reacts to left mouse button (GUI.Button reacts to all mouse buttons), so we
// can handle context click events for button areas etc.
internal static bool Button(Rect position, GUIContent content, GUIStyle style)
{
Event evt = Event.current;
switch (evt.type)
{
case EventType.MouseDown:
case EventType.MouseUp:
if (evt.button != 0)
return false; // ignore all input from other buttons than the left mouse button
break;
}
return GUI.Button(position, content, style);
}
// Button used for the icon selector where an icon can be selected by pressing and dragging the
// mouse cursor around to select different icons
internal static bool IconButton(int id, Rect position, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
switch (Event.current.GetTypeForControl(id))
{
case EventType.MouseDown:
// If the mouse is inside the button, we say that we're the hot control
if (position.Contains(Event.current.mousePosition))
{
GUIUtility.hotControl = id;
Event.current.Use();
return true;
}
return false;
case EventType.MouseUp:
if (GUIUtility.hotControl == id)
{
GUIUtility.hotControl = 0;
// If we got the mousedown, the mouseup is ours as well
// (no matter if the click was in the button or not)
Event.current.Use();
// But we only return true if the button was actually clicked
return position.Contains(Event.current.mousePosition);
}
return false;
case EventType.MouseDrag:
if (position.Contains(Event.current.mousePosition))
{
GUIUtility.hotControl = id;
Event.current.Use();
return true;
}
break;
case EventType.Repaint:
style.Draw(position, content, id);
break;
}
return false;
}
internal static float WidthResizer(Rect position, float width, float minWidth, float maxWidth)
{
bool hasControl;
return Resizer.Resize(position, width, minWidth, maxWidth, true, out hasControl);
}
internal static float WidthResizer(Rect position, float width, float minWidth, float maxWidth, out bool hasControl)
{
return Resizer.Resize(position, width, minWidth, maxWidth, true, out hasControl);
}
internal static float HeightResizer(Rect position, float height, float minHeight, float maxHeight)
{
bool hasControl;
return Resizer.Resize(position, height, minHeight, maxHeight, false, out hasControl);
}
internal static float HeightResizer(Rect position, float height, float minHeight, float maxHeight, out bool hasControl)
{
return Resizer.Resize(position, height, minHeight, maxHeight, false, out hasControl);
}
static class Resizer
{
static float s_StartSize;
static Vector2 s_MouseDeltaReaderStartPos;
internal static float Resize(Rect position, float size, float minSize, float maxSize, bool horizontal, out bool hasControl)
{
int id = EditorGUIUtility.GetControlID(s_MouseDeltaReaderHash, FocusType.Passive, position);
Event evt = Event.current;
switch (evt.GetTypeForControl(id))
{
case EventType.MouseDown:
if (GUIUtility.hotControl == 0 && position.Contains(evt.mousePosition) && evt.button == 0)
{
GUIUtility.hotControl = id;
GUIUtility.keyboardControl = 0;
s_MouseDeltaReaderStartPos = GUIClip.Unclip(evt.mousePosition); // We unclip to screenspace to prevent being affected by scrollviews
s_StartSize = size;
evt.Use();
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == id)
{
evt.Use();
Vector2 screenPos = GUIClip.Unclip(evt.mousePosition); // We unclip to screenspace to prevent being affected by scrollviews
float delta = horizontal ? (screenPos - s_MouseDeltaReaderStartPos).x : (screenPos - s_MouseDeltaReaderStartPos).y;
float newSize = s_StartSize + delta;
if (newSize >= minSize && newSize <= maxSize)
size = newSize;
else
size = Mathf.Clamp(newSize, minSize, maxSize);
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id && evt.button == 0)
{
GUIUtility.hotControl = 0;
evt.Use();
}
break;
case EventType.Repaint:
var cursor = horizontal ? MouseCursor.SplitResizeLeftRight : MouseCursor.SplitResizeUpDown;
EditorGUIUtility.AddCursorRect(position, cursor, id);
break;
}
hasControl = GUIUtility.hotControl == id;
return size;
}
}
// Get mouse delta values in different situations when click-dragging
static Vector2 s_MouseDeltaReaderLastPos;
internal static Vector2 MouseDeltaReader(Rect position, bool activated)
{
int id = EditorGUIUtility.GetControlID(s_MouseDeltaReaderHash, FocusType.Passive, position);
Event evt = Event.current;
switch (evt.GetTypeForControl(id))
{
case EventType.MouseDown:
if (activated && GUIUtility.hotControl == 0 && position.Contains(evt.mousePosition) && evt.button == 0)
{
GUIUtility.hotControl = id;
GUIUtility.keyboardControl = 0;
s_MouseDeltaReaderLastPos = GUIClip.Unclip(evt.mousePosition); // We unclip to screenspace to prevent being affected by scrollviews
evt.Use();
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == id)
{
Vector2 screenPos = GUIClip.Unclip(evt.mousePosition); // We unclip to screenspace to prevent being affected by scrollviews
Vector2 delta = (screenPos - s_MouseDeltaReaderLastPos);
s_MouseDeltaReaderLastPos = screenPos;
evt.Use();
return delta;
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id && evt.button == 0)
{
GUIUtility.hotControl = 0;
evt.Use();
}
break;
}
return Vector2.zero;
}
// Shows an active button and a triangle button on the right, which expands the dropdown list
// Returns true if button was activated, returns false if the the dropdown button was activated or the button was not clicked.
internal static bool ButtonWithDropdownList(string buttonName, string[] buttonNames, GenericMenu.MenuFunction2 callback, params GUILayoutOption[] options)
{
var content = EditorGUIUtility.TempContent(buttonName);
return ButtonWithDropdownList(content, buttonNames, callback, options);
}
// Shows an active button and a triangle button on the right, which expands the dropdown list
// Returns true if button was activated, returns false if the the dropdown button was activated or the button was not clicked.
internal static bool ButtonWithDropdownList(GUIContent content, string[] buttonNames, GenericMenu.MenuFunction2 callback, params GUILayoutOption[] options)
{
var rect = GUILayoutUtility.GetRect(content, EditorStyles.dropDownList, options);
var dropDownRect = rect;
const float kDropDownButtonWidth = 20f;
dropDownRect.xMin = dropDownRect.xMax - kDropDownButtonWidth;
if (Event.current.type == EventType.MouseDown && dropDownRect.Contains(Event.current.mousePosition))
{
var menu = new GenericMenu();
for (int i = 0; i != buttonNames.Length; i++)
menu.AddItem(new GUIContent(buttonNames[i]), false, callback, i);
menu.DropDown(rect);
Event.current.Use();
return false;
}
return GUI.Button(rect, content, EditorStyles.dropDownList);
}
internal static void GameViewSizePopup(Rect buttonRect, GameViewSizeGroupType groupType, int selectedIndex, IGameViewSizeMenuUser gameView, GUIStyle guiStyle)
{
var group = GameViewSizes.instance.GetGroup(groupType);
var text = "";
if (selectedIndex >= 0 && selectedIndex < group.GetTotalCount())
text = group.GetGameViewSize(selectedIndex).displayText;
if (EditorGUI.DropdownButton(buttonRect, GUIContent.Temp(text), FocusType.Passive, guiStyle))
{
var menuData = new GameViewSizesMenuItemProvider(groupType);
var flexibleMenu = new GameViewSizeMenu(menuData, selectedIndex, new GameViewSizesMenuModifyItemUI(), gameView);
PopupWindow.Show(buttonRect, flexibleMenu);
}
}
public static void DrawRect(Rect rect, Color color)
{
if (Event.current.type != EventType.Repaint)
return;
Color orgColor = GUI.color;
GUI.color = GUI.color * color;
GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture);
GUI.color = orgColor;
}
internal static void DrawDelimiterLine(Rect rect)
{
DrawRect(rect, kSplitLineSkinnedColor.color);
}
internal static void DrawOutline(Rect rect, float size, Color color)
{
if (Event.current.type != EventType.Repaint)
return;
Color orgColor = GUI.color;
GUI.color = GUI.color * color;
GUI.DrawTexture(new Rect(rect.x, rect.y, rect.width, size), EditorGUIUtility.whiteTexture);
GUI.DrawTexture(new Rect(rect.x, rect.yMax - size, rect.width, size), EditorGUIUtility.whiteTexture);
GUI.DrawTexture(new Rect(rect.x, rect.y + 1, size, rect.height - 2 * size), EditorGUIUtility.whiteTexture);
GUI.DrawTexture(new Rect(rect.xMax - size, rect.y + 1, size, rect.height - 2 * size), EditorGUIUtility.whiteTexture);
GUI.color = orgColor;
}
}
internal struct PropertyGUIData
{
public SerializedProperty property;
public Rect totalPosition;
public bool wasBoldDefaultFont;
public bool wasEnabled;
public Color color;
public PropertyGUIData(SerializedProperty property, Rect totalPosition, bool wasBoldDefaultFont, bool wasEnabled, Color color)
{
this.property = property;
this.totalPosition = totalPosition;
this.wasBoldDefaultFont = wasBoldDefaultFont;
this.wasEnabled = wasEnabled;
this.color = color;
}
}
internal class DebugUtils
{
internal static string ListToString<T>(IEnumerable<T> list)
{
if (list == null)
return "[null list]";
string r = "[";
int count = 0;
foreach (T item in list)
{
if (count != 0)
r += ", ";
if (item != null)
r += item.ToString();
else
r += "'null'";
count++;
}
r += "]";
if (count == 0)
return "[empty list]";
return "(" + count + ") " + r;
}
}
}