forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMenu.cs
More file actions
151 lines (129 loc) · 5.26 KB
/
Copy pathGenericMenu.cs
File metadata and controls
151 lines (129 loc) · 5.26 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
// 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.Collections;
namespace UnityEditor
{
public sealed class GenericMenu
{
// Callback function, called when a menu item is selected
public delegate void MenuFunction();
// Callback function with user data, called when a menu item is selected
public delegate void MenuFunction2(object userData);
// Add an item to the menu
public void AddItem(GUIContent content, bool on, MenuFunction func)
{
menuItems.Add(new MenuItem(content, false, on, func));
}
// Add an item to the menu
public void AddItem(GUIContent content, bool on, MenuFunction2 func, object userData)
{
menuItems.Add(new MenuItem(content, false, on, func, userData));
}
// Add a disabled item to the menu
public void AddDisabledItem(GUIContent content)
{
menuItems.Add(new MenuItem(content, false, false, null));
}
// Add a disabled item to the menu
public void AddDisabledItem(GUIContent content, bool on)
{
menuItems.Add(new MenuItem(content, false, on, null));
}
// Add a separator item to the menu
public void AddSeparator(string path)
{
menuItems.Add(new MenuItem(new GUIContent(path), true, false, null));
}
// Get number of items in the menu
public int GetItemCount()
{
return menuItems.Count;
}
public bool allowDuplicateNames {get; set; }
private ArrayList menuItems = new ArrayList();
private sealed class MenuItem
{
public GUIContent content;
public bool separator;
public bool on;
public MenuFunction func;
public MenuFunction2 func2;
public object userData;
public MenuItem(GUIContent _content, bool _separator, bool _on, MenuFunction _func)
{
content = _content;
separator = _separator;
on = _on;
func = _func;
}
public MenuItem(GUIContent _content, bool _separator, bool _on, MenuFunction2 _func, object _userData)
{
content = _content;
separator = _separator;
on = _on;
func2 = _func;
userData = _userData;
}
}
// Show the menu under the mouse
public void ShowAsContext()
{
if (Event.current == null)
return;
DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
// Show the menu at the given screen rect
public void DropDown(Rect position)
{
string[] titles = new string[menuItems.Count];
bool[] enabled = new bool[menuItems.Count];
ArrayList selected = new ArrayList();
bool[] separator = new bool[menuItems.Count];
for (int idx = 0; idx < menuItems.Count; idx++)
{
MenuItem item = (MenuItem)menuItems[idx];
titles[idx] = item.content.text;
enabled[idx] = ((item.func != null) || (item.func2 != null));
separator[idx] = item.separator;
if (item.on)
selected.Add(idx);
}
EditorUtility.DisplayCustomMenuWithSeparators(position, titles, enabled, separator, (int[])selected.ToArray(typeof(int)), CatchMenu, null, true, allowDuplicateNames);
}
// Display as a popup with /selectedIndex/. How this behaves depends on the platform (on Mac, it'll try to scroll the menu to the right place)
internal void Popup(Rect position, int selectedIndex)
{
DropDown(position);
}
private void CatchMenu(object userData, string[] options, int selected)
{
MenuItem i = (MenuItem)menuItems[selected];
if (i.func2 != null)
i.func2(i.userData);
else if (i.func != null)
i.func();
}
// Show object context menu with builtin menu items plus the ones from this GenericMenu.
internal void ObjectContextDropDown(Rect position, Object[] context, int contextUserData)
{
string[] titles = new string[menuItems.Count];
bool[] enabled = new bool[menuItems.Count];
ArrayList selected = new ArrayList();
bool[] separator = new bool[menuItems.Count];
for (int idx = 0; idx < menuItems.Count; idx++)
{
MenuItem item = (MenuItem)menuItems[idx];
titles[idx] = item.content.text;
enabled[idx] = ((item.func != null) || (item.func2 != null));
separator[idx] = item.separator;
if (item.on)
selected.Add(idx);
}
EditorUtility.DisplayObjectContextPopupMenuWithExtraItems(
position, context, contextUserData,
titles, enabled, separator, (int[])selected.ToArray(typeof(int)), CatchMenu, null, true);
}
}
}