forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropdownMenu.cs
More file actions
211 lines (180 loc) · 7.08 KB
/
DropdownMenu.cs
File metadata and controls
211 lines (180 loc) · 7.08 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
// 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 System.Collections.Generic;
namespace UnityEngine.UIElements
{
public class DropdownMenuEventInfo
{
public EventModifiers modifiers { get; }
public Vector2 mousePosition { get; }
public Vector2 localMousePosition { get; }
char character { get; }
KeyCode keyCode { get; }
public DropdownMenuEventInfo(EventBase e)
{
IMouseEvent mouseEvent = e as IMouseEvent;
if (mouseEvent != null)
{
mousePosition = mouseEvent.mousePosition;
localMousePosition = mouseEvent.localMousePosition;
modifiers = mouseEvent.modifiers;
character = '\0';
keyCode = KeyCode.None;
}
else
{
IKeyboardEvent keyboardEvent = e as IKeyboardEvent;
if (keyboardEvent != null)
{
character = keyboardEvent.character;
keyCode = keyboardEvent.keyCode;
modifiers = keyboardEvent.modifiers;
mousePosition = Vector2.zero;
localMousePosition = Vector2.zero;
}
}
}
}
public abstract class DropdownMenuItem {}
public class DropdownMenuSeparator : DropdownMenuItem
{
public string subMenuPath { get; }
public DropdownMenuSeparator(string subMenuPath)
{
this.subMenuPath = subMenuPath;
}
}
public class DropdownMenuAction : DropdownMenuItem
{
[Flags]
public enum Status
{
None = 0,
Normal = 1, // Enabled, unchecked, shown
Disabled = 2,
Checked = 4,
Hidden = 8
}
public string name { get; }
public Status status { get; private set; }
public DropdownMenuEventInfo eventInfo { get; private set; }
public object userData { get; private set; }
readonly Action<DropdownMenuAction> actionCallback;
readonly Func<DropdownMenuAction, Status> actionStatusCallback;
// ActionStatusCallback for action that are always enabled.
public static Status AlwaysEnabled(DropdownMenuAction a)
{
return Status.Normal;
}
// ActionStatusCallback for action that are always disabled.
public static Status AlwaysDisabled(DropdownMenuAction a)
{
return Status.Disabled;
}
public DropdownMenuAction(string actionName, Action<DropdownMenuAction> actionCallback, Func<DropdownMenuAction, Status> actionStatusCallback, object userData = null)
{
name = actionName;
this.actionCallback = actionCallback;
this.actionStatusCallback = actionStatusCallback;
this.userData = userData;
}
public void UpdateActionStatus(DropdownMenuEventInfo eventInfo)
{
this.eventInfo = eventInfo;
status = actionStatusCallback?.Invoke(this) ?? Status.Hidden;
}
public void Execute()
{
actionCallback?.Invoke(this);
}
}
public class DropdownMenu
{
List<DropdownMenuItem> menuItems = new List<DropdownMenuItem>();
DropdownMenuEventInfo m_DropdownMenuEventInfo;
public List<DropdownMenuItem> MenuItems()
{
return menuItems;
}
public void AppendAction(string actionName, Action<DropdownMenuAction> action, Func<DropdownMenuAction, DropdownMenuAction.Status> actionStatusCallback, object userData = null)
{
DropdownMenuAction menuAction = new DropdownMenuAction(actionName, action, actionStatusCallback, userData);
menuItems.Add(menuAction);
}
public void AppendAction(string actionName, Action<DropdownMenuAction> action, DropdownMenuAction.Status status = DropdownMenuAction.Status.Normal)
{
if (status == DropdownMenuAction.Status.Normal)
{
AppendAction(actionName, action, DropdownMenuAction.AlwaysEnabled);
}
else if (status == DropdownMenuAction.Status.Disabled)
{
AppendAction(actionName, action, DropdownMenuAction.AlwaysDisabled);
}
else
{
AppendAction(actionName, action, e => status);
}
}
public void InsertAction(int atIndex, string actionName, Action<DropdownMenuAction> action, Func<DropdownMenuAction, DropdownMenuAction.Status> actionStatusCallback, object userData = null)
{
DropdownMenuAction menuAction = new DropdownMenuAction(actionName, action, actionStatusCallback, userData);
menuItems.Insert(atIndex, menuAction);
}
public void InsertAction(int atIndex, string actionName, Action<DropdownMenuAction> action, DropdownMenuAction.Status status = DropdownMenuAction.Status.Normal)
{
if (status == DropdownMenuAction.Status.Normal)
{
InsertAction(atIndex, actionName, action, DropdownMenuAction.AlwaysEnabled);
}
else if (status == DropdownMenuAction.Status.Disabled)
{
InsertAction(atIndex, actionName, action, DropdownMenuAction.AlwaysDisabled);
}
else
{
InsertAction(atIndex, actionName, action, e => status);
}
}
public void AppendSeparator(string subMenuPath = null)
{
if (menuItems.Count > 0 && !(menuItems[menuItems.Count - 1] is DropdownMenuSeparator))
{
DropdownMenuSeparator separator = new DropdownMenuSeparator(subMenuPath ?? String.Empty);
menuItems.Add(separator);
}
}
public void InsertSeparator(string subMenuPath, int atIndex)
{
if (atIndex > 0 && atIndex <= menuItems.Count && !(menuItems[atIndex - 1] is DropdownMenuSeparator))
{
DropdownMenuSeparator separator = new DropdownMenuSeparator(subMenuPath ?? String.Empty);
menuItems.Insert(atIndex, separator);
}
}
public void RemoveItemAt(int index)
{
menuItems.RemoveAt(index);
}
public void PrepareForDisplay(EventBase e)
{
m_DropdownMenuEventInfo = e != null ? new DropdownMenuEventInfo(e) : null;
if (menuItems.Count == 0)
return;
foreach (DropdownMenuItem item in menuItems)
{
DropdownMenuAction action = item as DropdownMenuAction;
if (action != null)
{
action.UpdateActionStatus(m_DropdownMenuEventInfo);
}
}
if (menuItems[menuItems.Count - 1] is DropdownMenuSeparator)
{
menuItems.RemoveAt(menuItems.Count - 1);
}
}
}
}