MenuEntryAttribute.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Timeline.Actions
  4. {
  5. [Flags]
  6. enum MenuFilter
  7. {
  8. None = 0,
  9. Item = 1 << 0,
  10. Track = 1 << 1,
  11. MarkerHeader = 1 << 2,
  12. Default = Item | Track
  13. }
  14. /// <summary>
  15. /// Use this attribute to add a menu item to a context menu.
  16. /// Used to indicate path and priority that are auto added to the menu
  17. /// (examples can be found on <see href="https://docs.unity3d.com/ScriptReference/MenuItem.html"/>).
  18. /// </summary>
  19. /// <example>
  20. /// <code source="../../DocCodeExamples/TimelineAttributesExamples.cs" region="declare-menuEntryAttribute" title="menuEntryAttr"/>
  21. /// </example>
  22. /// <remarks>
  23. /// Unlike Menu item, MenuEntryAttribute doesn't handle shortcuts in the menu name. See <see cref="TimelineShortcutAttribute"/>.
  24. /// </remarks>
  25. [AttributeUsage(AttributeTargets.Class)]
  26. public class MenuEntryAttribute : Attribute
  27. {
  28. internal readonly int priority;
  29. internal readonly string name;
  30. internal readonly string subMenuPath;
  31. internal readonly MenuFilter filter;
  32. /// <summary>
  33. /// Constructor for Menu Entry Attribute to define information about the menu item for an action.
  34. /// </summary>
  35. /// <param name="path">Path to the menu. If there is a "/" in the path, it will create one (or multiple) submenu items.</param>
  36. /// <param name="priority">Priority to decide where the menu will be positioned in the menu.
  37. /// The lower the priority, the higher the menu item will be in the context menu.
  38. /// </param>
  39. /// <seealso cref="MenuPriority"/>
  40. public MenuEntryAttribute(string path = default, int priority = MenuPriority.defaultPriority) : this(path, priority, MenuFilter.Default) {}
  41. internal MenuEntryAttribute(string path, int priority, MenuFilter filter)
  42. {
  43. this.filter = filter;
  44. path = path ?? string.Empty;
  45. path = L10n.Tr(path);
  46. this.priority = priority;
  47. var index = path.LastIndexOf('/');
  48. if (index >= 0)
  49. {
  50. name = (index == path.Length - 1) ? string.Empty : path.Substring(index + 1);
  51. subMenuPath = path.Substring(0, index + 1);
  52. }
  53. else
  54. {
  55. name = path;
  56. subMenuPath = string.Empty;
  57. }
  58. }
  59. }
  60. static class MenuFilterExtensions
  61. {
  62. public static bool ShouldFilterOut(this MenuFilter filter, MenuEntryAttribute attr)
  63. {
  64. return (filter & attr.filter) != filter;
  65. }
  66. }
  67. }