MenuItemActionBase.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. namespace UnityEditor.Timeline
  8. {
  9. enum MenuActionDisplayState
  10. {
  11. Visible,
  12. Disabled,
  13. Hidden
  14. }
  15. struct MenuActionItem
  16. {
  17. public string category;
  18. public string entryName;
  19. public string shortCut;
  20. public int priority;
  21. public bool isChecked;
  22. public bool isActiveInMode;
  23. public MenuActionDisplayState state;
  24. public GenericMenu.MenuFunction callback;
  25. }
  26. class MenuItemActionBase
  27. {
  28. public Vector2? mousePosition { get; set; }
  29. protected static bool s_ShowActionTriggeredByShortcut = false;
  30. private static MenuEntryAttribute NoMenu = new MenuEntryAttribute(null, MenuOrder.DefaultPriority);
  31. private MenuEntryAttribute m_MenuInfo;
  32. private string m_ShortCut = null;
  33. public static IEnumerable<Type> GetActionsOfType(Type actionType)
  34. {
  35. var query = TypeCache.GetTypesDerivedFrom(actionType).Where(type => !type.IsGenericType && !type.IsNested && !type.IsAbstract);
  36. return query;
  37. }
  38. public static ShortcutAttribute GetShortcutAttributeForAction(MenuItemActionBase action)
  39. {
  40. var shortcutAttributes = action.GetType()
  41. .GetCustomAttributes(typeof(ShortcutAttribute), true)
  42. .Cast<ShortcutAttribute>();
  43. foreach (var shortcutAttribute in shortcutAttributes)
  44. {
  45. var shortcutOverride = shortcutAttribute as ShortcutPlatformOverrideAttribute;
  46. if (shortcutOverride != null)
  47. {
  48. if (shortcutOverride.MatchesCurrentPlatform())
  49. return shortcutOverride;
  50. }
  51. else
  52. {
  53. return shortcutAttribute;
  54. }
  55. }
  56. return null;
  57. }
  58. public static void BuildMenu(GenericMenu menu, List<MenuActionItem> items)
  59. {
  60. // sorted the outer menu by priority, then sort the innermenu by priority
  61. var sortedItems =
  62. items.GroupBy(x => string.IsNullOrEmpty(x.category) ? x.entryName : x.category).
  63. OrderBy(x => x.Min(y => y.priority)).
  64. SelectMany(x => x.OrderBy(z => z.priority));
  65. int lastPriority = Int32.MinValue;
  66. string lastCategory = string.Empty;
  67. foreach (var s in sortedItems)
  68. {
  69. if (s.state == MenuActionDisplayState.Hidden)
  70. continue;
  71. var priority = s.priority;
  72. if (lastPriority == Int32.MinValue)
  73. {
  74. lastPriority = priority;
  75. }
  76. else if ((priority / MenuOrder.SeparatorAt) > (lastPriority / MenuOrder.SeparatorAt))
  77. {
  78. string path = String.Empty;
  79. if (lastCategory == s.category)
  80. path = s.category;
  81. menu.AddSeparator(path);
  82. }
  83. lastPriority = priority;
  84. lastCategory = s.category;
  85. string entry = s.category + s.entryName;
  86. if (!string.IsNullOrEmpty(s.shortCut))
  87. entry += " " + s.shortCut;
  88. if (s.state == MenuActionDisplayState.Visible && s.isActiveInMode)
  89. menu.AddItem(new GUIContent(entry), s.isChecked, s.callback);
  90. else
  91. menu.AddDisabledItem(new GUIContent(entry));
  92. }
  93. }
  94. public static ActiveInModeAttribute GetActiveInModeAttribute(MenuItemActionBase action)
  95. {
  96. var attr = action.GetType().GetCustomAttributes(typeof(ActiveInModeAttribute), true);
  97. if (attr.Length > 0)
  98. return (attr[0] as ActiveInModeAttribute);
  99. return null;
  100. }
  101. public static bool IsActionActiveInMode(MenuItemActionBase action, TimelineModes mode)
  102. {
  103. ActiveInModeAttribute attr = GetActiveInModeAttribute(action);
  104. return attr != null && (attr.modes & mode) != 0;
  105. }
  106. public int priority
  107. {
  108. get { return menuInfo.priority; }
  109. }
  110. public string category
  111. {
  112. get { return menuInfo.subMenuPath; }
  113. }
  114. public string menuName
  115. {
  116. get
  117. {
  118. if (string.IsNullOrEmpty(menuInfo.name))
  119. return L10n.Tr(GetType().Name);
  120. return menuInfo.name;
  121. }
  122. }
  123. // shortcut used by the menu
  124. public string shortCut
  125. {
  126. get
  127. {
  128. if (m_ShortCut == null)
  129. {
  130. var shortcutAttribute = GetShortcutAttributeForAction(this);
  131. m_ShortCut = shortcutAttribute == null ? string.Empty : shortcutAttribute.GetMenuShortcut();
  132. }
  133. return m_ShortCut;
  134. }
  135. }
  136. public bool showInMenu
  137. {
  138. get { return menuInfo != NoMenu; }
  139. }
  140. private MenuEntryAttribute menuInfo
  141. {
  142. get
  143. {
  144. if (m_MenuInfo == null)
  145. m_MenuInfo = GetType().GetCustomAttributes(typeof(MenuEntryAttribute), false).OfType<MenuEntryAttribute>().DefaultIfEmpty(NoMenu).First();
  146. return m_MenuInfo;
  147. }
  148. }
  149. }
  150. }