MenuEntryAttribute.cs
2.62 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
using System;
using UnityEngine;
namespace UnityEditor.Timeline.Actions
{
[Flags]
enum MenuFilter
{
None = 0,
Item = 1 << 0,
Track = 1 << 1,
MarkerHeader = 1 << 2,
Default = Item | Track
}
/// <summary>
/// Use this attribute to add a menu item to a context menu.
/// Used to indicate path and priority that are auto added to the menu
/// (examples can be found on <see href="https://docs.unity3d.com/ScriptReference/MenuItem.html"/>).
/// </summary>
/// <example>
/// <code source="../../DocCodeExamples/TimelineAttributesExamples.cs" region="declare-menuEntryAttribute" title="menuEntryAttr"/>
/// </example>
/// <remarks>
/// Unlike Menu item, MenuEntryAttribute doesn't handle shortcuts in the menu name. See <see cref="TimelineShortcutAttribute"/>.
/// </remarks>
[AttributeUsage(AttributeTargets.Class)]
public class MenuEntryAttribute : Attribute
{
internal readonly int priority;
internal readonly string name;
internal readonly string subMenuPath;
internal readonly MenuFilter filter;
/// <summary>
/// Constructor for Menu Entry Attribute to define information about the menu item for an action.
/// </summary>
/// <param name="path">Path to the menu. If there is a "/" in the path, it will create one (or multiple) submenu items.</param>
/// <param name="priority">Priority to decide where the menu will be positioned in the menu.
/// The lower the priority, the higher the menu item will be in the context menu.
/// </param>
/// <seealso cref="MenuPriority"/>
public MenuEntryAttribute(string path = default, int priority = MenuPriority.defaultPriority) : this(path, priority, MenuFilter.Default) {}
internal MenuEntryAttribute(string path, int priority, MenuFilter filter)
{
this.filter = filter;
path = path ?? string.Empty;
path = L10n.Tr(path);
this.priority = priority;
var index = path.LastIndexOf('/');
if (index >= 0)
{
name = (index == path.Length - 1) ? string.Empty : path.Substring(index + 1);
subMenuPath = path.Substring(0, index + 1);
}
else
{
name = path;
subMenuPath = string.Empty;
}
}
}
static class MenuFilterExtensions
{
public static bool ShouldFilterOut(this MenuFilter filter, MenuEntryAttribute attr)
{
return (filter & attr.filter) != filter;
}
}
}