-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathContextualMenuBox.cs
241 lines (211 loc) · 8.28 KB
/
ContextualMenuBox.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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UIElements;
namespace ContextualMenuPlayer
{
internal class ContextualMenuBox : VisualElement
{
private const float k_SafeZoneTimeoutSecs = 0.3f;
private const long k_SafeZoneTimeoutMSecs = (long)(k_SafeZoneTimeoutSecs * 1000);
private static readonly ObjectPool<ContextualMenuBox> s_MenuBoxPool = new(Create, null, TearDown);
private readonly IVisualElementScheduledItem m_RetrySubMenuOpen;
private float m_SubMenuCreateTime;
private Triangle m_SubMenuTemporarySafeZone;
private ContextualMenu m_Root;
private IMenuBoxParent m_Parent;
private ContextualMenuItem m_LastSubMenuAttempt;
private RUIContextualMenuGrowDirection m_Direction;
public RUIContextualMenuGrowDirection Direction => m_Direction;
public ContextualMenu RootMenu => m_Root;
private ContextualMenuBox()
{
AddToClassList("contextual-menu-box");
// Temporary Safe Zone
m_SubMenuCreateTime = 2 * (-k_SafeZoneTimeoutSecs);
m_RetrySubMenuOpen = schedule.Execute(RetryTryOpenSubMenu);
m_RetrySubMenuOpen.Pause();
}
internal static ContextualMenuBox GetFromPool(MenuBoxCreationContext ctx)
{
// Grab from Pool
ContextualMenuBox menuBox = s_MenuBoxPool.Get();
// Set Root
menuBox.m_Root = ctx.rootMenu;
// Set Parent
menuBox.m_Parent = ctx.parentElement;
// Set Direction
menuBox.m_Direction = ctx.direction;
// Reset Layout
menuBox.style.top = -1;
menuBox.style.left = -1;
// menuBox.style.translate = StyleKeyword.None;
// Add Items
int childCount = ctx.menuData.ChildCount;
List<ContextualMenuNodeData> itemsData = ctx.menuData.Children;
for (int i = 0; i < childCount; i++)
{
// Grab Menu Data
ContextualMenuNodeData itemData = itemsData[i];
// Create Item
ContextualMenuItem itemElement = ContextualMenuItem.GetFromPool(new()
{
itemData = itemData,
parentMenu = menuBox,
});
// Add to Menu
menuBox.Add(itemElement);
}
// Add to Root
ctx.rootMenu.Add(menuBox);
// Add Event Handlers
menuBox.RegisterCallback<GeometryChangedEvent>(menuBox.OnGeometryChanged);
// Return
return menuBox;
}
internal static void ReleaseToPool(ContextualMenuBox menuBox)
=> s_MenuBoxPool.Release(menuBox);
private static ContextualMenuBox Create() => new();
private static void TearDown(ContextualMenuBox toTearDown)
{
toTearDown.UnregisterCallback<GeometryChangedEvent>(toTearDown.OnGeometryChanged);
toTearDown.RemoveFromHierarchy();
for (int i = toTearDown.childCount - 1; i >= 0; i--)
{
ContextualMenuItem.ReleaseToPool(toTearDown[i] as ContextualMenuItem);
}
toTearDown.m_Root = null;
toTearDown.m_Parent = null;
toTearDown.m_Direction = RUIContextualMenuGrowDirection.SE;
toTearDown.m_LastSubMenuAttempt = null;
}
internal void SetTemporarySafeZone(Triangle safeZone)
{
// Set Time
m_SubMenuCreateTime = Time.time;
// Set Zone
m_SubMenuTemporarySafeZone = safeZone;
}
internal bool IsInSafeZone(Vector2 point)
{
bool requiresSafeCheck = (Time.time - m_SubMenuCreateTime) < k_SafeZoneTimeoutSecs;
bool isSafe = requiresSafeCheck && m_SubMenuTemporarySafeZone.ContainsPoint(point);
return isSafe;
}
internal void TryOpenSubMenu(ContextualMenuItem item, Vector2 mousePosition)
{
if (!IsInSafeZone(mousePosition))
{
item.OpenSubMenu();
return;
}
// Try again after timeout
if (m_LastSubMenuAttempt != item)
{
m_LastSubMenuAttempt = item;
m_RetrySubMenuOpen.ExecuteLater(k_SafeZoneTimeoutMSecs);
}
}
internal void Close() => ReleaseToPool(this);
internal void CloseRoot() => m_Root.Close();
internal void CloseSubMenus(ContextualMenuItem excluding = null)
{
for (int i = 0; i < childCount; i++)
{
ContextualMenuItem childItem = this[i] as ContextualMenuItem;
if (childItem != excluding) childItem.CloseSubMenu();
}
}
private void RetryTryOpenSubMenu()
{
// Check if mouse is still hovering over last menu that tried to open
if (m_LastSubMenuAttempt != null && m_LastSubMenuAttempt.MouseIsHovering)
{
m_LastSubMenuAttempt.OpenSubMenu();
m_LastSubMenuAttempt = null;
}
}
private void OnGeometryChanged(GeometryChangedEvent evt)
{
// Viewport
float viewportWidth = m_Root.resolvedStyle.width;
float viewportHeight = m_Root.resolvedStyle.height;
// Parent Rect
Rect parentRect = m_Parent.AbsoluteRect();
// X-Axis Position
float normalOriginX = parentRect.x + parentRect.width;
float normalExtentX = normalOriginX + resolvedStyle.width;
float translatedOriginX = parentRect.x - resolvedStyle.width;
bool mustTranslateX = normalExtentX > viewportWidth;
bool willTranslateX;
if (!mustTranslateX)
{
bool canTranslateX = translatedOriginX >= 0;
bool parentTranslatedX = m_Parent.Direction() switch
{
RUIContextualMenuGrowDirection.NW or RUIContextualMenuGrowDirection.SW => true,
_ => false,
};
willTranslateX = parentTranslatedX && canTranslateX;
}
else
{
willTranslateX = true;
}
float finalX = willTranslateX
? translatedOriginX
: normalOriginX
;
// Y-Axis Position
float originY;
float extentY;
float distancePastViewport;
bool willTranslateY = m_Parent.Direction() switch
{
RUIContextualMenuGrowDirection.NE or RUIContextualMenuGrowDirection.NW => true,
_ => false,
};
if (willTranslateY)
{
extentY = parentRect.y + parentRect.height;
originY = extentY - resolvedStyle.height;
distancePastViewport = Mathf.Min(0, originY);
}
else
{
originY = parentRect.y;
extentY = originY + resolvedStyle.height;
distancePastViewport = Mathf.Max(0, extentY - viewportHeight);
}
float finalY = originY - distancePastViewport;
// Apply Position
style.left = new Length(finalX, LengthUnit.Pixel);
style.top = new Length(finalY, LengthUnit.Pixel);
// Store Direction
if (willTranslateX)
{
m_Direction = willTranslateY
? RUIContextualMenuGrowDirection.NW
: RUIContextualMenuGrowDirection.SW;
}
else
{
m_Direction = willTranslateY
? RUIContextualMenuGrowDirection.NE
: RUIContextualMenuGrowDirection.SE;
}
}
internal struct MenuBoxCreationContext
{
public ContextualMenuNodeData menuData;
public ContextualMenu rootMenu;
public IMenuBoxParent parentElement;
public RUIContextualMenuGrowDirection direction;
}
}
internal interface IMenuBoxParent
{
RUIContextualMenuGrowDirection Direction();
Rect AbsoluteRect();
}
}