-
Notifications
You must be signed in to change notification settings - Fork 6
/
ContextualMenuManager.cs
53 lines (47 loc) · 1.76 KB
/
ContextualMenuManager.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
using UnityEngine.UIElements;
namespace ContextualMenuPlayer
{
public class ContextualMenuManager : UnityEngine.UIElements.ContextualMenuManager
{
private readonly StyleSheet m_StyleSheet;
public ContextualMenuManager() : this(null) { }
public ContextualMenuManager(StyleSheet styleSheet)
{
m_StyleSheet = styleSheet;
}
// Do actual display here so it can be used independently of manipulator events
public void DisplayMenu(ContextualMenu.MenuCreationContext creationContext)
{
ContextualMenu.OpenMenu(creationContext);
}
// NOTE: I've only seen this used in the context of GraphView. Leaving for now.
public override void DisplayMenuIfEventMatches(
EventBase evt, IEventHandler eventHandler)
{ }
protected override void DoDisplayMenu(DropdownMenu menu, EventBase triggerEvent)
{
if (menu.MenuItems().Count == 0) return;
DisplayMenu(new()
{
menu = menu,
position = ((IMouseEvent)triggerEvent).mousePosition,
styleSheetOverride = m_StyleSheet,
root = GetElementRoot((VisualElement)triggerEvent.target),
direction = RUIContextualMenuGrowDirection.SE,
});
}
private VisualElement GetElementRoot(VisualElement visualElement)
{
VisualElement result = null;
for (; visualElement != null; visualElement = visualElement.hierarchy.parent)
{
if (visualElement is TemplateContainer)
{
result = visualElement;
break;
}
}
return result;
}
}
}