Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IAction.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor.ShortcutManagement;
  6. using UnityEngine;
  7. namespace UnityEditor.Timeline.Actions
  8. {
  9. /// interface indicating an Action class
  10. interface IAction { }
  11. /// extension methods for IActions
  12. static class ActionExtensions
  13. {
  14. const string kActionPostFix = "Action";
  15. public static string GetUndoName(this IAction action)
  16. {
  17. if (action == null)
  18. throw new ArgumentNullException(nameof(action));
  19. var attr = action.GetType().GetCustomAttribute<ApplyDefaultUndoAttribute>(false);
  20. if (attr != null && !string.IsNullOrWhiteSpace(attr.UndoTitle))
  21. return attr.UndoTitle;
  22. return action.GetDisplayName();
  23. }
  24. public static string GetMenuEntryName(this IAction action)
  25. {
  26. var menuAction = action as IMenuName;
  27. if (menuAction != null && !string.IsNullOrWhiteSpace(menuAction.menuName))
  28. return menuAction.menuName;
  29. var attr = action.GetType().GetCustomAttribute<MenuEntryAttribute>(false);
  30. if (attr != null && !string.IsNullOrWhiteSpace(attr.name))
  31. return attr.name;
  32. return action.GetDisplayName();
  33. }
  34. public static string GetDisplayName(this IAction action)
  35. {
  36. if (action == null)
  37. throw new ArgumentNullException(nameof(action));
  38. var attr = action.GetType().GetCustomAttribute<DisplayNameAttribute>(false);
  39. if (attr != null && !string.IsNullOrEmpty(attr.DisplayName))
  40. return attr.DisplayName;
  41. var name = action.GetType().Name;
  42. if (name.EndsWith(kActionPostFix))
  43. return ObjectNames.NicifyVariableName(name.Substring(0, name.Length - kActionPostFix.Length));
  44. return ObjectNames.NicifyVariableName(name);
  45. }
  46. public static bool HasAutoUndo(this IAction action)
  47. {
  48. return action != null && ActionManager.ActionsWithAutoUndo.Contains(action.GetType());
  49. }
  50. public static bool IsChecked(this IAction action)
  51. {
  52. return (action is IMenuChecked menuAction) && menuAction.isChecked;
  53. }
  54. public static bool IsActionActiveInMode(this IAction action, TimelineModes mode)
  55. {
  56. var attr = action.GetType().GetCustomAttribute<ActiveInModeAttribute>(true);
  57. return attr != null && (attr.modes & mode) != 0;
  58. }
  59. public static string GetShortcut(this IAction action)
  60. {
  61. if (action == null)
  62. throw new ArgumentNullException(nameof(action));
  63. var shortcutAttribute = GetShortcutAttributeForAction(action);
  64. var shortCut = shortcutAttribute == null ? string.Empty : shortcutAttribute.GetMenuShortcut();
  65. if (string.IsNullOrWhiteSpace(shortCut))
  66. {
  67. //Check if there is a static method with attribute
  68. var customShortcutMethod = action.GetType().GetMethods().FirstOrDefault(m => m.GetCustomAttribute<TimelineShortcutAttribute>(true) != null);
  69. if (customShortcutMethod != null)
  70. {
  71. var shortcutId = customShortcutMethod.GetCustomAttribute<TimelineShortcutAttribute>(true).identifier;
  72. var shortcut = ShortcutIntegration.instance.directory.FindShortcutEntry(shortcutId);
  73. if (shortcut != null && shortcut.combinations.Any())
  74. shortCut = KeyCombination.SequenceToMenuString(shortcut.combinations);
  75. }
  76. }
  77. return shortCut;
  78. }
  79. static ShortcutAttribute GetShortcutAttributeForAction(this IAction action)
  80. {
  81. if (action == null)
  82. throw new ArgumentNullException(nameof(action));
  83. var shortcutAttributes = action.GetType()
  84. .GetCustomAttributes(typeof(ShortcutAttribute), true)
  85. .Cast<ShortcutAttribute>();
  86. foreach (var shortcutAttribute in shortcutAttributes)
  87. {
  88. if (shortcutAttribute is ShortcutPlatformOverrideAttribute shortcutOverride)
  89. {
  90. if (shortcutOverride.MatchesCurrentPlatform())
  91. return shortcutOverride;
  92. }
  93. else
  94. {
  95. return shortcutAttribute;
  96. }
  97. }
  98. return null;
  99. }
  100. }
  101. }