-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathContextualMenuNodeData.cs
246 lines (212 loc) · 9.85 KB
/
ContextualMenuNodeData.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
using System;
using System.Collections.Generic;
using UnityEngine.UIElements;
namespace ContextualMenuPlayer
{
public abstract class ContextualMenuNodeData
{
public readonly string NodeName;
public abstract void AddChild(ContextualMenuNodeData node);
public abstract void Execute();
public abstract List<ContextualMenuNodeData> Children { get; }
public abstract int ChildCount { get; }
public abstract bool IsSubMenu { get; }
public abstract bool IsSeparator { get; }
public abstract bool IsDisabled { get; }
public abstract bool IsChecked { get; }
protected ContextualMenuNodeData(string name) => NodeName = name;
public static ContextualMenuNodeData ConstructMenuTree(DropdownMenu dropdownMenu)
{
SubMenuNode root = new(string.Empty);
root.ConstructFromMenu(dropdownMenu);
return root;
}
public static ContextualMenuNodeData NewTree() => new SubMenuNode(string.Empty);
private class SeparatorMenuNode : ContextualMenuNodeData
{
private static readonly NotImplementedException NoChildrenException
= new("Contexual menu separator cannot have children");
private static readonly NotImplementedException NoExecuteException
= new("Contexual menu separator cannot execute");
internal SeparatorMenuNode(string name) : base(name) { }
public override void AddChild(ContextualMenuNodeData node)
=> throw NoChildrenException;
public override void Execute() => throw NoExecuteException;
public override List<ContextualMenuNodeData> Children => throw NoChildrenException;
public override int ChildCount => throw NoChildrenException;
public override bool IsSeparator => true;
public override bool IsSubMenu => false;
public override bool IsDisabled => false;
public override bool IsChecked => false;
}
private class ActionMenuNode : ContextualMenuNodeData
{
private static readonly NotImplementedException NoChildrenException
= new("Contexual menu action cannot have children");
private readonly DropdownMenuAction m_MenuAction;
internal ActionMenuNode(string name, DropdownMenuAction action) : base(name)
=> m_MenuAction = action;
public override void AddChild(ContextualMenuNodeData node)
=> throw NoChildrenException;
public override void Execute() => m_MenuAction.Execute();
public override List<ContextualMenuNodeData> Children => throw NoChildrenException;
public override int ChildCount => throw NoChildrenException;
public override bool IsSeparator => false;
public override bool IsSubMenu => false;
public override bool IsDisabled => m_MenuAction.IsDisabled();
public override bool IsChecked => m_MenuAction.IsChecked();
}
private class SubMenuNode : ContextualMenuNodeData
{
private static readonly NotImplementedException NoExecuteException
= new("Contexual submenu cannot execute");
private readonly List<ContextualMenuNodeData> m_Children;
internal SubMenuNode(string name) : base(name) => m_Children = new();
public override void AddChild(ContextualMenuNodeData node) => m_Children.Add(node);
public override void Execute() => throw NoExecuteException;
public override List<ContextualMenuNodeData> Children => m_Children;
public override int ChildCount => m_Children.Count;
public override bool IsChecked => false;
public override bool IsDisabled => false;
public override bool IsSeparator => false;
public override bool IsSubMenu => true;
internal void ConstructFromMenu(DropdownMenu dropdownMenu)
{
foreach (DropdownMenuItem item in dropdownMenu.MenuItems())
{
switch (item)
{
case DropdownMenuAction action:
AddActionNode(action);
break;
case DropdownMenuSeparator separator:
AddSeparatorNode(separator);
break;
}
}
}
private void AddActionNode(DropdownMenuAction action)
{
// Skip Hidden
if (action.IsHidden()) return;
// Break Path
string[] pathElements = SplitPathAndClean(action.name);
// Create New Node
ActionMenuNode newNode = new(pathElements[^1], action);
// Add Node
AddMenuNode(pathElements, newNode);
}
private void AddSeparatorNode(DropdownMenuSeparator separator)
{
// Break Path
string[] pathElements = SplitPathAndClean(separator.subMenuPath, true);
// Create New Node
SeparatorMenuNode newNode = new(pathElements[^1]);
// Add Node
AddMenuNode(pathElements, newNode);
}
private void AddMenuNode(string[] pathElements, ContextualMenuNodeData newNode)
{
int endIndex = pathElements.Length - 1;
ContextualMenuNodeData currentNode = this;
for (int i = 0; i < pathElements.Length; i++)
{
// Current Path Element
string currentPathElement = pathElements[i];
// Is Last Index
bool isFinalElement = i == endIndex;
// Find or Create Path Element
bool found = false;
ContextualMenuNodeData nodeIterator = null;
if (currentPathElement == string.Empty)
{
// The only case where "" is acceptable is when we're adding as separator to
// the root menu.
if (!isFinalElement)
{
string errPath = string.Join('/', pathElements);
throw new($"Path cannot contain double forward slashes {errPath}");
}
found = true;
nodeIterator = currentNode;
}
else
{
int childCount = currentNode.ChildCount;
List<ContextualMenuNodeData> children = currentNode.Children;
for (int j = 0; j < childCount; j++)
{
nodeIterator = children[j];
if (nodeIterator.NodeName == currentPathElement)
{
found = true;
break;
}
}
}
// Not Found
if (!found)
{
ContextualMenuNodeData childToAdd;
// Add New Menu Node
if (isFinalElement) childToAdd = newNode;
// Add new Sub Menu Node
else childToAdd = new SubMenuNode(currentPathElement);
currentNode.AddChild(childToAdd);
currentNode = childToAdd;
}
else
{
if (isFinalElement)
{
// Duplicate Path Found
if (newNode is SeparatorMenuNode)
{
// Add Separator Node
nodeIterator.AddChild(newNode);
}
else throw new($"Duplicate menu entry found {string.Join('/', pathElements)}");
}
else
{
// Still Searching, Update Current Node
currentNode = nodeIterator;
}
}
}
}
private string[] SplitPathAndClean(string path, bool isSeparator = false)
{
// Sanity
if (path == null) throw new($"Invalid path menu {path}");
// Split
string[] pathElements = path.Split('/');
// Calculate empty element count
int emptyCount = 0;
for (int i = 0; i < pathElements.Length; i++)
{
if (pathElements[i] == string.Empty) emptyCount++;
}
// Remove empty elements, and if this is separator, add empty string to the end
string[] cleanedPathElements
= new string[pathElements.Length - emptyCount + (isSeparator ? 1 : 0)];
int j = 0;
for (int i = 0; i < pathElements.Length; i++)
{
if (pathElements[i] != string.Empty)
{
cleanedPathElements[j++] = pathElements[i];
}
}
if (isSeparator) cleanedPathElements[j] = string.Empty;
// Sanity
if (cleanedPathElements.Length == 0)
{
throw new($"Invalid path menu {path}");
}
// Return eleaned path elements
return cleanedPathElements;
}
}
}
}