MenuItemActionBase.cs
5.49 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityEditor.Timeline
{
enum MenuActionDisplayState
{
Visible,
Disabled,
Hidden
}
struct MenuActionItem
{
public string category;
public string entryName;
public string shortCut;
public int priority;
public bool isChecked;
public bool isActiveInMode;
public MenuActionDisplayState state;
public GenericMenu.MenuFunction callback;
}
class MenuItemActionBase
{
public Vector2? mousePosition { get; set; }
protected static bool s_ShowActionTriggeredByShortcut = false;
private static MenuEntryAttribute NoMenu = new MenuEntryAttribute(null, MenuOrder.DefaultPriority);
private MenuEntryAttribute m_MenuInfo;
private string m_ShortCut = null;
public static IEnumerable<Type> GetActionsOfType(Type actionType)
{
var query = TypeCache.GetTypesDerivedFrom(actionType).Where(type => !type.IsGenericType && !type.IsNested && !type.IsAbstract);
return query;
}
public static ShortcutAttribute GetShortcutAttributeForAction(MenuItemActionBase action)
{
var shortcutAttributes = action.GetType()
.GetCustomAttributes(typeof(ShortcutAttribute), true)
.Cast<ShortcutAttribute>();
foreach (var shortcutAttribute in shortcutAttributes)
{
var shortcutOverride = shortcutAttribute as ShortcutPlatformOverrideAttribute;
if (shortcutOverride != null)
{
if (shortcutOverride.MatchesCurrentPlatform())
return shortcutOverride;
}
else
{
return shortcutAttribute;
}
}
return null;
}
public static void BuildMenu(GenericMenu menu, List<MenuActionItem> items)
{
// sorted the outer menu by priority, then sort the innermenu by priority
var sortedItems =
items.GroupBy(x => string.IsNullOrEmpty(x.category) ? x.entryName : x.category).
OrderBy(x => x.Min(y => y.priority)).
SelectMany(x => x.OrderBy(z => z.priority));
int lastPriority = Int32.MinValue;
string lastCategory = string.Empty;
foreach (var s in sortedItems)
{
if (s.state == MenuActionDisplayState.Hidden)
continue;
var priority = s.priority;
if (lastPriority == Int32.MinValue)
{
lastPriority = priority;
}
else if ((priority / MenuOrder.SeparatorAt) > (lastPriority / MenuOrder.SeparatorAt))
{
string path = String.Empty;
if (lastCategory == s.category)
path = s.category;
menu.AddSeparator(path);
}
lastPriority = priority;
lastCategory = s.category;
string entry = s.category + s.entryName;
if (!string.IsNullOrEmpty(s.shortCut))
entry += " " + s.shortCut;
if (s.state == MenuActionDisplayState.Visible && s.isActiveInMode)
menu.AddItem(new GUIContent(entry), s.isChecked, s.callback);
else
menu.AddDisabledItem(new GUIContent(entry));
}
}
public static ActiveInModeAttribute GetActiveInModeAttribute(MenuItemActionBase action)
{
var attr = action.GetType().GetCustomAttributes(typeof(ActiveInModeAttribute), true);
if (attr.Length > 0)
return (attr[0] as ActiveInModeAttribute);
return null;
}
public static bool IsActionActiveInMode(MenuItemActionBase action, TimelineModes mode)
{
ActiveInModeAttribute attr = GetActiveInModeAttribute(action);
return attr != null && (attr.modes & mode) != 0;
}
public int priority
{
get { return menuInfo.priority; }
}
public string category
{
get { return menuInfo.subMenuPath; }
}
public string menuName
{
get
{
if (string.IsNullOrEmpty(menuInfo.name))
return L10n.Tr(GetType().Name);
return menuInfo.name;
}
}
// shortcut used by the menu
public string shortCut
{
get
{
if (m_ShortCut == null)
{
var shortcutAttribute = GetShortcutAttributeForAction(this);
m_ShortCut = shortcutAttribute == null ? string.Empty : shortcutAttribute.GetMenuShortcut();
}
return m_ShortCut;
}
}
public bool showInMenu
{
get { return menuInfo != NoMenu; }
}
private MenuEntryAttribute menuInfo
{
get
{
if (m_MenuInfo == null)
m_MenuInfo = GetType().GetCustomAttributes(typeof(MenuEntryAttribute), false).OfType<MenuEntryAttribute>().DefaultIfEmpty(NoMenu).First();
return m_MenuInfo;
}
}
}
}