File size: 4,603 Bytes
18a519f | 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 | using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using UnityEditor.ShortcutManagement;
using UnityEngine;
namespace UnityEditor.Timeline.Actions
{
/// interface indicating an Action class
interface IAction { }
/// extension methods for IActions
static class ActionExtensions
{
const string kActionPostFix = "Action";
public static string GetUndoName(this IAction action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var attr = action.GetType().GetCustomAttribute<ApplyDefaultUndoAttribute>(false);
if (attr != null && !string.IsNullOrWhiteSpace(attr.UndoTitle))
return attr.UndoTitle;
return action.GetDisplayName();
}
public static string GetMenuEntryName(this IAction action)
{
var menuAction = action as IMenuName;
if (menuAction != null && !string.IsNullOrWhiteSpace(menuAction.menuName))
return menuAction.menuName;
var attr = action.GetType().GetCustomAttribute<MenuEntryAttribute>(false);
if (attr != null && !string.IsNullOrWhiteSpace(attr.name))
return attr.name;
return action.GetDisplayName();
}
public static string GetDisplayName(this IAction action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var attr = action.GetType().GetCustomAttribute<DisplayNameAttribute>(false);
if (attr != null && !string.IsNullOrEmpty(attr.DisplayName))
return attr.DisplayName;
var name = action.GetType().Name;
if (name.EndsWith(kActionPostFix))
return ObjectNames.NicifyVariableName(name.Substring(0, name.Length - kActionPostFix.Length));
return ObjectNames.NicifyVariableName(name);
}
public static bool HasAutoUndo(this IAction action)
{
return action != null && ActionManager.ActionsWithAutoUndo.Contains(action.GetType());
}
public static bool IsChecked(this IAction action)
{
return (action is IMenuChecked menuAction) && menuAction.isChecked;
}
public static bool IsActionActiveInMode(this IAction action, TimelineModes mode)
{
var attr = action.GetType().GetCustomAttribute<ActiveInModeAttribute>(true);
return attr != null && (attr.modes & mode) != 0;
}
public static string GetShortcut(this IAction action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var shortcutAttribute = GetShortcutAttributeForAction(action);
var shortCut = shortcutAttribute == null ? string.Empty : shortcutAttribute.GetMenuShortcut();
if (string.IsNullOrWhiteSpace(shortCut))
{
//Check if there is a static method with attribute
var customShortcutMethod = action.GetType().GetMethods().FirstOrDefault(m => m.GetCustomAttribute<TimelineShortcutAttribute>(true) != null);
if (customShortcutMethod != null)
{
var shortcutId = customShortcutMethod.GetCustomAttribute<TimelineShortcutAttribute>(true).identifier;
var shortcut = ShortcutIntegration.instance.directory.FindShortcutEntry(shortcutId);
if (shortcut != null && shortcut.combinations.Any())
shortCut = KeyCombination.SequenceToMenuString(shortcut.combinations);
}
}
return shortCut;
}
static ShortcutAttribute GetShortcutAttributeForAction(this IAction action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var shortcutAttributes = action.GetType()
.GetCustomAttributes(typeof(ShortcutAttribute), true)
.Cast<ShortcutAttribute>();
foreach (var shortcutAttribute in shortcutAttributes)
{
if (shortcutAttribute is ShortcutPlatformOverrideAttribute shortcutOverride)
{
if (shortcutOverride.MatchesCurrentPlatform())
return shortcutOverride;
}
else
{
return shortcutAttribute;
}
}
return null;
}
}
}
|